From a5f432b3648bd2c2c7d5a20238a8af7d93d11ffe Mon Sep 17 00:00:00 2001 From: xtyuns Date: Mon, 17 Jun 2024 17:20:45 +0800 Subject: [PATCH] refactor: hide invalid services (#836) --- src/utils/service_instance.ts | 17 +++++++++++++++-- src/window/Config/pages/History/index.jsx | 6 +++++- .../Config/pages/Service/Collection/index.jsx | 9 ++++++++- .../Config/pages/Service/Recognize/index.jsx | 10 +++++++++- .../Config/pages/Service/Translate/index.jsx | 10 +++++++++- src/window/Config/pages/Service/Tts/index.jsx | 9 ++++++++- src/window/Config/pages/Service/index.jsx | 9 +++++---- src/window/Recognize/ControlArea/index.jsx | 16 +++++++++++----- src/window/Translate/index.jsx | 14 +++++++++++++- 9 files changed, 83 insertions(+), 17 deletions(-) diff --git a/src/utils/service_instance.ts b/src/utils/service_instance.ts index 6c8bb5a7d4..10fa7d7162 100644 --- a/src/utils/service_instance.ts +++ b/src/utils/service_instance.ts @@ -1,3 +1,10 @@ +export enum ServiceType { + TRANSLATE = 'translate', + RECOGNIZE = 'recognize', + TTS = 'tts', + COLLECTION = 'Collection' +} + export enum ServiceSourceType { BUILDIN = 'buildin', PLUGIN = 'plugin', @@ -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' \ No newline at end of file +export const INSTANCE_NAME_CONFIG_KEY = 'instanceName' + +export function whetherAvailableService(serviceInstanceKey: string, availableServices: Record>) { + const serviceSourceType = getServiceSouceType(serviceInstanceKey) + const serviceName = getServiceName(serviceInstanceKey) + return availableServices[serviceSourceType]?.[serviceName] !== undefined + +} diff --git a/src/window/Config/pages/History/index.jsx b/src/window/Config/pages/History/index.jsx index 0a68296a63..9fd385af5c 100644 --- a/src/window/Config/pages/History/index.jsx +++ b/src/window/Config/pages/History/index.jsx @@ -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', []); @@ -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] + }) && ( {item.service.startsWith('[plugin]') ? ( diff --git a/src/window/Config/pages/Service/Collection/index.jsx b/src/window/Config/pages/Service/Collection/index.jsx index 36d84b485c..cd507ff284 100644 --- a/src/window/Config/pages/Service/Collection/index.jsx +++ b/src/window/Config/pages/Service/Collection/index.jsx @@ -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; @@ -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 ( {recognizeServiceList !== null && - recognizeServiceList.map((x, i) => { + recognizeServiceList.filter(instanceKey => { + return whetherAvailableService(instanceKey, { + [ServiceSourceType.BUILDIN]: builtinRecognizeServices, + [ServiceSourceType.PLUGIN]: pluginList + }) + }).map((x, i) => { return ( {translateServiceInstanceList !== null && - translateServiceInstanceList.map((x, i) => { + translateServiceInstanceList.filter(instanceKey => { + return whetherAvailableService(instanceKey, { + [ServiceSourceType.BUILDIN]: builtinTranslateServices, + [ServiceSourceType.PLUGIN]: pluginList + }) + }).map((x, i) => { return ( {ttsServiceList !== null && - ttsServiceList.map((x, i) => { + ttsServiceList.filter(instanceKey => { + return whetherAvailableService(instanceKey, { + [ServiceSourceType.BUILDIN]: builtinTtsServices, + [ServiceSourceType.PLUGIN]: pluginList + }) + }).map((x, i) => { return ( - + - + - + - + ) diff --git a/src/window/Recognize/ControlArea/index.jsx b/src/window/Recognize/ControlArea/index.jsx index 851fa802dd..244a8f244e 100644 --- a/src/window/Recognize/ControlArea/index.jsx +++ b/src/window/Recognize/ControlArea/index.jsx @@ -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(); @@ -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 } /> } @@ -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 ( } diff --git a/src/window/Translate/index.jsx b/src/window/Translate/index.jsx index ef1b409dbd..30cb87b426 100644 --- a/src/window/Translate/index.jsx +++ b/src/window/Translate/index.jsx @@ -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; @@ -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;