"use client"; import { useEffect, useState, useRef } from "react"; import SideNav from "./components/SideNav"; import WindowManager from "./components/windows/WindowManager"; import DevConsole, { type LogEntry } from "./components/DevConsole"; export default function Home() { const [isAuthed, setIsAuthed] = useState(null); const [online, setOnline] = useState(false); const [mounted, setMounted] = useState(false); const [panelOpen, setPanelOpen] = useState(false); const [panelWidth, setPanelWidth] = useState(440); const [isMobile, setIsMobile] = useState(false); const [logs, setLogs] = useState([]); const logIdRef = useRef(0); useEffect(() => { setMounted(true); }, []); useEffect(() => { const check = () => setIsMobile(window.innerWidth < 768); check(); window.addEventListener("resize", check); return () => window.removeEventListener("resize", check); }, []); useEffect(() => { fetch("/api/auth/check").then((r) => setIsAuthed(r.ok)); }, []); // Lightweight online check useEffect(() => { const check = async () => { try { setOnline((await fetch("/api/stats")).ok); } catch { setOnline(false); } }; check(); const id = setInterval(check, 5000); return () => clearInterval(id); }, []); // Dev console fetch logger useEffect(() => { const original = window.fetch; window.fetch = async (input, init) => { const url = typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url; const shouldLog = (url.startsWith("/api/") && !url.startsWith("/api/dev/proxy")) || url.includes("localhost:3001"); if (!shouldLog) return original(input, init); const method = ((init?.method ?? (input instanceof Request ? input.method : "GET")) as string).toUpperCase(); const id = ++logIdRef.current; let path = url; try { path = new URL(url, window.location.origin).pathname; } catch {} const timestamp = new Date().toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", second: "2-digit" }); const start = Date.now(); setLogs((prev) => [ ...prev, { id, method, path, url, status: null, duration: null, timestamp, response: null }, ]); try { const res = await original(input, init); const clone = res.clone(); const text = await clone.text(); const duration = Date.now() - start; setLogs((prev) => prev.map((e) => (e.id === id ? { ...e, status: res.status, duration, response: text } : e)), ); return res; } catch (err) { const duration = Date.now() - start; setLogs((prev) => prev.map((e) => (e.id === id ? { ...e, status: 0, duration, response: String(err) } : e)), ); throw err; } }; return () => { window.fetch = original; }; }, []); return (
setPanelOpen((o) => !o)} />
{mounted && ( )}
{mounted && isAuthed && ( setPanelOpen(false)} onWidthChange={setPanelWidth} logs={logs} /> )}
); }