Spaces:
Sleeping
Sleeping
File size: 12,212 Bytes
74e7808 | 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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | import { useCallback, useMemo, useRef, useState } from 'react';
import { createParsedApiError, getParsedApiError, type ParsedApiError } from '../api/error';
import { systemConfigApi, SystemConfigConflictError, SystemConfigValidationError } from '../api/systemConfig';
import type {
ConfigValidationIssue,
SystemConfigCategorySchema,
SystemConfigItem,
SystemConfigUpdateItem,
} from '../types/systemConfig';
type ToastState = {
type: 'success';
message: string;
} | {
type: 'error';
error: ParsedApiError;
} | null;
type RetryAction = 'load' | 'save' | null;
type SaveResult = {
success: boolean;
message?: string;
issues?: ConfigValidationIssue[];
};
const CATEGORY_DISPLAY_ORDER: Record<string, number> = {
base: 10,
ai_model: 20,
data_source: 30,
notification: 40,
system: 50,
agent: 55,
backtest: 60,
uncategorized: 99,
};
function sortItemsByOrder(items: SystemConfigItem[]): SystemConfigItem[] {
return [...items].sort((a, b) => {
const left = a.schema?.displayOrder ?? 9999;
const right = b.schema?.displayOrder ?? 9999;
if (left !== right) {
return left - right;
}
return a.key.localeCompare(b.key);
});
}
function isMultiValueSchema(schema: SystemConfigItem['schema'] | undefined): boolean {
const validation = (schema?.validation ?? {}) as Record<string, unknown>;
return Boolean(validation.multiValue ?? validation.multi_value);
}
function normalizeFieldValue(value: string, schema: SystemConfigItem['schema'] | undefined): string {
if (!isMultiValueSchema(schema)) {
return value;
}
return value
.split(',')
.map((entry) => entry.trim())
.filter((entry) => entry.length > 0)
.join(',');
}
export function useSystemConfig() {
// Server state
const [configVersion, setConfigVersion] = useState<string>('');
const [maskToken, setMaskToken] = useState<string>('******');
const [serverItems, setServerItems] = useState<SystemConfigItem[]>([]);
// UI state
const [draftValues, setDraftValues] = useState<Record<string, string>>({});
const [activeCategory, setActiveCategory] = useState<string>('base');
const [validationIssues, setValidationIssues] = useState<ConfigValidationIssue[]>([]);
const [toast, setToast] = useState<ToastState>(null);
// Request state
const [isLoading, setIsLoading] = useState(false);
const [isSaving, setIsSaving] = useState(false);
const [loadError, setLoadError] = useState<ParsedApiError | null>(null);
const [saveError, setSaveError] = useState<ParsedApiError | null>(null);
const [retryAction, setRetryAction] = useState<RetryAction>(null);
const serverItemByKeyRef = useRef<Record<string, SystemConfigItem>>({});
const mergedItems = useMemo(() => {
return sortItemsByOrder(
serverItems.map((item) => ({
...item,
value: draftValues[item.key] ?? item.value,
})),
);
}, [draftValues, serverItems]);
const serverItemByKey = useMemo(() => {
const map: Record<string, SystemConfigItem> = {};
for (const item of serverItems) {
map[item.key] = item;
}
serverItemByKeyRef.current = map;
return map;
}, [serverItems]);
const categories = useMemo<SystemConfigCategorySchema[]>(() => {
// Infer tabs from loaded config item schema metadata.
const categoryMap = new Map<string, SystemConfigCategorySchema>();
for (const item of mergedItems) {
if (!item.schema) {
continue;
}
const category = item.schema.category;
if (!categoryMap.has(category)) {
categoryMap.set(category, {
category,
title: category.replace('_', ' ').replace(/\b\w/g, (char) => char.toUpperCase()),
description: '',
displayOrder: CATEGORY_DISPLAY_ORDER[category] ?? 999,
fields: [],
});
}
categoryMap.get(category)?.fields.push(item.schema);
}
return [...categoryMap.values()].sort((a, b) => a.displayOrder - b.displayOrder);
}, [mergedItems]);
const itemsByCategory = useMemo(() => {
const map: Record<string, SystemConfigItem[]> = {};
for (const item of mergedItems) {
const category = item.schema?.category ?? 'uncategorized';
if (!map[category]) {
map[category] = [];
}
map[category].push(item);
}
return map;
}, [mergedItems]);
const dirtyKeys = useMemo(() => {
const keys: string[] = [];
for (const item of serverItems) {
const draftRaw = draftValues[item.key];
if (draftRaw === undefined) {
continue;
}
const normalizedDraft = normalizeFieldValue(draftRaw, item.schema);
const normalizedCurrent = normalizeFieldValue(item.value, item.schema);
if (normalizedDraft !== normalizedCurrent) {
keys.push(item.key);
}
}
return keys;
}, [draftValues, serverItems]);
const hasDirty = dirtyKeys.length > 0;
const issueByKey = useMemo(() => {
const map: Record<string, ConfigValidationIssue[]> = {};
for (const issue of validationIssues) {
if (!map[issue.key]) {
map[issue.key] = [];
}
map[issue.key].push(issue);
}
return map;
}, [validationIssues]);
const applyServerPayload = useCallback(
(
items: SystemConfigItem[],
version: string,
token: string,
options?: { preserveDirty?: boolean; committedKeys?: string[] },
) => {
const sorted = sortItemsByOrder(items);
const previousServerMap = serverItemByKeyRef.current;
const committedKeys = new Set(options?.committedKeys ?? []);
const preserveDirty = options?.preserveDirty ?? false;
setServerItems(sorted);
setConfigVersion(version);
setMaskToken(token || '******');
setDraftValues((prevDraft) => {
const nextDraft: Record<string, string> = {};
for (const item of sorted) {
if (committedKeys.has(item.key)) {
nextDraft[item.key] = item.value;
continue;
}
if (preserveDirty) {
const previousServerValue = previousServerMap[item.key]?.value;
const hasDraft = prevDraft[item.key] !== undefined;
const wasDirty = hasDraft && prevDraft[item.key] !== previousServerValue;
nextDraft[item.key] = wasDirty ? prevDraft[item.key] : item.value;
continue;
}
nextDraft[item.key] = item.value;
}
return nextDraft;
});
const defaultCategory = sorted[0]?.schema?.category || 'base';
setActiveCategory((current) => {
const exists = sorted.some((item) => item.schema?.category === current);
return exists ? current : defaultCategory;
});
setValidationIssues([]);
},
[],
);
const load = useCallback(async () => {
setIsLoading(true);
setLoadError(null);
setRetryAction(null);
try {
const config = await systemConfigApi.getConfig(true);
applyServerPayload(config.items, config.configVersion, config.maskToken);
setToast(null);
} catch (error: unknown) {
setLoadError(getParsedApiError(error));
setRetryAction('load');
} finally {
setIsLoading(false);
}
}, [applyServerPayload]);
const resetDraft = useCallback(() => {
const next: Record<string, string> = {};
for (const item of serverItems) {
next[item.key] = item.value;
}
setDraftValues(next);
setValidationIssues([]);
setSaveError(null);
}, [serverItems]);
const applyPartialUpdate = useCallback((updatedItems: Array<{ key: string; value: string }>) => {
setDraftValues((prevDraft) => {
const nextDraft = { ...prevDraft };
for (const item of updatedItems) {
nextDraft[item.key] = item.value;
}
return nextDraft;
});
}, []);
const refreshAfterExternalSave = useCallback(
async (committedKeys: string[]) => {
const config = await systemConfigApi.getConfig(true);
applyServerPayload(config.items, config.configVersion, config.maskToken, {
preserveDirty: true,
committedKeys,
});
},
[applyServerPayload],
);
const setDraftValue = useCallback((key: string, value: string) => {
setDraftValues((previous) => ({
...previous,
[key]: value,
}));
}, []);
const getChangedItems = useCallback((): SystemConfigUpdateItem[] => {
return dirtyKeys
.map((key) => {
const serverItem = serverItemByKey[key];
const normalizedValue = normalizeFieldValue(draftValues[key] ?? '', serverItem?.schema);
return {
key,
value: normalizedValue,
};
})
.filter((item) => {
const serverItem = serverItemByKey[item.key];
const normalizedCurrent = normalizeFieldValue(serverItem?.value ?? '', serverItem?.schema);
return item.value !== normalizedCurrent;
});
}, [dirtyKeys, draftValues, serverItemByKey]);
const save = useCallback(async (): Promise<SaveResult> => {
if (!hasDirty) {
setToast({ type: 'success', message: '当前没有可保存的修改。' });
return { success: true, message: '当前没有可保存的修改' };
}
setIsSaving(true);
setSaveError(null);
setRetryAction(null);
const changedItems = getChangedItems();
try {
const validateResult = await systemConfigApi.validate({ items: changedItems });
setValidationIssues(validateResult.issues || []);
if (!validateResult.valid) {
setSaveError(createParsedApiError({
title: '配置校验未通过',
message: '请先修正表单错误后再保存。',
rawMessage: '配置校验未通过,请先修正表单错误。',
category: 'http_error',
}));
setRetryAction('save');
return {
success: false,
message: '配置校验未通过',
issues: validateResult.issues,
};
}
const updateResult = await systemConfigApi.update({
configVersion,
maskToken,
reloadNow: true,
items: changedItems,
});
const refreshed = await systemConfigApi.getConfig(true);
applyServerPayload(refreshed.items, refreshed.configVersion, refreshed.maskToken);
const warningText = updateResult.warnings?.length
? `;警告:${updateResult.warnings.join(';')}`
: '';
setToast({ type: 'success', message: `配置已更新${warningText}` });
return { success: true };
} catch (error: unknown) {
if (error instanceof SystemConfigValidationError) {
setValidationIssues(error.issues);
setSaveError(error.parsedError);
} else if (error instanceof SystemConfigConflictError) {
setSaveError(createParsedApiError({
title: '配置版本冲突',
message: `${error.message},请先重新加载配置。`,
rawMessage: error.parsedError.rawMessage,
status: error.parsedError.status,
category: error.parsedError.category,
}));
} else {
setSaveError(getParsedApiError(error));
}
setToast({ type: 'error', error: getParsedApiError(error) });
setRetryAction('save');
return { success: false, message: '保存失败' };
} finally {
setIsSaving(false);
}
}, [
applyServerPayload,
configVersion,
getChangedItems,
hasDirty,
maskToken,
]);
const retry = useCallback(async () => {
if (retryAction === 'load') {
await load();
return;
}
if (retryAction === 'save') {
await save();
}
}, [load, retryAction, save]);
const clearToast = useCallback(() => {
setToast(null);
}, []);
return {
// Server state
configVersion,
maskToken,
serverItems,
categories,
itemsByCategory,
issueByKey,
// UI state
activeCategory,
setActiveCategory,
hasDirty,
dirtyCount: dirtyKeys.length,
toast,
clearToast,
// Request state
isLoading,
isSaving,
loadError,
saveError,
retryAction,
// Actions
load,
retry,
save,
resetDraft,
setDraftValue,
applyPartialUpdate,
refreshAfterExternalSave,
};
}
|