mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 21:30:06 +08:00
refactor(frontend): outfox foxact, foxts, and eslint-plugin-sukka (#8808)
* chore(eslint): remove eslint-plugin-sukka * refactor(frontend): remove foxts and inline local utilities * refactor(frontend): remove foxact and replace with local hooks * refactor(proxy): reuse local storage for group preferences
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { useTheme } from '@mui/material/styles'
|
||||
import { useLocalStorage } from 'foxact/use-local-storage'
|
||||
import {
|
||||
memo,
|
||||
useCallback,
|
||||
@@ -13,6 +12,8 @@ import {
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { useLocalStorage } from '@/hooks/use-local-storage'
|
||||
|
||||
import {
|
||||
ConnectionColumnManager,
|
||||
type ConnectionColumnOption,
|
||||
@@ -62,6 +63,19 @@ type ColumnField =
|
||||
type ColumnSizingState = Record<string, number>
|
||||
type VisibilityState = Record<string, boolean>
|
||||
|
||||
const EMPTY_RECORD = {}
|
||||
const EMPTY_COLUMN_ORDER: string[] = []
|
||||
|
||||
const parseColumnVisibility = (value: string): VisibilityState => {
|
||||
const parsed = JSON.parse(value)
|
||||
return parsed && typeof parsed === 'object' ? parsed : EMPTY_RECORD
|
||||
}
|
||||
|
||||
const parseColumnOrder = (value: string): string[] => {
|
||||
const parsed = JSON.parse(value)
|
||||
return Array.isArray(parsed) ? parsed : EMPTY_COLUMN_ORDER
|
||||
}
|
||||
|
||||
interface BaseColumn {
|
||||
field: ColumnField
|
||||
headerName: string
|
||||
@@ -368,42 +382,20 @@ export const ConnectionTable = (props: Props) => {
|
||||
const theme = useTheme()
|
||||
const [columnWidths, setColumnWidths] = useLocalStorage<ColumnSizingState>(
|
||||
'connection-table-widths',
|
||||
{},
|
||||
EMPTY_RECORD,
|
||||
)
|
||||
|
||||
const [columnVisibilityModel, setColumnVisibilityModel] =
|
||||
useLocalStorage<VisibilityState>(
|
||||
'connection-table-visibility',
|
||||
{},
|
||||
{
|
||||
serializer: JSON.stringify,
|
||||
deserializer: (value) => {
|
||||
try {
|
||||
const parsed = JSON.parse(value)
|
||||
if (parsed && typeof parsed === 'object') return parsed
|
||||
} catch (err) {
|
||||
console.warn('Failed to parse connection-table-visibility', err)
|
||||
}
|
||||
return {}
|
||||
},
|
||||
},
|
||||
EMPTY_RECORD,
|
||||
parseColumnVisibility,
|
||||
)
|
||||
|
||||
const [columnOrder, setColumnOrder] = useLocalStorage<string[]>(
|
||||
'connection-table-order',
|
||||
[],
|
||||
{
|
||||
serializer: JSON.stringify,
|
||||
deserializer: (value) => {
|
||||
try {
|
||||
const parsed = JSON.parse(value)
|
||||
if (Array.isArray(parsed)) return parsed
|
||||
} catch (err) {
|
||||
console.warn('Failed to parse connection-table-order', err)
|
||||
}
|
||||
return []
|
||||
},
|
||||
},
|
||||
EMPTY_COLUMN_ORDER,
|
||||
parseColumnOrder,
|
||||
)
|
||||
|
||||
const baseColumns = useMemo<BaseColumn[]>(() => {
|
||||
|
||||
@@ -14,20 +14,18 @@ import {
|
||||
Typography,
|
||||
} from '@mui/material'
|
||||
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
|
||||
import { useEffect } from 'foxact/use-abortable-effect'
|
||||
import { useIntersection } from 'foxact/use-intersection'
|
||||
import { useLocalStorage } from 'foxact/use-local-storage'
|
||||
import type { XOR } from 'foxts/ts-xor'
|
||||
import {
|
||||
forwardRef,
|
||||
memo,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useEffectEvent,
|
||||
useMemo,
|
||||
useState,
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { useLocalStorage } from '@/hooks/use-local-storage'
|
||||
import { getIpInfo } from '@/services/api'
|
||||
import { useQuery } from '@/services/query-client'
|
||||
import getSystem from '@/utils/get-system'
|
||||
@@ -72,15 +70,14 @@ const getCountryFlag = (countryCode: string | undefined) => {
|
||||
return String.fromCodePoint(...codePoints)
|
||||
}
|
||||
|
||||
type CountDownState = XOR<
|
||||
{
|
||||
type: 'countdown'
|
||||
remainingSeconds: number
|
||||
},
|
||||
{
|
||||
type: 'revalidating'
|
||||
}
|
||||
>
|
||||
type CountDownState =
|
||||
| {
|
||||
type: 'countdown'
|
||||
remainingSeconds: number
|
||||
}
|
||||
| {
|
||||
type: 'revalidating'
|
||||
}
|
||||
|
||||
const IPInfoCardContainer = forwardRef<HTMLElement, React.PropsWithChildren>(
|
||||
({ children }, ref) => {
|
||||
@@ -114,10 +111,19 @@ export const IpInfoCard = () => {
|
||||
)
|
||||
const appWindow = useMemo(() => getCurrentWebviewWindow(), [])
|
||||
|
||||
const [hasIntersected, setHasIntersected] = useState(false)
|
||||
// Once intersected, the card stays eligible for refresh until unmount.
|
||||
const [containerRef, hasIntersected, _resetIntersected] = useIntersection({
|
||||
rootMargin: '0px',
|
||||
})
|
||||
const containerRef = useCallback((element: HTMLElement | null) => {
|
||||
if (!element) return
|
||||
const observer = new IntersectionObserver((entries) => {
|
||||
if (entries.some((e) => e.isIntersecting || e.intersectionRatio > 0)) {
|
||||
setHasIntersected(true)
|
||||
observer.disconnect()
|
||||
}
|
||||
})
|
||||
observer.observe(element)
|
||||
return () => observer.disconnect()
|
||||
}, [])
|
||||
|
||||
const [countdown, setCountdown] = useState<CountDownState>({
|
||||
type: 'countdown',
|
||||
|
||||
@@ -19,10 +19,6 @@ import {
|
||||
styled,
|
||||
} from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import {
|
||||
cancelIdleCallback,
|
||||
requestIdleCallback,
|
||||
} from 'foxact/request-idle-callback'
|
||||
import * as yaml from 'js-yaml'
|
||||
import {
|
||||
startTransition,
|
||||
@@ -350,10 +346,12 @@ export const GroupsEditorViewer = (props: Props) => {
|
||||
}
|
||||
}
|
||||
|
||||
const handle = requestIdleCallback(serialize)
|
||||
return () => {
|
||||
cancelIdleCallback(handle)
|
||||
if (typeof window.requestIdleCallback === 'function') {
|
||||
const handle = window.requestIdleCallback(serialize)
|
||||
return () => window.cancelIdleCallback(handle)
|
||||
}
|
||||
const handle = window.setTimeout(serialize, 1)
|
||||
return () => window.clearTimeout(handle)
|
||||
}
|
||||
}, [prependSeq, appendSeq, deleteSeq])
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useCallback, useEffect, useReducer } from 'react'
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
import { useLocalStorage } from '@/hooks/use-local-storage'
|
||||
import { useProfiles } from '@/hooks/use-profiles'
|
||||
|
||||
import { ProxySortType } from './use-filter-sort'
|
||||
@@ -16,9 +17,13 @@ export interface HeadState {
|
||||
testUrl: string
|
||||
}
|
||||
|
||||
type HeadStateStorage = Record<string, Record<string, HeadState>>
|
||||
type ProfileHeadState = Record<string, HeadState>
|
||||
type HeadStateStorage = Record<string, ProfileHeadState>
|
||||
|
||||
const HEAD_STATE_KEY = 'proxy-head-state'
|
||||
const EMPTY_STORAGE: HeadStateStorage = {}
|
||||
const EMPTY_PROFILE: ProfileHeadState = {}
|
||||
|
||||
export const DEFAULT_STATE: HeadState = {
|
||||
open: false,
|
||||
showType: true,
|
||||
@@ -31,77 +36,65 @@ export const DEFAULT_STATE: HeadState = {
|
||||
testUrl: '',
|
||||
}
|
||||
|
||||
type HeadStateAction =
|
||||
| { type: 'reset' }
|
||||
| { type: 'replace'; payload: Record<string, HeadState> }
|
||||
| { type: 'update'; groupName: string; patch: Partial<HeadState> }
|
||||
const parseStorage = (raw: string): HeadStateStorage => {
|
||||
const value = JSON.parse(raw) as HeadStateStorage | null
|
||||
return value && typeof value === 'object' && !Array.isArray(value)
|
||||
? value
|
||||
: EMPTY_STORAGE
|
||||
}
|
||||
|
||||
function headStateReducer(
|
||||
state: Record<string, HeadState>,
|
||||
action: HeadStateAction,
|
||||
): Record<string, HeadState> {
|
||||
switch (action.type) {
|
||||
case 'reset':
|
||||
return {}
|
||||
case 'replace':
|
||||
return action.payload
|
||||
case 'update': {
|
||||
const prev = state[action.groupName] || DEFAULT_STATE
|
||||
return { ...state, [action.groupName]: { ...prev, ...action.patch } }
|
||||
}
|
||||
default:
|
||||
return state
|
||||
}
|
||||
const getProfileState = (storage: HeadStateStorage, profile: string) => {
|
||||
const state = storage[profile]
|
||||
return state && typeof state === 'object' && !Array.isArray(state)
|
||||
? state
|
||||
: EMPTY_PROFILE
|
||||
}
|
||||
|
||||
export function useHeadStateNew() {
|
||||
const { profiles } = useProfiles()
|
||||
const current = profiles?.current || ''
|
||||
|
||||
const [state, dispatch] = useReducer(headStateReducer, {})
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
const data = JSON.parse(
|
||||
localStorage.getItem(HEAD_STATE_KEY)!,
|
||||
) as HeadStateStorage
|
||||
|
||||
const value = data[current] || {}
|
||||
|
||||
if (value && typeof value === 'object') {
|
||||
dispatch({ type: 'replace', payload: value })
|
||||
} else {
|
||||
dispatch({ type: 'reset' })
|
||||
}
|
||||
} catch {
|
||||
dispatch({ type: 'reset' })
|
||||
}
|
||||
}, [current])
|
||||
const [storage, setStorage] = useLocalStorage(
|
||||
HEAD_STATE_KEY,
|
||||
EMPTY_STORAGE,
|
||||
parseStorage,
|
||||
)
|
||||
const saved = getProfileState(storage, current)
|
||||
// The active profile owns its live state even if persistence fails.
|
||||
const [state, setState] = useState(() => ({
|
||||
profile: { id: current, initialGroups: saved },
|
||||
groups: saved,
|
||||
}))
|
||||
const { profile, groups } = state
|
||||
if (profile.id !== current) {
|
||||
setState({ profile: { id: current, initialGroups: saved }, groups: saved })
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (groups === profile.initialGroups) return
|
||||
const timer = setTimeout(() => {
|
||||
try {
|
||||
const item = localStorage.getItem(HEAD_STATE_KEY)
|
||||
|
||||
let data = (item ? JSON.parse(item) : {}) as HeadStateStorage
|
||||
|
||||
if (!data || typeof data !== 'object') data = {}
|
||||
|
||||
data[current] = state
|
||||
|
||||
localStorage.setItem(HEAD_STATE_KEY, JSON.stringify(data))
|
||||
} catch {}
|
||||
setStorage((previous) => ({
|
||||
...previous,
|
||||
[profile.id]: groups,
|
||||
}))
|
||||
})
|
||||
|
||||
return () => clearTimeout(timer)
|
||||
}, [state, current])
|
||||
}, [groups, profile, setStorage])
|
||||
|
||||
const setHeadState = useCallback(
|
||||
(groupName: string, obj: Partial<HeadState>) => {
|
||||
dispatch({ type: 'update', groupName, patch: obj })
|
||||
(groupName: string, patch: Partial<HeadState>) => {
|
||||
setState((previous) => {
|
||||
if (previous.profile !== profile) return previous
|
||||
const group = previous.groups[groupName] ?? DEFAULT_STATE
|
||||
return {
|
||||
profile,
|
||||
groups: { ...previous.groups, [groupName]: { ...group, ...patch } },
|
||||
}
|
||||
})
|
||||
},
|
||||
[],
|
||||
[profile],
|
||||
)
|
||||
|
||||
return [state, setHeadState] as const
|
||||
return [groups, setHeadState] as const
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user