Wrapping ~1,000 strings by hand is a thousand chances to mistype the copy
itself, and a parser does not get bored. scripts/i18n-extract.mjs does the
mechanical part -- JSX text and the attributes a person actually reads -- and
refuses the rest rather than guessing. 78% now: 515 wrapped, 143 left.
What it refuses matters as much as what it does. Text split around an
interpolation arrives as separate fragments, and wrapping each on its own
produces "Move " and " messages", which no translator can do anything with;
those are listed for a person to rebuild as sentences. So is anything
containing a double quote, which would end the literal.
Three things it had to be taught, each found by running it:
- <code>, <kbd> and <pre> are not prose. The first run wrapped `label:name`
inside <code> -- a search operator, where translating it breaks the thing it
documents. Subtrees marked translate="no" are skipped for the same reason.
- `t` is a natural name for a callback parameter and several files already use
it, so an import called `t` is shadowed inside those callbacks -- silently,
wherever the local happens to be callable. The name is checked per file now
and aliased to `translate` where it is taken.
- JSX decodes HTML entities and a JS string literal does not, so
`Language & region` moved into t("...") and rendered the entity on screen.
That last one is the one worth remembering. Typecheck passed, 443 tests
passed, and the page said "Language & region" in plain sight. It took
looking at a screenshot, and then a sweep of ten views to find the second
occurrence in a sentence I had written by hand earlier the same day. Nothing
in the toolchain was ever going to catch it: it is valid TypeScript rendering
valid text that happens to be wrong.
The codemod decodes entities now, and checks for a quote after decoding rather
than before.
66 lines
2.7 KiB
TypeScript
66 lines
2.7 KiB
TypeScript
import { useMemo, useState } from "react";
|
|
import { Folder, Inbox } from "lucide-react";
|
|
import { useMail } from "@/store/mail";
|
|
import { Dialog } from "@/ui/dialog";
|
|
import type { Id, Mailbox } from "@/jmap/types";
|
|
import { t } from "@/lib/i18n";
|
|
|
|
export function MailboxPicker({ title, onClose, onPick, exclude }: { title: string; onClose: () => void; onPick: (id: Id) => void; exclude?: Id[] }) {
|
|
const mailboxes = useMail((s) => s.mailboxes);
|
|
const mailboxPath = useMail((s) => s.mailboxPath);
|
|
const [q, setQ] = useState("");
|
|
const [active, setActive] = useState(0);
|
|
const list = useMemo(() => {
|
|
const all = Object.values(mailboxes)
|
|
.filter((m) => !exclude?.includes(m.id) && m.myRights.mayAddItems)
|
|
.map((m) => ({ m, path: mailboxPath(m.id) }))
|
|
.sort((a, b) => (a.m.role === "inbox" ? -1 : b.m.role === "inbox" ? 1 : a.path.localeCompare(b.path)));
|
|
const ql = q.trim().toLowerCase();
|
|
return ql ? all.filter((x) => x.path.toLowerCase().includes(ql)) : all;
|
|
}, [mailboxes, mailboxPath, q, exclude]);
|
|
|
|
return (
|
|
<Dialog open onClose={onClose} title={title} size="sm">
|
|
<input
|
|
className="input"
|
|
autoFocus
|
|
placeholder={t("Type a folder name…")}
|
|
value={q}
|
|
onChange={(e) => {
|
|
setQ(e.target.value);
|
|
setActive(0);
|
|
}}
|
|
onKeyDown={(e) => {
|
|
if (e.key === "ArrowDown") {
|
|
e.preventDefault();
|
|
setActive((a) => Math.min(list.length - 1, a + 1));
|
|
} else if (e.key === "ArrowUp") {
|
|
e.preventDefault();
|
|
setActive((a) => Math.max(0, a - 1));
|
|
} else if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
const m = list[active]?.m;
|
|
if (m) onPick(m.id);
|
|
}
|
|
}}
|
|
/>
|
|
<div style={{ maxHeight: 360, overflowY: "auto", marginTop: 8 }} role="listbox">
|
|
{list.map(({ m, path }, i) => (
|
|
<PickerRow key={m.id} m={m} path={path} active={i === active} onClick={() => onPick(m.id)} onHover={() => setActive(i)} />
|
|
))}
|
|
{!list.length && <div className="empty" style={{ padding: 24 }}>{t("No matching folders")}</div>}
|
|
</div>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
function PickerRow({ m, path, active, onClick, onHover }: { m: Mailbox; path: string; active: boolean; onClick: () => void; onHover: () => void }) {
|
|
return (
|
|
<button className={`menu-item ${active ? "active" : ""}`} onClick={onClick} onMouseEnter={onHover} role="option" aria-selected={active}>
|
|
{m.role === "inbox" ? <Inbox size={16} /> : <Folder size={16} />}
|
|
<span className="grow truncate">{path}</span>
|
|
<span className="menu-kbd">{m.totalEmails}</span>
|
|
</button>
|
|
);
|
|
}
|