forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-settings-visual-editor.tsx
More file actions
197 lines (172 loc) · 5.76 KB
/
Copy pathchat-settings-visual-editor.tsx
File metadata and controls
197 lines (172 loc) · 5.76 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { Plus, Search } from 'lucide-react'
import { useState, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { StaticDataTable } from '@/components/data-table/static/static-data-table'
import { StaticRowActions } from '@/components/data-table/static/static-row-actions'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { safeJsonParseWithValidation } from '../utils/json-parser'
import { isArray } from '../utils/json-validators'
import { ChatDialog, type ChatEntryData } from './chat-dialog'
type ChatSettingsVisualEditorProps = {
value: string
onChange: (value: string) => void
}
type ChatEntry = ChatEntryData
export function ChatSettingsVisualEditor({
value,
onChange,
}: ChatSettingsVisualEditorProps) {
const { t } = useTranslation()
const [searchText, setSearchText] = useState('')
const [dialogOpen, setDialogOpen] = useState(false)
const [editData, setEditData] = useState<ChatEntry | null>(null)
const chats = useMemo(() => {
const parsed = safeJsonParseWithValidation<unknown[]>(value, {
fallback: [],
validator: isArray,
validatorMessage: 'Chats must be a JSON array',
context: 'chats',
})
return parsed
.map((item) => {
if (typeof item === 'object' && item !== null && !Array.isArray(item)) {
const entries = Object.entries(item)
if (entries.length === 1) {
const [name, url] = entries[0]
return { name, url: String(url) }
}
}
return null
})
.filter((item): item is ChatEntry => item !== null)
}, [value])
const filteredChats = useMemo(() => {
if (!searchText) return chats
const lowerSearch = searchText.toLowerCase()
return chats.filter(
(chat) =>
chat.name.toLowerCase().includes(lowerSearch) ||
chat.url.toLowerCase().includes(lowerSearch)
)
}, [chats, searchText])
const handleSave = (data: ChatEntryData) => {
const chatsArray = safeJsonParseWithValidation<unknown[]>(value, {
fallback: [],
validator: isArray,
silent: true,
})
let updatedArray = [...chatsArray]
if (editData) {
updatedArray = updatedArray.filter((item) => {
if (typeof item === 'object' && item !== null && !Array.isArray(item)) {
return !Object.keys(item).includes(editData.name)
}
return true
})
}
updatedArray.push({ [data.name]: data.url })
onChange(JSON.stringify(updatedArray, null, 2))
}
const handleDelete = (name: string) => {
const chatsArray = safeJsonParseWithValidation<unknown[]>(value, {
fallback: [],
validator: isArray,
silent: true,
})
const updatedArray = chatsArray.filter((item) => {
if (typeof item === 'object' && item !== null && !Array.isArray(item)) {
return !Object.keys(item).includes(name)
}
return true
})
onChange(JSON.stringify(updatedArray, null, 2))
}
const handleEdit = (chat: ChatEntry) => {
setEditData(chat)
setDialogOpen(true)
}
const handleAdd = () => {
setEditData(null)
setDialogOpen(true)
}
return (
<div className='space-y-4'>
<div className='flex items-center gap-4'>
<div className='relative flex-1'>
<Search className='text-muted-foreground absolute top-2.5 left-2.5 h-4 w-4' />
<Input
placeholder={t('Search chat presets...')}
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
className='pl-9'
/>
</div>
<Button onClick={handleAdd}>
<Plus className='mr-2 h-4 w-4' />
{t('Add chat preset')}
</Button>
</div>
<StaticDataTable
data={filteredChats}
getRowKey={(chat) => chat.name}
emptyContent={
searchText
? t('No chat presets match your search')
: t(
'No chat presets configured. Click "Add chat preset" to get started.'
)
}
columns={[
{
id: 'name',
header: t('Chat Client Name'),
cellClassName: 'font-medium',
cell: (chat) => chat.name,
},
{
id: 'url',
header: t('URL'),
cellClassName: 'max-w-md truncate font-mono text-sm',
cell: (chat) => chat.url,
},
{
id: 'actions',
header: t('Actions'),
className: 'text-right',
cellClassName: 'text-right',
cell: (chat) => (
<StaticRowActions
editLabel={t('Edit')}
deleteLabel={t('Delete')}
menuLabel={t('Open menu')}
onEdit={() => handleEdit(chat)}
onDelete={() => handleDelete(chat.name)}
/>
),
},
]}
/>
<ChatDialog
open={dialogOpen}
onOpenChange={setDialogOpen}
onSave={handleSave}
editData={editData}
/>
</div>
)
}