import { ChevronDown, CircleDollarSign, CircleEllipsis, Clock3, Crown, FileEdit, Globe, Home, Inbox, ListChecks, LogOut, Mail, Menu, HandHelping, Plug, Send, Settings, Shield, ShieldAlert, Sparkles, Star, Trash2, Users, UsersRound, UserRound, X, } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { getCurrentAccount, getMyCompanyDomain } from "../api"; import { AUTH_ACCOUNT_KEY, AUTH_TOKEN_KEY, clearSession, getAccessToken, } from "../auth"; import Brand from "./Brand"; import MobileChrome from "./MobileChrome"; import { ShellModeContext } from "../shellMode"; import CompanyDnsSetup from "./CompanyDnsSetup"; import CompanyDomains from "./CompanyDomains"; import CompanyAdministrators from "./CompanyAdministrators"; import CompanyAliases from "./CompanyAliases"; import CompanyGroups from "./CompanyGroups"; import CompanyPeople from "./CompanyPeople"; import CompanySecurity from "./CompanySecurity"; import CompanyOverview from "./CompanyOverview"; import CompanyOverviewSetup, { isOrgSetupComplete } from "./CompanyOverviewSetup"; import ChangePasswordModal from "./ChangePasswordModal"; import ComposeDock, { triggerOpenCompose } from "./ComposeDock"; import HumanHandoff from "./HumanHandoff"; import AccountProfile from "./AccountProfile"; import AccountSettings from "./AccountSettings"; import MailboxView from "./MailboxView"; const mailItems = [ [Inbox, "Inbox", "unread"], [Star, "Starred", "starred"], [Clock3, "Snoozed"], [Send, "Sent"], [FileEdit, "Drafts", "drafts"], [ShieldAlert, "Spam"], [Trash2, "Trash"], [Clock3, "Scheduled"], ]; const labels = [ ["Important", "rose", "Important"], ["Projects", "violet", "Projects"], ["Clients", "blue", "Clients"], ["Finance", "amber", "Finance"], ["Personal", "green", "Personal"], ]; const agentItems = [ [HandHelping, "Human Handoff", "handoff"], [Sparkles, "Ask Agent", "ask"], [ListChecks, "Action Items", "actions"], [Sparkles, "Summaries", "summaries"], [Plug, "Integrations", "integrations"], ]; const companyNavItems = [ [Home, "Overview"], [Users, "People"], [UsersRound, "Groups"], [Globe, "Domains"], [CircleEllipsis, "Aliases"], ]; const manageNavItems = [ [Shield, "Security"], [Crown, "Administrators"], [CircleDollarSign, "Billing"], [Settings, "Settings"], ]; function SidebarSection({ title, children, className = "" }) { return ( {title} {children} ); } function NavBadge({ value, variant = "coral" }) { if (!value || value <= 0) return null; return {value}; } export default function DashboardPage() { const [account, setAccount] = useState(null); const [companyDomain, setCompanyDomain] = useState(null); const [needsDnsSetup, setNeedsDnsSetup] = useState(false); const [domainError, setDomainError] = useState(""); const [active, setActive] = useState("Inbox"); const [mobileMenuOpen, setMobileMenuOpen] = useState(false); const [sidebarExpanded, setSidebarExpanded] = useState( () => typeof window !== "undefined" && window.matchMedia("(min-width: 1200px)").matches, ); const [sheet, setSheet] = useState(null); const [notice, setNotice] = useState(""); const [loading, setLoading] = useState(true); const [mailRefreshKey, setMailRefreshKey] = useState(0); const [lens, setLens] = useState("all"); const [agentNav, setAgentNav] = useState(null); const [openThreadId, setOpenThreadId] = useState(null); const [labelsExpanded, setLabelsExpanded] = useState(false); const [companyActive, setCompanyActive] = useState(null); const [mustChangePassword, setMustChangePassword] = useState(false); const [mailStats, setMailStats] = useState({ unread: 0, starred: 0, important: 0, drafts: 0, actionItems: 0, labels: {}, }); const handleStats = useCallback((stats) => setMailStats(stats), []); useEffect(() => { const token = getAccessToken(); if (!token) { window.location.replace("/login"); return; } getCurrentAccount(token) .then(async (currentAccount) => { sessionStorage.setItem(AUTH_ACCOUNT_KEY, JSON.stringify(currentAccount)); setAccount(currentAccount); setMustChangePassword(Boolean(currentAccount.must_change_password)); if (currentAccount.account_type === "company" && currentAccount.is_domain_owner) { try { const domain = await getMyCompanyDomain(token); setCompanyDomain(domain); setNeedsDnsSetup(domain.status === "pending_dns"); } catch (error) { setDomainError(error.message || "Could not load your company domain."); } } setLoading(false); }) .catch(() => { if (!getAccessToken()) return; clearSession(); window.location.replace("/login"); }); }, []); useEffect(() => { if (!account?.is_domain_owner || needsDnsSetup) return; if (!isOrgSetupComplete(account.id)) { setCompanyActive("Overview"); } }, [account, needsDnsSetup]); function logout() { clearSession(); window.location.replace("/login"); } function closeMobileMenu() { if (window.matchMedia("(max-width: 767.98px)").matches) { setMobileMenuOpen(false); } } function toggleSidebar() { if (window.matchMedia("(max-width: 767.98px)").matches) { setMobileMenuOpen(false); return; } setSidebarExpanded((open) => !open); } const sidebarClasses = [ "dashboard-sidebar", mobileMenuOpen ? "open" : "", sidebarExpanded ? "expanded" : "", ].filter(Boolean).join(" "); function selectCompanyItem(label) { setCompanyActive(label); setAgentNav(null); closeMobileMenu(); setSheet(null); } function selectMailItem(item) { setCompanyActive(null); setAgentNav(null); if (item === "Starred") { setActive("Inbox"); setLens("starred"); } else if (item === "Sent") { setActive("Outbox"); setLens("all"); } else { setActive(item); setLens("all"); } closeMobileMenu(); setSheet(null); } function selectLabel(name) { setCompanyActive(null); setAgentNav(null); setActive("Inbox"); setLens(name); closeMobileMenu(); setSheet(null); } function selectAgentItem(item, actionKey) { setCompanyActive(null); setActive("Inbox"); setLens("all"); setAgentNav(actionKey); closeMobileMenu(); const compact = window.matchMedia("(max-width: 1199.98px)").matches; setSheet(actionKey === "handoff" || !compact ? null : "agent"); } function openAgentDestination(actionKey) { setCompanyActive(null); setActive("Inbox"); setLens("all"); setAgentNav(actionKey); closeMobileMenu(); setSheet("agent"); } function showMailboxForSheet() { setCompanyActive(null); setActive("Inbox"); setLens("all"); closeMobileMenu(); } function openSheet(next) { if (next === "agent" || next === "calendar" || next === "notes") { showMailboxForSheet(); setAgentNav(next === "agent" ? "ask" : null); } setSheet(next); } function openCompose(context = null) { triggerOpenCompose(context); } function openProfile() { setCompanyActive(null); setActive("Profile"); setAgentNav(null); closeMobileMenu(); setSheet(null); } function updateAccount(next) { setAccount(next); sessionStorage.setItem("agmail_account", JSON.stringify(next)); } function openExternalThread(threadId) { setCompanyActive(null); setActive("Inbox"); setLens("all"); setAgentNav(null); setOpenThreadId(threadId); } function emailSent() { setActive("Outbox"); setMailRefreshKey((key) => key + 1); setNotice("Message accepted by the mail server. Delivery can take a few minutes — check spam if it does not arrive."); window.setTimeout(() => setNotice(""), 4000); } if (loading || !account) { return Opening your workspace…; } if (account.account_type === "company" && domainError) { return ( Log out Company setup Domain not ready {domainError} ); } if (needsDnsSetup && companyDomain) { return ( { setCompanyDomain({ ...companyDomain, status: "active" }); setNeedsDnsSetup(false); setCompanyActive("Overview"); }} onLogout={logout} /> ); } const workspaceLabel = account.account_type === "company" ? "Company" : "Individual"; const workspaceTitle = account.account_type === "company" && companyDomain ? companyDomain.domain_name : "Personal workspace"; const isDomainOwner = account.account_type === "company" && account.is_domain_owner; const isOrgOnboarding = isDomainOwner && !isOrgSetupComplete(account.id); const mailActive = lens === "starred" ? "Starred" : active === "Outbox" ? "Sent" : labels.some(([name]) => lens === name) ? "Inbox" : active; return ( { setSheet(null); setMobileMenuOpen(true); }}> {mobileMenuOpen && ( setMobileMenuOpen(false)} /> )} {companyActive && !["Overview", "Domains", "People", "Groups", "Aliases", "Security", "Administrators", "Settings"].includes(companyActive) && ( {workspaceTitle}{companyActive} {workspaceLabel} )} {!companyActive && !["Inbox", "Outbox", "Trash", "Profile", "Settings"].includes(active) && ( {workspaceTitle}{active} {workspaceLabel} )} {!companyActive && active === "Inbox" && agentNav === "handoff" && ( )} {!companyActive && active === "Inbox" && agentNav !== "handoff" && ( selectMailItem("Settings")} onOpenProfile={openProfile} onAccountUpdate={updateAccount} onCompose={openCompose} onStats={handleStats} /> )} {!companyActive && active === "Outbox" && ( selectMailItem("Settings")} onOpenProfile={openProfile} onAccountUpdate={updateAccount} onCompose={openCompose} /> )} {!companyActive && active === "Trash" && ( selectMailItem("Settings")} onOpenProfile={openProfile} onAccountUpdate={updateAccount} onCompose={openCompose} /> )} {!companyActive && active === "Profile" && ( )} {companyActive === "Overview" && isDomainOwner && !isOrgSetupComplete(account.id) && ( { setCompanyActive(null); setActive("Inbox"); }} /> )} {companyActive === "Overview" && isDomainOwner && isOrgSetupComplete(account.id) && ( )} {companyActive === "Domains" && isDomainOwner && ( )} {companyActive === "People" && isDomainOwner && ( )} {companyActive === "Groups" && isDomainOwner && ( )} {companyActive === "Aliases" && isDomainOwner && ( )} {companyActive === "Security" && isDomainOwner && ( )} {companyActive === "Administrators" && isDomainOwner && ( )} {companyActive && !["Overview", "Domains", "People", "Groups", "Aliases", "Security", "Administrators", "Settings"].includes(companyActive) && ( {companyActive} Company {companyActive.toLowerCase()} controls will appear here. )} {(active === "Settings" || companyActive === "Settings") && ( )} {!companyActive && !["Inbox", "Outbox", "Trash", "Settings", "Profile"].includes(active) && ( {active} is ready for you Your personal mail and agent tools will appear here. )} {notice && {notice}} {mustChangePassword && ( { setAccount(updated); setMustChangePassword(false); sessionStorage.setItem("agmail_account", JSON.stringify(updated)); }} /> )} {sheet && sheet !== "more" && ( setSheet(null)} /> )} {!isOrgOnboarding && ( selectMailItem("Inbox")} onAgent={() => openSheet("agent")} onCompose={() => openCompose()} onCalendar={() => openSheet("calendar")} onMore={() => setSheet((current) => (current === "more" ? null : "more"))} onClose={() => setSheet(null)} onSettings={() => selectMailItem("Settings")} onNotes={() => openSheet("notes")} onHandoff={() => selectAgentItem("Human Handoff", "handoff")} onActionItems={() => openAgentDestination("actions")} onSummaries={() => openAgentDestination("summaries")} onIntegrations={() => openAgentDestination("integrations")} onLogout={logout} /> )} ); }
Company setup
{domainError}
{workspaceTitle}
Company {companyActive.toLowerCase()} controls will appear here.
Your personal mail and agent tools will appear here.