Skip to content

Commit

Permalink
refactor: hide invalid services (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtyuns authored Jun 17, 2024
1 parent 553c057 commit a5f432b
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 17 deletions.
17 changes: 15 additions & 2 deletions src/utils/service_instance.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export enum ServiceType {
TRANSLATE = 'translate',
RECOGNIZE = 'recognize',
TTS = 'tts',
COLLECTION = 'Collection'
}

export enum ServiceSourceType {
BUILDIN = 'buildin',
PLUGIN = 'plugin',
Expand Down Expand Up @@ -29,9 +36,15 @@ export function getServiceName(serviceInstanceKey: string): string {
return serviceInstanceKey.split('@')[0]
}


export function getDisplayInstanceName(instanceName: string, serviceNameSupplier: () => string): string {
return instanceName || serviceNameSupplier()
}

export const INSTANCE_NAME_CONFIG_KEY = 'instanceName'
export const INSTANCE_NAME_CONFIG_KEY = 'instanceName'

export function whetherAvailableService(serviceInstanceKey: string, availableServices: Record<ServiceSourceType, Record<string, any>>) {
const serviceSourceType = getServiceSouceType(serviceInstanceKey)
const serviceName = getServiceName(serviceInstanceKey)
return availableServices[serviceSourceType]?.[serviceName] !== undefined

}
6 changes: 5 additions & 1 deletion src/window/Config/pages/History/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useConfig, useToastStyle } from '../../../../hooks';
import { LanguageFlag } from '../../../../utils/language';
import { store } from '../../../../utils/store';
import { osType } from '../../../../utils/env';
import { ServiceSourceType, ServiceType, whetherAvailableService } from '../../../../utils/service_instance';

export default function History() {
const [collectionServiceList] = useConfig('collection_service_list', []);
Expand Down Expand Up @@ -145,7 +146,10 @@ export default function History() {
emptyContent={'No History to display.'}
items={items}
>
{(item) => (
{(item) => whetherAvailableService(item.service, {
[ServiceSourceType.BUILDIN]: builtinServices,
[ServiceSourceType.PLUGIN]: pluginList[ServiceType.TRANSLATE]
}) && (
<TableRow key={item.id}>
<TableCell>
{item.service.startsWith('[plugin]') ? (
Expand Down
9 changes: 8 additions & 1 deletion src/window/Config/pages/Service/Collection/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { useConfig } from '../../../../../hooks';
import ServiceItem from './ServiceItem';
import SelectModal from './SelectModal';
import ConfigModal from './ConfigModal';
import * as builtinCollectionServices from '../../../../../services/collection';
import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance';

export default function Collection(props) {
const { pluginList } = props;
Expand Down Expand Up @@ -67,7 +69,12 @@ export default function Collection(props) {
{...provided.droppableProps}
>
{collectionServiceList !== null &&
collectionServiceList.map((x, i) => {
collectionServiceList.filter(instanceKey => {
return whetherAvailableService(instanceKey, {
[ServiceSourceType.BUILDIN]: builtinCollectionServices,
[ServiceSourceType.PLUGIN]: pluginList
})
}).map((x, i) => {
return (
<Draggable
key={x}
Expand Down
10 changes: 9 additions & 1 deletion src/window/Config/pages/Service/Recognize/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { useConfig } from '../../../../../hooks';
import ServiceItem from './ServiceItem';
import SelectModal from './SelectModal';
import ConfigModal from './ConfigModal';
import * as builtinRecognizeServices from '../../../../../services/recognize';
import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance';


export default function Recognize(props) {
const { pluginList } = props;
Expand Down Expand Up @@ -79,7 +82,12 @@ export default function Recognize(props) {
{...provided.droppableProps}
>
{recognizeServiceList !== null &&
recognizeServiceList.map((x, i) => {
recognizeServiceList.filter(instanceKey => {
return whetherAvailableService(instanceKey, {
[ServiceSourceType.BUILDIN]: builtinRecognizeServices,
[ServiceSourceType.PLUGIN]: pluginList
})
}).map((x, i) => {
return (
<Draggable
key={x}
Expand Down
10 changes: 9 additions & 1 deletion src/window/Config/pages/Service/Translate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import ServiceItem from './ServiceItem';
import SelectModal from './SelectModal';
import ConfigModal from './ConfigModal';

import * as builtinTranslateServices from '../../../../../services/translate';
import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance';

export default function Translate(props) {
const { pluginList } = props;
const {
Expand Down Expand Up @@ -81,7 +84,12 @@ export default function Translate(props) {
{...provided.droppableProps}
>
{translateServiceInstanceList !== null &&
translateServiceInstanceList.map((x, i) => {
translateServiceInstanceList.filter(instanceKey => {
return whetherAvailableService(instanceKey, {
[ServiceSourceType.BUILDIN]: builtinTranslateServices,
[ServiceSourceType.PLUGIN]: pluginList
})
}).map((x, i) => {
return (
<Draggable
key={x}
Expand Down
9 changes: 8 additions & 1 deletion src/window/Config/pages/Service/Tts/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { useConfig } from '../../../../../hooks';
import ServiceItem from './ServiceItem';
import SelectModal from './SelectModal';
import ConfigModal from './ConfigModal';
import * as builtinTtsServices from '../../../../../services/tts';
import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance';

export default function Tts(props) {
const { pluginList } = props;
Expand Down Expand Up @@ -76,7 +78,12 @@ export default function Tts(props) {
{...provided.droppableProps}
>
{ttsServiceList !== null &&
ttsServiceList.map((x, i) => {
ttsServiceList.filter(instanceKey => {
return whetherAvailableService(instanceKey, {
[ServiceSourceType.BUILDIN]: builtinTtsServices,
[ServiceSourceType.PLUGIN]: pluginList
})
}).map((x, i) => {
return (
<Draggable
key={x}
Expand Down
9 changes: 5 additions & 4 deletions src/window/Config/pages/Service/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Translate from './Translate';
import Recognize from './Recognize';
import Collection from './Collection';
import Tts from './Tts';
import { ServiceType } from '../../../../utils/service_instance';

let unlisten = null;

Expand Down Expand Up @@ -66,25 +67,25 @@ export default function Service() {
key='translate'
title={t(`config.service.translate`)}
>
<Translate pluginList={pluginList['translate']} />
<Translate pluginList={pluginList[ServiceType.TRANSLATE]} />
</Tab>
<Tab
key='recognize'
title={t(`config.service.recognize`)}
>
<Recognize pluginList={pluginList['recognize']} />
<Recognize pluginList={pluginList[ServiceType.RECOGNIZE]} />
</Tab>
<Tab
key='tts'
title={t(`config.service.tts`)}
>
<Tts pluginList={pluginList['tts']} />
<Tts pluginList={pluginList[ServiceType.TTS]} />
</Tab>
<Tab
key='collection'
title={t(`config.service.collection`)}
>
<Collection pluginList={pluginList['collection']} />
<Collection pluginList={pluginList[ServiceType.COLLECTION]} />
</Tab>
</Tabs>
)
Expand Down
16 changes: 11 additions & 5 deletions src/window/Recognize/ControlArea/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useConfig } from '../../../hooks';
import { textAtom } from '../TextArea';
import { pluginListAtom } from '..';
import { osType } from '../../../utils/env';
import { whetherAvailableService } from '../../../utils/service_instance';

export const serviceNameAtom = atom();
export const languageAtom = atom();
Expand Down Expand Up @@ -53,8 +54,8 @@ export default function ControlArea() {
serviceName.startsWith('[plugin]')
? pluginList[serviceName].icon
: builtinService[serviceName].info.icon === 'system'
? `logo/${osType}.svg`
: builtinService[serviceName].info.icon
? `logo/${osType}.svg`
: builtinService[serviceName].info.icon
}
/>
}
Expand All @@ -71,7 +72,12 @@ export default function ControlArea() {
setServiceName(key);
}}
>
{serviceList.map((name) => {
{serviceList.filter(instanceKey => {
return whetherAvailableService(instanceKey, {
[ServiceSourceType.BUILDIN]: builtinService,
[ServiceSourceType.PLUGIN]: pluginList
})
}).map((name) => {
return (
<DropdownItem
key={name}
Expand All @@ -82,8 +88,8 @@ export default function ControlArea() {
name.startsWith('[plugin]')
? pluginList[name].icon
: builtinService[name].info.icon === 'system'
? `logo/${osType}.svg`
: builtinService[name].info.icon
? `logo/${osType}.svg`
: builtinService[name].info.icon
}
/>
}
Expand Down
14 changes: 13 additions & 1 deletion src/window/Translate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { osType } from '../../utils/env';
import { useConfig } from '../../hooks';
import { store } from '../../utils/store';
import { info } from 'tauri-plugin-log-api';

import * as builtinTranslateServices from '../../services/translate';
import { ServiceSourceType, ServiceType, whetherAvailableService } from '../../utils/service_instance';

let blurTimeout = null;
let resizeTimeout = null;
let moveTimeout = null;
Expand Down Expand Up @@ -268,7 +272,15 @@ export default function Translate() {
{...provided.droppableProps}
>
{translateServiceInstanceList !== null && serviceInstanceConfigMap !== null &&
translateServiceInstanceList.map((serviceInstanceKey, index) => {
translateServiceInstanceList.filter(serviceInstanceKey => {
return whetherAvailableService(
serviceInstanceKey,
{
[ServiceSourceType.PLUGIN]: pluginList[ServiceType.TRANSLATE],
[ServiceSourceType.BUILDIN]: builtinTranslateServices
}
)
}).map((serviceInstanceKey, index) => {
const config = serviceInstanceConfigMap[serviceInstanceKey] ?? {};
const enable = config['enable'] ?? true;

Expand Down

0 comments on commit a5f432b

Please sign in to comment.