import { lazy, Suspense, useState, useEffect } from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate, useNavigate } from 'react-router-dom';
import Login from './components/Login';
import Layout from './components/Layout';
import Dashboard from './components/Dashboard';
import CommandPalette from './components/CommandPalette/CommandPalette';
import ShortcutsHelp from './components/ShortcutsHelp/ShortcutsHelp';
import { ToastProvider } from './components/Toasts/ToastProvider';
import { useToast } from './components/Toasts/useToast';
import { useGlobalHotkeys } from './hooks/useGlobalHotkeys';
// Page components are code-split per route. Each lands as its own
// chunk and only downloads when the user navigates to that path —
// initial page-load stays slim. Dashboard stays eager because it's
// the landing page: lazy-loading it would Suspense-flicker on every
// login for zero gain.
const DeckyFleet = lazy(() => import('./components/DeckyFleet'));
const LiveLogs = lazy(() => import('./components/LiveLogs'));
const Webhooks = lazy(() => import('./components/Webhooks'));
const Attackers = lazy(() => import('./components/Attackers'));
const AttackerDetail = lazy(() => import('./components/AttackerDetail'));
const Config = lazy(() => import('./components/Config'));
const Bounty = lazy(() => import('./components/Bounty'));
const Credentials = lazy(() => import('./components/Credentials'));
const RemoteUpdates = lazy(() => import('./components/RemoteUpdates'));
const SwarmHosts = lazy(() => import('./components/SwarmHosts'));
const MazeNET = lazy(() => import('./components/MazeNET/MazeNET'));
const TopologyList = lazy(() => import('./components/TopologyList/TopologyList'));
/* Minimal fallback rendered while a lazy-loaded route chunk is in
* flight. Matches the house "dim mono" voice — no spinner library,
* no new CSS. Visible for a few frames on first navigation to a
* route; cached thereafter. */
const RouteFallback: React.FC = () => (
LOADING…
);
/* Unified MazeNET entrypoint: no ?topology → topology selector,
* ?topology= → editor bound to that topology. */
function MazeNETRoute() {
const qs = typeof window !== 'undefined' ? window.location.search : '';
const hasId = new URLSearchParams(qs).get('topology');
return hasId ? : ;
}
function isTokenValid(token: string): boolean {
try {
const payload = JSON.parse(atob(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')));
return typeof payload.exp === 'number' && payload.exp * 1000 > Date.now();
} catch {
return false;
}
}
function getValidToken(): string | null {
const stored = localStorage.getItem('token');
if (stored && isTokenValid(stored)) return stored;
if (stored) localStorage.removeItem('token');
return null;
}
const ACTION_LABELS: Record = {
'deploy': 'DEPLOY · OPENING WIZARD',
'pause-logs': 'STREAM · TOGGLE QUEUED',
'mutate-all': 'MUTATE ALL · QUEUED',
'export-bounty': 'EXPORT BOUNTY · QUEUED',
};
interface AuthedShellProps {
onLogout: () => void;
onSearch: (q: string) => void;
searchQuery: string;
}
const AuthedShell: React.FC = ({ onLogout, onSearch, searchQuery }) => {
const navigate = useNavigate();
const { push } = useToast();
const [cmdOpen, setCmdOpen] = useState(false);
const [helpOpen, setHelpOpen] = useState(false);
useGlobalHotkeys({ cmdOpen, setCmdOpen, helpOpen, setHelpOpen });
const handleAction = (id: string) => {
if (id === 'shortcuts-help') { setHelpOpen(true); return; }
if (id === 'deploy') navigate('/fleet');
window.dispatchEvent(new CustomEvent('decnet:cmd', { detail: { id } }));
push({ text: ACTION_LABELS[id] ?? `${id.toUpperCase()} · QUEUED`, tone: 'violet', icon: 'terminal' });
};
return (
<>
setCmdOpen(true)}>
}>
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
setCmdOpen(false)}
onNav={navigate}
onAction={handleAction}
/>
setHelpOpen(false)} />
>
);
};
function App() {
const [token, setToken] = useState(getValidToken);
const [searchQuery, setSearchQuery] = useState('');
useEffect(() => {
const onAuthLogout = () => setToken(null);
window.addEventListener('auth:logout', onAuthLogout);
return () => window.removeEventListener('auth:logout', onAuthLogout);
}, []);
useEffect(() => {
let accent = 'matrix';
try {
const raw = localStorage.getItem('decnet_tweaks');
if (raw) {
const parsed = JSON.parse(raw);
if (parsed?.accent === 'matrix' || parsed?.accent === 'violet') accent = parsed.accent;
}
} catch { /* fall through to default */ }
document.documentElement.setAttribute('data-accent', accent);
}, []);
const handleLogin = (newToken: string) => setToken(newToken);
const handleLogout = () => { localStorage.removeItem('token'); setToken(null); };
const handleSearch = (query: string) => setSearchQuery(query);
if (!token) {
return ;
}
return (
);
}
export default App;