|
| 1 | +import React, { useEffect, useMemo, useRef, useState } from "react"; |
| 2 | +import type { ImperativePanelGroupHandle } from "react-resizable-panels"; |
| 3 | +import type { PanelNode } from "../store/panelStore"; |
| 4 | +import { usePanelStore } from "../store/panelStore"; |
| 5 | +import { Panel } from "./Panel"; |
| 6 | +import { PanelGroup } from "./PanelGroup"; |
| 7 | +import { PanelResizeHandle } from "./PanelResizeHandle"; |
| 8 | +import { compilePanelTree } from "./PanelTree"; |
| 9 | +import { TabbedPanel } from "./TabbedPanel"; |
| 10 | + |
| 11 | +interface PanelLayoutProps { |
| 12 | + tree: React.ReactElement; |
| 13 | +} |
| 14 | + |
| 15 | +const PanelLayoutRenderer: React.FC<{ node: PanelNode }> = ({ node }) => { |
| 16 | + const [activeTabs, setActiveTabs] = useState<Record<string, string>>({}); |
| 17 | + const updateSizes = usePanelStore((state) => state.updateSizes); |
| 18 | + const groupRefs = useRef<Map<string, ImperativePanelGroupHandle>>(new Map()); |
| 19 | + |
| 20 | + const handleSetActiveTab = (panelId: string, tabId: string) => { |
| 21 | + setActiveTabs((prev) => ({ ...prev, [panelId]: tabId })); |
| 22 | + }; |
| 23 | + |
| 24 | + const setGroupRef = ( |
| 25 | + groupId: string, |
| 26 | + ref: ImperativePanelGroupHandle | null, |
| 27 | + ) => { |
| 28 | + if (ref) { |
| 29 | + groupRefs.current.set(groupId, ref); |
| 30 | + } else { |
| 31 | + groupRefs.current.delete(groupId); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const renderNode = (currentNode: PanelNode): React.ReactNode => { |
| 36 | + if (currentNode.type === "leaf") { |
| 37 | + const activeTabId = |
| 38 | + activeTabs[currentNode.id] || currentNode.content.activeTabId; |
| 39 | + const content = { |
| 40 | + ...currentNode.content, |
| 41 | + activeTabId, |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <TabbedPanel |
| 46 | + panelId={currentNode.id} |
| 47 | + content={content} |
| 48 | + onActiveTabChange={handleSetActiveTab} |
| 49 | + /> |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + if (currentNode.type === "group") { |
| 54 | + return ( |
| 55 | + <PanelGroup |
| 56 | + ref={(ref) => setGroupRef(currentNode.id, ref)} |
| 57 | + direction={currentNode.direction} |
| 58 | + onLayout={(sizes) => { |
| 59 | + // Only update store, don't normalize here |
| 60 | + // The library is the source of truth for sizes during user interaction |
| 61 | + updateSizes(currentNode.id, sizes); |
| 62 | + }} |
| 63 | + > |
| 64 | + {currentNode.children.map((child, index) => ( |
| 65 | + <React.Fragment key={child.id}> |
| 66 | + <Panel |
| 67 | + id={child.id} |
| 68 | + order={index} |
| 69 | + defaultSize={ |
| 70 | + currentNode.sizes?.[index] ?? |
| 71 | + 100 / currentNode.children.length |
| 72 | + } |
| 73 | + minSize={15} |
| 74 | + > |
| 75 | + {renderNode(child)} |
| 76 | + </Panel> |
| 77 | + {index < currentNode.children.length - 1 && <PanelResizeHandle />} |
| 78 | + </React.Fragment> |
| 79 | + ))} |
| 80 | + </PanelGroup> |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + return null; |
| 85 | + }; |
| 86 | + |
| 87 | + // Sync store changes to library when sizes change from operations like split/cleanup |
| 88 | + useEffect(() => { |
| 89 | + const syncSizesToLibrary = (currentNode: PanelNode) => { |
| 90 | + if (currentNode.type === "group" && currentNode.sizes) { |
| 91 | + const groupRef = groupRefs.current.get(currentNode.id); |
| 92 | + if (groupRef) { |
| 93 | + // Get current layout from library |
| 94 | + const currentLayout = groupRef.getLayout(); |
| 95 | + |
| 96 | + // Only update if sizes are significantly different (avoid feedback loops) |
| 97 | + const isDifferent = currentLayout.some( |
| 98 | + (size, i) => Math.abs(size - (currentNode.sizes?.[i] ?? 0)) > 0.1, |
| 99 | + ); |
| 100 | + |
| 101 | + if ( |
| 102 | + isDifferent && |
| 103 | + currentNode.sizes.length === currentLayout.length |
| 104 | + ) { |
| 105 | + groupRef.setLayout(currentNode.sizes); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Recursively sync child groups |
| 110 | + currentNode.children.forEach(syncSizesToLibrary); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + syncSizesToLibrary(node); |
| 115 | + }, [node]); |
| 116 | + |
| 117 | + return <>{renderNode(node)}</>; |
| 118 | +}; |
| 119 | + |
| 120 | +export const PanelLayout: React.FC<PanelLayoutProps> = ({ tree }) => { |
| 121 | + const compiledNode = useMemo(() => compilePanelTree(tree), [tree]); |
| 122 | + const setRoot = usePanelStore((state) => state.setRoot); |
| 123 | + const root = usePanelStore((state) => state.root); |
| 124 | + |
| 125 | + useEffect(() => { |
| 126 | + // Only initialize if root is null (store not yet initialized) |
| 127 | + // Once initialized, store is the source of truth |
| 128 | + if (root === null) { |
| 129 | + setRoot(compiledNode); |
| 130 | + } |
| 131 | + }, [compiledNode, setRoot, root]); |
| 132 | + |
| 133 | + return root ? <PanelLayoutRenderer node={root} /> : null; |
| 134 | +}; |
0 commit comments