MCPcopy Create free account
hub / github.com/Power-Playground/app / useDrawerPanelController

Function useDrawerPanelController

core/src/components/drawerPanelCreator.ts:50–88  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

48}
49
50export const useDrawerPanelController = (): DrawerPanelController => {
51 const store = useStore()
52 const [panels, setPanels] = useAtom(drawerPanelsAtom, { store })
53 const [activePanelId, setActivePanelId] = useAtom(activeDrawerPanelIdAtom, { store })
54 const activePanel = useMemo(() => panels.find(panel => panel.id === activePanelId) ?? null, [panels, activePanelId])
55 const setPanelsRef = useRefCallback(setPanels)
56 return {
57 activePanel,
58 addPanel: useCallback(panel => {
59 setPanelsRef(panels => {
60 if (panels.some(p => p.id === panel.id)) {
61 console.warn(`Panel with id "${panel.id}" already exists.`)
62 return panels
63 }
64 return [...panels, panel]
65 })
66 }, [setPanelsRef]),
67 setPanel: useCallback(panel => {
68 setPanelsRef(panels => {
69 const index = panels.findIndex(p => p.id === panel.id)
70 if (index === -1) {
71 return panels.concat(panel)
72 }
73 panels[index] = panel
74 return [...panels]
75 })
76 }, [setPanelsRef]),
77 removePanel: useCallback(id => {
78 setPanelsRef(panels => panels.filter(panel => panel.id !== id))
79 }, [setPanelsRef]),
80 openPanel: useCallback(id => setActivePanelId(id), [setActivePanelId]),
81 closePanel: useCallback(id => {
82 setActivePanelId(activePanelId => activePanelId === id ? null : activePanelId)
83 }, [setActivePanelId]),
84 togglePanel: useCallback(id => {
85 setActivePanelId(activePanelId => activePanelId === id ? null : id)
86 }, [setActivePanelId])
87 }
88}

Callers 2

LeftBarFunction · 0.90
DrawerPanelFunction · 0.90

Calls 1

useRefCallbackFunction · 0.85

Tested by

no test coverage detected