-
-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
294 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/window/Config/pages/Service/Recognize/PluginConfig/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { Button, Input } from '@nextui-org/react'; | ||
import toast, { Toaster } from 'react-hot-toast'; | ||
import React, { useState } from 'react'; | ||
import { useAtomValue } from 'jotai'; | ||
|
||
import { useConfig, useToastStyle } from '../../../../../../hooks'; | ||
import { invoke } from '@tauri-apps/api'; | ||
import { pluginListAtom } from '..'; | ||
|
||
export function PluginConfig(props) { | ||
const pluginList = useAtomValue(pluginListAtom); | ||
const { updateServiceList, onClose, name } = props; | ||
const [loading, setLoading] = useState(false); | ||
const [pluginConfig, setPluginConfig] = useConfig(name, {}, { sync: false }); | ||
|
||
const toastStyle = useToastStyle(); | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<> | ||
<Toaster /> | ||
{pluginList[name].needs.length === 0 ? ( | ||
<div>{t('services.no_need')}</div> | ||
) : ( | ||
pluginList[name].needs.map((x) => { | ||
return ( | ||
pluginConfig && ( | ||
<div | ||
key={x.key} | ||
className={`config-item`} | ||
> | ||
<h3 className='my-auto'>{x.display}</h3> | ||
<Input | ||
value={`${pluginConfig.hasOwnProperty(x.key) ? pluginConfig[x.key] : ''}`} | ||
variant='bordered' | ||
className='max-w-[50%]' | ||
onValueChange={(value) => { | ||
setPluginConfig({ | ||
...pluginConfig, | ||
[x.key]: value, | ||
}); | ||
}} | ||
/> | ||
</div> | ||
) | ||
); | ||
}) | ||
)} | ||
|
||
<div> | ||
<Button | ||
isLoading={loading} | ||
fullWidth | ||
color='primary' | ||
onPress={() => { | ||
if (Object.keys(pluginConfig).length !== 0) { | ||
setLoading(true); | ||
invoke('invoke_plugin', { | ||
name, | ||
pluginType: 'recognize', | ||
text: 'Hello', | ||
from: pluginList[name].language['auto'], | ||
to: pluginList[name].language['zh_cn'], | ||
needs: pluginConfig, | ||
}).then( | ||
(_) => { | ||
setLoading(false); | ||
setPluginConfig(pluginConfig, true); | ||
updateServiceList(name); | ||
onClose(); | ||
}, | ||
(err) => { | ||
setLoading(false); | ||
toast.error(err.toString(), { style: toastStyle }); | ||
} | ||
); | ||
} else { | ||
setPluginConfig(pluginConfig, true); | ||
updateServiceList(name); | ||
onClose(); | ||
} | ||
}} | ||
> | ||
{t('common.save')} | ||
</Button> | ||
</div> | ||
</> | ||
); | ||
} |
133 changes: 133 additions & 0 deletions
133
src/window/Config/pages/Service/Recognize/SelectPluginModal/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, Button } from '@nextui-org/react'; | ||
import { removeDir, BaseDirectory } from '@tauri-apps/api/fs'; | ||
import toast, { Toaster } from 'react-hot-toast'; | ||
import { MdDeleteOutline } from 'react-icons/md'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { open } from '@tauri-apps/api/dialog'; | ||
import { invoke } from '@tauri-apps/api'; | ||
import React, { useState } from 'react'; | ||
import { useAtomValue } from 'jotai'; | ||
|
||
import { useToastStyle } from '../../../../../../hooks'; | ||
import { pluginListAtom } from '..'; | ||
|
||
export default function SelectPluginModal(props) { | ||
const { isOpen, onOpenChange, setConfigName, onConfigOpen, getPluginList } = props; | ||
const [installing, setInstalling] = useState(false); | ||
const pluginList = useAtomValue(pluginListAtom); | ||
const { t } = useTranslation(); | ||
const toastStyle = useToastStyle(); | ||
|
||
return ( | ||
<Modal | ||
isOpen={isOpen} | ||
onOpenChange={onOpenChange} | ||
scrollBehavior='inside' | ||
> | ||
<Toaster /> | ||
<ModalContent className='max-h-[80vh]'> | ||
{(onClose) => ( | ||
<> | ||
<ModalHeader>{t('config.service.add_service')}</ModalHeader> | ||
<ModalBody> | ||
{Object.keys(pluginList).map((x) => { | ||
return ( | ||
<div | ||
className='flex justify-between' | ||
key={x} | ||
> | ||
<Button | ||
fullWidth | ||
className='mr-[8px]' | ||
onPress={() => { | ||
setConfigName(x); | ||
onConfigOpen(); | ||
}} | ||
> | ||
<div className='w-full'>{pluginList[x].display}</div> | ||
</Button> | ||
<Button | ||
isIconOnly | ||
color='danger' | ||
variant='flat' | ||
onPress={() => { | ||
removeDir(`plugins/recognize/${x}`, { | ||
dir: BaseDirectory.AppConfig, | ||
recursive: true, | ||
}).then( | ||
(v) => { | ||
toast.success(t('config.service.uninstall_success'), { | ||
style: toastStyle, | ||
}); | ||
getPluginList(); | ||
}, | ||
(e) => { | ||
toast.error(e.toString(), { style: toastStyle }); | ||
} | ||
); | ||
}} | ||
> | ||
<MdDeleteOutline className='text-xl' /> | ||
</Button> | ||
</div> | ||
); | ||
})} | ||
<div> | ||
<Button | ||
fullWidth | ||
isLoading={installing} | ||
color='secondary' | ||
variant='flat' | ||
onPress={async () => { | ||
setInstalling(true); | ||
const selected = await open({ | ||
multiple: true, | ||
directory: false, | ||
filters: [ | ||
{ | ||
name: '*.potext', | ||
extensions: ['potext'], | ||
}, | ||
], | ||
}); | ||
if (selected !== null) { | ||
invoke('install_plugin', { | ||
pathList: selected, | ||
pluginType: 'recognize', | ||
}).then( | ||
(count) => { | ||
setInstalling(false); | ||
toast.success('Installed ' + count + ' plugins', { | ||
style: toastStyle, | ||
}); | ||
getPluginList(); | ||
}, | ||
(e) => { | ||
setInstalling(false); | ||
toast.error(e.toString(), { style: toastStyle }); | ||
} | ||
); | ||
} else { | ||
setInstalling(false); | ||
} | ||
}} | ||
> | ||
<div className='w-full'>{t('config.service.install_plugin')}</div> | ||
</Button> | ||
</div> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button | ||
color='danger' | ||
variant='light' | ||
onClick={onClose} | ||
> | ||
{t('common.cancel')} | ||
</Button> | ||
</ModalFooter> | ||
</> | ||
)} | ||
</ModalContent> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters