forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-settings-form.ts
More file actions
286 lines (237 loc) · 7.31 KB
/
Copy pathuse-settings-form.ts
File metadata and controls
286 lines (237 loc) · 7.31 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
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 i18next from 'i18next'
import { useEffect, useMemo, useRef } from 'react'
import {
useForm,
type UseFormProps,
type FieldValues,
type FieldNamesMarkedBoolean,
} from 'react-hook-form'
import { toast } from 'sonner'
type SettingsFormOptions<T extends FieldValues> = UseFormProps<T> & {
onSubmit: (data: T, changedFields: Record<string, unknown>) => Promise<void>
compareValues?: (a: unknown, b: unknown) => boolean
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function flattenValues<T extends FieldValues>(
values: T,
parentKey?: string,
result: Record<string, unknown> = {}
): Record<string, unknown> {
if (!isPlainObject(values)) {
if (typeof parentKey === 'string') {
result[parentKey] = values
}
return result
}
Object.entries(values).forEach(([key, value]) => {
const nextKey = parentKey ? `${parentKey}.${key}` : key
if (isPlainObject(value)) {
flattenValues(value as FieldValues, nextKey, result)
return
}
if (Array.isArray(value)) {
result[nextKey] = value
return
}
result[nextKey] = value
})
return result
}
function collectDirtyValues<TFieldValues extends FieldValues>(
dirtyFields: Partial<FieldNamesMarkedBoolean<TFieldValues>> | boolean,
values: unknown,
parentKey?: string,
result: Record<string, unknown> = {}
) {
if (dirtyFields === true) {
if (typeof parentKey === 'string') {
result[parentKey] = values
}
return result
}
if (!dirtyFields || typeof dirtyFields !== 'object') {
return result
}
Object.entries(dirtyFields).forEach(([key, value]) => {
const nextKey = parentKey ? `${parentKey}.${key}` : key
const source =
values && (isPlainObject(values) || Array.isArray(values))
? (values as Record<string, unknown>)[key]
: undefined
if (value === true) {
result[nextKey] = source
return
}
if (value && typeof value === 'object') {
collectDirtyValues(
value as Partial<FieldNamesMarkedBoolean<FieldValues>>,
source as FieldValues,
nextKey,
result
)
}
})
return result
}
function setDeepValue(
target: Record<string, unknown>,
path: string,
value: unknown
) {
const segments = path.split('.')
let current: Record<string, unknown> = target
segments.forEach((segment, index) => {
const isLast = index === segments.length - 1
if (isLast) {
current[segment] = value
return
}
const next = current[segment]
if (isPlainObject(next)) {
current = next as Record<string, unknown>
return
}
current[segment] = {}
current = current[segment] as Record<string, unknown>
})
}
function expandDotPaths<T extends FieldValues>(
values: T | undefined
): T | undefined {
if (!values || !isPlainObject(values)) {
return values
}
const result: Record<string, unknown> = {}
Object.entries(values).forEach(([key, value]) => {
if (key.includes('.')) {
setDeepValue(result, key, value)
return
}
if (isPlainObject(value)) {
result[key] = expandDotPaths(value as FieldValues)
return
}
result[key] = value
})
return result as T
}
/**
* Unified hook for system settings forms
*
* Key features:
* - Initializes form with defaultValues only on mount
* - No automatic resets that could overwrite user input
* - Tracks changed fields to minimize API calls
* - Provides manual reset functionality
*
* @example
* ```tsx
* const { form, handleSubmit, handleReset } = useSettingsForm({
* resolver: zodResolver(schema),
* defaultValues,
* onSubmit: async (data, changed) => {
* for (const [key, value] of Object.entries(changed)) {
* await updateOption.mutateAsync({ key, value })
* }
* }
* })
* ```
*/
export function useSettingsForm<T extends FieldValues>({
onSubmit,
compareValues,
defaultValues,
...formOptions
}: SettingsFormOptions<T>) {
const expandedDefaults = useMemo(
() => expandDotPaths(defaultValues),
[defaultValues]
)
const form = useForm<T>({ ...formOptions, defaultValues: expandedDefaults })
const defaultValuesRef = useRef<T>((expandedDefaults ?? ({} as T)) as T)
const baselineRef = useRef<Record<string, unknown>>(
flattenValues((expandedDefaults ?? ({} as T)) as T)
)
/* eslint-disable react-hooks/refs */
const serializedDefaultsRef = useRef<string>(
JSON.stringify(baselineRef.current)
)
/* eslint-enable react-hooks/refs */
useEffect(() => {
if (!expandedDefaults) return
const flattened = flattenValues(expandedDefaults as T)
const serialized = JSON.stringify(flattened)
if (serialized === serializedDefaultsRef.current) {
return
}
baselineRef.current = flattened
defaultValuesRef.current = expandedDefaults as T
serializedDefaultsRef.current = serialized
form.reset(expandedDefaults as T)
}, [expandedDefaults, form])
const defaultCompare = (a: unknown, b: unknown): boolean => {
if (a === b) return true
if (Array.isArray(a) && Array.isArray(b)) {
return JSON.stringify(a) === JSON.stringify(b)
}
if (typeof a !== typeof b) return false
// Handle arrays
// Handle objects (but not null)
if (a && b && typeof a === 'object' && typeof b === 'object') {
return JSON.stringify(a) === JSON.stringify(b)
}
return false
}
const compare = compareValues || defaultCompare
const handleSubmit = async (data: T) => {
const dirtyValues = collectDirtyValues(form.formState.dirtyFields, data)
const changedEntries = Object.entries(dirtyValues).filter(
([key, value]) => !compare(value, baselineRef.current[key])
)
if (changedEntries.length === 0) {
toast.info(i18next.t('No changes to save'))
return
}
const changedFields = changedEntries.reduce<Record<string, unknown>>(
(acc, [key, value]) => {
acc[key] = value
return acc
},
{}
)
await onSubmit(data, changedFields)
const flattenedValues = flattenValues(data)
baselineRef.current = flattenedValues
defaultValuesRef.current = data
serializedDefaultsRef.current = JSON.stringify(flattenedValues)
form.reset(data)
}
const handleReset = () => {
form.reset(defaultValuesRef.current)
toast.success(i18next.t('Form reset to saved values'))
}
return {
form,
// eslint-disable-next-line react-hooks/refs
handleSubmit: form.handleSubmit(handleSubmit),
handleReset,
isDirty: form.formState.isDirty,
isSubmitting: form.formState.isSubmitting,
}
}