mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
fix(02-12): hold peer tabs during record switches
- Clear scoped reads before the server session changes - Reconcile every tab with a fresh access response
This commit is contained in:
1 parent
c66b29d22a
commit
38214e4726
13 files changed
+711
-45
No files matched your search
@@ -55,6 +55,9 @@ const mockAuthLoadingRef = { value: false };
|
||||
const mockAccountAccessStatusRef = {
|
||||
value: "valid" as "absent" | "valid" | "invalid",
|
||||
};
|
||||
const mockRecordSessionPhaseRef = {
|
||||
value: "ready" as "ready" | "blocking" | "resolving",
|
||||
};
|
||||
|
||||
vi.mock("@/hooks/use-auth", () => ({
|
||||
useAuth: () => ({
|
||||
@@ -76,6 +79,14 @@ vi.mock("@/hooks/use-auth", () => ({
|
||||
clearCachesForSessionEnd: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/hooks/use-record-session-transition", () => ({
|
||||
useRecordSessionTransition: () => ({
|
||||
id: "cross-tab-switch",
|
||||
phase: mockRecordSessionPhaseRef.value,
|
||||
expectedScope: "record-a",
|
||||
}),
|
||||
}));
|
||||
|
||||
const mockPathRef = { value: "/" };
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
@@ -192,6 +203,21 @@ describe("<AuthShell> — the owner-only doors", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("holds protected children and owner-only doors while a peer tab changes the session", () => {
|
||||
mockRecordSessionPhaseRef.value = "blocking";
|
||||
try {
|
||||
const html = render(OWN_RECORD, "/labs");
|
||||
|
||||
expect(html).toContain('data-slot="record-scope-hydration-gate"');
|
||||
expect(html).not.toContain("sentinel-page");
|
||||
for (const slot of DOORS) {
|
||||
expect(html).not.toContain(slot);
|
||||
}
|
||||
} finally {
|
||||
mockRecordSessionPhaseRef.value = "ready";
|
||||
}
|
||||
});
|
||||
|
||||
it("does not delay public routes for record capability resolution", () => {
|
||||
mockAuthLoadingRef.value = true;
|
||||
try {
|
||||
|
||||
@@ -11,6 +11,7 @@ import { LayoutCoachMount } from "@/components/insights/layout-coach-mount";
|
||||
import { TourLauncher } from "@/components/onboarding/tour-launcher";
|
||||
import { clearCachesForSessionEnd, useAuth } from "@/hooks/use-auth";
|
||||
import { useRecordCapabilities } from "@/hooks/use-record-capabilities";
|
||||
import { useRecordSessionTransition } from "@/hooks/use-record-session-transition";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { CoachLaunchProvider } from "@/lib/insights/coach-launch-context";
|
||||
import { isDestinationInSharedRecord } from "./nav-model";
|
||||
@@ -58,6 +59,7 @@ export function AuthShell({
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
const queryClient = useQueryClient();
|
||||
const recordSessionTransition = useRecordSessionTransition();
|
||||
|
||||
const isPublicPage = PUBLIC_PATHS.some((p) => pathname.startsWith(p));
|
||||
// v1.4.26 — `/privacy` is a long-form legal page that brings its own
|
||||
@@ -79,8 +81,13 @@ export function AuthShell({
|
||||
// decides what to mount and the surfaces beneath it decide what to offer,
|
||||
// and the two disagreeing is the whole class of defect this hook exists to
|
||||
// close.
|
||||
const { inSharedRecord, recordKind, sections, accessRefused } =
|
||||
useRecordCapabilities();
|
||||
const {
|
||||
inSharedRecord,
|
||||
recordKind,
|
||||
sections,
|
||||
accessRefused,
|
||||
recordSessionPending,
|
||||
} = useRecordCapabilities();
|
||||
// A managed profile has a deliberately small Settings surface of its own.
|
||||
// Let that route family reach its record-aware gate; adult shared records
|
||||
// stay in the generic refusal branch even at MANAGE.
|
||||
@@ -231,7 +238,11 @@ export function AuthShell({
|
||||
// Admin. The payload resolves the active record as well as identity, so a
|
||||
// child mounted before it could issue an actor-scoped read before the shell
|
||||
// knows that a switched record must be refused or scoped differently.
|
||||
if (isLoading) {
|
||||
if (
|
||||
isLoading ||
|
||||
recordSessionTransition.phase !== "ready" ||
|
||||
recordSessionPending
|
||||
) {
|
||||
return <RecordScopeHydrationGate label={t("nav.loadingScreen")} />;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,8 +23,16 @@ import { VersionPoller } from "@/components/version-poller";
|
||||
import { ServiceWorkerRegistrar } from "@/components/service-worker-registrar";
|
||||
import { SharedRecordGrantLossBridge } from "@/components/layout/shared-record-grant-loss-bridge";
|
||||
import { useAuth, type AccountAccessStatus } from "@/hooks/use-auth";
|
||||
import { useRecordSessionTransition } from "@/hooks/use-record-session-transition";
|
||||
import { isDashboardSnapshotEnabled } from "@/lib/dashboard/snapshot-flag";
|
||||
import { prefetchDashboardSnapshot } from "@/lib/queries/use-dashboard-snapshot";
|
||||
import {
|
||||
discardDashboardSnapshotPreload,
|
||||
prefetchDashboardSnapshot,
|
||||
} from "@/lib/queries/use-dashboard-snapshot";
|
||||
import {
|
||||
getRecordSessionTransition,
|
||||
subscribeToRecordSessionTransition,
|
||||
} from "@/lib/query-keys/record-session-transition";
|
||||
import { prefetchMedicationsList } from "@/lib/queries/prefetch-medications";
|
||||
import {
|
||||
getRecordScope,
|
||||
@@ -208,16 +216,19 @@ function DashboardSnapshotPreloader() {
|
||||
const queryClient = useQueryClient();
|
||||
const pathname = usePathname();
|
||||
const { user, isLoading } = useAuth();
|
||||
const transition = useRecordSessionTransition();
|
||||
const recordScope = useSyncExternalStore(
|
||||
subscribeToClientRecordScope,
|
||||
getClientRecordScope,
|
||||
getServerRecordScope,
|
||||
);
|
||||
const ready = isRecordPreloadReady({
|
||||
isLoading,
|
||||
accountAccessStatus: user?.accountAccessStatus,
|
||||
activeAccountId: user?.accountAccess?.active?.accountId ?? null,
|
||||
});
|
||||
const ready =
|
||||
transition.phase === "ready" &&
|
||||
isRecordPreloadReady({
|
||||
isLoading,
|
||||
accountAccessStatus: user?.accountAccessStatus,
|
||||
activeAccountId: user?.accountAccess?.active?.accountId ?? null,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!ready) return;
|
||||
@@ -237,7 +248,7 @@ function DashboardSnapshotPreloader() {
|
||||
// A transition to an unresolved, refused, or different record scope must
|
||||
// abort a preloader before its response can be adopted by the next route.
|
||||
return () => controller.abort();
|
||||
}, [pathname, queryClient, ready, recordScope]);
|
||||
}, [pathname, queryClient, ready, recordScope, transition.phase]);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -245,9 +256,13 @@ function DashboardSnapshotPreloader() {
|
||||
|
||||
function QueryVisibilityRefreshBridge() {
|
||||
const queryClient = useQueryClient();
|
||||
const transition = useRecordSessionTransition();
|
||||
useEffect(
|
||||
() => subscribeToMeaningfulVisibilityRefresh(queryClient),
|
||||
[queryClient],
|
||||
() =>
|
||||
transition.phase === "ready"
|
||||
? subscribeToMeaningfulVisibilityRefresh(queryClient)
|
||||
: undefined,
|
||||
[queryClient, transition.phase],
|
||||
);
|
||||
return null;
|
||||
}
|
||||
@@ -261,10 +276,16 @@ function QueryVisibilityRefreshBridge() {
|
||||
// empty skeletons. Build-version + age gated; cleared on logout.
|
||||
function QueryPersistenceBridge() {
|
||||
const queryClient = useQueryClient();
|
||||
const transition = useRecordSessionTransition();
|
||||
useEffect(() => {
|
||||
if (transition.phase !== "ready") return;
|
||||
let stop: (() => void) | undefined;
|
||||
let cancelled = false;
|
||||
void restorePersistedQueryCache(queryClient, SHELL_VERSION).finally(() => {
|
||||
void restorePersistedQueryCache(
|
||||
queryClient,
|
||||
SHELL_VERSION,
|
||||
() => !cancelled,
|
||||
).finally(() => {
|
||||
if (!cancelled) {
|
||||
stop = startPersistingQueryCache(queryClient, SHELL_VERSION);
|
||||
}
|
||||
@@ -273,7 +294,42 @@ function QueryPersistenceBridge() {
|
||||
cancelled = true;
|
||||
stop?.();
|
||||
};
|
||||
}, [queryClient, transition.phase]);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear tab-local reads immediately whenever the browser-wide transition
|
||||
* store enters its hold. The AuthShell consumes the same store synchronously,
|
||||
* so protected children unmount while this bridge cancels their observers.
|
||||
*/
|
||||
function RecordSessionTransitionBridge() {
|
||||
const queryClient = useQueryClient();
|
||||
const { refetch } = useAuth();
|
||||
const transition = useRecordSessionTransition();
|
||||
|
||||
useEffect(() => {
|
||||
const clearForTransition = () => {
|
||||
if (getRecordSessionTransition().phase === "ready") return;
|
||||
discardDashboardSnapshotPreload();
|
||||
void queryClient.cancelQueries();
|
||||
queryClient.clear();
|
||||
};
|
||||
// A tab opened while another tab is already switching needs the same
|
||||
// cache purge even though it has no future broadcast to receive.
|
||||
clearForTransition();
|
||||
return subscribeToRecordSessionTransition(clearForTransition);
|
||||
}, [queryClient]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
transition.phase === "resolving" ||
|
||||
(transition.phase === "ready" && transition.id !== null)
|
||||
) {
|
||||
void refetch();
|
||||
}
|
||||
}, [refetch, transition.id, transition.phase]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -329,6 +385,7 @@ export function Providers({
|
||||
<ThemeProvider>
|
||||
<I18nProvider initialLocale={initialLocale}>
|
||||
<QueryPersistenceBridge />
|
||||
<RecordSessionTransitionBridge />
|
||||
<QueryVisibilityRefreshBridge />
|
||||
<DashboardSnapshotPreloader />
|
||||
<OfflineMutationToaster />
|
||||
|
||||
Reference in new issue
Block a user