You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
998 B
29 lines
998 B
import CacheBrowserDialog from '../components/CacheBrowser/CacheBrowserDialog' |
|
import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from 'react' |
|
|
|
type CacheBrowserContextValue = { |
|
openBrowseCache: () => void |
|
} |
|
|
|
const CacheBrowserContext = createContext<CacheBrowserContextValue | undefined>(undefined) |
|
|
|
export function CacheBrowserProvider({ children }: { children: ReactNode }) { |
|
const [open, setOpen] = useState(false) |
|
const openBrowseCache = useCallback(() => setOpen(true), []) |
|
const value = useMemo(() => ({ openBrowseCache }), [openBrowseCache]) |
|
|
|
return ( |
|
<CacheBrowserContext.Provider value={value}> |
|
{children} |
|
<CacheBrowserDialog open={open} onOpenChange={setOpen} /> |
|
</CacheBrowserContext.Provider> |
|
) |
|
} |
|
|
|
export function useCacheBrowser(): CacheBrowserContextValue { |
|
const ctx = useContext(CacheBrowserContext) |
|
if (!ctx) { |
|
throw new Error('useCacheBrowser must be used within CacheBrowserProvider') |
|
} |
|
return ctx |
|
}
|
|
|