Repository Public
1// SP Mail Next — boîte de réception : pastilles d'aperçu calculées SANS modèle (infolettres, à répondre, sans réponse) ; cliquer en2// ouvre la liste calme sous la bande. Si le tri par IA est activé, une pastille « Important » s'y ajoute, étiquetée du fournisseur.3import { useEffect, useId, useMemo, useState } from "react"4import { fmtListDate, t } from "../../i18n"5import { aiOn, aiSettings, aiUi, confirmedSettings, triageQueue } from "../../next/ai"6import { insightChips, insightRows, insightStore, loadInsights, type InsightKind, type InsightRow } from "../../next/insights"7import { messageHref } from "../../next/nav"8import { S } from "../../state/app"9import { linkProps } from "../../state/session"10import { useStore } from "../../state/store"11import { Icon, IconButton, type IconName } from "../kit"12import { AiByLine } from "./AiParts"13import "./ai.css"1415type Chip = InsightKind | "important"16const LABEL: Record<Chip, "nx.ai.insight.newsletters" | "nx.ai.insight.needsReply" | "nx.ai.insight.unansweredSent" | "nx.ai.insight.important"> = {17 newsletters: "nx.ai.insight.newsletters", needs_reply: "nx.ai.insight.needsReply", unanswered_sent: "nx.ai.insight.unansweredSent", important: "nx.ai.insight.important",18}19const HINT: Record<Chip, "nx.ai.insight.newslettersHint" | "nx.ai.insight.needsReplyHint" | "nx.ai.insight.unansweredSentHint" | "nx.ai.insight.importantHint"> = {20 newsletters: "nx.ai.insight.newslettersHint", needs_reply: "nx.ai.insight.needsReplyHint", unanswered_sent: "nx.ai.insight.unansweredSentHint", important: "nx.ai.insight.importantHint",21}22const ICON: Record<Chip, IconName> = { newsletters: "mail", needs_reply: "reply", unanswered_sent: "clock", important: "bolt" }2324export default function InsightChips() {25 const mb = useStore(S, (s) => s.mb)26 const emailState = useStore(S, (s) => (s.mb ? (s.accounts[s.mb]?.emailState ?? "") : ""))27 const online = useStore(S, (s) => s.online)28 const st = useStore(insightStore, (s) => s)29 const settings = useStore(aiSettings.store, (s) => s)30 const triageOn = aiOn(confirmedSettings(settings), "triage")31 const triageRev = useStore(aiUi, (s) => s.triageRev)32 const triageBy = useStore(aiUi, (s) => s.triageBy)33 const listIds = useStore(S, (s) => s.list.ids)34 const [open, setOpen] = useState<Chip | null>(null)35 const panelId = useId()3637 // lecture à l'affichage, au changement de boîte, puis après un changement d'état du courrier (sans rafale)38 useEffect(() => {39 if (!mb || !online) return40 const h = setTimeout(() => void loadInsights(mb), emailState ? 400 : 0)41 return () => clearTimeout(h)42 }, [mb, emailState, online])43 useEffect(() => setOpen(null), [mb])4445 const data = st.mailboxId === mb ? st.data : null46 const important = useMemo<InsightRow[]>(() => {47 if (!triageOn || !mb) return []48 const emails = S.get().emails49 return listIds.flatMap((id) => {50 const e = id ? emails[id] : undefined51 if (!id || !e || !triageQueue.peek(id)?.important) return []52 return [{ mailboxId: mb, emailId: id, threadId: e.threadId, who: e.from?.[0]?.name?.trim() || e.from?.[0]?.email || "", subject: e.subject ?? "", receivedAt: e.receivedAt, unread: !e.keywords.$seen, view: "inbox" }]53 })54 // eslint-disable-next-line react-hooks/exhaustive-deps55 }, [triageOn, mb, listIds, triageRev])5657 const chips: { kind: Chip; count: number }[] = [...(important.length ? [{ kind: "important" as const, count: important.length }] : []), ...insightChips(data)]58 if (!mb || !chips.length) return null59 const shown = open && chips.some((c) => c.kind === open) ? open : null60 const rows = shown === "important" ? important : shown ? insightRows(data, shown) : []6162 return (63 <div className="nx-insights" data-nx-insights="">64 <div className="nx-insights-strip" role="group" aria-label={t("nx.ai.insight.region")}>65 {chips.map((c) => (66 <button67 key={c.kind}68 type="button"69 className={`nx-chip nx-insight-chip${c.kind === "important" ? " nx-insight-ai" : ""}`}70 data-insight={c.kind}71 aria-pressed={shown === c.kind}72 aria-expanded={shown === c.kind}73 aria-controls={shown === c.kind ? panelId : undefined}74 title={t(HINT[c.kind])}75 onClick={() => setOpen(shown === c.kind ? null : c.kind)}76 >77 <Icon name={ICON[c.kind]} small />78 <span>{t(LABEL[c.kind])}</span>79 <span className="nx-insight-count">{c.count}</span>80 </button>81 ))}82 {triageOn && triageBy ? (83 <span className="nx-insights-by">84 <span className="sr-only">{t("nx.ai.triage.by")}</span>85 <AiByLine by={triageBy} />86 </span>87 ) : null}88 </div>89 {shown ? (90 <section className="nx-insights-panel" id={panelId} aria-label={t(LABEL[shown])}>91 <div className="nx-insights-head">92 <p className="nx-note">{t(HINT[shown])}</p>93 <IconButton icon="close" label={t("nx.ai.insight.close")} onClick={() => setOpen(null)} />94 </div>95 <ul className="nx-list nx-insights-list">96 {rows.map((r) => (97 <li key={r.emailId}>98 <a className={`nx-item nx-insight-row${r.unread ? " unread" : ""}`} {...linkProps(messageHref(r.mailboxId, r.threadId, r.view))}>99 <span className="nx-item-main">100 <span className="truncate nx-insight-who">{r.who || t("nx.ai.insight.unknown")}</span>101 <span className="truncate small">{r.subject || t("nx.ai.insight.noSubject")}</span>102 </span>103 <span className="nx-meta">{fmtListDate(r.receivedAt)}</span>104 </a>105 </li>106 ))}107 </ul>108 </section>109 ) : null}110 </div>111 )112}113