Repository Public
1// Plateau des pièces jointes : vignettes d'images, pictogrammes par type, poids total face à la limite, réordonnancement à la souris et au clavier.2import { useState } from "react"3import { getLang, t } from "../../i18n"4import { MAX_UPLOAD_BYTES, attachmentRisk, extensionOf, formatSize } from "../../mail/attachments"5import { pruneInline } from "../../mail/compose"6import { attachmentUrl, moveAttachment, removeAttachment, type ComposeAttachment, type ComposeDraft } from "../../state/composer"7import { Icon, type IconName } from "../kit"89const DRAG_TYPE = "application/x-spmail-attachment"10function glyph(a: ComposeAttachment): { icon: IconName; tone: string } {11 const ext = extensionOf(a.name)12 if (/^image\//.test(a.type)) return { icon: "image", tone: "image" }13 if (ext === "pdf" || a.type === "application/pdf") return { icon: "file", tone: "pdf" }14 if (/^(xlsx?|csv|ods|numbers)$/.test(ext)) return { icon: "list", tone: "sheet" }15 if (/^(docx?|odt|rtf|pages|txt|md)$/.test(ext) || /^text\//.test(a.type)) return { icon: "text", tone: "doc" }16 if (/^(zip|7z|rar|gz|tar|tgz)$/.test(ext)) return { icon: "archive", tone: "archive" }17 if (/^(audio|video)\//.test(a.type)) return { icon: "eye", tone: "media" }18 return { icon: "file", tone: "other" }19}2021export function AttachmentTray({ draft }: { draft: ComposeDraft }) {22 const lang = getLang()23 const [dragKey, setDragKey] = useState<string | null>(null)24 const [overKey, setOverKey] = useState<string | null>(null)25 const [announce, setAnnounce] = useState("")26 const files = draft.attachments.filter((a) => !a.inline)27 // les images du texte voyagent dans le même message : elles comptent dans le poids, tant qu'elles y sont référencées28 const counted = pruneInline(draft.attachments, draft.mode === "html" ? draft.html : null)29 const total = counted.reduce((n, a) => n + a.size, 0)30 const inline = counted.filter((a) => a.inline).length31 if (!files.length && !inline) return null32 const ratio = Math.min(1, total / MAX_UPLOAD_BYTES)33 const indexOf = (key: string) => draft.attachments.findIndex((a) => a.key === key)34 const move = (a: ComposeAttachment, dir: 1 | -1) => {35 const i = files.indexOf(a)36 const other = files[i + dir]37 if (!other) return38 moveAttachment(a.key, indexOf(other.key), draft.key)39 setAnnounce(t("cmp.att.moved", { name: a.name, n: i + dir + 1, total: files.length }))40 }41 return (42 <div className="cmp-tray">43 {files.length ? (44 <ul className="composer-atts cmp-tray-list" aria-label={t("reader.attachments")}>45 {files.map((a, i) => {46 const url = attachmentUrl(draft, a)47 const g = glyph(a)48 const ext = extensionOf(a.name).slice(0, 4).toUpperCase()49 return (50 <li51 key={a.key}52 className={`composer-att cmp-att${a.error ? " error" : ""}${dragKey === a.key ? " cmp-att-dragging" : ""}${overKey === a.key && dragKey !== a.key ? " cmp-att-over" : ""}`}53 draggable54 onDragStart={(e) => {55 e.dataTransfer.setData(DRAG_TYPE, a.key)56 e.dataTransfer.effectAllowed = "move"57 setDragKey(a.key)58 }}59 onDragEnd={() => {60 setDragKey(null)61 setOverKey(null)62 }}63 onDragOver={(e) => {64 if (!e.dataTransfer.types.includes(DRAG_TYPE)) return65 e.preventDefault()66 e.stopPropagation()67 e.dataTransfer.dropEffect = "move"68 setOverKey(a.key)69 }}70 onDrop={(e) => {71 const key = e.dataTransfer.getData(DRAG_TYPE)72 if (!key) return73 e.preventDefault()74 e.stopPropagation()75 setOverKey(null)76 if (key !== a.key) moveAttachment(key, indexOf(a.key), draft.key)77 }}78 >79 <button80 type="button"81 className="cmp-att-main"82 aria-describedby={`cmp-att-keys-${draft.key}`}83 title={a.name}84 onKeyDown={(e) => {85 if (e.key === "Delete" || e.key === "Backspace") {86 e.preventDefault()87 removeAttachment(a.key, draft.key)88 setAnnounce(t("cmp.att.removed", { name: a.name }))89 } else if (e.altKey && (e.key === "ArrowLeft" || e.key === "ArrowUp")) {90 e.preventDefault()91 move(a, -1)92 } else if (e.altKey && (e.key === "ArrowRight" || e.key === "ArrowDown")) {93 e.preventDefault()94 move(a, 1)95 }96 }}97 >98 <span className={`cmp-att-thumb cmp-att-${g.tone}`} aria-hidden="true">99 {url ? <img src={url} alt="" loading="lazy" draggable={false} /> : <Icon name={g.icon} />}100 {!url && ext ? <span className="cmp-att-ext">{ext}</span> : null}101 </span>102 <span className="cmp-att-text">103 <span className="cmp-att-name truncate">{a.name}</span>104 <span className="small muted">{formatSize(a.size, lang)}</span>105 </span>106 <span className="sr-only">{t("cmp.att.position", { n: i + 1, total: files.length })}</span>107 </button>108 {attachmentRisk(a.name, a.type) ? <span className="badge warn">{t("compose.riskyType")}</span> : null}109 {a.error ? (110 <span className="small att-error" role="alert">111 {a.error}112 </span>113 ) : !a.blobId ? (114 <progress value={Math.round(a.progress * 100)} max={100} aria-label={t("compose.uploading", { name: a.name })} />115 ) : null}116 <button type="button" className="icon-btn small-btn" aria-label={t("compose.removeAttachment", { name: a.name })} onClick={() => removeAttachment(a.key, draft.key)}>117 <Icon name="close" small />118 </button>119 </li>120 )121 })}122 </ul>123 ) : null}124 <div className={`small muted composer-atts-total cmp-tray-total${ratio > 0.9 ? " cmp-tray-near" : ""}`}>125 <span className="cmp-meter" aria-hidden="true">126 <span style={{ transform: `scaleX(${ratio})` }} />127 </span>128 <span>129 {t("compose.attachmentsTotal", { size: formatSize(total, lang), max: formatSize(MAX_UPLOAD_BYTES, lang) })}130 {inline ? ` · ${t("cmp.att.inline", { n: inline })}` : ""}131 </span>132 </div>133 <span id={`cmp-att-keys-${draft.key}`} hidden>134 {t("cmp.att.keys")}135 </span>136 <span className="sr-only" role="status">137 {announce}138 </span>139 </div>140 )141}142