mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-23 13:02:18 +08:00
* perf(ui): hoist static style, menu and OS identities Dialog controls rebuilt static option arrays, style objects and label maps on every render; the sysproxy viewer also probed the OS and split the default bypass string per render. These are now module constants or single memos keyed on their real inputs (translation function, active stage). * perf(ui): memoize derived validation and list computations The TUN dialog re-split its route-exclusion list, the rule provider dialog re-sorted its entries and the group-delays hook re-stringified its group key on every render; each now recomputes only when its input identity changes. The provider-button hunk re-indents its JSX block because the sorted entries moved out of the render expression. * perf(ui): hoist hot regexes and stabilize clash mutators The clash log parser and the URI validators recompiled their patterns per call; the patterns are module constants now. mutateClash and invalidateClashConfig are stable callbacks so consumers can keep them in dependency arrays; mutateClash revalidates through the module-level query helper instead of the per-render refetch closure (no caller reads its resolved value). * perf(ui): batch notice removal and hoist drag sensors hideNoticesByCode removed matching notices one by one, notifying subscribers per item; it now filters in one pass and notifies once. The chain list, test card and grouped editor list allocated their drag sensor arrays per render; they are module constants now. * refactor(ui): share the switch adapter and drop dead memos Three settings files each defined the same boolean switch-format adapter; it is exported once from the Switch module. The update dialog memoized trivial arithmetic, the profile-more reset callback keyed on a per-render document object instead of its stable setter, and the profile viewer's imperative handle had no dependency array.
165 lines
3.8 KiB
TypeScript
165 lines
3.8 KiB
TypeScript
import { useLockFn } from 'ahooks'
|
|
import i18n from 'i18next'
|
|
import { useCallback } from 'react'
|
|
import { getVersion } from 'tauri-plugin-mihomo-api'
|
|
|
|
import {
|
|
getClashInfo,
|
|
getClashMode,
|
|
getRuntimeConfig,
|
|
patchClashConfig,
|
|
} from '@/services/cmds'
|
|
import { mutate } from '@/services/mutate'
|
|
import {
|
|
revalidateQuery,
|
|
setCacheData,
|
|
useQuery,
|
|
} from '@/services/query-client'
|
|
|
|
type MutateClashUpdater =
|
|
| ((old: IConfigData | undefined) => IConfigData | undefined)
|
|
| IConfigData
|
|
| undefined
|
|
|
|
const PORT_KEYS = [
|
|
'port',
|
|
'socks-port',
|
|
'mixed-port',
|
|
'redir-port',
|
|
'tproxy-port',
|
|
] as const
|
|
|
|
type ClashInfoPatch = Partial<
|
|
Pick<
|
|
IConfigData,
|
|
| 'port'
|
|
| 'socks-port'
|
|
| 'mixed-port'
|
|
| 'redir-port'
|
|
| 'tproxy-port'
|
|
| 'external-controller'
|
|
| 'secret'
|
|
>
|
|
>
|
|
|
|
const hasClashInfoPayload = (patch: ClashInfoPatch) =>
|
|
PORT_KEYS.some((key) => patch[key] != null) ||
|
|
patch['external-controller'] != null ||
|
|
patch.secret != null
|
|
|
|
const validatePortRange = (port: number) => {
|
|
if (port < 1000) {
|
|
throw new Error(
|
|
i18n.t(($) => $.settings.modals.clashPort.messages.portTooLow),
|
|
)
|
|
}
|
|
if (port > 65535) {
|
|
throw new Error(
|
|
i18n.t(($) => $.settings.modals.clashPort.messages.portTooHigh),
|
|
)
|
|
}
|
|
}
|
|
|
|
const validatePorts = (patch: ClashInfoPatch) => {
|
|
PORT_KEYS.forEach((key) => {
|
|
const port = patch[key]
|
|
if (!port) return
|
|
validatePortRange(port)
|
|
})
|
|
}
|
|
|
|
export const useRuntimeConfig = (shouldFetch: boolean = true) => {
|
|
return useQuery({
|
|
queryKey: ['getRuntimeConfig'],
|
|
queryFn: getRuntimeConfig,
|
|
enabled: shouldFetch,
|
|
})
|
|
}
|
|
|
|
// Fault-tolerant fallback for the current proxy mode, read straight from the
|
|
// saved clash config on the backend (bypasses the strict BaseConfig path).
|
|
export const useClashMode = (shouldFetch: boolean = true) => {
|
|
return useQuery({
|
|
queryKey: ['getClashMode'],
|
|
queryFn: getClashMode,
|
|
enabled: shouldFetch,
|
|
})
|
|
}
|
|
|
|
export const useClash = () => {
|
|
const { data: clash } = useRuntimeConfig()
|
|
|
|
const { data: versionData, refetch: mutateVersion } = useQuery({
|
|
queryKey: ['getVersion'],
|
|
queryFn: getVersion,
|
|
})
|
|
|
|
// Stable identity: consumers pass mutateClash into their own callbacks.
|
|
// revalidateQuery hits the same cache key refetch would, without capturing
|
|
// the per-render refetch closure.
|
|
const mutateClash = useCallback(
|
|
(updater?: MutateClashUpdater, revalidate?: boolean) => {
|
|
if (updater === undefined) {
|
|
return revalidateQuery(['getRuntimeConfig'])
|
|
}
|
|
void setCacheData<IConfigData>(['getRuntimeConfig'], updater)
|
|
if (revalidate !== false) {
|
|
return revalidateQuery(['getRuntimeConfig'])
|
|
}
|
|
return Promise.resolve()
|
|
},
|
|
[],
|
|
)
|
|
|
|
const patchClash = useLockFn(async (patch: Partial<IConfigData>) => {
|
|
await mutate(() => patchClashConfig(patch), {
|
|
id: 'patch-clash-config',
|
|
revalidate: [['getRuntimeConfig']],
|
|
errorNotice: false,
|
|
})
|
|
})
|
|
|
|
const version = versionData?.meta
|
|
? `${versionData.version} Mihomo`
|
|
: versionData?.version || '-'
|
|
|
|
return {
|
|
clash,
|
|
version,
|
|
mutateClash,
|
|
mutateVersion,
|
|
patchClash,
|
|
}
|
|
}
|
|
|
|
export const useClashInfo = () => {
|
|
const { data: clashInfo, refetch: mutateInfo } = useQuery({
|
|
queryKey: ['getClashInfo'],
|
|
queryFn: getClashInfo,
|
|
})
|
|
|
|
const patchInfo = useLockFn(async (patch: ClashInfoPatch) => {
|
|
if (!hasClashInfoPayload(patch)) return
|
|
|
|
validatePorts(patch)
|
|
|
|
await mutate(() => patchClashConfig(patch), {
|
|
id: 'patch-clash-info',
|
|
revalidate: [['getClashInfo'], ['getClashConfig']],
|
|
errorNotice: false,
|
|
})
|
|
})
|
|
|
|
const invalidateClashConfig = useCallback(
|
|
() => revalidateQuery(['getClashConfig']),
|
|
[],
|
|
)
|
|
|
|
return {
|
|
clashInfo,
|
|
mutateInfo,
|
|
patchInfo,
|
|
invalidateClashConfig,
|
|
}
|
|
}
|