import { createContext, useContext, useEffect, useMemo, useRef } from 'react'; import type { MutableRefObject, ReactNode } from 'react'; export type PaletteOps = { openFile: (path: string) => void; openSettings: (tab?: string) => void; refreshProjects: () => Promise | void; }; type Registry = MutableRefObject>; const PaletteOpsContext = createContext(null); const defaultOps: PaletteOps = { openFile: () => undefined, openSettings: () => undefined, refreshProjects: () => undefined, }; export function PaletteOpsProvider({ children }: { children: ReactNode }) { const ref = useRef>({}); return {children}; } export function usePaletteOps(): PaletteOps { const ref = useContext(PaletteOpsContext); return useMemo( () => ({ openFile: (path) => (ref?.current.openFile ?? defaultOps.openFile)(path), openSettings: (tab) => (ref?.current.openSettings ?? defaultOps.openSettings)(tab), refreshProjects: () => (ref?.current.refreshProjects ?? defaultOps.refreshProjects)(), }), [ref], ); } export function usePaletteOpsRegister(partial: Partial) { const ref = useContext(PaletteOpsContext); const { openFile, openSettings, refreshProjects } = partial; useEffect(() => { if (!ref) return undefined; const prev = { ...ref.current }; if (openFile) ref.current.openFile = openFile; if (openSettings) ref.current.openSettings = openSettings; if (refreshProjects) ref.current.refreshProjects = refreshProjects; return () => { if (openFile && ref.current.openFile === openFile) ref.current.openFile = prev.openFile; if (openSettings && ref.current.openSettings === openSettings) ref.current.openSettings = prev.openSettings; if (refreshProjects && ref.current.refreshProjects === refreshProjects) ref.current.refreshProjects = prev.refreshProjects; }; }, [ref, openFile, openSettings, refreshProjects]); }