-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathindex.jsx
166 lines (159 loc) · 7.43 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
import { Card, Spacer, Button, useDisclosure } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import React, { useState } from 'react';
import { useToastStyle } from '../../../../../hooks';
import SelectPluginModal from '../SelectPluginModal';
import { osType } from '../../../../../utils/env';
import { useConfig } from '../../../../../hooks';
import ServiceItem from './ServiceItem';
import SelectModal from './SelectModal';
import ConfigModal from './ConfigModal';
import * as builtinTranslateServices from '../../../../../services/translate';
import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance';
export default function Translate(props) {
const { pluginList } = props;
const {
isOpen: isSelectPluginOpen,
onOpen: onSelectPluginOpen,
onOpenChange: onSelectPluginOpenChange,
} = useDisclosure();
const { isOpen: isSelectOpen, onOpen: onSelectOpen, onOpenChange: onSelectOpenChange } = useDisclosure();
const { isOpen: isConfigOpen, onOpen: onConfigOpen, onOpenChange: onConfigOpenChange } = useDisclosure();
const [currentConfigKey, setCurrentConfigKey] = useState('deepl');
// now it's service instance list
const [translateServiceInstanceList, setTranslateServiceInstanceList] = useConfig('translate_service_list', [
'deepl',
'bing',
'yandex',
'google',
]);
const { t } = useTranslation();
const toastStyle = useToastStyle();
const reorder = (list, startIndex, endIndex) => {
const result = Array.from(list);
const [removed] = result.splice(startIndex, 1);
result.splice(endIndex, 0, removed);
return result;
};
const onDragEnd = async (result) => {
if (!result.destination) return;
const items = reorder(translateServiceInstanceList, result.source.index, result.destination.index);
setTranslateServiceInstanceList(items);
};
const deleteServiceInstance = (instanceKey) => {
if (translateServiceInstanceList.length === 1) {
toast.error(t('config.service.least'), { style: toastStyle });
return;
} else {
setTranslateServiceInstanceList(translateServiceInstanceList.filter((x) => x !== instanceKey));
}
};
const updateServiceInstanceList = (instanceKey) => {
if (translateServiceInstanceList.includes(instanceKey)) {
return;
} else {
const newList = [...translateServiceInstanceList, instanceKey];
setTranslateServiceInstanceList(newList);
}
};
return (
<>
<Toaster />
<Card
className={`${osType === 'Linux' ? 'h-[calc(100vh-140px)]' : 'h-[calc(100vh-120px)]'
} overflow-y-auto p-5 flex justify-between`}
>
<DragDropContext onDragEnd={onDragEnd}>
<Droppable
droppableId='droppable'
direction='vertical'
>
{(provided) => (
<div
className='overflow-y-auto h-full'
ref={provided.innerRef}
{...provided.droppableProps}
>
{translateServiceInstanceList !== null &&
translateServiceInstanceList.filter(instanceKey => {
return whetherAvailableService(instanceKey, {
[ServiceSourceType.BUILDIN]: builtinTranslateServices,
[ServiceSourceType.PLUGIN]: pluginList
})
}).map((x, i) => {
return (
<Draggable
key={x}
draggableId={x}
index={i}
>
{(provided) => {
return (
<div
ref={provided.innerRef}
{...provided.draggableProps}
>
<ServiceItem
{...provided.dragHandleProps}
key={x}
serviceInstanceKey={x}
pluginList={pluginList}
deleteServiceInstance={deleteServiceInstance}
setCurrentConfigKey={setCurrentConfigKey}
onConfigOpen={onConfigOpen}
/>
<Spacer y={2} />
</div>
);
}}
</Draggable>
);
})}
</div>
)}
</Droppable>
</DragDropContext>
<Spacer y={2} />
<div className='flex'>
<Button
fullWidth
onPress={onSelectOpen}
>
{t('config.service.add_builtin_service')}
</Button>
<Spacer x={2} />
<Button
fullWidth
onPress={onSelectPluginOpen}
>
{t('config.service.add_external_service')}
</Button>
</div>
</Card>
<SelectPluginModal
isOpen={isSelectPluginOpen}
onOpenChange={onSelectPluginOpenChange}
setConfigName={setCurrentConfigKey}
onConfigOpen={onConfigOpen}
pluginType='translate'
pluginList={pluginList}
deleteService={deleteServiceInstance}
/>
<SelectModal
isOpen={isSelectOpen}
onOpenChange={onSelectOpenChange}
setCurrentConfigKey={setCurrentConfigKey}
onConfigOpen={onConfigOpen}
/>
<ConfigModal
serviceInstanceKey={currentConfigKey}
pluginList={pluginList}
isOpen={isConfigOpen}
onOpenChange={onConfigOpenChange}
updateServiceInstanceList={updateServiceInstanceList}
/>
</>
);
}