From 4e78b50b2d3a7486cc7b8dc55e5a4e26cf0bce0f Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Sun, 23 Jun 2024 18:34:14 +0800 Subject: [PATCH] feat: support multiple instance for translate service --- src/hooks/useConfig.jsx | 7 +-- src/services/translate/alibaba/Config.jsx | 29 ++++++++-- src/services/translate/alibaba/index.jsx | 8 +-- src/services/translate/baidu/Config.jsx | 29 ++++++++-- src/services/translate/baidu/index.jsx | 8 +-- src/services/translate/baidu_field/Config.jsx | 30 +++++++++-- src/services/translate/baidu_field/index.jsx | 8 +-- src/services/translate/caiyun/Config.jsx | 29 ++++++++-- src/services/translate/caiyun/index.jsx | 8 +-- src/services/translate/chatglm/Config.jsx | 29 ++++++++-- src/services/translate/chatglm/index.jsx | 7 +-- src/services/translate/deepl/Config.jsx | 29 ++++++++-- src/services/translate/deepl/index.jsx | 11 ++-- src/services/translate/geminipro/Config.jsx | 29 ++++++++-- src/services/translate/geminipro/index.jsx | 7 +-- src/services/translate/google/Config.jsx | 29 ++++++++-- src/services/translate/google/index.jsx | 8 +-- src/services/translate/niutrans/Config.jsx | 29 ++++++++-- src/services/translate/niutrans/index.jsx | 9 +--- src/services/translate/ollama/Config.jsx | 34 +++++++++--- src/services/translate/ollama/index.jsx | 7 +-- src/services/translate/openai/Config.jsx | 54 +++++++++---------- src/services/translate/tencent/Config.jsx | 33 +++++++++--- src/services/translate/tencent/index.jsx | 8 +-- src/services/translate/transmart/Config.jsx | 29 ++++++++-- src/services/translate/transmart/index.jsx | 8 +-- src/services/translate/volcengine/Config.jsx | 29 ++++++++-- src/services/translate/volcengine/index.jsx | 8 +-- src/services/translate/youdao/Config.jsx | 29 ++++++++-- src/services/translate/youdao/index.jsx | 8 +-- .../pages/Service/PluginConfig/index.jsx | 29 ++++++++-- 31 files changed, 427 insertions(+), 192 deletions(-) diff --git a/src/hooks/useConfig.jsx b/src/hooks/useConfig.jsx index 1f1f31125a..5559507ff2 100644 --- a/src/hooks/useConfig.jsx +++ b/src/hooks/useConfig.jsx @@ -13,7 +13,8 @@ export const useConfig = (key, defaultValue, options = {}) => { debounce((v) => { store.set(key, v); store.save(); - emit(`${key}_changed`, v); + let eventKey = key.replaceAll('.', '_').replaceAll('@', ':'); + emit(`${eventKey}_changed`, v); }), [] ); @@ -44,8 +45,8 @@ export const useConfig = (key, defaultValue, options = {}) => { // 初始化 useEffect(() => { syncToState(null); - if (key.includes('[')) return; - const unlisten = listen(`${key}_changed`, (e) => { + const eventKey = key.replaceAll('.', '_').replaceAll('@', ':'); + const unlisten = listen(`${eventKey}_changed`, (e) => { syncToState(e.payload); }); return () => { diff --git a/src/services/translate/alibaba/Config.jsx b/src/services/translate/alibaba/Config.jsx index 953103d7e1..a34f608b3e 100644 --- a/src/services/translate/alibaba/Config.jsx +++ b/src/services/translate/alibaba/Config.jsx @@ -1,3 +1,4 @@ +import { INSTANCE_NAME_CONFIG_KEY } from '../../../utils/service_instance'; import { Input, Button } from '@nextui-org/react'; import toast, { Toaster } from 'react-hot-toast'; import { useTranslation } from 'react-i18next'; @@ -10,10 +11,12 @@ import { translate } from './index'; import { Language } from './index'; export function Config(props) { - const { updateServiceList, onClose } = props; + const { instanceKey, updateServiceList, onClose } = props; + const { t } = useTranslation(); const [config, setConfig] = useConfig( - 'alibaba', + instanceKey, { + [INSTANCE_NAME_CONFIG_KEY]: t('services.translate.alibaba.title'), accesskey_id: '', accesskey_secret: '', }, @@ -21,7 +24,6 @@ export function Config(props) { ); const [isLoading, setIsLoading] = useState(false); - const { t } = useTranslation(); const toastStyle = useToastStyle(); return ( @@ -34,7 +36,7 @@ export function Config(props) { () => { setIsLoading(false); setConfig(config, true); - updateServiceList('alibaba'); + updateServiceList(instanceKey); onClose(); }, (e) => { @@ -45,6 +47,25 @@ export function Config(props) { }} > +
+ { + setConfig({ + ...config, + [INSTANCE_NAME_CONFIG_KEY]: value, + }); + }} + /> +

{t('services.help')}