import { ElementIds } from '@/Constants/ElementIDs' import { isMobileScreen } from '@/Utils' import { useEffect, ReactNode, useMemo, createContext, useCallback, useContext, useState } from 'react' import { AppPaneId } from './AppPaneMetadata' type ResponsivePaneData = { selectedPane: AppPaneId toggleAppPane: (paneId: AppPaneId) => void } const ResponsivePaneContext = createContext(undefined) export const useResponsiveAppPane = () => { const value = useContext(ResponsivePaneContext) if (!value) { throw new Error('Component must be a child of ') } return value } type Props = { children: ReactNode } const ResponsivePaneProvider = ({ children }: Props) => { const [currentSelectedPane, setCurrentSelectedPane] = useState( isMobileScreen() ? AppPaneId.Items : AppPaneId.Editor, ) const [previousSelectedPane, setPreviousSelectedPane] = useState( isMobileScreen() ? AppPaneId.Items : AppPaneId.Editor, ) const toggleAppPane = useCallback( (paneId: AppPaneId) => { if (paneId === currentSelectedPane) { setCurrentSelectedPane(previousSelectedPane ? previousSelectedPane : AppPaneId.Editor) setPreviousSelectedPane(paneId) } else { setPreviousSelectedPane(currentSelectedPane) setCurrentSelectedPane(paneId) } }, [currentSelectedPane, previousSelectedPane], ) useEffect(() => { if (previousSelectedPane) { const previousPaneElement = document.getElementById(ElementIds[previousSelectedPane]) previousPaneElement?.classList.remove('selected') } const currentPaneElement = document.getElementById(ElementIds[currentSelectedPane]) currentPaneElement?.classList.add('selected') }, [currentSelectedPane, previousSelectedPane]) const contextValue = useMemo( () => ({ selectedPane: currentSelectedPane, toggleAppPane, }), [currentSelectedPane, toggleAppPane], ) return {children} } export default ResponsivePaneProvider