import { StrictMode, Suspense, lazy } from 'react';
import ReactDOM from 'react-dom/client';
import '@fontsource/mona-sans/400.css';
import '@fontsource/mona-sans/500.css';
import '@fontsource/mona-sans/600.css';
import '@fontsource/mona-sans/700.css';
import '@fontsource/space-grotesk/400.css';
import '@fontsource/space-grotesk/500.css';
import '@fontsource/space-grotesk/600.css';
import '@fontsource/space-grotesk/700.css';
import '@fontsource/jetbrains-mono/400.css';
import '@fontsource/jetbrains-mono/500.css';
import '@fontsource/jetbrains-mono/600.css';
import App from './App';
import { AppLockGate } from './components/AppLockGate';
import { loadLocaleMessages } from './application/i18n/messages';
import { DEFAULT_UI_LOCALE, resolveSupportedLocale } from './infrastructure/config/i18n';
import { STORAGE_KEY_UI_LANGUAGE } from './infrastructure/config/storageKeys';
import { localStorageAdapter } from './infrastructure/persistence/localStorageAdapter';
// Startup timeline marks (see electron/perfTrace.cjs); visible in DevTools too.
performance.mark('netcatty:renderer:entry-evaluated');
const LazySettingsPage = lazy(() => import('./components/SettingsPage'));
const LazyTrayPanel = lazy(() => import('./components/TrayPanel'));
const LazyTerminalPopupPage = lazy(() => import('./components/TerminalPopupPage'));
const LazyEditorWindowPage = lazy(() => import('./components/editor/EditorWindowPage'));
function SettingsWindowFallback() {
return (
{Array.from({ length: 7 }).map((_, index) => (
))}
{Array.from({ length: 6 }).map((_, index) => (
))}
);
}
function TerminalPopupWindowFallback() {
return (
);
}
const rootElement = document.getElementById('root');
if (!rootElement) {
throw new Error("Could not find root element to mount to");
}
// Simple hash-based routing for separate windows
const getRoute = () => {
const hash = window.location.hash;
if (hash === '#/settings' || hash.startsWith('#/settings')) {
return 'settings';
}
if (hash === '#/tray' || hash.startsWith('#/tray')) {
return 'tray';
}
if (hash === '#/terminal-popup' || hash.startsWith('#/terminal-popup')) {
return 'terminal-popup';
}
if (hash === '#/editor-window' || hash.startsWith('#/editor-window')) {
return 'editor-window';
}
if (hash === '#/session-window' || hash.startsWith('#/session-window')) {
return 'main';
}
return 'main';
};
const root = ReactDOM.createRoot(rootElement);
const syncTrayWindowClass = (route: string) => {
const rootEl = document.documentElement;
if (route === 'tray') {
rootEl.classList.add('tray-window');
document.getElementById('splash')?.remove();
} else {
rootEl.classList.remove('tray-window');
}
};
const renderApp = () => {
const route = getRoute();
const isPeerSessionWindow = window.location.hash.startsWith('#/session-window');
const isEditorWindow = route === 'editor-window';
// Peer session windows must not drive the main window's settings IPC sync
// and must not re-apply OS-level system settings effects (tray, global
// shortcuts, …) — they follow the main window through the chrome stores.
// Editors consume their own settings instance, so keep IPC/storage sync on.
const settingsOptions = isPeerSessionWindow
? { enableSettingsSync: false, enableSystemEffects: false }
: isEditorWindow ? { enableSystemEffects: false } : undefined;
syncTrayWindowClass(route);
if (route === 'settings') {
root.render(
{({ settings, appLock }) => (
}>
)}
);
} else if (route === 'tray') {
root.render(
{({ settings }) => (
}>
)}
);
} else if (route === 'terminal-popup') {
// forceRenderChildren: main sends terminalPopupConfig immediately after
// loadURL; the page must mount (and register onTerminalPopupConfig) without
// waiting for async app-lock init. notifyRendererReady stays false — this
// route is not on the deep-link readiness path.
root.render(
{({ settings, appLock }) => (
}>
{/* forceRenderChildren mounts the page so config IPC registers while
locked; defer starting the terminal until unlock (Codex P2). */}
)}
);
} else if (route === 'editor-window') {
root.render(
{({ settings, appLock }) => (
}>
)}
);
} else {
root.render(
{({ settings, appLock }) => }
);
}
};
// Load the saved UI language before the first render (only English ships in
// the startup chunk), so no window flashes English first. Render regardless
// if the dictionary fails to load; I18nProvider retries and falls back to en.
void loadLocaleMessages(
resolveSupportedLocale(localStorageAdapter.readString(STORAGE_KEY_UI_LANGUAGE) || DEFAULT_UI_LOCALE),
)
.catch(() => {})
.finally(() => {
renderApp();
// Listen for hash changes
window.addEventListener('hashchange', renderApp);
});