mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-10-25 21:22:20 +08:00
- 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.
82 lines
1.8 KiB
TypeScript
82 lines
1.8 KiB
TypeScript
/* eslint-disable @eslint-react/set-state-in-effect */
|
|
import { useEffect } from 'foxact/use-abortable-effect'
|
|
import { useCallback, useState } from 'react'
|
|
|
|
import { showNotice } from '@/services/notice-service'
|
|
|
|
interface UseEditorDocumentOptions {
|
|
open: boolean
|
|
load: () => Promise<string>
|
|
}
|
|
|
|
export const useEditorDocument = ({ open, load }: UseEditorDocumentOptions) => {
|
|
const [value, setValue] = useState('')
|
|
const [savedValue, setSavedValue] = useState('')
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
const resetDocumentState = useCallback(() => {
|
|
setValue('')
|
|
setSavedValue('')
|
|
setLoading(true)
|
|
}, [])
|
|
|
|
const applyLoadedValue = useCallback(
|
|
(nextValue: string | null | undefined) => {
|
|
const normalized = nextValue ?? ''
|
|
setValue(normalized)
|
|
setSavedValue(normalized)
|
|
return normalized
|
|
},
|
|
[],
|
|
)
|
|
|
|
const reload = useCallback(async () => {
|
|
setLoading(true)
|
|
try {
|
|
return applyLoadedValue(await load())
|
|
} catch (error) {
|
|
showNotice.error(error)
|
|
throw error
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}, [applyLoadedValue, load])
|
|
|
|
useEffect(
|
|
(signal) => {
|
|
resetDocumentState()
|
|
|
|
if (!open) return
|
|
|
|
load()
|
|
.then((nextValue) => {
|
|
if (signal.aborted) return
|
|
applyLoadedValue(nextValue)
|
|
})
|
|
.catch((error) => {
|
|
if (!signal.aborted) showNotice.error(error)
|
|
})
|
|
.finally(() => {
|
|
if (!signal.aborted) setLoading(false)
|
|
})
|
|
},
|
|
[applyLoadedValue, load, open, resetDocumentState],
|
|
)
|
|
|
|
const markSaved = useCallback((nextValue: string) => {
|
|
setSavedValue(nextValue)
|
|
}, [])
|
|
|
|
const dirty = value !== savedValue
|
|
|
|
return {
|
|
value,
|
|
setValue,
|
|
savedValue,
|
|
loading,
|
|
dirty,
|
|
markSaved,
|
|
reload,
|
|
}
|
|
}
|