Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : SP Mail8 * File : web/src/ui/next/ContactBits.tsx9 * Module : ui10 * Repository : https://git.spboucher.ai/sp-mail11 * Project Website : https://mail.spboucher.ai12 * Created : 2026-11-2013 * Last Modified : 2026-11-2014 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// SP Mail Next — contacts : petites briques partagées par l'écran, la fiche, les dialogues et la fiche d'expéditeur (pastille, erreurs, garde réseau).44import type { CSSProperties } from "react"45import { t } from "../../i18n"46import { toNextError } from "../../next/api"47import { colorToken, contactInitials, type Contact } from "../../next/contacts"48import { S, toast } from "../../state/app"49import { useStore } from "../../state/store"5051/** Pastille d'initiales à la couleur de la fiche (décorative : le nom est toujours écrit à côté). */52export function ContactAvatar({ contact, size }: { contact: Pick<Contact, "name" | "emails" | "phones" | "avatar_color">; size?: "s" | "l" }) {53 return (54 <span className={`nx-ct-avatar${size ? ` ${size}` : ""}`} style={{ "--c": colorToken(contact.avatar_color || contact.emails[0]?.email || contact.name) } as CSSProperties} aria-hidden="true">55 {contactInitials(contact)}56 </span>57 )58}5960/** Message d'erreur précis d'une écriture de contact ou de groupe. */61export function contactError(e: unknown): string {62 const err = toNextError(e)63 if (err.kind === "offline") return t("nx.offline")64 if (err.code === "email_in_use") return t("nx.contacts.err.emailInUse")65 if (err.code === "name_taken") return t("nx.contacts.err.nameTaken")66 if (err.code === "limit_reached") return t("nx.contacts.err.limit")67 if (err.code === "invalid_vcard") return t("nx.contacts.err.vcard")68 if (err.kind === "too_large") return t("nx.contacts.err.vcfTooLarge")69 if (err.kind === "rate_limited") return t("nx.error.rateLimited")70 if (err.kind === "not_found") return t("nx.error.notFound")71 if (err.kind === "unavailable") return t("nx.error.unavailable")72 if (err.kind === "invalid") return t("nx.contacts.err.invalid")73 return t("nx.error.generic")74}75/** Exécute une écriture ; un échec devient une notification précise. Rend vrai si elle a abouti. */76export async function attempt(action: () => Promise<unknown>, okText?: string): Promise<boolean> {77 if (!S.get().online) {78 toast({ kind: "error", text: t("nx.offline") })79 return false80 }81 try {82 await action()83 if (okText) toast({ kind: "success", text: okText })84 return true85 } catch (e) {86 toast({ kind: "error", text: contactError(e) })87 return false88 }89}90export const useOnline = (): boolean => useStore(S, (s) => s.online)91