fix(boot): a new tab knows its account before anything renders
With Settings -> Appearance -> Startup turned off, every NEW tab or window still opened on Home; only a same-tab reload honoured it. The provider tree mounted before /api/me answered, so NavigationProvider's initializer could not read the per-account opt-out, landed on Home and mirrored ?page=home into the address bar. When App pinned the account a moment later, the re-hydrate re-read that mirror and stayed on Home. The same mount-then-patch shape made a new tab start on default filters, the default theme and the browser language, and correct each after the first paint. BootGate (main.tsx, above the providers) now owns the setup-status and /api/me queries and mounts the tree only once the identity is settled: it pins the tab's account and adopts the account's language in a layout effect, so the first frame of the app is already right. The tree is keyed on the account id, which makes "per-account state is read once, for this account" a structural guarantee. With that, the whole re-hydrate machinery is gone: useAccountRehydrate, the reactive active-account store, the markDirty guards, the pin-deferred saved-view seed and the present-dimension bookkeeping of the shared Channels/Plex links. PrefsProvider, FeedFiltersProvider, usePref and App's panel layouts seed from the me cache instead of adopting it in an effect; the theme is applied in layout effects. The app chunk is fetched in parallel with the probe, and the boot screen only fades its label in after 400 ms, on one clock across the gate and the Suspense fallback. Module switches no longer flash a placeholder either. Measured frame by frame in Chromium and WebKit, every first visit to a module showed "Loading..." for 17-70 ms: the lazy chunk's Suspense fallback, then often the first fetch's LoadingState. Pages are now lazyWithPreload components, and App fetches the chunks of every module the account can reach (plus the channel page) once the first page is idle; a preloaded page renders synchronously and never suspends. LoadingState, the one shared loading visual, fades in only after 300 ms, so a fast load shows nothing in between and a slow one still gets its spinner.
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
|
||||
// The account a browser TAB is acting as (per-tab, in sessionStorage — see api.ts), plus its reactive
|
||||
// wrapper. Its OWN module (imports nothing internal) so it can be shared by both `storage.ts` (which
|
||||
// keys per-account localStorage on it) and `useAccountRehydrate.ts` without a circular import — the
|
||||
// account id is a lower-level primitive than the storage helpers that consume it.
|
||||
// The account a browser TAB is acting as (per-tab, in sessionStorage — see api.ts). Its OWN module
|
||||
// (imports nothing internal) so it can be shared by `storage.ts`, which keys per-account localStorage
|
||||
// on it, without a circular import — the account id is a lower-level primitive than the storage
|
||||
// helpers that consume it.
|
||||
//
|
||||
// Deliberately NOT reactive: BootGate pins the account before any per-account state is read, and a
|
||||
// switch reloads the page, so nothing mounted ever sees the id change underneath it.
|
||||
//
|
||||
// ACTIVE_ACCOUNT_KEY is duplicated here rather than imported from api.ts so this stays dependency-free.
|
||||
export const ACTIVE_ACCOUNT_KEY = "siftlode.activeAccount";
|
||||
@@ -29,22 +30,3 @@ export function withActiveAccount(path: string): string {
|
||||
if (id == null) return path;
|
||||
return `${path}${path.includes("?") ? "&" : "?"}account=${id}`;
|
||||
}
|
||||
|
||||
// Reactive active-account. setActiveAccount/clearActiveAccount write sessionStorage — a silent op that
|
||||
// triggers no React render — so this tab's per-account state (sort/filter/view/layout, all keyed by the
|
||||
// account) would never re-read once the account is finally known. That bites exactly once, on first
|
||||
// load: the provider tree above <App> mounts with NO account, then <App> pins it after /api/me resolves.
|
||||
// The account setters call notifyActiveAccountChange() so useActiveAccountId subscribers can re-hydrate.
|
||||
const accountListeners = new Set<() => void>();
|
||||
function subscribeActiveAccount(cb: () => void): () => void {
|
||||
accountListeners.add(cb);
|
||||
return () => accountListeners.delete(cb);
|
||||
}
|
||||
export function notifyActiveAccountChange(): void {
|
||||
for (const cb of [...accountListeners]) cb();
|
||||
}
|
||||
/** The current tab's active account id, as reactive state — re-renders when it is (re)pinned. The
|
||||
* snapshot is a primitive (number | null), so useSyncExternalStore's Object.is check is stable. */
|
||||
export function useActiveAccountId(): number | null {
|
||||
return useSyncExternalStore(subscribeActiveAccount, activeAccountId, activeAccountId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user