Initial dev-1.0 commit for TS and Shadcn migration

This commit is contained in:
LukeGus
2025-07-17 01:13:30 -05:00
commit 00a827df09
82 changed files with 45402 additions and 0 deletions

474
src/apps/SSH/SSH.tsx Normal file
View File

@@ -0,0 +1,474 @@
import React, { useState, useRef } from "react";
import { SSHSidebar } from "@/apps/SSH/SSHSidebar.tsx";
import { SSHTerminal } from "./SSHTerminal.tsx";
import { SSHTopbar } from "@/apps/SSH/SSHTopbar.tsx";
import { ResizablePanelGroup, ResizablePanel, ResizableHandle } from '@/components/ui/resizable';
interface ConfigEditorProps {
onSelectView: (view: string) => void;
}
type Tab = {
id: number;
title: string;
hostConfig: any;
terminalRef: React.RefObject<any>;
};
function TerminalOverlay({ tabId, splitScreen }: { tabId: number, splitScreen: boolean }) {
React.useEffect(() => {
const el = document.getElementById(`terminal-container-${tabId}`);
if (el) {
el.style.opacity = '1';
el.style.zIndex = '10';
el.style.left = splitScreen ? '8px' : '0px';
el.style.width = splitScreen ? 'calc(100% - 8px)' : '100%';
}
return () => {
if (el) {
el.style.opacity = '0';
el.style.zIndex = '1';
}
};
}, [tabId, splitScreen]);
return <div style={{ width: '100%', height: '100%', position: 'relative' }} />;
}
export function SSH({ onSelectView }: ConfigEditorProps): React.ReactElement {
const [allTabs, setAllTabs] = useState<Tab[]>([]);
const [currentTab, setCurrentTab] = useState<number | null>(null);
const [allSplitScreenTab, setAllSplitScreenTab] = useState<number[]>([]);
const nextTabId = useRef(1);
const [splitKey, setSplitKey] = useState(0);
const setActiveTab = (tabId: number) => {
setCurrentTab(tabId);
};
// Helper to fit all visible terminals
const fitVisibleTerminals = () => {
allTabs.forEach((terminal) => {
const isVisible =
(allSplitScreenTab.length === 0 && terminal.id === currentTab) ||
(allSplitScreenTab.length > 0 && (terminal.id === currentTab || allSplitScreenTab.includes(terminal.id)));
if (isVisible && terminal.terminalRef && terminal.terminalRef.current && typeof terminal.terminalRef.current.fit === 'function') {
terminal.terminalRef.current.fit();
}
});
};
// Wrap setSplitScreenTab to fit before and after
const setSplitScreenTab = (tabId: number) => {
fitVisibleTerminals();
setAllSplitScreenTab((prev) => {
let next;
if (prev.includes(tabId)) {
next = prev.filter((id) => id !== tabId);
} else if (prev.length < 3) {
next = [...prev, tabId];
} else {
next = prev;
}
setTimeout(() => fitVisibleTerminals(), 0);
return next;
});
};
const setCloseTab = (tabId: number) => {
// Find the tab and call disconnect on its terminal
const tab = allTabs.find((t) => t.id === tabId);
if (tab && tab.terminalRef && tab.terminalRef.current && typeof tab.terminalRef.current.disconnect === "function") {
tab.terminalRef.current.disconnect();
}
setAllTabs((prev) => prev.filter((tab) => tab.id !== tabId));
setAllSplitScreenTab((prev) => prev.filter((id) => id !== tabId));
if (currentTab === tabId) {
const remainingTabs = allTabs.filter((tab) => tab.id !== tabId);
setCurrentTab(remainingTabs.length > 0 ? remainingTabs[0].id : null);
}
};
// Render all terminals absolutely positioned, always mounted
const renderAllTerminals = () => (
<div style={{ position: 'absolute', top: 0, left: 0, width: '100%', height: '100%', pointerEvents: 'none', zIndex: 1 }}>
{allTabs.map((tab) => (
<div
key={tab.id}
id={`terminal-container-${tab.id}`}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
zIndex: 1,
opacity: 0,
pointerEvents: 'auto',
transition: 'opacity 0.15s',
}}
data-terminal-id={tab.id}
>
<SSHTerminal
key={tab.id}
ref={tab.terminalRef}
hostConfig={tab.hostConfig}
isVisible={false}
title={tab.title}
showTitle={false}
splitScreen={false}
/>
</div>
))}
</div>
);
// Helper to show a terminal in a panel by toggling zIndex/opacity
const showTerminal = (tab: Tab, splitScreen: boolean) => (
<TerminalOverlay tabId={tab.id} splitScreen={splitScreen} />
);
const renderTerminals = () => {
if (allSplitScreenTab.length === 0) {
return (
<>
{allTabs.map((tab) => (
<div
key={tab.id}
style={{
height: '100%',
width: '100%',
position: 'absolute',
top: 0,
left: 0,
zIndex: tab.id === currentTab ? 10 : 1,
opacity: tab.id === currentTab ? 1 : 0,
transition: 'opacity 0.15s',
marginTop: 0,
}}
>
<TerminalOverlay tabId={tab.id} splitScreen={false} />
</div>
))}
</>
);
}
// Split screen logic
const splitTabs = allTabs.filter((tab) => allSplitScreenTab.includes(tab.id));
const mainTab = allTabs.find((tab) => tab.id === currentTab);
const layoutTabs = [mainTab, ...splitTabs.filter((t) => t && t.id !== currentTab)].filter((t): t is Tab => !!t);
// 2 splits: horizontal
if (layoutTabs.length === 2) {
const [tab1, tab2] = layoutTabs;
return (
<ResizablePanelGroup key={splitKey} direction="horizontal" className="h-full w-full">
<ResizablePanel key={tab1.id} defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
}}>{tab1.title}</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(tab1, true)}
</div>
</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel key={tab2.id} defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
}}>{tab2.title}</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(tab2, true)}
</div>
</div>
</ResizablePanel>
</ResizablePanelGroup>
);
}
// 3 splits: vertical group (top: horizontal with 2, bottom: single)
if (layoutTabs.length === 3) {
return (
<ResizablePanelGroup key={splitKey} direction="vertical" className="h-full w-full">
<ResizablePanel defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<ResizablePanelGroup direction="horizontal" className="h-full w-full">
{/* Left/top panel */}
<ResizablePanel key={layoutTabs[0].id} defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
}}>{layoutTabs[0].title}</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(layoutTabs[0], true)}
</div>
</div>
</ResizablePanel>
<ResizableHandle />
{/* Right/top panel (no reset button here) */}
<ResizablePanel key={layoutTabs[1].id} defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
}}>
<span>{layoutTabs[1].title}</span>
</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(layoutTabs[1], true)}
</div>
</div>
</ResizablePanel>
</ResizablePanelGroup>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
display: 'flex',
alignItems: 'center',
}}>{layoutTabs[2].title}</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(layoutTabs[2], true)}
</div>
</div>
</ResizablePanel>
</ResizablePanelGroup>
);
}
// 4 splits: 2x2 grid (vertical group with two horizontal groups)
if (layoutTabs.length === 4) {
return (
<ResizablePanelGroup key={splitKey} direction="vertical" className="h-full w-full">
<ResizablePanel defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<ResizablePanelGroup direction="horizontal" className="h-full w-full">
<ResizablePanel key={layoutTabs[0].id} defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
}}>{layoutTabs[0].title}</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(layoutTabs[0], true)}
</div>
</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel key={layoutTabs[1].id} defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
}}>{layoutTabs[1].title}</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(layoutTabs[1], true)}
</div>
</div>
</ResizablePanel>
</ResizablePanelGroup>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<ResizablePanelGroup direction="horizontal" className="h-full w-full">
<ResizablePanel key={layoutTabs[2].id} defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
}}>{layoutTabs[2].title}</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(layoutTabs[2], true)}
</div>
</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel key={layoutTabs[3].id} defaultSize={50} minSize={20} className="!overflow-hidden h-full w-full">
<div style={{height: '100%', width: '100%', display: 'flex', flexDirection: 'column', background: '#09090b', margin: 0, padding: 0, position: 'relative'}}>
<div style={{
background: '#18181b',
color: '#fff',
fontSize: 13,
height: 28,
lineHeight: '28px',
padding: '0 10px',
borderBottom: '1px solid #222224',
letterSpacing: 1,
margin: 0,
}}>{layoutTabs[3].title}</div>
<div style={{flex: 1, position: 'relative', height: '100%', width: '100%', margin: 0, padding: 0}}>
{showTerminal(layoutTabs[3], true)}
</div>
</div>
</ResizablePanel>
</ResizablePanelGroup>
</ResizablePanel>
</ResizablePanelGroup>
);
}
return null;
};
const onAddHostSubmit = (data: any) => {
const id = nextTabId.current++;
const title = `${data.ip || "Host"}:${data.port || 22}`;
const terminalRef = React.createRef<any>();
const newTab: Tab = {
id,
title,
hostConfig: data,
terminalRef,
};
setAllTabs((prev) => [...prev, newTab]);
setCurrentTab(id);
setAllSplitScreenTab((prev) => prev.filter((tid) => tid !== id));
};
const getLayoutStyle = () => {
if (allSplitScreenTab.length === 0) {
return "flex flex-col h-full w-full";
} else if (allSplitScreenTab.length === 1) {
return "grid grid-cols-2 h-full w-full";
} else if (allSplitScreenTab.length === 2) {
return "grid grid-cols-2 grid-rows-2 h-full w-full";
} else {
return "grid grid-cols-2 grid-rows-2 h-full w-full";
}
};
return (
<div style={{ display: 'flex', width: '100vw', height: '100vh', overflow: 'hidden' }}>
{/* Sidebar: fixed width */}
<div style={{ width: 256, flexShrink: 0, height: '100vh', position: 'relative', zIndex: 2, margin: 0, padding: 0, border: 'none' }}>
<SSHSidebar
onSelectView={onSelectView}
onAddHostSubmit={onAddHostSubmit}
/>
</div>
{/* Main area: fills the rest */}
<div
className="terminal-container"
style={{
flex: 1,
height: '100vh',
position: 'relative',
overflow: 'hidden',
margin: 0,
padding: 0,
border: 'none',
}}
>
{/* Always render the topbar at the top */}
<div style={{ position: 'absolute', top: 0, left: 0, width: '100%', zIndex: 10 }}>
<SSHTopbar
allTabs={allTabs}
currentTab={currentTab ?? -1}
setActiveTab={setActiveTab}
allSplitScreenTab={allSplitScreenTab}
setSplitScreenTab={setSplitScreenTab}
setCloseTab={setCloseTab}
/>
</div>
{/* Split area below the topbar */}
<div style={{ height: 'calc(100% - 46px)', marginTop: 46, position: 'relative' }}>
{/* Absolutely render all terminals for persistence */}
{allSplitScreenTab.length > 0 && (
<div style={{ position: 'absolute', top: 0, right: 0, zIndex: 20, height: 28 }}>
<button
style={{
background: '#18181b',
color: '#fff',
borderLeft: '1px solid #222224',
borderRight: '1px solid #222224',
borderTop: 'none',
borderBottom: '1px solid #222224',
borderRadius: 0,
padding: '2px 10px',
cursor: 'pointer',
fontSize: 13,
margin: 0,
height: 28,
display: 'flex',
alignItems: 'center',
}}
onClick={() => setSplitKey((k) => k + 1)}
>
Reset Split Sizes
</button>
</div>
)}
{renderAllTerminals()}
{renderTerminals()}
</div>
</div>
</div>
);
}

198
src/apps/SSH/SSHSidebar.tsx Normal file
View File

@@ -0,0 +1,198 @@
import React from 'react';
import {
CornerDownLeft,
Plus
} from "lucide-react"
import {
Button
} from "@/components/ui/button.tsx"
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuItem, SidebarProvider,
} from "@/components/ui/sidebar.tsx"
import {
Separator,
} from "@/components/ui/separator.tsx"
import {
Sheet,
SheetClose,
SheetContent,
SheetFooter,
SheetHeader,
SheetTitle,
SheetTrigger
} from "@/components/ui/sheet.tsx";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form.tsx";
import { useForm } from "react-hook-form"
import { Input } from "@/components/ui/input.tsx";
interface SidebarProps {
onSelectView: (view: string) => void;
onAddHostSubmit: (data: any) => void;
}
export function SSHSidebar({ onSelectView, onAddHostSubmit }: SidebarProps): React.ReactElement {
const addHostForm = useForm({
defaultValues: {
}
})
const onAddHostSubmitReset = (data: any) => {
addHostForm.reset();
onAddHostSubmit(data);
}
return (
<SidebarProvider>
<Sidebar className="h-full flex flex-col">
<SidebarContent className="flex flex-col flex-grow h-full">
<SidebarGroup className="flex flex-col flex-grow h-full">
<SidebarGroupLabel className="text-lg text-center font-bold text-white">
Termix / SSH
</SidebarGroupLabel>
<Separator className="p-0.25 mt-1 mb-1" />
<SidebarGroupContent className="flex flex-col flex-grow h-full">
<SidebarMenu className="flex flex-col flex-grow h-full">
<SidebarMenuItem key="Homepage">
<Button
className="w-full mt-2 mb-2 h-8"
onClick={() => onSelectView("homepage")}
variant="outline"
>
<CornerDownLeft />
Return
</Button>
<Separator className="p-0.25 mt-1 mb-1" />
</SidebarMenuItem>
<SidebarMenuItem key="AddHost">
<Sheet>
<SheetTrigger asChild>
<Button
className="w-full mt-2 mb-2 h-8"
variant="outline"
>
<Plus />
Add Host
</Button>
</SheetTrigger>
<SheetContent
side="left"
className="w-[256px] fixed top-0 left-0 h-full z-[100] flex flex-col"
>
<SheetHeader className="pb-0.5">
<SheetTitle>Add Host</SheetTitle>
</SheetHeader>
{/* Scrollable content */}
<div className="flex-1 overflow-y-auto px-4">
<Form {...addHostForm}>
<form
id="add-host-form"
onSubmit={addHostForm.handleSubmit(onAddHostSubmitReset)}
className="space-y-3"
>
<FormField
control={addHostForm.control}
name="ip"
render={({ field }) => (
<FormItem>
<FormLabel>IP</FormLabel>
<FormControl>
<Input placeholder="127.0.0.1" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={addHostForm.control}
name="port"
render={({ field }) => (
<FormItem>
<FormLabel>Port</FormLabel>
<FormControl>
<Input placeholder="22" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={addHostForm.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="username123" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={addHostForm.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input placeholder="password123" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
</div>
<Separator className="p-0.25 mt-2" />
<SheetFooter className="px-4 pt-1 pb-4">
<SheetClose asChild>
<Button type="submit" form="add-host-form">
Add Host
</Button>
</SheetClose>
<SheetClose asChild>
<Button variant="outline">Close</Button>
</SheetClose>
</SheetFooter>
</SheetContent>
</Sheet>
</SidebarMenuItem>
<SidebarMenuItem key="Main" className="flex flex-col flex-grow">
<div className="flex w-full flex-grow rounded-md bg-[#09090b] border border-[#434345] p-2 mb-1">
</div>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
</Sidebar>
</SidebarProvider>
);
}

View File

@@ -0,0 +1,79 @@
import React from "react";
import {Button} from "@/components/ui/button.tsx";
import {X, SeparatorVertical} from "lucide-react"
interface TerminalTab {
id: number;
title: string;
}
interface SSHTabListProps {
allTabs: TerminalTab[];
currentTab: number;
setActiveTab: (tab: number) => void;
allSplitScreenTab: number[];
setSplitScreenTab: (tab: number) => void;
setCloseTab: (tab: number) => void;
}
export function SSHTabList({
allTabs,
currentTab,
setActiveTab,
allSplitScreenTab = [],
setSplitScreenTab,
setCloseTab,
}: SSHTabListProps): React.ReactElement {
const isSplitScreenActive = allSplitScreenTab.length > 0;
return (
<div className="inline-flex items-center h-full px-[0.5rem] overflow-x-auto">
{allTabs.map((terminal, index) => {
const isActive = terminal.id === currentTab;
const isSplit = allSplitScreenTab.includes(terminal.id);
const isSplitButtonDisabled =
(isActive && !isSplitScreenActive) ||
(allSplitScreenTab.length >= 3 && !isSplit);
return (
<div
key={terminal.id}
className={index < allTabs.length - 1 ? "mr-[0.5rem]" : ""}
>
<div className="inline-flex rounded-md shadow-sm" role="group">
{/* Set Active Tab Button */}
<Button
onClick={() => setActiveTab(terminal.id)}
disabled={isSplit}
variant="outline"
className={`rounded-r-none ${isActive ? '!bg-[#1d1d1f] !text-white !border-[#2d2d30] !hover:bg-[#1d1d1f] !active:bg-[#1d1d1f] !focus:bg-[#1d1d1f] !hover:text-white !active:text-white !focus:text-white' : ''}`}
>
{terminal.title}
</Button>
{/* Split Screen Button */}
<Button
onClick={() => setSplitScreenTab(terminal.id)}
disabled={isSplitButtonDisabled || isActive}
variant="outline"
className="rounded-none p-0 !w-9 !h-9"
>
<SeparatorVertical className="!w-5 !h-5" strokeWidth={2.5} />
</Button>
{/* Close Tab Button */}
<Button
onClick={() => setCloseTab(terminal.id)}
disabled={(isSplitScreenActive && isActive) || isSplit}
variant="outline"
className="rounded-l-none p-0 !w-9 !h-9"
>
<X className="!w-5 !h-5" strokeWidth={2.5} />
</Button>
</div>
</div>
);
})}
</div>
);
}

View File

@@ -0,0 +1,178 @@
import React, { useEffect, useRef, useState, useImperativeHandle, forwardRef } from 'react';
import { useXTerm } from 'react-xtermjs';
import { FitAddon } from '@xterm/addon-fit';
import { ClipboardAddon } from '@xterm/addon-clipboard';
interface SSHTerminalProps {
hostConfig: any;
isVisible: boolean;
title?: string;
showTitle?: boolean;
splitScreen?: boolean;
}
export const SSHTerminal = forwardRef<any, SSHTerminalProps>(function SSHTerminal(
{ hostConfig, isVisible, splitScreen = false },
ref
) {
console.log('Rendering SSHTerminal', { hostConfig, isVisible });
const { instance: terminal, ref: xtermRef } = useXTerm();
const fitAddonRef = useRef<FitAddon | null>(null);
const webSocketRef = useRef<WebSocket | null>(null);
const resizeTimeout = useRef<NodeJS.Timeout | null>(null);
const [visible, setVisible] = useState(false);
useImperativeHandle(ref, () => ({
disconnect: () => {
if (webSocketRef.current) {
webSocketRef.current.close();
}
},
fit: () => {
if (fitAddonRef.current) {
fitAddonRef.current.fit();
}
}
}), []);
useEffect(() => {
function handleWindowResize() {
fitAddonRef.current?.fit();
}
window.addEventListener('resize', handleWindowResize);
return () => window.removeEventListener('resize', handleWindowResize);
}, []);
useEffect(() => {
if (!terminal || !xtermRef.current || !hostConfig) return;
const fitAddon = new FitAddon();
const clipboardAddon = new ClipboardAddon();
fitAddonRef.current = fitAddon;
terminal.loadAddon(fitAddon);
terminal.loadAddon(clipboardAddon);
terminal.open(xtermRef.current);
terminal.options = {
cursorBlink: true,
cursorStyle: 'bar',
scrollback: 5000,
fontSize: 15,
theme: {
background: '#09090b',
foreground: '#f7f7f7',
},
};
const onResize = () => {
if (!xtermRef.current) return;
const { width, height } = xtermRef.current.getBoundingClientRect();
if (width < 100 || height < 50) return;
if (resizeTimeout.current) clearTimeout(resizeTimeout.current);
resizeTimeout.current = setTimeout(() => {
fitAddonRef.current?.fit();
// Always send cols + 1
const cols = terminal.cols + 1;
const rows = terminal.rows;
webSocketRef.current?.send(JSON.stringify({
type: 'resize',
data: { cols, rows }
}));
}, 100);
};
const resizeObserver = new ResizeObserver(onResize);
resizeObserver.observe(xtermRef.current);
setTimeout(() => {
fitAddon.fit();
setVisible(true);
// Always send cols + 1
const cols = terminal.cols + 1;
const rows = terminal.rows;
const ws = new WebSocket('ws://localhost:8082');
webSocketRef.current = ws;
ws.addEventListener('open', () => {
terminal.writeln('WebSocket opened');
ws.send(JSON.stringify({
type: 'connectToHost',
data: {
cols,
rows,
hostConfig: hostConfig
}
}));
terminal.onData((data) => {
ws.send(JSON.stringify({
type: 'input',
data
}));
});
});
ws.addEventListener('message', (event) => {
try {
const msg = JSON.parse(event.data);
console.log('WS message received:', msg); // Debug log
if (msg.type === 'data') {
terminal.write(msg.data);
} else if (msg.type === 'error') {
terminal.writeln(`\r\n[ERROR] ${msg.message}`);
} else if (msg.type === 'connected') {
terminal.writeln('[SSH connected. Waiting for prompt...]');
} else {
console.log('Unhandled message:', msg);
}
} catch (err) {
console.error('Failed to parse message', err);
}
});
ws.addEventListener('close', () => {
terminal.writeln('\r\n[Connection closed]');
});
ws.addEventListener('error', () => {
terminal.writeln('\r\n[Connection error]');
});
}, 300);
return () => {
resizeObserver.disconnect();
if (resizeTimeout.current) clearTimeout(resizeTimeout.current);
webSocketRef.current?.close();
};
}, [xtermRef, terminal, hostConfig]);
useEffect(() => {
if (isVisible && fitAddonRef.current) {
fitAddonRef.current.fit();
}
}, [isVisible]);
return (
<div
ref={xtermRef}
style={{
position: 'absolute',
top: splitScreen ? 0 : 48,
left: 0,
right: '-1ch',
bottom: 0,
marginLeft: 2,
opacity: visible && isVisible ? 1 : 0,
}}
/>
);
});

View File

@@ -0,0 +1,37 @@
import {SSHTabList} from "@/apps/SSH/SSHTabList.tsx";
import React from "react";
interface TerminalTab {
id: number;
title: string;
}
interface SSHTopbarProps {
allTabs: TerminalTab[];
currentTab: number;
setActiveTab: (tab: number) => void;
allSplitScreenTab: number[];
setSplitScreenTab: (tab: number) => void;
setCloseTab: (tab: number) => void;
}
export function SSHTopbar({ allTabs, currentTab, setActiveTab, allSplitScreenTab, setSplitScreenTab, setCloseTab }: SSHTopbarProps): React.ReactElement {
return (
<div className="flex h-11.5 z-100" style={{
position: 'absolute',
left: 0,
width: '100%',
backgroundColor: '#18181b',
borderBottom: '1px solid #222224',
}}>
<SSHTabList
allTabs={allTabs}
currentTab={currentTab}
setActiveTab={setActiveTab}
allSplitScreenTab={allSplitScreenTab}
setSplitScreenTab={setSplitScreenTab}
setCloseTab={setCloseTab}
/>
</div>
)
}