Files
HealthLog/src/lib/onboarding/load-flow-state.ts
T
Marc-André Bombeck 1c2e7c29de Add the setup flow's welcome screen and shell
The front door reads the same ledger every screen reads: a record that
never entered the flow sees the welcome, one mid-way is sent to the step
it owes, a finished one is offered the questions again. The shell prints
the counter and an ordered step list off the step machine, so it shows
exactly the screens this flow has, and every screen moves focus to its
heading on arrival.

"Skip for now" is a real exit — it stamps the account past the first-run
redirect and opens the checklist on the dashboard — and the one-time
medical disclaimer now gates it as well as "Set up", because a skip that
reached the dashboard without it would be a way round the acknowledgment.

The five-step wizard's welcome carousel, goal picker and source grid go
with it; the numbered step route follows in the next commit.
2026-09-10 15:10:50 +02:00

41 lines
1.5 KiB
TypeScript

/**
* v1.39 (C2) — the setup flow's state, for the server-rendered pages.
*
* One read per request: the page that guards a URL and the metadata that
* titles it both ask for the same row, and `cache` makes the second ask free.
* The DTO is the same one `GET /api/auth/me` publishes (`toOnboardingStateDto`
* with the account's own unit columns), so the screen the server paints and
* the screen the client reasons about after hydration are built from one
* shape.
*
* The caller's OWN record, always. The flow's three writes refuse under an
* acting-account switch, so a page that read the switched record's state would
* show questions nobody could answer.
*/
import { cache } from "react";
import { getSession } from "@/lib/auth/session";
import { prisma } from "@/lib/db";
import { readHeldUnitPreferences, type OnboardingStateDto } from "./needs";
import { loadOnboardingRecordRow, toOnboardingStateDto } from "./needs-store";
export interface OnboardingFlowPageState {
userId: string;
userLocale: string | null;
state: OnboardingStateDto;
}
export const loadOnboardingFlowState = cache(
async (): Promise<OnboardingFlowPageState | null> => {
const session = await getSession();
if (!session) return null;
const { user } = session;
const row = await loadOnboardingRecordRow(prisma, user.id);
return {
userId: user.id,
userLocale: user.locale ?? null,
state: toOnboardingStateDto(row, readHeldUnitPreferences(user)),
};
},
);