import React, { useEffect, useRef, useState } from "react"; import { Terminal } from "@/ui/Desktop/Apps/Terminal/Terminal.tsx"; import { Server as ServerView } from "@/ui/Desktop/Apps/Server/Server.tsx"; import { FileManager } from "@/ui/Desktop/Apps/File Manager/FileManager.tsx"; import { useTabs } from "@/ui/Desktop/Navigation/Tabs/TabContext.tsx"; import { ResizablePanelGroup, ResizablePanel, ResizableHandle, } from "@/components/ui/resizable.tsx"; import * as ResizablePrimitive from "react-resizable-panels"; import { useSidebar } from "@/components/ui/sidebar.tsx"; import { RefreshCcw } from "lucide-react"; import { Button } from "@/components/ui/button.tsx"; interface TabData { id: number; type: string; title: string; terminalRef?: { current?: { fit?: () => void; notifyResize?: () => void; refresh?: () => void; }; }; hostConfig?: unknown; [key: string]: unknown; } interface TerminalViewProps { isTopbarOpen?: boolean; } export function AppView({ isTopbarOpen = true, }: TerminalViewProps): React.ReactElement { const { tabs, currentTab, allSplitScreenTab, removeTab } = useTabs() as { tabs: TabData[]; currentTab: number; allSplitScreenTab: number[]; removeTab: (id: number) => void; }; const { state: sidebarState } = useSidebar(); const terminalTabs = tabs.filter( (tab: TabData) => tab.type === "terminal" || tab.type === "server" || tab.type === "file_manager", ); const containerRef = useRef(null); const panelRefs = useRef>({}); const [panelRects, setPanelRects] = useState>( {}, ); const [ready, setReady] = useState(true); const [resetKey, setResetKey] = useState(0); const updatePanelRects = () => { const next: Record = {}; Object.entries(panelRefs.current).forEach(([id, el]) => { if (el) next[id] = el.getBoundingClientRect(); }); setPanelRects(next); }; const fitActiveAndNotify = () => { const visibleIds: number[] = []; if (allSplitScreenTab.length === 0) { if (currentTab) visibleIds.push(currentTab); } else { const splitIds = allSplitScreenTab as number[]; visibleIds.push(currentTab, ...splitIds.filter((i) => i !== currentTab)); } terminalTabs.forEach((t: TabData) => { if (visibleIds.includes(t.id)) { const ref = t.terminalRef?.current; if (ref?.fit) ref.fit(); if (ref?.notifyResize) ref.notifyResize(); if (ref?.refresh) ref.refresh(); } }); }; const layoutScheduleRef = useRef(null); const scheduleMeasureAndFit = () => { if (layoutScheduleRef.current) cancelAnimationFrame(layoutScheduleRef.current); layoutScheduleRef.current = requestAnimationFrame(() => { updatePanelRects(); layoutScheduleRef.current = requestAnimationFrame(() => { fitActiveAndNotify(); }); }); }; const hideThenFit = () => { setReady(false); requestAnimationFrame(() => { updatePanelRects(); requestAnimationFrame(() => { fitActiveAndNotify(); setReady(true); }); }); }; useEffect(() => { hideThenFit(); }, [currentTab, terminalTabs.length, allSplitScreenTab.join(",")]); useEffect(() => { scheduleMeasureAndFit(); }, [allSplitScreenTab.length, isTopbarOpen, sidebarState, resetKey]); useEffect(() => { const roContainer = containerRef.current ? new ResizeObserver(() => { updatePanelRects(); fitActiveAndNotify(); }) : null; if (containerRef.current && roContainer) roContainer.observe(containerRef.current); return () => roContainer?.disconnect(); }, []); useEffect(() => { const onWinResize = () => { updatePanelRects(); fitActiveAndNotify(); }; window.addEventListener("resize", onWinResize); return () => window.removeEventListener("resize", onWinResize); }, []); const HEADER_H = 28; const renderTerminalsLayer = () => { const styles: Record = {}; const splitTabs = terminalTabs.filter((tab: TabData) => allSplitScreenTab.includes(tab.id), ); const mainTab = terminalTabs.find((tab: TabData) => tab.id === currentTab); const layoutTabs = [ mainTab, ...splitTabs.filter( (t: TabData) => t && t.id !== (mainTab && (mainTab as TabData).id), ), ].filter((t): t is TabData => t !== null && t !== undefined); if (allSplitScreenTab.length === 0 && mainTab) { const isFileManagerTab = mainTab.type === "file_manager"; styles[mainTab.id] = { position: "absolute", top: isFileManagerTab ? 0 : 4, left: isFileManagerTab ? 0 : 4, right: isFileManagerTab ? 0 : 4, bottom: isFileManagerTab ? 0 : 4, zIndex: 20, display: "block", pointerEvents: "auto", opacity: ready ? 1 : 0, }; } else { layoutTabs.forEach((t: TabData) => { const rect = panelRects[String(t.id)]; const parentRect = containerRef.current?.getBoundingClientRect(); if (rect && parentRect) { styles[t.id] = { position: "absolute", top: rect.top - parentRect.top + HEADER_H + 4, left: rect.left - parentRect.left + 4, width: rect.width - 8, height: rect.height - HEADER_H - 8, zIndex: 20, display: "block", pointerEvents: "auto", opacity: ready ? 1 : 0, }; } }); } return (
{terminalTabs.map((t: TabData) => { const hasStyle = !!styles[t.id]; const isVisible = hasStyle || (allSplitScreenTab.length === 0 && t.id === currentTab); const finalStyle: React.CSSProperties = hasStyle ? { ...styles[t.id], overflow: "hidden" } : ({ position: "absolute", inset: 0, visibility: "hidden", pointerEvents: "none", zIndex: 0, } as React.CSSProperties); const effectiveVisible = isVisible && ready; return (
{t.type === "terminal" ? ( 0} onClose={() => removeTab(t.id)} /> ) : t.type === "server" ? ( ) : ( removeTab(t.id)} /> )}
); })}
); }; const ResetButton = ({ onClick }: { onClick: () => void }) => ( ); const handleReset = () => { setResetKey((k) => k + 1); requestAnimationFrame(() => scheduleMeasureAndFit()); }; const renderSplitOverlays = () => { const splitTabs = terminalTabs.filter((tab: TabData) => allSplitScreenTab.includes(tab.id), ); const mainTab = terminalTabs.find((tab: TabData) => tab.id === currentTab); const layoutTabs = [ mainTab, ...splitTabs.filter( (t: TabData) => t && t.id !== (mainTab && (mainTab as TabData).id), ), ].filter((t): t is TabData => t !== null && t !== undefined); if (allSplitScreenTab.length === 0) return null; const handleStyle = { pointerEvents: "auto", zIndex: 12, background: "var(--color-dark-border)", } as React.CSSProperties; const commonGroupProps: { onLayout: () => void; onResize: () => void; } = { onLayout: scheduleMeasureAndFit, onResize: scheduleMeasureAndFit, }; if (layoutTabs.length === 2) { const [a, b] = layoutTabs; return (
{ panelRefs.current[String(a.id)] = el; }} className="h-full w-full flex flex-col bg-transparent relative" >
{a.title}
{ panelRefs.current[String(b.id)] = el; }} className="h-full w-full flex flex-col bg-transparent relative" >
{b.title}
); } if (layoutTabs.length === 3) { const [a, b, c] = layoutTabs; return (
{ panelRefs.current[String(a.id)] = el; }} className="h-full w-full flex flex-col relative" >
{a.title}
{ panelRefs.current[String(b.id)] = el; }} className="h-full w-full flex flex-col relative" >
{b.title}
{ panelRefs.current[String(c.id)] = el; }} className="h-full w-full flex flex-col relative" >
{c.title}
); } if (layoutTabs.length === 4) { const [a, b, c, d] = layoutTabs; return (
{ panelRefs.current[String(a.id)] = el; }} className="h-full w-full flex flex-col relative" >
{a.title}
{ panelRefs.current[String(b.id)] = el; }} className="h-full w-full flex flex-col relative" >
{b.title}
{ panelRefs.current[String(c.id)] = el; }} className="h-full w-full flex flex-col relative" >
{c.title}
{ panelRefs.current[String(d.id)] = el; }} className="h-full w-full flex flex-col relative" >
{d.title}
); } return null; }; const currentTabData = tabs.find((tab: TabData) => tab.id === currentTab); const isFileManager = currentTabData?.type === "file_manager"; const isSplitScreen = allSplitScreenTab.length > 0; const topMarginPx = isTopbarOpen ? 74 : 26; const leftMarginPx = sidebarState === "collapsed" ? 26 : 8; const bottomMarginPx = 8; return (
{renderTerminalsLayer()} {renderSplitOverlays()}
); }