import React, { useEffect, useRef, useState, useMemo } from "react"; import { Terminal } from "@/ui/desktop/apps/terminal/Terminal.tsx"; import { ServerStats as ServerView } from "@/ui/desktop/apps/server-stats/ServerStats.tsx"; import { FileManager } from "@/ui/desktop/apps/file-manager/FileManager.tsx"; import { TunnelManager } from "@/ui/desktop/apps/tunnel/TunnelManager.tsx"; import { DockerManager } from "@/ui/desktop/apps/docker/DockerManager.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"; import { TERMINAL_THEMES, DEFAULT_TERMINAL_CONFIG, } from "@/constants/terminal-themes"; import { SSHAuthDialog } from "@/ui/desktop/navigation/SSHAuthDialog.tsx"; interface TabData { id: number; type: string; title: string; terminalRef?: { current?: { fit?: () => void; notifyResize?: () => void; refresh?: () => void; }; }; hostConfig?: any; [key: string]: unknown; } interface TerminalViewProps { isTopbarOpen?: boolean; rightSidebarOpen?: boolean; rightSidebarWidth?: number; } export function AppView({ isTopbarOpen = true, rightSidebarOpen = false, rightSidebarWidth = 400, }: 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 = useMemo( () => tabs.filter( (tab: TabData) => tab.type === "terminal" || tab.type === "server" || tab.type === "file_manager" || tab.type === "tunnel" || tab.type === "docker", ), [tabs], ); const containerRef = useRef(null); const panelRefs = useRef>({}); const [panelRects, setPanelRects] = useState>( {}, ); const [ready, setReady] = useState(true); const [resetKey, setResetKey] = useState(0); const previousStylesRef = useRef>({}); const updatePanelRects = React.useCallback(() => { const next: Record = {}; Object.entries(panelRefs.current).forEach(([id, el]) => { if (el) next[id] = el.getBoundingClientRect(); }); setPanelRects(next); }, []); const fitActiveAndNotify = React.useCallback(() => { 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(); } }); }, [allSplitScreenTab, currentTab, terminalTabs]); const layoutScheduleRef = useRef(null); const scheduleMeasureAndFit = React.useCallback(() => { if (layoutScheduleRef.current) cancelAnimationFrame(layoutScheduleRef.current); layoutScheduleRef.current = requestAnimationFrame(() => { updatePanelRects(); layoutScheduleRef.current = requestAnimationFrame(() => { fitActiveAndNotify(); }); }); }, [updatePanelRects, fitActiveAndNotify]); const hideThenFit = React.useCallback(() => { requestAnimationFrame(() => { updatePanelRects(); requestAnimationFrame(() => { fitActiveAndNotify(); }); }); }, [updatePanelRects, fitActiveAndNotify]); const prevStateRef = useRef({ terminalTabsLength: terminalTabs.length, currentTab, splitScreenTabsStr: allSplitScreenTab.join(","), terminalTabIds: terminalTabs.map((t) => t.id).join(","), }); useEffect(() => { const prev = prevStateRef.current; const currentTabIds = terminalTabs.map((t) => t.id).join(","); const lengthChanged = prev.terminalTabsLength !== terminalTabs.length; const currentTabChanged = prev.currentTab !== currentTab; const splitChanged = prev.splitScreenTabsStr !== allSplitScreenTab.join(","); const tabIdsChanged = prev.terminalTabIds !== currentTabIds; const isJustReorder = !lengthChanged && tabIdsChanged && !currentTabChanged && !splitChanged; if ( (lengthChanged || currentTabChanged || splitChanged) && !isJustReorder ) { hideThenFit(); } prevStateRef.current = { terminalTabsLength: terminalTabs.length, currentTab, splitScreenTabsStr: allSplitScreenTab.join(","), terminalTabIds: currentTabIds, }; }, [ currentTab, terminalTabs.length, allSplitScreenTab.join(","), terminalTabs, hideThenFit, ]); useEffect(() => { scheduleMeasureAndFit(); }, [ scheduleMeasureAndFit, allSplitScreenTab.length, isTopbarOpen, sidebarState, resetKey, rightSidebarOpen, rightSidebarWidth, ]); useEffect(() => { const roContainer = containerRef.current ? new ResizeObserver(() => { updatePanelRects(); fitActiveAndNotify(); }) : null; if (containerRef.current && roContainer) roContainer.observe(containerRef.current); return () => roContainer?.disconnect(); }, [updatePanelRects, fitActiveAndNotify]); useEffect(() => { const onWinResize = () => { updatePanelRects(); fitActiveAndNotify(); }; window.addEventListener("resize", onWinResize); return () => window.removeEventListener("resize", onWinResize); }, [updatePanelRects, fitActiveAndNotify]); const HEADER_H = 28; const terminalIdMapRef = useRef>(new Set()); useEffect(() => { terminalTabs.forEach((t) => terminalIdMapRef.current.add(t.id)); }, [terminalTabs]); const renderTerminalsLayer = () => { const styles: Record = {}; const layoutTabs = allSplitScreenTab .map((tabId) => terminalTabs.find((tab: TabData) => tab.id === tabId)) .filter((t): t is TabData => t !== null && t !== undefined); const mainTab = terminalTabs.find((tab: TabData) => tab.id === currentTab); if (allSplitScreenTab.length === 0 && mainTab) { const isFileManagerTab = mainTab.type === "file_manager" || mainTab.type === "tunnel" || mainTab.type === "docker"; const newStyle = { position: "absolute" as const, top: isFileManagerTab ? 0 : 4, left: isFileManagerTab ? 0 : 4, right: isFileManagerTab ? 0 : 4, bottom: isFileManagerTab ? 0 : 4, zIndex: 20, display: "block" as const, pointerEvents: "auto" as const, opacity: 1, transition: "opacity 150ms ease-in-out", }; styles[mainTab.id] = newStyle; previousStylesRef.current[mainTab.id] = newStyle; } else { layoutTabs.forEach((t: TabData) => { const rect = panelRects[String(t.id)]; const parentRect = containerRef.current?.getBoundingClientRect(); if (rect && parentRect) { const newStyle = { position: "absolute" as const, 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" as const, pointerEvents: "auto" as const, opacity: 1, transition: "opacity 150ms ease-in-out", }; styles[t.id] = newStyle; previousStylesRef.current[t.id] = newStyle; } }); } const sortedTerminalTabs = [...terminalTabs].sort((a, b) => a.id - b.id); return (
{sortedTerminalTabs.map((t: TabData) => { const hasStyle = !!styles[t.id]; const isVisible = hasStyle || (allSplitScreenTab.length === 0 && t.id === currentTab); const effectiveVisible = isVisible; const previousStyle = previousStylesRef.current[t.id]; const isFileManagerTab = t.type === "file_manager" || t.type === "tunnel" || t.type === "docker"; const standardStyle = { position: "absolute" as const, top: isFileManagerTab ? 0 : 4, left: isFileManagerTab ? 0 : 4, right: isFileManagerTab ? 0 : 4, bottom: isFileManagerTab ? 0 : 4, }; const finalStyle: React.CSSProperties = hasStyle ? { ...styles[t.id], overflow: "hidden" } : effectiveVisible ? { ...(previousStyle || standardStyle), opacity: 1, pointerEvents: "auto", zIndex: 20, display: "block", transition: "opacity 150ms ease-in-out", overflow: "hidden", } : ({ ...(previousStyle || standardStyle), opacity: 0, pointerEvents: "none", zIndex: 0, transition: "opacity 150ms ease-in-out", overflow: "hidden", } as React.CSSProperties); const isTerminal = t.type === "terminal"; const terminalConfig = { ...DEFAULT_TERMINAL_CONFIG, ...(t.hostConfig as any)?.terminalConfig, }; const themeColors = TERMINAL_THEMES[terminalConfig.theme]?.colors || TERMINAL_THEMES.termix.colors; const backgroundColor = themeColors.background; return (
{t.type === "terminal" ? ( 0} onClose={() => removeTab(t.id)} /> ) : t.type === "server" ? ( ) : t.type === "tunnel" ? ( ) : t.type === "docker" ? ( ) : ( removeTab(t.id)} /> )}
); })}
); }; const ResetButton = ({ onClick }: { onClick: () => void }) => ( ); const handleReset = () => { setResetKey((k) => k + 1); requestAnimationFrame(() => scheduleMeasureAndFit()); }; const renderSplitOverlays = () => { const layoutTabs = allSplitScreenTab .map((tabId) => terminalTabs.find((tab: TabData) => tab.id === tabId)) .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 isTunnel = currentTabData?.type === "tunnel"; const isDocker = currentTabData?.type === "docker"; const isTerminal = currentTabData?.type === "terminal"; const isSplitScreen = allSplitScreenTab.length > 0; const terminalConfig = { ...DEFAULT_TERMINAL_CONFIG, ...(currentTabData?.hostConfig as any)?.terminalConfig, }; const themeColors = TERMINAL_THEMES[terminalConfig.theme]?.colors || TERMINAL_THEMES.termix.colors; const terminalBackgroundColor = themeColors.background; const topMarginPx = isTopbarOpen ? 74 : 26; const leftMarginPx = sidebarState === "collapsed" ? 26 : 8; const bottomMarginPx = 8; let containerBackground = "var(--color-dark-bg)"; if ((isFileManager || isTunnel || isDocker) && !isSplitScreen) { containerBackground = "var(--color-dark-bg-darkest)"; } else if (isTerminal) { containerBackground = terminalBackgroundColor; } return (
{renderTerminalsLayer()} {renderSplitOverlays()}
); }