`i18n:coverage` reported 100% while a hundred-odd strings rendered
English in every language. It was not wrong about what it measured: it
reads JSX text, and none of these were JSX text. They were toast
arguments, `confirmDialog({ title, confirmLabel })` props, `title=` and
`aria-label=` attributes, and template literals — every one built from an
expression the codemod cannot read.
176 source strings and 15 plural sets now go through t() and plural(),
translated into all nine languages. Where English put a word in a slot,
the sentence is spelled out per branch instead: `Filter ${verb}` became
"Filter saved" and "Filter created", because which word agrees with what,
and where it sits, is not a property English gets to decide for everyone.
Counts that were `${n} message${n === 1 ? "" : "s"}` are plural() calls,
so Russian and Ukrainian get three forms and Japanese and Chinese get the
one they actually have.
Two of the catalogue's own conventions were worth learning the hard way.
Plural entries are keyed on the English *other* form, not `one` — `one`
is a form English happens to have and Japanese does not. And a constant
table holding English that is translated at the render site is fine: the
literal is a key, not a leak.
Which is what the new check encodes. `scripts/i18n-literals.mjs` accepts
a string that is wrapped where it is written or is a catalogue key
somewhere, and refuses one that is neither — a string no catalogue can
translate, however many languages ship. It found twenty more than my own
sweep had, including the stale-folder toast seen in production. It runs
as part of `npm run i18n:check`.
Also fixed: the catalogue is now awaited before the first paint. The
tree is rebuilt when a catalogue lands, so components recover on their
own, but a string computed in an effect does not — a toast fired in that
gap is emitted in English and stays English. The wait costs nothing
visible, since the session bootstrap already shows a spinner and English
resolves immediately.
And the Japanese agenda title loses a space Japanese does not use:
"{date} からの予定" was written with the English habit of spacing around
a placeholder.
89 lines
4.5 KiB
TypeScript
89 lines
4.5 KiB
TypeScript
/**
|
|
* Signature images: Stalwart caps identity signatures at 2 KB, so pictures can't
|
|
* be embedded as data: URLs. Instead we store them in JMAP Files (persistent
|
|
* blobs) under an "ihasmail" folder and reference them by blob URL; the composer
|
|
* turns such references into inline cid: parts when sending.
|
|
*/
|
|
import { CAP, client, setErrorMessage } from "@/jmap/client";
|
|
import type { FileNode, QueryResponse, SetResponse } from "@/jmap/types";
|
|
import { fileCreate } from "@/lib/filenode";
|
|
import { ensureFolder, nodeBlobId } from "@/lib/appFolder";
|
|
import { useSession } from "@/store/session";
|
|
import { toast } from "@/ui/toast";
|
|
import { t } from "@/lib/i18n";
|
|
|
|
/** Upload an image for use in a signature; returns a same-origin blob URL. */
|
|
export async function uploadSignatureImage(file: File): Promise<string> {
|
|
const accountId = useSession.getState().ownAccountFor(CAP.filenode);
|
|
if (!accountId || !client.hasCapability(CAP.filenode)) {
|
|
toast.error(t("Images in signatures need the Files feature, which this account doesn't have."));
|
|
throw new Error("filenode unavailable");
|
|
}
|
|
if (file.size > 512 * 1024) {
|
|
toast.error(t("Please use an image under 512 KB for signatures."));
|
|
throw new Error("too large");
|
|
}
|
|
try {
|
|
const type = file.type || "image/png";
|
|
const up = await client.upload(accountId, file, { type });
|
|
const folderId = await ensureFolder(accountId);
|
|
const name = `${Date.now()}-${file.name.replace(/[^\w.-]+/g, "_")}`;
|
|
const res = await client.call<SetResponse<FileNode>>("FileNode/set", { accountId, create: { f: fileCreate(folderId, name, up.blobId, type) } });
|
|
const err = res.notCreated?.f;
|
|
if (err) throw new Error(setErrorMessage(err));
|
|
const created = res.created?.f as Partial<FileNode> | undefined;
|
|
// Prefer the node's (persistent) blobId if the server returned one.
|
|
const blobId = created?.blobId ?? (await nodeBlobId(accountId, created?.id)) ?? up.blobId;
|
|
return client.downloadUrl(accountId, blobId, name, type, true);
|
|
} catch (err) {
|
|
toast.error(t("Could not store image: {error}", { error: (err as Error).message }));
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
/** Store the full HTML of an over-sized signature in Files; returns the blob id. */
|
|
export async function storeSignatureHtml(html: string): Promise<string> {
|
|
const accountId = useSession.getState().ownAccountFor(CAP.filenode);
|
|
if (!accountId || !client.hasCapability(CAP.filenode)) throw new Error("This signature is too long for the server and the Files feature (needed to store long signatures) is not available.");
|
|
const up = await client.upload(accountId, new Blob([html], { type: "text/html" }), { type: "text/html" });
|
|
const folderId = await ensureFolder(accountId);
|
|
const name = `signature-${Date.now()}.html`;
|
|
const res = await client.call<SetResponse<FileNode>>("FileNode/set", { accountId, create: { f: fileCreate(folderId, name, up.blobId, "text/html") } });
|
|
const err = res.notCreated?.f;
|
|
if (err) throw new Error(setErrorMessage(err));
|
|
const created = res.created?.f as Partial<FileNode> | undefined;
|
|
return created?.blobId ?? (await nodeBlobId(accountId, created?.id)) ?? up.blobId;
|
|
}
|
|
|
|
/** Replace data: URL images (pasted pictures) in signature HTML with stored blob URLs. */
|
|
export async function externalizeDataImages(html: string): Promise<string> {
|
|
if (!html.includes("data:image/")) return html;
|
|
const doc = new DOMParser().parseFromString(`<div id="r">${html}</div>`, "text/html");
|
|
const root = doc.getElementById("r")!;
|
|
const imgs = Array.from(root.querySelectorAll("img")).filter((i) => i.getAttribute("src")?.startsWith("data:image/"));
|
|
for (const img of imgs) {
|
|
const m = /^data:(image\/[\w.+-]+);base64,(.*)$/s.exec(img.getAttribute("src")!);
|
|
if (!m) {
|
|
img.remove();
|
|
continue;
|
|
}
|
|
const bin = atob(m[2]!);
|
|
const bytes = new Uint8Array(bin.length);
|
|
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
|
|
const file = new File([bytes], `image.${m[1]!.split("/")[1]?.replace("jpeg", "jpg") ?? "png"}`, { type: m[1]! });
|
|
img.setAttribute("src", await uploadSignatureImage(file));
|
|
}
|
|
return root.innerHTML;
|
|
}
|
|
|
|
/** Load the full HTML of a marker signature. */
|
|
export async function loadStoredSignature(blobId: string, type = "text/html"): Promise<string> {
|
|
// No `?? accountId` fallback: a signature is the reader's own, and the
|
|
// selected account may be somebody else's shared one.
|
|
const accountId = useSession.getState().ownAccountFor(CAP.filenode);
|
|
if (!accountId) throw new Error("no account");
|
|
return client.fetchBlobText(accountId, blobId, type);
|
|
}
|
|
|
|
export type { QueryResponse };
|