mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-08 02:52:07 +08:00
feat(settings): add searchable settings navigation
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import {
|
||||
createContext,
|
||||
use,
|
||||
useEffect,
|
||||
useImperativeHandle,
|
||||
type Ref,
|
||||
} from 'react'
|
||||
|
||||
import type { DialogRef } from '@/components/base'
|
||||
|
||||
export interface SearchDialog {
|
||||
id: string
|
||||
open: () => void
|
||||
isOpen: boolean
|
||||
}
|
||||
export const SettingsSearchContext = createContext<Set<SearchDialog> | null>(
|
||||
null,
|
||||
)
|
||||
|
||||
export function useSettingDialog(
|
||||
ref: Ref<DialogRef> | undefined,
|
||||
id: string,
|
||||
open: boolean,
|
||||
create: () => DialogRef,
|
||||
) {
|
||||
useImperativeHandle(ref, create)
|
||||
const registry = use(SettingsSearchContext)
|
||||
useEffect(() => {
|
||||
if (!registry) return
|
||||
const handle = {
|
||||
id,
|
||||
isOpen: open,
|
||||
open: () => {
|
||||
if (!open) create().open()
|
||||
},
|
||||
}
|
||||
registry.add(handle)
|
||||
return () => {
|
||||
registry.delete(handle)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user