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.
140 lines
5.2 KiB
TypeScript
140 lines
5.2 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { ChevronRight, File as FileIcon, Folder, HardDrive, Users } from "lucide-react";
|
|
import { Dialog } from "@/ui/dialog";
|
|
import { Spinner } from "@/ui/misc";
|
|
import { useFiles } from "@/store/files";
|
|
import type { AttachableFile } from "@/store/compose";
|
|
import type { FileNode } from "@/jmap/types";
|
|
import { formatSize } from "@/lib/format";
|
|
import { t } from "@/lib/i18n";
|
|
|
|
/**
|
|
* Pick something already in Files to attach.
|
|
*
|
|
* Browsing is the store's, so this shows the same folders the Files view does,
|
|
* shared accounts included -- a file somebody shared with you is a file you can
|
|
* send on, and having to download it first only to upload it again would be
|
|
* the sort of detour the rest of this avoids.
|
|
*
|
|
* It borrows the Files store rather than keeping its own copy, which means
|
|
* opening the picker moves where Files is browsing. Closing it puts that back:
|
|
* a detour through somebody's shared folder to find an attachment should not
|
|
* leave the file manager somewhere else afterwards.
|
|
*/
|
|
export function FilePicker({ onPick, onClose }: { onPick: (files: AttachableFile[]) => void; onClose: () => void }) {
|
|
const files = useFiles();
|
|
const [cur, setCur] = useState<string | null>(null);
|
|
const [picked, setPicked] = useState<Record<string, FileNode>>({});
|
|
const [returnTo] = useState(() => files.accountId);
|
|
|
|
useEffect(() => {
|
|
void files.loadChildren(cur);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [cur, files.accountId]);
|
|
|
|
const close = () => {
|
|
if (files.accountId !== returnTo) files.openAccount(returnTo);
|
|
onClose();
|
|
};
|
|
|
|
const openAccount = (accountId: string | null) => {
|
|
files.openAccount(accountId);
|
|
setCur(null);
|
|
setPicked({});
|
|
};
|
|
|
|
const nodes = (files.children[cur ?? "root"] ?? []).map((id) => files.nodes[id]).filter((n): n is FileNode => Boolean(n));
|
|
const path = files.pathTo(cur);
|
|
const chosen = Object.values(picked);
|
|
const viewingShare = files.accountId !== files.ownAccountId;
|
|
|
|
return (
|
|
<Dialog
|
|
open
|
|
onClose={close}
|
|
title={t("Attach from Files")}
|
|
size="md"
|
|
footer={
|
|
<>
|
|
<button className="btn" onClick={close}>{t("Cancel")}</button>
|
|
<button
|
|
className="btn btn-primary"
|
|
disabled={!chosen.length}
|
|
onClick={() => {
|
|
onPick(chosen.map((n) => ({ accountId: files.accountId!, name: n.name, type: n.type, size: n.size, blobId: n.blobId! })));
|
|
close();
|
|
}}
|
|
>
|
|
{chosen.length > 1 ? `Attach ${chosen.length} files` : "Attach"}
|
|
</button>
|
|
</>
|
|
}
|
|
>
|
|
{files.sharedAccounts.length > 0 && (
|
|
<div className="row wrap gap-4" style={{ marginBottom: 10 }}>
|
|
<button className={`btn btn-sm ${viewingShare ? "" : "btn-primary"}`} onClick={() => openAccount(files.ownAccountId)}>
|
|
<HardDrive size={14} /> My files
|
|
</button>
|
|
{files.sharedAccounts.map((a) => (
|
|
<button key={a.id} className={`btn btn-sm ${files.accountId === a.id ? "btn-primary" : ""}`} onClick={() => openAccount(a.id)}>
|
|
<Users size={14} /> {a.name}
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
<div className="breadcrumb mb-8">
|
|
<button onClick={() => setCur(null)}><HardDrive size={14} /></button>
|
|
{path.map((n) => (
|
|
<span key={n.id} className="row gap-4">
|
|
<ChevronRight size={12} />
|
|
<button onClick={() => setCur(n.id)}>{n.name}</button>
|
|
</span>
|
|
))}
|
|
</div>
|
|
|
|
{files.loading && !nodes.length ? (
|
|
<Spinner />
|
|
) : !nodes.length ? (
|
|
<p className="hint">{t("This folder is empty.")}</p>
|
|
) : (
|
|
nodes.map((n) =>
|
|
n.nodeType === "directory" ? (
|
|
<button key={n.id} className="menu-item" onClick={() => setCur(n.id)}>
|
|
<Folder size={16} />
|
|
<span className="grow truncate">{n.name}</span>
|
|
<ChevronRight size={14} />
|
|
</button>
|
|
) : (
|
|
<label key={n.id} className="menu-item" style={{ cursor: n.blobId ? "pointer" : "not-allowed", opacity: n.blobId ? 1 : 0.5 }}>
|
|
<input
|
|
type="checkbox"
|
|
disabled={!n.blobId}
|
|
checked={Boolean(picked[n.id])}
|
|
onChange={(e) =>
|
|
setPicked((p) => {
|
|
const next = { ...p };
|
|
if (e.target.checked) next[n.id] = n;
|
|
else delete next[n.id];
|
|
return next;
|
|
})
|
|
}
|
|
/>
|
|
<FileIcon size={16} />
|
|
<span className="grow truncate">{n.name}</span>
|
|
<span className="hint">{formatSize(n.size)}</span>
|
|
</label>
|
|
),
|
|
)
|
|
)}
|
|
|
|
{viewingShare && chosen.length > 0 && (
|
|
// Blobs belong to the account holding them, so one from a share has to
|
|
// be copied into yours before a draft can reference it. Worth saying,
|
|
// because it is the difference between instant and a wait.
|
|
<p className="hint" style={{ marginTop: 10 }}>{t("Shared files are copied to your account when attached.")}</p>
|
|
)}
|
|
</Dialog>
|
|
);
|
|
}
|