feat(api): say why a module's surfaces are not there, not only that they are not

`GET /api/auth/me` publishes a boolean per module, and three different
situations collapse into the same `false`: the record has the module switched
off, the active grant's sections do not open it, or the operator switched it
off for the whole instance. A client holding the boolean has to guess between
them, so the empty state either says nothing or says something that may be
untrue -- offering a delegate a switch that is not theirs to reach, or telling
an account holder to turn on a module the operator removed.

`moduleAccess` carries the reason beside the booleans, which keep their exact
meaning: `enabled`, `disabled`, `not_granted`, `unavailable`, resolved
outside-in so the reason published is the one nothing further in can change.
`modules[key]` stays exactly `moduleAccess[key] === "enabled"` for every key,
and the two halves are built independently -- the boolean still runs through
`maskModulesToSections` -- so the test of that pairing is an assertion rather
than a restatement of a derivation. It is checked on the pure builder across a
matrix of grants, records and operator blobs, and again on the real route for a
scoped delegate, an unscoped one, and an operator-disabled module.

Both ends in one change, because a payload field with no reader is the failure
this repository keeps rediscovering. The reader is `ModuleDisabledNotice`, the
shared module-off empty state the empty-states audit asks for: it names the
reason in the reader's own language and offers the enable control only when the
record's own switch is what is off, since neither of the other two is fixable
from that page. The nutrients page adopts it and drops its bespoke block, which
had been handing every delegate a button that would refuse them.

Registered in the OpenAPI registry as `ModuleAccessMap` and carried on the
account payload; the native contract gains a field and loses nothing, since a
Bearer request carries no switch and never reads `not_granted`.
This commit is contained in:
Marc-André Bombeck committed 2026-09-10 04:41:38 +02:00
1 parent 0c46df1413
commit 47a5079cfa
17 files changed
+859 -21

No files matched your search

+25
View File
@@ -22,6 +22,7 @@ import { isTimeFormatPreference, storeTimeFormat } from "@/lib/time-format";
import { isDateFormatPreference, storeDateFormat } from "@/lib/date-format";
import { storeTimezone } from "@/lib/timezone-mirror";
import type { ModuleKey } from "@/lib/modules/registry";
import type { ModuleAccessState } from "@/lib/sharing/module-disclosure";
import type { TourProgress } from "@/lib/onboarding/tour-progress";
import { clearOfflineCachesForSessionEnd } from "@/lib/pwa/query-persister";
import {
@@ -196,6 +197,22 @@ export interface AuthUser {
* payload so a missing map reads as all-available.
*/
moduleAvailability?: Partial<Record<ModuleKey, boolean>>;
/**
* Why each module is or is not available for the record this browser is
* inside — `"enabled"`, `"disabled"` (the record has it switched off),
* `"not_granted"` (the active grant does not open the module's section) or
* `"unavailable"` (the operator switched it off for the whole instance).
* Precedence outside-in: unavailable > not_granted > disabled > enabled.
*
* Additive beside `modules`, never instead of it: `modules[key]` is exactly
* `moduleAccess[key] === "enabled"`, so every gate keeps reading the boolean
* and only the surfaces that have to EXPLAIN a missing module read this. A
* client holding the boolean alone has to guess between three different
* reasons, which is how an empty state ends up offering a delegate a switch
* they cannot reach. Coerced to `{}` against a stale /me payload, and a
* missing key falls back to the boolean.
*/
moduleAccess?: Partial<Record<ModuleKey, ModuleAccessState>>;
/**
* v1.36.0 — account sharing, resolved server-side. `accounts` is the
* switcher's menu, `active` is the record this browser is inside (null when
@@ -389,6 +406,14 @@ export async function fetchMe(): Promise<AuthUser> {
data.moduleAvailability && typeof data.moduleAvailability === "object"
? data.moduleAvailability
: {},
// The reason behind each module's boolean. Coerced against a stale /me
// payload to an empty map; `useModuleAccess` then falls back to the
// boolean, so an older server image reads "disabled" rather than
// inventing a reason it was never told.
moduleAccess:
data.moduleAccess && typeof data.moduleAccess === "object"
? data.moduleAccess
: {},
accountAccess,
accountAccessStatus,
};