Files
ihasmail/web/src/views/mail/AddressMenu.tsx
T
jcoffey-dev a6863e98cc Second extraction pass: the strings the codemod could not see
`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.
2026-08-31 14:14:30 -07:00

95 lines
3.8 KiB
TypeScript

import { useCallback, useState, type MouseEvent, type ReactNode } from "react";
import { Copy, Mail, Pencil, UserPlus } from "lucide-react";
import type { EmailAddress } from "@/jmap/types";
import { useContacts } from "@/store/contacts";
import { useCompose } from "@/store/compose";
import { contactFromAddress } from "@/lib/contacts";
import { formatAddress } from "@/lib/address";
import { MenuItem, MenuSep, Popover, type Anchor } from "@/ui/popover";
import { toast } from "@/ui/toast";
import { ContactEditor } from "../contacts/ContactEditor";
import { t } from "@/lib/i18n";
/**
* Right-click on anyone named in a message — sender, recipients, Reply-To — to
* add them to the address book. The contact editor opens prefilled rather than
* saving silently, so the address book gets a real card and not just a stray
* email address.
*/
export function useAddressMenu() {
const [menu, setMenu] = useState<{ anchor: Anchor; address: EmailAddress } | null>(null);
const [editing, setEditing] = useState<ReturnType<typeof contactFromAddress> | null>(null);
const contacts = useContacts();
const openCompose = useCompose((s) => s.open);
const open = useCallback((ev: MouseEvent, address: EmailAddress) => {
if (!address.email) return;
ev.preventDefault();
ev.stopPropagation();
setMenu({ anchor: { x: ev.clientX, y: ev.clientY }, address });
// The books are needed the moment "Add to contacts" is chosen.
const st = useContacts.getState();
if (st.available && !st.loaded && !st.loading) void st.loadAll();
}, []);
const close = () => setMenu(null);
const known = menu ? contacts.lookupByEmail(menu.address.email) : undefined;
const books = Object.values(contacts.books);
const defaultBookId = (books.find((b) => b.isDefault) ?? books[0])?.id ?? null;
const node: ReactNode = (
<>
{menu && (
<Popover anchor={menu.anchor} onClose={close} width={230} ariaLabel={`Actions for ${menu.address.email}`}>
<div className="menu-title truncate">{formatAddress(menu.address)}</div>
{contacts.available && (
known ? (
<MenuItem icon={<Pencil size={16} />} label={t("Edit contact")} onClick={() => { setEditing(known); close(); }} />
) : (
<MenuItem icon={<UserPlus size={16} />} label={t("Add to contacts")} onClick={() => { setEditing(contactFromAddress(menu.address)); close(); }} />
)
)}
<MenuItem icon={<Mail size={16} />} label={t("New message to this address")} onClick={() => { openCompose({ to: [menu.address] }); close(); }} />
<MenuSep />
<MenuItem
icon={<Copy size={16} />}
label={t("Copy email address")}
onClick={() => {
void navigator.clipboard?.writeText(menu.address.email).then(
() => toast.show(t("Address copied")),
() => toast.error(t("Could not copy the address")),
);
close();
}}
/>
</Popover>
)}
{editing && (
<ContactEditor
card={editing}
defaultBookId={defaultBookId}
onClose={() => setEditing(null)}
onSaved={() => setEditing(null)}
/>
)}
</>
);
return { open, node };
}
/** Comma-separated addresses, each of them right-clickable. */
export function AddressList({ list, onContext, empty = "—" }: { list: EmailAddress[] | null | undefined; onContext: (ev: MouseEvent, a: EmailAddress) => void; empty?: string }) {
if (!list?.length) return <>{empty}</>;
return (
<>
{list.map((a, i) => (
<span key={`${a.email}-${i}`}>
{i > 0 && ", "}
<span className="addr" onContextMenu={(ev) => onContext(ev, a)} title={t("Right-click for options")}>{formatAddress(a)}</span>
</span>
))}
</>
);
}