refactor(sharing): retire the coarse "may this person add" answer

`canWrite` is the grant's LEVEL and carries no scope term. Its docblock said
"May this caller ADD to that record", and the client hook republished it as
`canAdd`, so fourteen controls across measurements, medications, labs, illness
and the dashboard gated a create on a boolean that answers `true` for a WRITE
grant scoped to the document vault — a grant whose writable-section list is
empty, because the vault takes no delegated write at any level.

Nothing shipped was visibly wrong before the capture picker, because the nav
drops a section a grant does not open. That is a property of one surface, not
of the answer, and the answer is the thing many controls read.

So the coarse answer is gone from the hook, and every consumer asks
`canWriteDomain` for its own section instead: `measurements` on the two
measurement surfaces and the dashboard's empty state, `medications` on the
medication list, table, ledger, card menu and side effects, `labs` on the lab
list and biomarker detail, `illness` on the episode list. Each of those is the
section its own route declares. `canWrite` keeps its name and its wire shape —
native clients read it — and its docblock now says what it is: whether the
grant is a reading one, with the per-section lists beside it for the question a
control actually has.

The hook table gained the vault-scoped WRITE grant as its own leg: level true,
every section false. The take-all-due fixture described a grant the server
never mints — `canWrite: true` over an empty writable list — and now derives
its list from the same table the server derives it from.
This commit is contained in:
Marc-André Bombeck committed 2026-09-10 11:02:32 +02:00
1 parent b6d3f9903f
commit e7a1950f3a
25 files changed
+146 -88

No files matched your search

@@ -14,8 +14,8 @@
* path is not proven at all. That leg lives in `e2e/account-sharing.spec.ts`.
*
* Mutation checks, run:
* - `useRecordCapabilities` returning `canAdd: true` unconditionally → the
* read-only legs for the intake row and the card menu go red.
* - `useRecordCapabilities` answering `canWriteDomain` for every section →
* the read-only legs for the intake row and the card menu go red.
* - `DeleteButton` dropping its `canManageDomain` bail → "no row delete inside
* somebody else's record" goes red; answering it for every section → the
* vault leg of "a guardian's row delete follows the section" goes red.
@@ -533,9 +533,10 @@ describe("the capture picker's kinds", () => {
});
it("offers a scoped WRITE delegate only the kinds its sections cover", () => {
// The gap this closes. `canAdd` is the grant's LEVEL with no scope term,
// so a labs-scoped WRITE grant answered true and the picker offered a
// weight form and a dose the server refuses at `grantCoversDomain`.
// The gap this closes. The coarse answer was the grant's LEVEL with no
// scope term, so a labs-scoped WRITE grant read as true and the picker
// offered a weight form and a dose the server refuses at
// `grantCoversDomain`.
expect(visibleCaptureKinds(caps("write", ["labs"]), [...ALL])).toEqual([]);
expect(
visibleCaptureKinds(caps("write", ["measurements"]), [...ALL]),
@@ -14,7 +14,7 @@ vi.mock("next/navigation", () => ({
vi.mock("@/hooks/use-record-capabilities", () => ({
useRecordCapabilities: () => ({
canManage: true,
canAdd: true,
canWriteDomain: () => true,
canManageDomain: () => true,
}),
}));
+4 -3
View File
@@ -320,7 +320,8 @@ export function IllnessView() {
// v1.36.x — opening an illness entry is an admitted delegated write. Logging
// a day against one, editing it, resolving it and deleting it are not, so a
// delegate can start the record of an illness and nothing more.
const { canAdd, inSharedRecord } = useRecordCapabilities();
const { canWriteDomain, inSharedRecord } = useRecordCapabilities();
const canAddEpisode = canWriteDomain("illness");
const {
data: episodes,
isLoading,
@@ -412,7 +413,7 @@ export function IllnessView() {
)}
{/* v1.18.6 (MOD-02) — the add button reads "hinzufügen" like every
other module, not the bespoke "neue Episode". */}
{canAdd && (
{canAddEpisode && (
<Button
onClick={() => setNewOpen(true)}
className="min-h-11 sm:min-h-9"
@@ -476,7 +477,7 @@ export function IllnessView() {
description={t("illness.empty.body")}
ctaSize="lg"
action={
canAdd ? (
canAddEpisode ? (
<Button onClick={() => setNewOpen(true)}>
<Plus className="h-4 w-4" />
{t("illness.newEpisode")}
+4 -3
View File
@@ -95,7 +95,8 @@ const READINGS_PAGE_SIZE = 200;
*/
export function LabBiomarkerDetail({ biomarkerId }: { biomarkerId: string }) {
const { user } = useAuth();
const { canAdd, canManageDomain } = useRecordCapabilities();
const { canWriteDomain, canManageDomain } = useRecordCapabilities();
const canAddLab = canWriteDomain("labs");
const canManageLabs = canManageDomain("labs");
const { t, locale } = useTranslations();
const router = useRouter();
@@ -378,7 +379,7 @@ export function LabBiomarkerDetail({ biomarkerId }: { biomarkerId: string }) {
) : null}
{/* v1.36.x — entering a result is an admitted delegated write;
renaming or removing the marker is not. */}
{canAdd && (
{canAddLab && (
<Button
onClick={() => setAddOpen(true)}
// v1.18.10 (W10) — on the narrowest phones the h1 + Edit + Delete +
@@ -436,7 +437,7 @@ export function LabBiomarkerDetail({ biomarkerId }: { biomarkerId: string }) {
title={t("labs.detail.emptyTitle")}
description={t("labs.detail.emptyDescription")}
action={
canAdd ? (
canAddLab ? (
<Button onClick={() => setAddOpen(true)}>
{t("labs.addResult")}
</Button>
+3 -2
View File
@@ -93,7 +93,8 @@ function LabRangeBarSlot({ reading }: { reading: LabResultDto }) {
export function LabList({ onAddFirst }: { onAddFirst?: () => void } = {}) {
const { t } = useTranslations();
const { canAdd } = useRecordCapabilities();
const { canWriteDomain } = useRecordCapabilities();
const canAddLab = canWriteDomain("labs");
const { prefs } = useModuleListPrefs("labs");
// v1.22 — a short, factual line under each marker heading describing what the
@@ -184,7 +185,7 @@ export function LabList({ onAddFirst }: { onAddFirst?: () => void } = {}) {
title={t("labs.emptyTitle")}
description={t("labs.emptyDescription")}
action={
onAddFirst && canAdd ? (
onAddFirst && canAddLab ? (
<Button onClick={onAddFirst}>{t("labs.addFirst")}</Button>
) : undefined
}
@@ -18,7 +18,6 @@ vi.mock("@/hooks/use-record-capabilities", () => ({
useRecordCapabilities: () => ({
inSharedRecord: false,
canWrite: false,
canAdd: true,
canManage: true,
canWriteDomain: () => true,
canManageDomain: () => true,
@@ -11,7 +11,6 @@ vi.mock("@/hooks/use-record-capabilities", () => ({
useRecordCapabilities: () => ({
inSharedRecord: false,
canWrite: false,
canAdd: true,
canManage: true,
canWriteDomain: () => true,
canManageDomain: () => true,
+4 -5
View File
@@ -55,11 +55,10 @@ type CaptureKind = "measurement" | "medication" | "mood";
*
* One question per kind, asked of that kind's own section. The picker used to
* ask a coarse one as well — a closed list of kinds a WRITE grant admits,
* offered whenever `canAdd` was true — and `canAdd` is bound to the grant's
* LEVEL with no scope term at all. A WRITE grant scoped to `["labs"]`
* published `canAdd: true` and got a weight form the server refuses; one
* scoped to `["documents"]`, which takes no delegated write at any level, got
* both. `writableDomains` is already the level × scope × route-table
* offered whenever the grant could write anywhere — and that answer is the
* grant's LEVEL with no scope term at all. A WRITE grant scoped to `["labs"]`
* read as true and got a weight form the server refuses; one scoped to
* `["documents"]`, which takes no delegated write at any level, got both. `writableDomains` is already the level × scope × route-table
* intersection, so asking it per section answers both cases and the mood
* entry's MANAGE-only create as well.
*/
@@ -29,7 +29,7 @@ vi.mock("@/hooks/use-encounters", () => ({
vi.mock("@/hooks/use-record-capabilities", () => ({
useRecordCapabilities: () => ({
canManage: true,
canAdd: true,
canWriteDomain: () => true,
canManageDomain: () => true,
}),
}));
@@ -292,8 +292,9 @@ export function MeasurementList({
// v1.36.x — inside somebody else's record, only the add path survives:
// entering a reading is an admitted delegated write, editing and deleting
// one are not. The row controls are dropped rather than disabled.
const { canAdd, canManage, inSharedRecord, canWrite } =
const { canWriteDomain, canManage, inSharedRecord, canWrite } =
useRecordCapabilities();
const canAddMeasurement = canWriteDomain("measurements");
const activeRecord = user?.accountAccess?.active ?? null;
// Named only when the sentence below is owed: a delegate who may add but
// not change. In the caller's own record, and for a read-only delegate who
@@ -1068,7 +1069,7 @@ export function MeasurementList({
>
{t("measurements.emptyResetFilter")}
</Button>
) : onAddFirst && canAdd ? (
) : onAddFirst && canAddMeasurement ? (
<Button onClick={onAddFirst}>
<Plus className="h-4 w-4" />
{t("measurements.emptyAddFirst")}
@@ -36,8 +36,9 @@ export function MedicationIntakeActions({
// v1.36.x — marking a dose taken or skipped is the verb the delegation was
// written for. A read-only delegate sees the card and its schedule and no
// action row; the card is otherwise unchanged.
const { canAdd } = useRecordCapabilities();
if (!canAdd) return null;
const { canWriteDomain } = useRecordCapabilities();
const canRecordIntake = canWriteDomain("medications");
if (!canRecordIntake) return null;
return (
<div className="flex gap-2">
@@ -175,7 +175,7 @@ export function LedgerAddDialog({
// confirmation names the record, because "Taken" alone is the one
// confirmation a person acting for somebody else does not need. The
// ledger's "+ Eintrag" button that opens this dialog is gated on
// `canAdd`, so a delegate reaches it.
// `canWriteDomain("medications")`, so a delegate reaches it.
const toastOptions = intakeToastOptions({
recordName,
eventId: undefined,
@@ -173,7 +173,8 @@ export function DoseHistoryLedger({
// v1.36.x — a delegate may record a dose, which is what "add" and the
// inline "mark taken" do. Editing, deleting, pinning and un-skipping an
// existing event are the owner's, so the row menu is absent for them.
const { canAdd } = useRecordCapabilities();
const { canWriteDomain } = useRecordCapabilities();
const canAddIntake = canWriteDomain("medications");
// The record this ledger's writes land in, when it is not the caller's own.
// The two sibling intake paths in `use-medication-intake.ts` already drop
// Undo and name the record; this third one builds its own toast and missed
@@ -434,7 +435,7 @@ export function DoseHistoryLedger({
{t("medications.detail.verlauf.summaryEmpty")}
</p>
)}
{canAdd && (
{canAddIntake && (
<Button
variant="outline"
size="sm"
@@ -598,7 +599,8 @@ export function LedgerRowItem({
onUnpin: (eventId: string) => void;
}) {
const { t } = useTranslations();
const { canAdd, canManageDomain } = useRecordCapabilities();
const { canWriteDomain, canManageDomain } = useRecordCapabilities();
const canAddIntake = canWriteDomain("medications");
const canManageMedications = canManageDomain("medications");
const fmt = useFormatters();
@@ -705,7 +707,7 @@ export function LedgerRowItem({
</div>
<div className="flex shrink-0 items-center gap-1">
{showTakenButton && canAdd && (
{showTakenButton && canAddIntake && (
<Button
variant="outline"
size="sm"
@@ -734,7 +736,7 @@ export function LedgerRowItem({
items in one menu, a delegate gets skip as its own control beside
take, which is the shape the card already uses. The owner keeps it
where it was. */}
{showTakenButton && canAdd && !canManageMedications && (
{showTakenButton && canAddIntake && !canManageMedications && (
<Button
variant="outline"
size="sm"
@@ -62,9 +62,10 @@ export function MedicationCardMenu({
// medication and opening its history editor are not. When nothing in the
// menu survives, the trigger goes with it rather than opening onto an empty
// sheet. The card itself is untouched: no wash, no badge, no colour change.
const { canAdd, canManageDomain } = useRecordCapabilities();
const { canWriteDomain, canManageDomain } = useRecordCapabilities();
const canAddSideEffect = canWriteDomain("medications");
const canManageMedications = canManageDomain("medications");
const showSideEffect = onLogSideEffect != null && canAdd;
const showSideEffect = onLogSideEffect != null && canAddSideEffect;
if (!canManageMedications && !showSideEffect) return null;
return (
@@ -407,7 +407,8 @@ function MedicationTableRowItem({
const { intakeLoading, recordIntake } = useMedicationIntake({ medication });
// v1.36.x — marking a dose is admitted; a read-only delegate sees the row
// without its action pair.
const { canAdd } = useRecordCapabilities();
const { canWriteDomain } = useRecordCapabilities();
const canAddIntake = canWriteDomain("medications");
// SAME batched compliance source as the cards.
const { data: compliance } = useMedicationComplianceSummary(medication.id);
@@ -685,7 +686,7 @@ function MedicationTableRowItem({
</TableCell>
<TableCell className="text-sm">{stockCell}</TableCell>
<TableCell>
{medication.active && canAdd ? (
{medication.active && canAddIntake ? (
<div className="flex gap-1.5">
<Button
size="icon"
@@ -112,7 +112,8 @@ function entryI18nKey(entry: MedicationSideEffectEntry): string {
export function SideEffectsSection({ medicationId }: SideEffectsSectionProps) {
const { t } = useTranslations();
const { canAdd, canManageDomain } = useRecordCapabilities();
const { canWriteDomain, canManageDomain } = useRecordCapabilities();
const canAddSideEffect = canWriteDomain("medications");
const canManageMedications = canManageDomain("medications");
const fmt = useFormatters();
const queryClient = useQueryClient();
@@ -250,7 +251,7 @@ export function SideEffectsSection({ medicationId }: SideEffectsSectionProps) {
)}
{/* v1.36.x — noting a side effect is an admitted delegated write.
Removing one that is already noted is not. */}
{canAdd && (
{canAddSideEffect && (
<Button
size="sm"
variant="outline"