"use client"; import { useState, useEffect, useCallback } from "react"; import { useRouter } from "next/navigation"; import { API_BASE } from "@/lib/api"; import { useAuth } from "@/lib/auth"; import DashboardLayout from "@/components/DashboardLayout"; import StatCard from "@/components/ui/StatCard"; import GlassCard from "@/components/ui/GlassCard"; type CompanyInfo = { id: string; name?: string; industry?: string; company_size?: string; skill_count?: number; source_count?: number; last_compile?: { completed_at: string; result_version: string } | null; }; export default function Dashboard() { const [companyId, setCompanyId] = useState(""); const [loading, setLoading] = useState(false); const [company, setCompany] = useState(null); const [fetching, setFetching] = useState(false); const [error, setError] = useState(""); const [autoLoaded, setAutoLoaded] = useState(false); const router = useRouter(); const { user } = useAuth(); const fetchCompany = useCallback(async (id: string) => { if (!id) return; setFetching(true); setError(""); try { const res = await fetch(`${API_BASE}/companies/${id}`); if (!res.ok) throw new Error("Not found"); const data = await res.json(); setCompany(data); // Persist the company ID so it auto-loads next time sessionStorage.setItem("kernl_company_id", id); } catch { setCompany(null); setError("Company not found. Create it first via Onboarding."); } finally { setFetching(false); } }, []); // Auto-load saved company when user is signed in useEffect(() => { if (autoLoaded) return; const savedCompanyId = sessionStorage.getItem("kernl_company_id"); if (savedCompanyId) { setCompanyId(savedCompanyId); fetchCompany(savedCompanyId); setAutoLoaded(true); } }, [autoLoaded, fetchCompany]); const handleCompile = async () => { if (!companyId) return; setLoading(true); try { const res = await fetch(`${API_BASE}/compile`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ company_id: companyId }), }); const data = await res.json(); if (data.job_id) router.push(`/compile/${data.job_id}`); } catch { alert("Failed to start compilation"); } finally { setLoading(false); } }; // Determine if we should show the lookup section // Hide it when we have a loaded company (auto or manual) const showLookup = !company; return (
{/* Page Header */}

{company ? company.name || companyId : "Command Center"}

{company ? "Operational brain dashboard" : "Compile operational knowledge into an executable AI brain"}

{company && ( )}
{/* Company Lookup — only visible when no company is loaded */} {showLookup && (
setCompanyId(e.target.value)} onKeyDown={(e) => e.key === "Enter" && companyId && fetchCompany(companyId) } />
{error && (

{error}

)} {!company && !error && (

Try:{" "} {" "} (pre-loaded demo company)

)}
)} {/* Company Dashboard */} {company && (
{/* Stat Cards */}
} /> } /> } /> } />
{/* Quick Actions */}

Quick Actions

{/* Last Compilation */} {company.last_compile && (

Last Compilation

{new Date(company.last_compile.completed_at).toLocaleString()} {company.last_compile.result_version && ( {company.last_compile.result_version} )}

)}
)} {/* Empty State — only when no company ID has been entered and nothing loaded */} {!company && !error && !companyId && (

Your company's operational brain

Upload SOPs, Slack exports, and support tickets. Kernl compiles them into structured, queryable skills that make your AI agent actually understand your business.

)}
); }