-
-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathindex.jsx
160 lines (154 loc) · 7.01 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
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 builtinTtsServices from '../../../../../services/tts';
import { ServiceSourceType, whetherAvailableService } from '../../../../../utils/service_instance';
export default function Tts(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 [openConfigName, setOpenConfigName] = useState('lingva_tts');
const [ttsServiceList, setTtsServiceList] = useConfig('tts_service_list', ['lingva_tts']);
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(ttsServiceList, result.source.index, result.destination.index);
setTtsServiceList(items);
};
const deleteService = (name) => {
if (ttsServiceList.length === 1) {
toast.error(t('config.service.least'), { style: toastStyle });
return;
} else {
setTtsServiceList(ttsServiceList.filter((x) => x !== name));
}
};
const updateServiceList = (name) => {
if (ttsServiceList.includes(name)) {
return;
} else {
const newList = [...ttsServiceList, name];
setTtsServiceList(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}
>
{ttsServiceList !== null &&
ttsServiceList.filter(instanceKey => {
return whetherAvailableService(instanceKey, {
[ServiceSourceType.BUILDIN]: builtinTtsServices,
[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}
name={x}
key={x}
pluginList={pluginList}
deleteService={deleteService}
setConfigName={setOpenConfigName}
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={setOpenConfigName}
onConfigOpen={onConfigOpen}
pluginType='tts'
pluginList={pluginList}
deleteService={deleteService}
/>
<SelectModal
isOpen={isSelectOpen}
onOpenChange={onSelectOpenChange}
setConfigName={setOpenConfigName}
onConfigOpen={onConfigOpen}
/>
<ConfigModal
name={openConfigName}
isOpen={isConfigOpen}
pluginList={pluginList}
onOpenChange={onConfigOpenChange}
updateServiceList={updateServiceList}
/>
</>
);
}