Repository Public
1// SP Mail Next — pièces jointes : actions communes à la galerie et à l'aperçu (joindre à un nouveau message, messages d'erreur).2import { t } from "../../i18n"3import { MAX_UPLOAD_BYTES } from "../../mail/attachments"4import { toNextError } from "../../next/api"5import type { PreviewItem } from "../../next/attachments"6import { S, toast } from "../../state/app"78export function attachmentError(e: unknown): string {9 const err = toNextError(e)10 if (err.kind === "offline") return t("nx.offline")11 if (err.kind === "too_large") return t("nx.attachments.err.zipTooLarge")12 if (err.kind === "not_found") return t("nx.attachments.err.gone")13 if (err.kind === "rate_limited") return t("nx.attachments.err.rate")14 if (err.kind === "unavailable") return t("nx.error.unavailable")15 return t("nx.error.generic")16}1718/**19 * Ouvre un nouveau message portant ces pièces, par `addExistingAttachment` de la rédaction : une pièce de la boîte du brouillon20 * est reprise par son blob (déjà sur le serveur), celle d'une autre boîte y est recopiée. Rend le nombre de pièces jointes.21 */22export async function attachToNewMessage(items: PreviewItem[]): Promise<number> {23 if (!items.length) return 024 if (!S.get().online) {25 toast({ kind: "error", text: t("nx.offline") })26 return 027 }28 if (items.reduce((n, a) => n + a.size, 0) > MAX_UPLOAD_BYTES) {29 toast({ kind: "error", text: t("nx.attachments.err.composeTooLarge") })30 return 031 }32 const composer = await import("../../state/composer")33 // la rédaction s'ouvre dans la boîte de la première pièce quand c'est la boîte affichée ; sinon dans la boîte courante34 const key = composer.openComposer()35 if (!key) return 036 let done = 037 for (const a of items) {38 if (!composer.C.get().drafts.some((d) => d.key === key)) break39 const ok = await composer.addExistingAttachment({ mailboxId: a.mailboxId, blobId: a.blobId, name: a.name, type: a.type, size: a.size }, key).catch(() => false)40 if (ok) done++41 else toast({ kind: "error", text: t("nx.attachments.err.attachOne", { name: a.name }) })42 }43 if (done) toast({ kind: "success", text: t("nx.attachments.attached", { n: done }) })44 return done45}46