Files
Termix/src/main.tsx
T

272 lines
8.1 KiB
TypeScript
Raw Normal View History

+7
2025-11-05 10:36:16 -06:00
/* eslint-disable react-refresh/only-export-components */
2026-05-06 15:12:07 -05:00
import { prepareClientCacheVersion } from "@/lib/client-cache-version";
2026-05-11 01:23:11 -05:00
import { StrictMode, Suspense, lazy, useState, useRef, useEffect } from "react";
2025-09-12 14:42:00 -05:00
import { createRoot } from "react-dom/client";
2026-05-11 01:23:11 -05:00
import "./ui/index.css";
2025-09-12 14:42:00 -05:00
import { ThemeProvider } from "@/components/theme-provider";
2026-05-11 01:23:11 -05:00
import "./ui/i18n/i18n";
2026-05-06 15:12:07 -05:00
import { isElectron } from "@/lib/electron";
2026-05-11 01:23:11 -05:00
import { Toaster } from "@/components/sonner";
import { Auth, getStoredAuth, clearStoredAuth } from "@/auth/Auth";
2026-05-28 22:29:20 -05:00
import { getUserInfo, getCurrentToken, appReadyPromise } from "@/main-axios";
2026-05-11 01:23:11 -05:00
import { applyAccentColor, applyFontSize } from "@/lib/theme";
import type { FontSizeId } from "@/types/ui-types";
import { useServiceWorker } from "@/hooks/use-service-worker";
2026-05-28 22:29:20 -05:00
import { useTranslation } from "react-i18next";
2026-05-06 15:12:07 -05:00
2026-05-11 01:23:11 -05:00
const AppShell = lazy(() =>
import("@/AppShell").then((m) => ({ default: m.AppShell })),
2026-05-06 15:12:07 -05:00
);
2026-05-11 01:23:11 -05:00
// Full-screen apps opened via query params (e.g. from external links or Electron)
2026-06-04 15:16:53 -04:00
const TerminalApp = lazy(() =>
import("@/features/terminal/TerminalApp").then((m) => ({
default: m.default,
})),
2026-05-06 15:12:07 -05:00
);
2026-06-04 15:16:53 -04:00
const FileManagerApp = lazy(() =>
import("@/features/file-manager/FileManagerApp").then((m) => ({
default: m.default,
})),
);
const TunnelApp = lazy(() =>
import("@/features/tunnel/TunnelApp").then((m) => ({ default: m.default })),
);
+2
2026-06-16 15:59:53 -05:00
const HostMetricsApp = lazy(() =>
import("@/features/host-metrics/HostMetricsApp").then((m) => ({
2026-06-04 15:16:53 -04:00
default: m.default,
})),
);
const DockerApp = lazy(() =>
import("@/features/docker/DockerApp").then((m) => ({ default: m.default })),
);
const GuacamoleApp = lazy(() =>
import("@/features/guacamole/GuacamoleApp").then((m) => ({
default: m.default,
})),
2026-05-06 15:12:07 -05:00
);
+2
2026-06-16 15:59:53 -05:00
// --- tmux-monitor ---
const TmuxMonitorApp = lazy(() =>
import("@/features/tmux-monitor/TmuxMonitorApp").then((m) => ({
default: m.default,
})),
);
2026-05-11 01:23:11 -05:00
2026-05-06 15:12:07 -05:00
const ElectronVersionCheck = lazy(() =>
2026-05-11 01:23:11 -05:00
import("@/user/ElectronVersionCheck").then((module) => ({
2026-05-06 15:12:07 -05:00
default: module.ElectronVersionCheck,
})),
);
2026-01-24 19:49:42 -06:00
2026-05-28 22:29:20 -05:00
type Phase =
| "verifying"
| "idle-auth"
| "fading-in"
| "idle-app"
| "fading-out";
2026-05-11 01:23:11 -05:00
function FullscreenApp() {
2026-01-24 19:49:42 -06:00
const searchParams = new URLSearchParams(window.location.search);
const view = searchParams.get("view");
const hostId = searchParams.get("hostId");
+2
2026-06-16 15:59:53 -05:00
const tmuxSession = searchParams.get("tmuxSession");
2026-01-24 19:49:42 -06:00
switch (view) {
case "terminal":
+2
2026-06-16 15:59:53 -05:00
return (
<TerminalApp
hostId={hostId || undefined}
tmuxSession={tmuxSession || undefined}
/>
);
2026-01-24 19:49:42 -06:00
case "file-manager":
return <FileManagerApp hostId={hostId || undefined} />;
case "tunnel":
return <TunnelApp hostId={hostId || undefined} />;
+2
2026-06-16 15:59:53 -05:00
case "host-metrics":
2026-01-24 19:49:42 -06:00
case "server-stats":
+2
2026-06-16 15:59:53 -05:00
return <HostMetricsApp hostId={hostId || undefined} />;
2026-01-24 19:49:42 -06:00
case "docker":
return <DockerApp hostId={hostId || undefined} />;
2026-03-14 20:05:05 -05:00
case "rdp":
case "vnc":
case "telnet":
+2
2026-06-16 15:59:53 -05:00
return (
<GuacamoleApp
hostId={hostId || undefined}
protocol={view as "rdp" | "vnc" | "telnet"}
/>
);
case "tmux-monitor": // --- tmux-monitor ---
case "tmux_monitor": // tab type spelling, so copied links also resolve
return <TmuxMonitorApp hostId={hostId || undefined} />;
2026-01-24 19:49:42 -06:00
default:
2026-05-11 01:23:11 -05:00
return null;
2026-01-24 19:49:42 -06:00
}
2026-05-11 01:23:11 -05:00
}
2025-08-07 02:20:27 -05:00
2026-05-11 01:23:11 -05:00
function App() {
const stored = getStoredAuth();
const [phase, setPhase] = useState<Phase>(
2026-05-28 22:29:20 -05:00
stored?.loggedIn ? "verifying" : "idle-auth",
2026-05-11 01:23:11 -05:00
);
const [authUsername, setAuthUsername] = useState(stored?.username ?? "");
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
2025-09-12 14:42:00 -05:00
useEffect(() => {
2026-05-11 01:23:11 -05:00
const savedAccent = localStorage.getItem("termix-accent");
if (savedAccent) applyAccentColor(savedAccent);
const savedSize = localStorage.getItem(
"termix-font-size",
) as FontSizeId | null;
applyFontSize(savedSize ?? "lg");
2025-09-12 14:42:00 -05:00
return () => {
2026-05-11 01:23:11 -05:00
if (timerRef.current) clearTimeout(timerRef.current);
2025-09-12 14:42:00 -05:00
};
}, []);
2026-05-28 22:29:20 -05:00
// Verify stored session against the server before rendering AppShell.
// Wait for API instances to be initialized with correct embedded/server config first.
// In Electron, also repopulate localStorage["jwt"] so WebSocket connections can auth
// after a session restore (the token is only written to localStorage during a fresh login).
useEffect(() => {
if (phase !== "verifying") return;
appReadyPromise
.then(() => getUserInfo())
.then(() => {
if (isElectron()) {
getCurrentToken()
.then((token) => {
if (token) localStorage.setItem("jwt", token);
})
.catch(() => {});
}
setPhase("fading-in");
timerRef.current = setTimeout(() => setPhase("idle-app"), 450);
})
.catch(() => {
clearStoredAuth();
setPhase("idle-auth");
});
}, [phase]);
2026-05-11 01:23:11 -05:00
function handleLogin(u: string) {
setAuthUsername(u);
setPhase("fading-in");
timerRef.current = setTimeout(() => setPhase("idle-app"), 450);
2026-05-28 22:29:20 -05:00
if (isElectron()) {
window.electronAPI?.startC2SAutoStartTunnels?.().catch(() => {});
}
2026-05-11 01:23:11 -05:00
}
function handleLogout() {
clearStoredAuth();
setPhase("fading-out");
timerRef.current = setTimeout(() => {
setAuthUsername("");
setPhase("idle-auth");
}, 450);
}
const showApp =
phase === "idle-app" || phase === "fading-in" || phase === "fading-out";
const showAuth =
phase === "idle-auth" || phase === "fading-in" || phase === "fading-out";
const appOpacity = phase === "idle-app" ? 1 : 0;
const authOpacity = phase === "idle-auth" ? 1 : 0;
2026-05-28 22:29:20 -05:00
const { t } = useTranslation();
const isTransitioning = phase === "fading-in" || phase === "fading-out";
if (phase === "verifying") {
return (
<div className="fixed inset-0 flex items-center justify-center bg-background">
<div className="flex flex-col items-center gap-4">
<div className="w-5 h-5 border-2 border-primary border-t-transparent rounded-full animate-spin" />
<p className="text-sm text-muted-foreground">{t("common.loading")}</p>
</div>
</div>
);
}
2026-05-11 01:23:11 -05:00
return (
<>
2026-05-28 22:29:20 -05:00
{isTransitioning && (
<div className="fixed inset-0 z-0 flex items-center justify-center bg-background">
<div className="flex flex-col items-center gap-4">
<div className="w-5 h-5 border-2 border-primary border-t-transparent rounded-full animate-spin" />
<p className="text-sm text-muted-foreground">
{t("common.loading")}
</p>
</div>
</div>
)}
2026-05-11 01:23:11 -05:00
{showApp && (
<div
2026-05-28 22:29:20 -05:00
className="fixed inset-0 z-10 transition-opacity duration-[450ms] ease-in-out"
2026-05-11 01:23:11 -05:00
style={{
opacity: appOpacity,
pointerEvents: phase === "idle-app" ? "auto" : "none",
}}
>
<Suspense fallback={null}>
<AppShell username={authUsername} onLogout={handleLogout} />
</Suspense>
</div>
)}
{showAuth && (
<div
2026-05-28 22:29:20 -05:00
className="fixed inset-0 z-20 transition-opacity duration-[450ms] ease-in-out"
2026-05-11 01:23:11 -05:00
style={{
opacity: authOpacity,
pointerEvents: phase === "idle-auth" ? "auto" : "none",
}}
>
<Auth onLogin={handleLogin} />
</div>
)}
<Toaster position="bottom-right" />
</>
);
2025-09-12 14:42:00 -05:00
}
function RootApp() {
+7
2025-11-05 10:36:16 -06:00
const [showVersionCheck, setShowVersionCheck] = useState(true);
2025-09-12 14:42:00 -05:00
2026-01-24 19:49:42 -06:00
useServiceWorker();
const searchParams = new URLSearchParams(window.location.search);
const isFullscreen = searchParams.has("view");
2026-05-11 01:23:11 -05:00
if (isFullscreen) {
return (
<Suspense fallback={null}>
<FullscreenApp />
</Suspense>
);
}
2026-01-24 19:49:42 -06:00
2026-05-11 01:23:11 -05:00
if (isElectron() && showVersionCheck) {
return (
<Suspense fallback={null}>
<ElectronVersionCheck onContinue={() => setShowVersionCheck(false)} />
</Suspense>
);
}
+7
2025-11-05 10:36:16 -06:00
2026-05-11 01:23:11 -05:00
return <App />;
2025-09-12 14:42:00 -05:00
}
2026-05-06 15:12:07 -05:00
prepareClientCacheVersion().finally(() => {
createRoot(document.getElementById("root")!).render(
<StrictMode>
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
<RootApp />
</ThemeProvider>
</StrictMode>,
);
});