SPB Git

Repository Public

ContactCard.tsx 11.5 KB 242 lines tsx

1// SP Mail Next — fiche d'un contact : coordonnées, groupes, notes, statistiques, rappels en attente et échanges récents (toutes boîtes).2import { fmtDateTime, fmtListDate, fmtNumber, t } from "../../i18n"3import { CT, CU, colorToken, contactTitle, deleteContact, groups, reloadHistory, saveSuggestion, updateContact, type Contact, type HistoryItem } from "../../next/contacts"4import { mailboxAddress, messageHref, nextHref, operator, searchHref } from "../../next/nav"5import { S } from "../../state/app"6import { composeNew } from "../../state/composer"7import { linkProps, navigate } from "../../state/session"8import { shallowEqual, useStore } from "../../state/store"9import { Icon, IconButton, MenuButton, Spinner, confirmAsync } from "../kit"10import { ContactAvatar, attempt, useOnline } from "./ContactBits"1112/** Vue où ouvrir un échange d'après ses dossiers (boîte de réception d'abord). */13function viewOf(roles: string[]): string {14  for (const r of ["inbox", "sent", "archive", "drafts"]) if (roles.includes(r)) return r15  return "inbox"16}17const writeTo = (c: Contact, email: string) => composeNew({ to: [{ name: c.name || null, email }] })1819async function remove(c: Contact) {20  const name = contactTitle(c) || t("nx.contacts.unnamed")21  const ok = await confirmAsync({ title: t("nx.contacts.delete.title"), body: t("nx.contacts.delete.body", { name }), confirmLabel: t("nx.contacts.delete"), danger: true })22  if (!ok) return23  if (await attempt(() => deleteContact(c.id), t("nx.contacts.deleted"))) navigate(nextHref("contacts"), { replace: true })24}2526function History({ items }: { items: HistoryItem[] }) {27  const many = useStore(S, (s) => (s.me?.mailboxes.length ?? 0) > 1)28  return (29    <ul className="nx-list nx-ct-history">30      {items.map((m) => (31        <li key={`${m.mailbox_id}:${m.email_id}`}>32          <a {...linkProps(messageHref(m.mailbox_id, m.thread_id, viewOf(m.folder_roles ?? [])))} className="nx-item">33            <Icon name={m.direction === "sent" ? "sent" : "inbox"} small />34            <span className="sr-only">{m.direction === "sent" ? t("nx.contacts.history.sent") : t("nx.contacts.history.received")}</span>35            <span className="nx-item-main">36              <span className="truncate">{m.subject || t("nx.contacts.history.noSubject")}</span>37              <span className="small truncate">{many ? `${mailboxAddress(m.mailbox_id)} · ${m.preview}` : m.preview}</span>38            </span>39            <time className="nx-meta" dateTime={m.received_at} title={fmtDateTime(m.received_at)}>40              {fmtListDate(m.received_at)}41            </time>42          </a>43        </li>44      ))}45    </ul>46  )47}4849export function ContactCard() {50  const st = useStore(CT, (s) => ({ c: s.card, status: s.cardStatus, error: s.cardError, history: s.history, reminders: s.reminders, hs: s.historyStatus, id: s.cardId }), shallowEqual)51  const groupList = groups.use()52  const online = useOnline()53  const c = st.c54  if (st.status === "loading" || (st.status === "idle" && !c)) {55    return (56      <div className="nx-ct-cardbody" aria-busy="true">57        <div className="nx-ct-hero">58          <span className="skeleton nx-ct-avatar l" />59          <span className="nx-item-main">60            <span className="skeleton nx-ct-skel" style={{ width: "40%", height: 22 }} />61            <span className="skeleton nx-ct-skel thin" style={{ width: "28%" }} />62          </span>63        </div>64        <Spinner label={t("nx.loading")} />65      </div>66    )67  }68  if (!c) {69    return (70      <div className="nx-empty nx-ct-none" role="alert">71        <Icon name="warn" />72        <p>{st.error === "not_found" ? t("nx.contacts.gone") : st.error === "offline" ? t("nx.offline") : t("nx.contacts.loadFailed")}</p>73        <a {...linkProps(nextHref("contacts"))} className="btn">74          {t("nx.contacts.backToList")}75        </a>76      </div>77    )78  }79  const name = contactTitle(c) || t("nx.contacts.unnamed")80  const role = [c.title, c.organization].filter(Boolean).join(" · ")81  const first = c.emails[0]?.email82  const toggleGroup = (id: string) => void attempt(() => updateContact(c.id, { groups: c.groups.includes(id) ? c.groups.filter((g) => g !== id) : [...c.groups, id] }))83  const off = online ? undefined : t("nx.offline")84  return (85    <div className="nx-ct-cardbody">86      <header className="nx-ct-hero">87        <ContactAvatar contact={c} size="l" />88        <div className="nx-item-main">89          <h2 className="nx-ct-name">{name}</h2>90          {role ? <p className="nx-ct-role">{role}</p> : null}91          <p className="nx-ct-badges">92            {c.kind === "suggested" ? <span className="badge">{t("nx.contacts.suggested")}</span> : null}93            {c.vip ? <span className="badge accent">{t("nx.contacts.vip")}</span> : null}94            {c.favorite ? (95              <span className="badge warn">96                <Icon name="star" small />97                {t("nx.contacts.favorite")}98              </span>99            ) : null}100          </p>101        </div>102      </header>103      <div className="nx-toolbar nx-ct-actions">104        {c.kind === "suggested" ? (105          <button type="button" className="btn primary" disabled={!online} title={off} onClick={() => void attempt(() => saveSuggestion(c.id), t("nx.contacts.added"))}>106            <Icon name="userPlus" />107            <span>{t("nx.contacts.addToContacts")}</span>108          </button>109        ) : null}110        {first ? (111          <button type="button" className={`btn${c.kind === "suggested" ? "" : " primary"}`} onClick={() => writeTo(c, first)}>112            <Icon name="edit" />113            <span>{t("nx.contacts.write")}</span>114          </button>115        ) : null}116        <span className="spacer" />117        <IconButton icon="star" label={c.favorite ? t("nx.contacts.unfavorite") : t("nx.contacts.makeFavorite")} pressed={c.favorite} className={c.favorite ? "starred" : ""} disabled={!online} onClick={() => void attempt(() => updateContact(c.id, { favorite: !c.favorite }))} />118        <IconButton icon="crown" label={c.vip ? t("nx.contacts.unvip") : t("nx.contacts.makeVip")} pressed={c.vip} disabled={!online} onClick={() => void attempt(() => updateContact(c.id, { vip: !c.vip }))} />119        <IconButton icon="edit" label={t("nx.contacts.edit")} onClick={() => CU.set({ dialog: { type: "edit", contact: c } })} />120        <MenuButton121          label={t("nx.contacts.cardMore")}122          items={[123            ...(first ? [{ label: t("nx.contacts.searchFrom"), icon: "search" as const, onSelect: () => navigate(searchHref(operator("from", first), { all: true })) }] : []),124            { label: t("nx.contacts.duplicates"), icon: "merge" as const, disabled: !online, onSelect: () => CU.set({ dialog: { type: "duplicates" } }) },125            "sep" as const,126            { label: t("nx.contacts.delete"), icon: "trash" as const, danger: true, disabled: !online, onSelect: () => void remove(c) },127          ]}128        />129      </div>130131      <section className="nx-ct-section" aria-labelledby="nx-ct-h-coords">132        <h3 id="nx-ct-h-coords">{t("nx.contacts.section.details")}</h3>133        {c.emails.length || c.phones.length ? (134          <dl className="nx-ct-fields">135            {c.emails.map((e) => (136              <div key={e.email} className="nx-ct-field">137                <dt>{e.label || t("nx.contacts.field.email")}</dt>138                <dd>139                  <span className="truncate nx-ct-value">{e.email}</span>140                  <IconButton icon="edit" label={t("nx.contacts.writeTo", { email: e.email })} onClick={() => writeTo(c, e.email)} />141                  <a {...linkProps(searchHref(operator("from", e.email), { all: true }))} className="icon-btn" aria-label={t("nx.contacts.searchFromNamed", { email: e.email })} title={t("nx.contacts.searchFromNamed", { email: e.email })}>142                    <Icon name="search" />143                  </a>144                </dd>145              </div>146            ))}147            {c.phones.map((p, i) => (148              <div key={`${p.number}-${i}`} className="nx-ct-field">149                <dt>{p.label || t("nx.contacts.field.phone")}</dt>150                <dd>151                  <span className="truncate nx-ct-value">{p.number}</span>152                </dd>153              </div>154            ))}155          </dl>156        ) : (157          <p className="nx-note">{t("nx.contacts.noDetails")}</p>158        )}159      </section>160161      {groupList.length ? (162        <section className="nx-ct-section" aria-labelledby="nx-ct-h-groups">163          <h3 id="nx-ct-h-groups">{t("nx.contacts.groups")}</h3>164          <div className="nx-ct-groups" role="group" aria-labelledby="nx-ct-h-groups">165            {groupList.map((g) => (166              <button key={g.id} type="button" className="nx-chip" aria-pressed={c.groups.includes(g.id)} disabled={!online} title={off} onClick={() => toggleGroup(g.id)}>167                <span className="nx-dot" style={{ background: colorToken(g.color) }} aria-hidden="true" />168                <span className="truncate">{g.name}</span>169              </button>170            ))}171          </div>172        </section>173      ) : null}174175      {c.notes ? (176        <section className="nx-ct-section" aria-labelledby="nx-ct-h-notes">177          <h3 id="nx-ct-h-notes">{t("nx.contacts.field.notes")}</h3>178          <p className="nx-ct-notes">{c.notes}</p>179        </section>180      ) : null}181182      <section className="nx-ct-section" aria-labelledby="nx-ct-h-stats">183        <h3 id="nx-ct-h-stats">{t("nx.contacts.section.stats")}</h3>184        <dl className="nx-ct-stats">185          <div>186            <dt>{t("nx.contacts.stats.received")}</dt>187            <dd>{fmtNumber(c.stats.received)}</dd>188          </div>189          <div>190            <dt>{t("nx.contacts.stats.sent")}</dt>191            <dd>{fmtNumber(c.stats.sent)}</dd>192          </div>193          <div>194            <dt>{t("nx.contacts.stats.last")}</dt>195            <dd className="nx-ct-last">{c.stats.last_at ? fmtListDate(c.stats.last_at) : "—"}</dd>196          </div>197        </dl>198      </section>199200      {st.reminders.length ? (201        <section className="nx-ct-section" aria-labelledby="nx-ct-h-rem">202          <h3 id="nx-ct-h-rem">{t("nx.contacts.section.reminders")}</h3>203          <ul className="nx-list">204            {st.reminders.map((r) => (205              <li key={r.id}>206                <a {...linkProps(messageHref(r.mailbox_id, r.thread_id))} className="nx-item">207                  <Icon name="alarm" small />208                  <span className="nx-item-main">209                    <span className="truncate">{r.subject || t("nx.contacts.history.noSubject")}</span>210                    {r.note ? <span className="small truncate">{r.note}</span> : null}211                  </span>212                  <time className="nx-meta" dateTime={r.due_at ?? undefined}>213                    {fmtDateTime(r.due_at)}214                  </time>215                </a>216              </li>217            ))}218          </ul>219        </section>220      ) : null}221222      <section className="nx-ct-section" aria-labelledby="nx-ct-h-hist">223        <h3 id="nx-ct-h-hist">{t("nx.contacts.section.history")}</h3>224        {st.hs === "loading" ? (225          <Spinner label={t("nx.loading")} />226        ) : st.hs === "error" ? (227          <p className="nx-note" role="alert">228            {t("nx.contacts.history.failed")}{" "}229            <button type="button" className="btn small" onClick={reloadHistory}>230              {t("nx.retry")}231            </button>232          </p>233        ) : st.history.length ? (234          <History items={st.history} />235        ) : (236          <p className="nx-note">{c.emails.length ? t("nx.contacts.history.empty") : t("nx.contacts.history.noEmail")}</p>237        )}238      </section>239    </div>240  )241}242
Files main
Menu

Theme