Files
ihasmail-inbuxa/web/src/views/mail/LabelPicker.tsx
T
jcoffey-dev 8ea611f7f7 Extract 515 strings by codemod, and the two bugs only a screenshot caught
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 &amp; 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 &amp; 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.
2026-08-31 09:58:33 -07:00

85 lines
3.4 KiB
TypeScript

import { useState } from "react";
import { Plus } from "lucide-react";
import { useSettings } from "@/store/settings";
import { useMail } from "@/store/mail";
import { Popover } from "@/ui/popover";
import type { Id } from "@/jmap/types";
import { CALENDAR_COLORS } from "@/ui/misc";
import { t } from "@/lib/i18n";
/** Labels are IMAP keywords on the messages; their names/colors live in settings. */
export function LabelPicker({ ids, anchor, onClose, onApplied }: { ids: Id[]; anchor: { x: number; y: number }; onClose: () => void; onApplied?: () => void }) {
const labels = useSettings((s) => s.settings.labels);
const update = useSettings((s) => s.update);
const emails = useMail((s) => s.emails);
const setKeyword = useMail((s) => s.setKeyword);
const [q, setQ] = useState("");
const [creating, setCreating] = useState(false);
const has = (kw: string) => ids.every((id) => emails[id]?.keywords[kw]);
const some = (kw: string) => ids.some((id) => emails[id]?.keywords[kw]);
const filtered = labels.filter((l) => l.name.toLowerCase().includes(q.toLowerCase()));
const create = () => {
const name = q.trim();
if (!name) return;
const keyword = name.toLowerCase().replace(/[^a-z0-9_.-]+/g, "_").replace(/^_+|_+$/g, "") || `label${Date.now()}`;
if (labels.some((l) => l.keyword === keyword)) return;
const color = CALENDAR_COLORS[labels.length % CALENDAR_COLORS.length]!;
update({ labels: [...labels, { keyword, name, color }] });
void setKeyword(ids, keyword, true).then(onApplied);
setQ("");
setCreating(false);
};
return (
<Popover anchor={{ x: anchor.x, y: anchor.y, w: 0, h: 0 }} onClose={onClose} width={260} closeOnClick={false}>
<div className="menu-title">{t("Label as")}</div>
<div className="menu-search">
<input
className="input sm"
autoFocus
placeholder={labels.length ? "Search or create label" : "New label name"}
value={q}
onChange={(e) => setQ(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
if (filtered.length === 1 && !creating) {
const l = filtered[0]!;
void setKeyword(ids, l.keyword, !has(l.keyword)).then(onApplied);
} else create();
}
}}
/>
</div>
{filtered.map((l) => {
const all = has(l.keyword);
const partial = !all && some(l.keyword);
return (
<label key={l.keyword} className="menu-item" style={{ cursor: "pointer" }}>
<input
type="checkbox"
checked={all}
ref={(el) => {
if (el) el.indeterminate = partial;
}}
onChange={(e) => void setKeyword(ids, l.keyword, e.target.checked).then(onApplied)}
style={{ accentColor: l.color }}
/>
<span className="label-dot" style={{ background: l.color }} />
<span className="grow truncate">{l.name}</span>
</label>
);
})}
{q.trim() && !labels.some((l) => l.name.toLowerCase() === q.trim().toLowerCase()) && (
<button className="menu-item" onClick={create}>
<Plus size={16} />
<span>Create {q.trim()}</span>
</button>
)}
{!labels.length && !q && <div className="hint" style={{ padding: "4px 10px 8px" }}>{t("Type a name to create your first label.")}</div>}
</Popover>
);
}