import { useState } from "react"; import { CalendarDays, Check, ChevronDown, ChevronRight, ClipboardList, CircleHelp, FileSearch, FileText, ListChecks, Mail, Paperclip, PenLine, Send, Settings, Sparkles, } from "lucide-react"; import AgentInsight from "./AgentInsight"; import AgentSpark from "./AgentSpark"; import SenderAvatar from "./SenderAvatar"; import { inboxContext } from "../utils/inboxSignals"; const ICON = { size: 14, strokeWidth: 1.75 }; const ICON_SM = { size: 13, strokeWidth: 1.75 }; const cards = [ { key: "important", label: "Important emails", captionKey: "importantFromClients", captionSuffix: "from clients", hint: "In your inbox", icon: Mail, tone: "rose", }, { key: "actionItems", label: "Action items", hint: "Need your response", icon: ClipboardList, tone: "blue", }, { key: "meetings", label: "Meetings mentioned", hint: "In your emails", icon: CalendarDays, tone: "green", }, { key: "invoices", label: "Invoices & bills", hint: "This week", icon: FileText, tone: "purple", }, ]; function greeting(name) { const hour = new Date().getHours(); const period = hour < 12 ? "Good morning" : hour < 17 ? "Good afternoon" : "Good evening"; return `${period}, ${name}`; } function statCaption(card, stats) { const count = stats[card.key] || 0; if (count === 0) return card.hint; if (card.captionKey && stats[card.captionKey] > 0) { return `${stats[card.captionKey]} ${card.captionSuffix}`; } return card.hint; } function metricTotal(stats) { return cards.reduce((sum, card) => sum + (stats[card.key] || 0), 0); } function emailContextSummary(email) { const text = (email?.body_text || email?.snippet || "").trim().toLowerCase(); const sender = email?.sender?.name || email?.sender_display_name || "The sender"; if (!text) return "This message is open — a summary will appear here."; if (text.includes("check") && text.includes("inbox") && text.includes("repl")) { return `${sender} asks you to check the inbox and review messages and replies.`; } if (text.includes("check") && text.includes("inbox")) { return `${sender} asks you to check the inbox.`; } if (text.includes("repl")) { return `${sender} is following up about replies or responses.`; } if (text.includes("attach")) { return `${sender} references an attachment that may need review.`; } const snippet = (email?.body_text || email?.snippet || "").trim(); const firstSentence = snippet.split(/[.!?\n]/).find((part) => part.trim().length > 8)?.trim(); if (firstSentence) { return `${sender} wrote: “${firstSentence.length > 120 ? `${firstSentence.slice(0, 117)}…` : firstSentence}.”`; } return `${sender} sent a short message that may need a response.`; } function emailContextActions(email) { const text = (email?.body_text || email?.snippet || "").toLowerCase(); const actions = []; if (text.includes("check") && text.includes("inbox")) actions.push("Check inbox"); if (text.includes("message")) actions.push("Review messages"); if (text.includes("repl")) actions.push("Review replies"); if (text.includes("attach")) actions.push("Review attachments"); if (!actions.length) actions.push("Review message"); return actions.slice(0, 3); } export default function AgAgentPanel({ account, emails, contextEmail, focus, insight, loading = false, mailboxEmpty = false, isBrandNewMailbox = false, onCompose, onQuickAction, onOpenEmail, onAgentAsk, }) { const { stats, needsReply, today, deadlines } = inboxContext(emails); const [agentQuery, setAgentQuery] = useState(""); const [panelOpen, setPanelOpen] = useState(true); const firstName = account?.first_name || "there"; const allMetricsZero = metricTotal(stats) === 0; const hasQuery = agentQuery.trim().length > 0; const emailContextMode = Boolean(contextEmail) && !insight; const showOnboarding = !insight && !loading && mailboxEmpty && isBrandNewMailbox; const showQuietEmpty = !insight && !loading && mailboxEmpty && !isBrandNewMailbox; const showFullPanel = !insight && !loading && !mailboxEmpty && !emailContextMode; function submitAgentQuery(event) { event.preventDefault(); const query = agentQuery.trim(); if (!query) return; onAgentAsk?.(query); setAgentQuery(""); } return (

AG Agent

Your email co-pilot

{panelOpen && (
{!insight && (
{greeting(firstName)} {emailContextMode ? "About this email" : showOnboarding ? "Your new inbox is ready." : showQuietEmpty ? "Nothing needs your attention right now." : "Here's what needs your attention today."}
)} {emailContextMode && (

Summary

{emailContextSummary(contextEmail)}

Action items

    {emailContextActions(contextEmail).map((item) => (
  • ))}
)} {!insight && loading && ( )} {showOnboarding && (

AGMail is ready

No messages yet. I'll start organizing and surfacing important items as email arrives.

)} {showQuietEmpty && (

You're all caught up.

Your inbox is clear for now.

)} {showFullPanel && !emailContextMode && !allMetricsZero && (
{cards.map((card) => { const Icon = card.icon; const count = stats[card.key] || 0; const isEmpty = count === 0; return ( ); })}
)} {showFullPanel && allMetricsZero && (

You're all caught up.

No urgent messages or outstanding actions.

)} {!insight && focus && showFullPanel && !allMetricsZero && (

{focus}

)}

{showOnboarding ? "Quick start" : "Quick actions"}

{showOnboarding ? ( <> ) : ( <> )}
{showFullPanel && !insight && (

Today's agenda

{today.length > 0 ? (
    {today.map((item) => (
  • ))}
) : (

No meetings in your emails today.

)}
)} {showFullPanel && !insight && (

Needs attention

{needsReply.length > 0 ? (
    {needsReply.slice(0, 3).map((item) => (
  • ))}
) : (

Nothing needs your attention right now.

)}
)} {showFullPanel && !insight && deadlines.length > 0 && (

Deadlines

    {deadlines.map((item) => (
  • {item.label} {item.due}
  • ))}
)}
setAgentQuery(event.target.value)} placeholder="Ask about your emails..." />
)}
); }