import { GripVertical } from "lucide-react"; import { createContext, forwardRef, useContext } from "react"; import * as ResizablePrimitive from "react-resizable-panels"; import cookieJs from "cookiejs"; import { cn } from "~/lib/utils"; import { usePageContext } from "~/renderer/context"; import { useDebounce } from "~/hooks/useDebounce"; const ResizableContext = createContext<{ initialSize: number[] }>(null!); const ResizablePanelGroup = ({ className, autoSaveId, direction, ...props }: React.ComponentProps) => { const { cookies } = usePageContext(); const [debouncePersistLayout] = useDebounce((sizes: number[]) => { if (autoSaveId && typeof window !== "undefined") { cookieJs.set(panelKey, JSON.stringify(sizes)); } }, 500); const panelKey = ["panel", direction, autoSaveId].join(":"); let initialSize: number[] = []; if (autoSaveId && cookies && cookies[panelKey]) { initialSize = JSON.parse(cookies[panelKey]) || []; } const onLayout = (sizes: number[]) => { if (props.onLayout) { props.onLayout(sizes); } debouncePersistLayout(sizes); }; return ( ); }; type ResizablePanelProps = React.ComponentProps< typeof ResizablePrimitive.Panel > & { panelId: number; }; const ResizablePanel = forwardRef((props: ResizablePanelProps, ref: any) => { const { panelId, defaultSize, ...restProps } = props; const ctx = useContext(ResizableContext); let initialSize = defaultSize; if (panelId != null) { const size = ctx?.initialSize[panelId]; if (size != null) { initialSize = size; } } return ( ); }); const ResizableHandle = ({ withHandle, className, ...props }: React.ComponentProps & { withHandle?: boolean; }) => ( div]:rotate-90", className )} {...props} > {withHandle && (
)}
); export { ResizablePanelGroup, ResizablePanel, ResizableHandle };