import { createContext, useContext, useState, useCallback, useRef, ReactNode } from 'react' import { DetailPanelContent } from '@/types/pmr' interface DetailPanelContextValue { content: DetailPanelContent | null openPanel: (content: DetailPanelContent) => void closePanel: () => void isOpen: boolean isClosing: boolean } const DetailPanelContext = createContext( undefined ) interface DetailPanelProviderProps { children: ReactNode } export function DetailPanelProvider({ children }: DetailPanelProviderProps) { const [content, setContent] = useState(null) const [isClosing, setIsClosing] = useState(false) const closeTimerRef = useRef(0) const openPanel = useCallback((newContent: DetailPanelContent) => { // If we're in the middle of closing, cancel it if (closeTimerRef.current) { window.clearTimeout(closeTimerRef.current) closeTimerRef.current = 0 } setIsClosing(false) setContent(newContent) }, []) const closePanel = useCallback(() => { setIsClosing(true) closeTimerRef.current = window.setTimeout(() => { setIsClosing(false) setContent(null) closeTimerRef.current = 0 }, 250) // match panel-slide-out duration }, []) const isOpen = content !== null const value: DetailPanelContextValue = { content, openPanel, closePanel, isOpen, isClosing, } return ( {children} ) } // eslint-disable-next-line react-refresh/only-export-components export function useDetailPanel(): DetailPanelContextValue { const context = useContext(DetailPanelContext) if (!context) { throw new Error('useDetailPanel must be used within DetailPanelProvider') } return context }