From 4801ed527543c0220661176e735d33b89440b697 Mon Sep 17 00:00:00 2001 From: xtyuns Date: Sun, 9 Jun 2024 18:04:24 +0800 Subject: [PATCH 1/3] hide invalid services --- src/utils/service_instance.ts | 16 ++++++++++++++-- src/window/Config/pages/History/index.jsx | 6 +++++- .../Config/pages/Service/Collection/index.jsx | 9 ++++++++- .../Config/pages/Service/Recognize/index.jsx | 9 ++++++++- .../Config/pages/Service/Translate/index.jsx | 10 +++++++++- src/window/Config/pages/Service/Tts/index.jsx | 9 ++++++++- src/window/Translate/index.jsx | 14 +++++++++++++- 7 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/utils/service_instance.ts b/src/utils/service_instance.ts index 6c8bb5a7d4..dae42d12c8 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,14 @@ 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, availableServiceNames: Record) { + const serviceSourceType = getServiceSouceType(serviceInstanceKey) + const serviceName = getServiceName(serviceInstanceKey) + return availableServiceNames[serviceSourceType].findIndex((it: string) => it === serviceName) != -1 +} diff --git a/src/window/Config/pages/History/index.jsx b/src/window/Config/pages/History/index.jsx index 0a68296a63..508732e33a 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, 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 + }) && ( {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..293aa561a9 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 { 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 ( {translateServiceInstanceList !== null && serviceInstanceConfigMap !== null && - translateServiceInstanceList.map((serviceInstanceKey, index) => { + translateServiceInstanceList.filter(serviceInstanceKey => { + return whetherAvailableService( + serviceInstanceKey, + { + [ServiceSourceType.PLUGIN]: pluginList, + [ServiceSourceType.BUILDIN]: builtinTranslateServices + } + ) + }).map((serviceInstanceKey, index) => { const config = serviceInstanceConfigMap[serviceInstanceKey] ?? {}; const enable = config['enable'] ?? true; From 71ee5a9535f49c6a0c49cc1df3f23fcd2e7f7da8 Mon Sep 17 00:00:00 2001 From: xtyuns Date: Sun, 16 Jun 2024 19:54:57 +0800 Subject: [PATCH 2/3] Recognize ControlArea --- src/window/Recognize/ControlArea/index.jsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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 ( } From 3d79ebe4d716bc09241202e310457a4ff6b73e70 Mon Sep 17 00:00:00 2001 From: xtyuns Date: Sun, 16 Jun 2024 23:31:45 +0800 Subject: [PATCH 3/3] fix error --- src/utils/service_instance.ts | 5 +++-- src/window/Config/pages/History/index.jsx | 4 ++-- src/window/Config/pages/Service/Collection/index.jsx | 4 ++-- src/window/Config/pages/Service/Recognize/index.jsx | 5 +++-- src/window/Config/pages/Service/Translate/index.jsx | 2 +- src/window/Config/pages/Service/Tts/index.jsx | 4 ++-- src/window/Config/pages/Service/index.jsx | 9 +++++---- src/window/Translate/index.jsx | 6 +++--- 8 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/utils/service_instance.ts b/src/utils/service_instance.ts index dae42d12c8..10fa7d7162 100644 --- a/src/utils/service_instance.ts +++ b/src/utils/service_instance.ts @@ -42,8 +42,9 @@ export function getDisplayInstanceName(instanceName: string, serviceNameSupplier export const INSTANCE_NAME_CONFIG_KEY = 'instanceName' -export function whetherAvailableService(serviceInstanceKey: string, availableServiceNames: Record) { +export function whetherAvailableService(serviceInstanceKey: string, availableServices: Record>) { const serviceSourceType = getServiceSouceType(serviceInstanceKey) const serviceName = getServiceName(serviceInstanceKey) - return availableServiceNames[serviceSourceType].findIndex((it: string) => it === serviceName) != -1 + return availableServices[serviceSourceType]?.[serviceName] !== undefined + } diff --git a/src/window/Config/pages/History/index.jsx b/src/window/Config/pages/History/index.jsx index 508732e33a..9fd385af5c 100644 --- a/src/window/Config/pages/History/index.jsx +++ b/src/window/Config/pages/History/index.jsx @@ -17,7 +17,7 @@ import { useConfig, useToastStyle } from '../../../../hooks'; import { LanguageFlag } from '../../../../utils/language'; import { store } from '../../../../utils/store'; import { osType } from '../../../../utils/env'; -import { ServiceSourceType, whetherAvailableService } from '../../../../utils/service_instance'; +import { ServiceSourceType, ServiceType, whetherAvailableService } from '../../../../utils/service_instance'; export default function History() { const [collectionServiceList] = useConfig('collection_service_list', []); @@ -148,7 +148,7 @@ export default function History() { > {(item) => whetherAvailableService(item.service, { [ServiceSourceType.BUILDIN]: builtinServices, - [ServiceSourceType.PLUGIN]: pluginList + [ServiceSourceType.PLUGIN]: pluginList[ServiceType.TRANSLATE] }) && ( diff --git a/src/window/Config/pages/Service/Collection/index.jsx b/src/window/Config/pages/Service/Collection/index.jsx index 293aa561a9..cd507ff284 100644 --- a/src/window/Config/pages/Service/Collection/index.jsx +++ b/src/window/Config/pages/Service/Collection/index.jsx @@ -9,8 +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 { whetherAvailableService } from '../../../../../utils/service_instance'; +import * as builtinCollectionServices from '../../../../../services/collection'; +import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance'; export default function Collection(props) { const { pluginList } = props; diff --git a/src/window/Config/pages/Service/Recognize/index.jsx b/src/window/Config/pages/Service/Recognize/index.jsx index 3186a7b03a..a1c44d5f7b 100644 --- a/src/window/Config/pages/Service/Recognize/index.jsx +++ b/src/window/Config/pages/Service/Recognize/index.jsx @@ -11,8 +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 { whetherAvailableService } from '../../../../../utils/service_instance'; +import * as builtinRecognizeServices from '../../../../../services/recognize'; +import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance'; + export default function Recognize(props) { const { pluginList } = props; diff --git a/src/window/Config/pages/Service/Translate/index.jsx b/src/window/Config/pages/Service/Translate/index.jsx index 634375954c..97e288a9d1 100644 --- a/src/window/Config/pages/Service/Translate/index.jsx +++ b/src/window/Config/pages/Service/Translate/index.jsx @@ -12,7 +12,7 @@ import ServiceItem from './ServiceItem'; import SelectModal from './SelectModal'; import ConfigModal from './ConfigModal'; -import * as builtinTranslateServices from '../../../../services/translate'; +import * as builtinTranslateServices from '../../../../../services/translate'; import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance'; export default function Translate(props) { diff --git a/src/window/Config/pages/Service/Tts/index.jsx b/src/window/Config/pages/Service/Tts/index.jsx index 32cf91f786..aeaad1d65c 100644 --- a/src/window/Config/pages/Service/Tts/index.jsx +++ b/src/window/Config/pages/Service/Tts/index.jsx @@ -11,8 +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 { whetherAvailableService } from '../../../../../utils/service_instance'; +import * as builtinTtsServices from '../../../../../services/tts'; +import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance'; export default function Tts(props) { const { pluginList } = props; diff --git a/src/window/Config/pages/Service/index.jsx b/src/window/Config/pages/Service/index.jsx index 3b62a33897..4c53c21081 100644 --- a/src/window/Config/pages/Service/index.jsx +++ b/src/window/Config/pages/Service/index.jsx @@ -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; @@ -66,25 +67,25 @@ export default function Service() { key='translate' title={t(`config.service.translate`)} > - + - + - + - + ) diff --git a/src/window/Translate/index.jsx b/src/window/Translate/index.jsx index 165eb3b116..30cb87b426 100644 --- a/src/window/Translate/index.jsx +++ b/src/window/Translate/index.jsx @@ -17,8 +17,8 @@ import { useConfig } from '../../hooks'; import { store } from '../../utils/store'; import { info } from 'tauri-plugin-log-api'; -import * as builtinTranslateServices from '../../../../services/translate'; -import { ServiceSourceType, whetherAvailableService } from '../../utils/service_instance'; +import * as builtinTranslateServices from '../../services/translate'; +import { ServiceSourceType, ServiceType, whetherAvailableService } from '../../utils/service_instance'; let blurTimeout = null; let resizeTimeout = null; @@ -276,7 +276,7 @@ export default function Translate() { return whetherAvailableService( serviceInstanceKey, { - [ServiceSourceType.PLUGIN]: pluginList, + [ServiceSourceType.PLUGIN]: pluginList[ServiceType.TRANSLATE], [ServiceSourceType.BUILDIN]: builtinTranslateServices } )