25 lines
1.5 KiB
TypeScript
25 lines
1.5 KiB
TypeScript
import type { QueryClient } from "@tanstack/react-query";
|
|
import { isSupported } from "../i18n";
|
|
import { api } from "./api";
|
|
import { LANGUAGE_ORDERED } from "./queryKeys";
|
|
import { updateMeCache } from "./useMe";
|
|
|
|
/** Store the reader's language choice on the server, then ask again for the lists the server orders
|
|
* by it (`LANGUAGE_ORDERED`): only once the save has landed does the server sort in the new alphabet.
|
|
* The cached `me` takes it too, so what reads the stored language (`serverOrderLang`) agrees with the
|
|
* server at once, not at the next `me` poll. Rejects with the save's error; nothing is touched then. */
|
|
export async function saveLanguage(qc: QueryClient, code: string): Promise<void> {
|
|
await api.savePrefs({ language: code });
|
|
updateMeCache(qc, (me) => ({ ...me, preferences: { ...me.preferences, language: code } }));
|
|
for (const queryKey of LANGUAGE_ORDERED) void qc.invalidateQueries({ queryKey });
|
|
}
|
|
|
|
/** The language the server orders this account's lists in (backend `mailtext.lang_for_user`): the
|
|
* stored preference, else English. Not always the UI's: an account with none stored stores the one
|
|
* it is shown in at boot (BootGate), but until that save lands, or if it fails, the two differ — so a
|
|
* list re-slotted here to match the server's order uses this, not the UI's. */
|
|
export function serverOrderLang(preferences: Record<string, unknown> | null | undefined): string {
|
|
const lang = preferences?.language;
|
|
return typeof lang === "string" && isSupported(lang) ? lang : "en";
|
|
}
|