mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
fix(i18n): mirror the language to the account only when signed in
The provider sent PUT /api/auth/me/locale on mount and on every switch, signed in or not. Signed out the route answers 401, the once-per-tab marker was never set, and every signed-out page load repeated the refused write. The mirror moves out of the provider into LocaleServerSyncBridge, which watches the account payload without fetching it and writes only once an account is known. The choice made on the sign-in page therefore reaches the account right after signing in, and the marker is keyed on the account as well as the language, so another account signing in on the same tab is written for too. The browser test stub no longer treats the PUT as ambient, which is what lets its test see a stray write.
This commit is contained in:
1 parent
bbef4eea99
commit
2490a2d218
6 files changed
+134
-28
No files matched your search
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* The language reaches the account only when there is an account.
|
||||
*
|
||||
* The provider used to PUT the locale on every mount, signed in or not. A
|
||||
* signed-out page got a 401, the once-per-tab marker was never set, and every
|
||||
* signed-out page load sent the refused write again. The write now belongs to
|
||||
* `LocaleServerSyncBridge`, which waits for the account payload without
|
||||
* fetching it and then sends the choice once.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
import { authUser, renderWithApp } from "../../../../tests/browser/render";
|
||||
import { stubFetch } from "../../../../tests/browser/fetch-stub";
|
||||
import { LocaleServerSyncBridge } from "../locale-server-sync-bridge";
|
||||
|
||||
const LOCALE_PUT = "/api/auth/me/locale";
|
||||
|
||||
async function settle(): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 150));
|
||||
}
|
||||
|
||||
describe("LocaleServerSyncBridge", () => {
|
||||
it("sends nothing while nobody is signed in, and never asks who is", async () => {
|
||||
const fetchStub = stubFetch().on("PUT", LOCALE_PUT, { data: null });
|
||||
await renderWithApp(<LocaleServerSyncBridge />, {
|
||||
user: null,
|
||||
locale: "de",
|
||||
});
|
||||
await settle();
|
||||
expect(fetchStub.calls("PUT", LOCALE_PUT)).toHaveLength(0);
|
||||
expect(fetchStub.calls("GET", "/api/auth/me")).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("sends the choice made while signed out once an account appears", async () => {
|
||||
const fetchStub = stubFetch().on("PUT", LOCALE_PUT, { data: null });
|
||||
const { queryClient } = await renderWithApp(<LocaleServerSyncBridge />, {
|
||||
user: null,
|
||||
locale: "de",
|
||||
});
|
||||
await settle();
|
||||
expect(fetchStub.calls("PUT", LOCALE_PUT)).toHaveLength(0);
|
||||
|
||||
queryClient.setQueryData(
|
||||
queryKeys.authMe(),
|
||||
authUser({ id: "account-signed-in-later" }),
|
||||
);
|
||||
await expect.poll(() => fetchStub.calls("PUT", LOCALE_PUT).length).toBe(1);
|
||||
expect(fetchStub.calls("PUT", LOCALE_PUT)[0].body).toEqual({
|
||||
locale: "de",
|
||||
});
|
||||
});
|
||||
|
||||
it("writes once per tab for the same account and language", async () => {
|
||||
const fetchStub = stubFetch().on("PUT", LOCALE_PUT, { data: null });
|
||||
const first = await renderWithApp(<LocaleServerSyncBridge />, {
|
||||
user: { id: "account-already-synced" },
|
||||
locale: "en",
|
||||
});
|
||||
await expect.poll(() => fetchStub.calls("PUT", LOCALE_PUT).length).toBe(1);
|
||||
await first.screen.unmount();
|
||||
|
||||
await renderWithApp(<LocaleServerSyncBridge />, {
|
||||
user: { id: "account-already-synced" },
|
||||
locale: "en",
|
||||
});
|
||||
await settle();
|
||||
expect(fetchStub.calls("PUT", LOCALE_PUT)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
import { fetchMe } from "@/hooks/use-auth";
|
||||
import { persistLocaleToServer, useTranslations } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
/**
|
||||
* Keeps `User.locale` in step with the language on screen, for a signed-in
|
||||
* account only, so the proactive Coach nudge and the other cookie-blind
|
||||
* background paths write in the language the person reads.
|
||||
*
|
||||
* It watches the account payload without fetching it (`enabled: false`): the
|
||||
* shell already reads `/api/auth/me` on every signed-in page, and a signed-out
|
||||
* page has no account to write to. Fetching here would trade the refused
|
||||
* locale PUT for a refused account read on every signed-out load. Same key
|
||||
* and same fetcher as `useAuth`, so the cache entry is shared, not shadowed.
|
||||
*
|
||||
* Fires when an account appears (right after signing in, carrying the choice
|
||||
* made on the sign-in page) and on every switch; `persistLocaleToServer`
|
||||
* skips a value this tab already wrote for this account.
|
||||
*/
|
||||
export function LocaleServerSyncBridge() {
|
||||
const { locale } = useTranslations();
|
||||
const { data: account } = useQuery({
|
||||
queryKey: queryKeys.authMe(),
|
||||
queryFn: fetchMe,
|
||||
enabled: false,
|
||||
});
|
||||
const accountId = account?.id ?? null;
|
||||
|
||||
useEffect(() => {
|
||||
if (accountId) persistLocaleToServer(locale, accountId);
|
||||
}, [locale, accountId]);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { ServiceWorkerRegistrar } from "@/components/service-worker-registrar";
|
||||
import { SharedRecordGrantLossBridge } from "@/components/layout/shared-record-grant-loss-bridge";
|
||||
import { RecordSessionFenceBridge } from "@/components/layout/record-session-fence-bridge";
|
||||
import { OidcReproofReturnBridge } from "@/components/auth/oidc-reproof-return";
|
||||
import { LocaleServerSyncBridge } from "@/components/layout/locale-server-sync-bridge";
|
||||
import { useAuth, type AccountAccessStatus } from "@/hooks/use-auth";
|
||||
import { useRecordSessionTransition } from "@/hooks/use-record-session-transition";
|
||||
import { isDashboardSnapshotEnabled } from "@/lib/dashboard/snapshot-flag";
|
||||
@@ -434,6 +435,7 @@ export function Providers({
|
||||
<SharedRecordGrantLossBridge />
|
||||
<RecordSessionFenceBridge />
|
||||
<OidcReproofReturnBridge />
|
||||
<LocaleServerSyncBridge />
|
||||
{children}
|
||||
<AppToaster />
|
||||
<KeyboardInsetBridge />
|
||||
|
||||
Reference in new issue
Block a user