refactor: structure config validation outcomes

- Replace boolean/string validation results with structured ValidationOutcome values across config validation, profile switching, DNS validation, and script validation. Callers can now distinguish valid, invalid, skipped, and busy states, with invalid results carrying an error kind and message.

- Route core config updates through structured outcomes and checked forced updates. Generation and validation failures now discard runtime drafts and surface validation failures instead of relying on empty success messages.

- Preserve existing frontend ergonomics by keeping boolean service wrappers where callers only need success, while direct DNS and script validation consumers receive ValidationOutcome. Failed profile editor saves reload the backend-restored file content and synchronize Monaco models so reopened editors show the restored state.
This commit is contained in:
Tunglies
2026-04-25 13:31:08 +08:00
parent 5882722031
commit b85b435e00
26 changed files with 612 additions and 482 deletions
+13
View File
@@ -70,6 +70,13 @@ export const EditorViewer = ({
const resolvedTitle = title ?? t('profiles.components.menu.editFile')
const disableSave = loading || saveDisabled || dirty === false
const syncEditorValue = useCallback(() => {
const model = editorRef.current?.getModel()
if (model && model.getValue() !== value) {
model.setValue(value)
}
}, [value])
const syncMaximizedState = useCallback(async () => {
try {
setIsMaximized(await appWindow.isMaximized())
@@ -149,6 +156,11 @@ export const EditorViewer = ({
void syncMaximizedState()
}, [open, syncMaximizedState])
useEffect(() => {
if (!open || loading) return
syncEditorValue()
}, [loading, open, syncEditorValue])
useEffect(() => {
if (!open) return
@@ -209,6 +221,7 @@ export const EditorViewer = ({
beforeMount={beforeEditorMount}
onMount={(editorInstance) => {
editorRef.current = editorInstance
syncEditorValue()
}}
onChange={(nextValue) => onChange?.(nextValue ?? '')}
onValidate={onValidate}
@@ -508,7 +508,11 @@ export const GroupsEditorViewer = (props: Props) => {
setCurrData(nextData)
}
await saveProfileFile(property, nextData)
if (!(await saveProfileFile(property, nextData))) {
await fetchContent()
onClose()
return
}
showNotice.success('shared.feedback.notifications.saved')
setPrevData(nextData)
onSave?.(prevData, nextData)
+12 -3
View File
@@ -630,7 +630,10 @@ export const ProfileItem = (props: Props) => {
const handleSaveProfileDocument = useLockFn(async () => {
const currentValue = profileDocument.value
await saveProfileFile(uid, currentValue)
if (!(await saveProfileFile(uid, currentValue))) {
await profileDocument.reload()
return
}
onSave?.(profileDocument.savedValue, currentValue)
profileDocument.markSaved(currentValue)
})
@@ -638,7 +641,10 @@ export const ProfileItem = (props: Props) => {
const handleSaveMergeDocument = useLockFn(async () => {
const mergeUid = option?.merge ?? ''
const currentValue = mergeDocument.value
await saveProfileFile(mergeUid, currentValue)
if (!(await saveProfileFile(mergeUid, currentValue))) {
await mergeDocument.reload()
return
}
onSave?.(mergeDocument.savedValue, currentValue)
mergeDocument.markSaved(currentValue)
})
@@ -646,7 +652,10 @@ export const ProfileItem = (props: Props) => {
const handleSaveScriptDocument = useLockFn(async () => {
const scriptUid = option?.script ?? ''
const currentValue = scriptDocument.value
await saveProfileFile(scriptUid, currentValue)
if (!(await saveProfileFile(scriptUid, currentValue))) {
await scriptDocument.reload()
return
}
onSave?.(scriptDocument.savedValue, currentValue)
scriptDocument.markSaved(currentValue)
})
+4 -1
View File
@@ -86,7 +86,10 @@ export const ProfileMore = (props: Props) => {
const handleSave = useLockFn(async () => {
const currentValue = document.value
await saveProfileFile(id, currentValue)
if (!(await saveProfileFile(id, currentValue))) {
await document.reload()
return
}
onSave?.(document.savedValue, currentValue)
document.markSaved(currentValue)
})
@@ -355,7 +355,11 @@ export const ProxiesEditorViewer = (props: Props) => {
const handleSave = useLockFn(async () => {
try {
await saveProfileFile(property, currData)
if (!(await saveProfileFile(property, currData))) {
await fetchContent()
onClose()
return
}
showNotice.success('shared.feedback.notifications.saved')
onSave?.(prevData, currData)
onClose()
@@ -565,7 +565,11 @@ export const RulesEditorViewer = (props: Props) => {
const handleSave = useLockFn(async () => {
try {
await saveProfileFile(property, currData)
if (!(await saveProfileFile(property, currData))) {
await fetchContent()
onClose()
return
}
showNotice.success('shared.feedback.notifications.saved')
onSave?.(prevData, currData)
onClose()
+6 -2
View File
@@ -528,12 +528,16 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
await invoke('save_dns_config', { dnsConfig: config })
// 验证配置
const [isValid, errorMsg] = await invoke<[boolean, string]>(
const validation = await invoke<ValidationOutcome>(
'validate_dns_config',
{},
)
if (!isValid) {
if (validation.status !== 'valid') {
const errorMsg =
validation.status === 'invalid'
? validation.message
: 'Configuration validation skipped'
let cleanErrorMsg = errorMsg
// 提取关键错误信息