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:
Tunglies
2026-12-02 01:34:55 +00:00
committed by GitHub
parent 87de9bd5bf
commit 186ec116fa
14 changed files with 409 additions and 318 deletions
+2 -5
View File
@@ -1,4 +1,4 @@
import { useLocalStorage } from 'foxact/use-local-storage'
import { useLocalStorage } from './use-local-storage'
const defaultClashLog: IClashLog = {
enable: true,
@@ -8,7 +8,4 @@ const defaultClashLog: IClashLog = {
}
export const useClashLog = () =>
useLocalStorage<IClashLog>('clash-log', defaultClashLog, {
serializer: JSON.stringify,
deserializer: JSON.parse,
})
useLocalStorage<IClashLog>('clash-log', defaultClashLog)
+1 -5
View File
@@ -1,4 +1,4 @@
import { useLocalStorage } from 'foxact/use-local-storage'
import { useLocalStorage } from './use-local-storage'
const defaultConnectionSetting: IConnectionSetting = { layout: 'table' }
@@ -6,8 +6,4 @@ export const useConnectionSetting = () =>
useLocalStorage<IConnectionSetting>(
'connections-setting',
defaultConnectionSetting,
{
serializer: JSON.stringify,
deserializer: JSON.parse,
},
)
+21 -20
View File
@@ -1,6 +1,5 @@
/* eslint-disable @eslint-react/set-state-in-effect */
import { useEffect } from 'foxact/use-abortable-effect'
import { useCallback, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import { showNotice } from '@/services/notice-service'
@@ -42,26 +41,28 @@ export const useEditorDocument = ({ open, load }: UseEditorDocumentOptions) => {
}
}, [applyLoadedValue, load])
useEffect(
(signal) => {
resetDocumentState()
useEffect(() => {
let cancelled = false
resetDocumentState()
if (!open) return
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],
)
load()
.then((nextValue) => {
if (cancelled) return
applyLoadedValue(nextValue)
})
.catch((error) => {
if (!cancelled) showNotice.error(error)
})
.finally(() => {
if (!cancelled) setLoading(false)
})
return () => {
cancelled = true
}
}, [applyLoadedValue, load, open, resetDocumentState])
const markSaved = useCallback((nextValue: string) => {
setSavedValue(nextValue)
+148
View File
@@ -0,0 +1,148 @@
import { useCallback, useMemo, useSyncExternalStore } from 'react'
interface StorageEntry {
listeners: Set<() => void>
raw: string | null
parsedRaw: string | null
parsed: unknown
deserializer?: (value: string) => unknown
}
const entries = new Map<string, StorageEntry>()
const readRaw = (key: string): string | null => {
try {
return window.localStorage.getItem(key)
} catch (error) {
console.warn(`[useLocalStorage] Failed to read "${key}"`, error)
return null
}
}
const getRaw = (key: string): string | null => {
const entry = entries.get(key)
return entry ? entry.raw : readRaw(key)
}
const handleStorageEvent = (event: StorageEvent) => {
if (event.storageArea !== window.localStorage) return
if (event.key === null) {
entries.forEach((entry) => {
entry.raw = null
entry.parsedRaw = null
entry.parsed = undefined
entry.listeners.forEach((listener) => listener())
})
return
}
const entry = entries.get(event.key)
if (!entry) return
entry.raw = event.newValue
entry.parsedRaw = null
entry.parsed = undefined
entry.listeners.forEach((listener) => listener())
}
const subscribeKey = (key: string, listener: () => void) => {
if (entries.size === 0) {
window.addEventListener('storage', handleStorageEvent)
}
let entry = entries.get(key)
if (!entry) {
entry = {
listeners: new Set(),
raw: readRaw(key),
parsedRaw: null,
parsed: undefined,
}
entries.set(key, entry)
}
entry.listeners.add(listener)
return () => {
const current = entries.get(key)
if (current) {
current.listeners.delete(listener)
if (current.listeners.size === 0) entries.delete(key)
}
if (entries.size === 0) {
window.removeEventListener('storage', handleStorageEvent)
}
}
}
const parseRaw = <T>(
key: string,
raw: string | null,
defaultValue: T,
deserializer: (value: string) => T,
): T => {
if (raw === null) return defaultValue
const entry = entries.get(key)
if (entry && entry.parsedRaw === raw && entry.deserializer === deserializer) {
return entry.parsed as T
}
try {
const value = deserializer(raw)
if (entry) {
entry.parsedRaw = raw
entry.parsed = value
entry.deserializer = deserializer
}
return value
} catch (error) {
console.warn(`[useLocalStorage] Failed to deserialize "${key}"`, error)
return defaultValue
}
}
export const useLocalStorage = <T>(
key: string,
defaultValue: T,
deserializer: (value: string) => T = JSON.parse,
): [T, (value: T | ((prev: T) => T)) => void] => {
const subscribe = useCallback(
(listener: () => void) => subscribeKey(key, listener),
[key],
)
const raw = useSyncExternalStore(subscribe, () => getRaw(key))
const value = useMemo<T>(
() => parseRaw(key, raw, defaultValue, deserializer),
[key, raw, defaultValue, deserializer],
)
const setValue = useCallback(
(next: T | ((prev: T) => T)) => {
try {
const entry = entries.get(key)
const currentRaw = entry ? entry.raw : readRaw(key)
const resolved =
typeof next === 'function'
? (next as (prev: T) => T)(
parseRaw(key, currentRaw, defaultValue, deserializer),
)
: next
const serialized = JSON.stringify(resolved)
if (currentRaw === serialized) return
window.localStorage.setItem(key, serialized)
if (entry) {
entry.raw = serialized
entry.parsedRaw = null
entry.parsed = undefined
entry.listeners.forEach((listener) => listener())
}
} catch (error) {
console.warn(`[useLocalStorage] Failed to write "${key}"`, error)
}
},
[key, defaultValue, deserializer],
)
return [value, setValue]
}