-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathindex.jsx
93 lines (89 loc) · 3.46 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { readDir, BaseDirectory, readTextFile, exists } from '@tauri-apps/api/fs';
import { listen } from '@tauri-apps/api/event';
import { useTranslation } from 'react-i18next';
import { Tabs, Tab } from '@nextui-org/react';
import { appConfigDir, join } from '@tauri-apps/api/path';
import { convertFileSrc } from '@tauri-apps/api/tauri';
import React, { useEffect, useState } from 'react';
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;
export default function Service() {
const [pluginList, setPluginList] = useState(null);
const { t } = useTranslation();
const loadPluginList = async () => {
const serviceTypeList = ['translate', 'tts', 'recognize', 'collection'];
let temp = {};
for (const serviceType of serviceTypeList) {
temp[serviceType] = {};
if (await exists(`plugins/${serviceType}`, { dir: BaseDirectory.AppConfig })) {
const plugins = await readDir(`plugins/${serviceType}`, { dir: BaseDirectory.AppConfig });
for (const plugin of plugins) {
const infoStr = await readTextFile(`plugins/${serviceType}/${plugin.name}/info.json`, {
dir: BaseDirectory.AppConfig,
});
let pluginInfo = JSON.parse(infoStr);
if ('icon' in pluginInfo) {
const appConfigDirPath = await appConfigDir();
const iconPath = await join(
appConfigDirPath,
`/plugins/${serviceType}/${plugin.name}/${pluginInfo.icon}`
);
pluginInfo.icon = convertFileSrc(iconPath);
}
temp[serviceType][plugin.name] = pluginInfo;
}
}
}
setPluginList({ ...temp });
};
useEffect(() => {
loadPluginList();
if (unlisten) {
unlisten.then((f) => {
f();
});
}
unlisten = listen('reload_plugin_list', loadPluginList);
return () => {
if (unlisten) {
unlisten.then((f) => {
f();
});
}
};
}, []);
return (
pluginList !== null && (
<Tabs className='flex justify-center max-h-[calc(100%-40px)] overflow-y-auto'>
<Tab
key='translate'
title={t(`config.service.translate`)}
>
<Translate pluginList={pluginList[ServiceType.TRANSLATE]} />
</Tab>
<Tab
key='recognize'
title={t(`config.service.recognize`)}
>
<Recognize pluginList={pluginList[ServiceType.RECOGNIZE]} />
</Tab>
<Tab
key='tts'
title={t(`config.service.tts`)}
>
<Tts pluginList={pluginList[ServiceType.TTS]} />
</Tab>
<Tab
key='collection'
title={t(`config.service.collection`)}
>
<Collection pluginList={pluginList[ServiceType.COLLECTION]} />
</Tab>
</Tabs>
)
);
}