Skip to content

Commit

Permalink
feat: impl multiple instance for collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Jun 23, 2024
1 parent c196c59 commit 5627d69
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 33 deletions.
34 changes: 31 additions & 3 deletions src/services/collection/anki/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INSTANCE_NAME_CONFIG_KEY } from '../../../utils/service_instance';
import { Button, Input } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
Expand All @@ -10,9 +11,17 @@ import { collection } from './index';

export function Config(props) {
const [isLoading, setIsLoading] = useState(false);
const { updateServiceList, onClose } = props;
const [ankiConfig, setAnkiConfig] = useConfig('anki', { port: 8765 }, { sync: false });
const { t } = useTranslation();
const { instanceKey, updateServiceList, onClose } = props;
const [ankiConfig, setAnkiConfig] = useConfig(
instanceKey,
{
[INSTANCE_NAME_CONFIG_KEY]: t('services.collection.anki.title'),
port: 8765,
},
{ sync: false }
);

const toastStyle = useToastStyle();

return (
Expand All @@ -27,7 +36,7 @@ export function Config(props) {
() => {
setIsLoading(false);
setAnkiConfig(ankiConfig, true);
updateServiceList('anki');
updateServiceList(instanceKey);
onClose();
},
(e) => {
Expand All @@ -37,6 +46,25 @@ export function Config(props) {
);
}}
>
<div className='config-item'>
<Input
label={t('services.instance_name')}
labelPlacement='outside-left'
value={ankiConfig[INSTANCE_NAME_CONFIG_KEY]}
variant='bordered'
classNames={{
base: 'justify-between',
label: 'text-[length:--nextui-font-size-medium]',
mainWrapper: 'max-w-[50%]',
}}
onValueChange={(value) => {
setAnkiConfig({
...ankiConfig,
[INSTANCE_NAME_CONFIG_KEY]: value,
});
}}
/>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.help')}</h3>
<Button
Expand Down
35 changes: 32 additions & 3 deletions src/services/collection/eudic/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INSTANCE_NAME_CONFIG_KEY } from '../../../utils/service_instance';
import { Button, Input } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
Expand All @@ -10,9 +11,18 @@ import { collection } from './index';

export function Config(props) {
const [isLoading, setIsLoading] = useState(false);
const { updateServiceList, onClose } = props;
const [config, setConfig] = useConfig('eudic', { name: 'pot', token: '' }, { sync: false });
const { instanceKey, updateServiceList, onClose } = props;
const { t } = useTranslation();
const [config, setConfig] = useConfig(
instanceKey,
{
[INSTANCE_NAME_CONFIG_KEY]: t('services.collection.eudic.title'),
name: 'pot',
token: '',
},
{ sync: false }
);

const toastStyle = useToastStyle();

return (
Expand All @@ -27,7 +37,7 @@ export function Config(props) {
() => {
setIsLoading(false);
setConfig(config, true);
updateServiceList('eudic');
updateServiceList(instanceKey);
onClose();
},
(e) => {
Expand All @@ -37,6 +47,25 @@ export function Config(props) {
);
}}
>
<div className='config-item'>
<Input
label={t('services.instance_name')}
labelPlacement='outside-left'
value={config[INSTANCE_NAME_CONFIG_KEY]}
variant='bordered'
classNames={{
base: 'justify-between',
label: 'text-[length:--nextui-font-size-medium]',
mainWrapper: 'max-w-[50%]',
}}
onValueChange={(value) => {
setConfig({
...config,
[INSTANCE_NAME_CONFIG_KEY]: value,
});
}}
/>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.help')}</h3>
<Button
Expand Down
10 changes: 2 additions & 8 deletions src/services/collection/eudic/index.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { fetch, Body } from '@tauri-apps/api/http';
import { store } from '../../../utils/store';

export async function collection(source, target, options = {}) {
const { config } = options;

let eudicConfig = (await store.get('eudic')) ?? {};
if (config !== undefined) {
eudicConfig = config;
}
const name = eudicConfig['name'] ?? 'pot';
const token = eudicConfig['token'] ?? '';
const name = config['name'] ?? 'pot';
const token = config['token'] ?? '';

let categoryId = await checkCategory(name, token);
return await addWordToCategory(categoryId, source, token);
Expand Down
32 changes: 24 additions & 8 deletions src/window/Config/pages/History/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ 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';
import {
ServiceSourceType,
ServiceType,
getServiceName,
getServiceSouceType,
whetherAvailableService,
} from '../../../../utils/service_instance';

export default function History() {
const [collectionServiceList] = useConfig('collection_service_list', []);
Expand Down Expand Up @@ -274,19 +280,22 @@ export default function History() {
</Button>
<ButtonGroup>
{collectionServiceList &&
collectionServiceList.map((serviceName) => {
collectionServiceList.map((instanceKey) => {
return (
<Button
key={serviceName}
key={instanceKey}
isIconOnly
variant='light'
onPress={async () => {
if (serviceName.startsWith('plugin')) {
if (
getServiceSouceType(instanceKey) ===
ServiceSourceType.PLUGIN
) {
const pluginConfig =
(await store.get(serviceName)) ?? {};
(await store.get(instanceKey)) ?? {};
let [func, utils] = await invoke_plugin(
'collection',
serviceName
getServiceName(instanceKey)
);
func(selectedItem.text, selectedItem.result, {
config: pluginConfig,
Expand All @@ -307,10 +316,17 @@ export default function History() {
}
);
} else {
builtinCollectionServices[serviceName]
const instanceConfig =
(await store.get(instanceKey)) ?? {};
builtinCollectionServices[
getServiceName(instanceKey)
]
.collection(
selectedItem.text,
selectedItem.result
selectedItem.result,
{
config: instanceConfig,
}
)
.then(
(_) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default function ServiceItem(props) {
variant='light'
color='danger'
onPress={() => {
deleteService(name);
deleteServiceInstance(serviceInstanceKey);
}}
>
<MdDeleteOutline className='text-2xl' />
Expand Down
2 changes: 1 addition & 1 deletion src/window/Config/pages/Service/Collection/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function Collection(props) {
setCollectionServiceInstanceList(items);
};

const deleteService = (instanceKey) => {
const deleteServiceInstance = (instanceKey) => {
setCollectionServiceInstanceList(collectionServiceInstanceList.filter((x) => x !== instanceKey));
deleteKey(instanceKey);
};
Expand Down
29 changes: 20 additions & 9 deletions src/window/Translate/components/TargetArea/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -792,19 +792,23 @@ export default function TargetArea(props) {
</Tooltip>
{/* available collection service instance */}
{collectionServiceList &&
collectionServiceList.map((collectionServiceName) => {
collectionServiceList.map((collectionServiceInstanceName) => {
return (
<Button
key={collectionServiceName}
key={collectionServiceInstanceName}
isIconOnly
variant='light'
size='sm'
onPress={async () => {
if (collectionServiceName.startsWith('plugin')) {
const pluginConfig = (await store.get(collectionServiceName)) ?? {};
if (
getServiceSouceType(collectionServiceInstanceName) ===
ServiceSourceType.PLUGIN
) {
const pluginConfig =
serviceInstanceConfigMap[collectionServiceInstanceName];
let [func, utils] = await invoke_plugin(
'collection',
collectionServiceName
getServiceName(collectionServiceInstanceName)
);
func(sourceText.trim(), result.toString(), {
config: pluginConfig,
Expand All @@ -820,7 +824,9 @@ export default function TargetArea(props) {
}
);
} else {
builtinCollectionServices[collectionServiceName]
const instanceConfig =
serviceInstanceConfigMap[collectionServiceInstanceName];
builtinCollectionServices[collectionServiceInstanceName]
.collection(sourceText, result)
.then(
(_) => {
Expand All @@ -837,9 +843,14 @@ export default function TargetArea(props) {
>
<img
src={
collectionServiceName.startsWith('plugin')
? pluginList['collection'][collectionServiceName].icon
: builtinCollectionServices[collectionServiceName].info.icon
getServiceSouceType(collectionServiceInstanceName) ===
ServiceSourceType.PLUGIN
? pluginList['collection'][
getServiceName(collectionServiceInstanceName)
].icon
: builtinCollectionServices[
getServiceName(collectionServiceInstanceName)
].info.icon
}
className='h-[16px] w-[16px]'
/>
Expand Down

0 comments on commit 5627d69

Please sign in to comment.