Files
ihasmail-inbuxa/web/src/lib/sw/swFacts.ts
T
jcoffey 9e3844e94a Call the app by its name in every sentence that names it (#406)
APP_NAME renames an instance, but only the sign-in page, the title bar and
a few headings used it. Two dozen sentences wrote "ihasmail" into
themselves, so a renamed instance still told people to keep an ihasmail
tab open and offered to open mail links "in ihasmail".

Those sentences now take the name as {app}, which also lets a translator
put it where their language wants it. brand.ts grew useAppName() for
components and currentAppName() for the few places that build strings
outside React.

Left as they are: the Files folder "ihasmail", the Sieve script
"ihasmail" and ihasmail.org. Those name things a person can go and look
at, and renaming them would rename real data.

All nine catalogues keep their translations: the name inside each one
became the placeholder. Three of the strings had no translation before
and still fall back to English.

A test walks the sources and the catalogues so a new sentence can't
hard-code the name again.
2026-09-19 14:27:34 -07:00

71 lines
2.6 KiB
TypeScript

/*
* What the service worker cannot work out for itself.
*
* The worker can act on mail — see the note on `jmap()` in sw.js — but it
* cannot read a catalog or a store. It is plain JavaScript copied into the
* build, outside the bundle, with no i18n and no idea which mailbox is the
* archive. Both of those are things a tab knows and can simply write down.
*
* So the app leaves a short briefing in the same cache it uses for every other
* handoff, and the worker reads it when a notification arrives. Where there is
* none, the worker offers no actions at all rather than guessing: an untitled
* button that files mail somewhere is worse than a notification you have to
* open.
*
* That means the actions appear once ihasmail has been opened since the worker
* was installed, which is the same condition background notifications already
* carry — a push subscription has to be renewed from a tab too.
*/
import { currentAppName } from "@/lib/brand";
import { withBase } from "../basePath";
import { SW_CACHE_NAME } from "./swCache";
import { t } from "../i18n";
export const FACTS_KEY = "/ihasmail-worker-facts";
export interface WorkerFacts {
/** The account the notifications are about. */
accountId: string;
/** Where Archive files to; null where the account has no archive folder. */
archiveId: string | null;
/** The worker's own user-visible text, in the language this tab is in. */
strings: {
newMail: string;
newMessage: string;
noSubject: string;
archive: string;
markRead: string;
failed: string;
};
}
/**
* Write the briefing.
*
* Called again whenever what is in it could have changed — the language, the
* account, the archive folder — because it is what the worker will still be
* reading in a week's time. Rewriting it is one cache put; there is nothing to
* gain by working out whether it differs.
*/
export async function publishWorkerFacts(accountId: string | null, archiveId: string | null): Promise<void> {
if (typeof caches === "undefined" || !accountId) return;
const facts: WorkerFacts = {
accountId,
archiveId,
strings: {
newMail: t("New mail"),
newMessage: t("New message"),
noSubject: t("(no subject)"),
archive: t("Archive"),
markRead: t("Mark as read"),
failed: t("Could not do that — open {app} and try again", { app: currentAppName() }),
},
};
try {
const cache = await caches.open(SW_CACHE_NAME);
await cache.put(withBase(FACTS_KEY), new Response(JSON.stringify(facts), { headers: { "content-type": "application/json" } }));
} catch {
/* no cache storage: the worker falls back to a notification with no actions */
}
}