fix: preserve sharing failure states

Accepting, revoking and handing back a grant had no failure rendering at all.
The request came back refused, or never left the machine, and the panel did
nothing: the confirm dialog had already closed, the list did not change, and
the only signal was the absence of one. The two refusals that most need a
reader were the two that arrived most quietly — an invitation that had lapsed
before it was opened, and the last guardian being told they may not hand back
a profile that would then have nobody looking after it.

Each refusal now renders on the row it belongs to, announced, in content
contrast, beside a control that runs the same act again. The sentences are
split by what the person does next rather than by status code, so lapsed and
withdrawn read differently and the guardian refusal names the way out. A
request that never reached the server says so instead of reporting a refusal
nobody issued.

The failed state is read off the mutation rather than copied into local state
from onError: the copy is what goes stale, and the mutation already knows
which row it was for. The invitation form keeps every field it had, and only
a successful send clears them.
This commit is contained in:
Marc-André Bombeck committed 2026-08-06 23:39:39 +02:00
1 parent c92a80ee4c
commit 71ad8d66b6
11 files changed
+765 -10

No files matched your search

@@ -0,0 +1,143 @@
"use client";
import { Button } from "@/components/ui/button";
import { ApiError } from "@/lib/api/api-fetch";
import { useTranslations } from "@/lib/i18n/context";
/**
* v1.37.0 — what a refused accept, revoke or renounce says, and where it says
* it.
*
* ## The state this replaces
*
* These three mutations had no failure rendering at all. They fired, the
* server answered 409 or 410, or the request never left the machine, and the
* panel did nothing — the confirm dialog had already closed, the list did not
* change, and the only signal was the absence of one. The two refusals that
* matter most were the two that arrived most quietly: an invitation that had
* lapsed, and the last Guardian being refused a renunciation that would leave
* a managed profile with nobody looking after it. Both are recoverable, and
* neither is recoverable by somebody who was not told.
*
* ## Every arm names its own recovery
*
* A refusal is only worth rendering if it changes what the person does next,
* so the sentences are split by what the next step is rather than by status
* code. "Lapsed" leads to asking for a new invitation; "withdrawn" leads
* nowhere and says so; the last-Guardian refusal names the two ways out the
* lifecycle actually offers. The offline arm is the one where retrying IS the
* recovery, so it is separated from a server refusal even though both surface
* as a failed mutation: a request that never left the machine changed nothing,
* and telling somebody their access "could not be ended" when it was never
* asked is a different and worse sentence.
*
* `ApiError` carries the server's stable `meta.errorCode`; anything that is
* not an `ApiError` never reached a response at all, which is what makes the
* offline test a type check rather than a status check.
*/
export type GrantAction = "accept" | "revoke" | "renounce";
/** The generic sentence for each act, when nothing more specific applies. */
const FAILED_KEY: Record<GrantAction, string> = {
accept: "recordSharing.actionError.acceptFailed",
revoke: "recordSharing.actionError.revokeFailed",
renounce: "recordSharing.actionError.renounceFailed",
};
/**
* The message key for a refused grant action.
*
* Pure and exported so the mapping can be pinned without a click: the panel's
* error states are otherwise only reachable by driving a browser, and a
* mapping nobody can test is a mapping that drifts.
*/
export function grantActionErrorKey(err: unknown, action: GrantAction): string {
// Not an `ApiError` means no response came back — a dropped connection, a
// sleeping laptop, a server that is not there. Nothing was decided, so the
// sentence says so and points at the retry rather than reporting a refusal
// the server never issued.
if (!(err instanceof ApiError)) return "recordSharing.actionError.offline";
const code =
typeof err.meta?.errorCode === "string" ? err.meta.errorCode : null;
switch (code) {
// The one refusal that protects a person rather than a row. It reaches
// both sides — the owner withdrawing the last Guardian's access and the
// last Guardian handing it back — and it is the same fact either way, so
// it is the same sentence.
case "managed_profile.guardian.required":
return "recordSharing.actionError.lastGuardian";
case "sharing.accept.expired":
return "recordSharing.actionError.acceptExpired";
case "sharing.accept.revoked":
return "recordSharing.actionError.acceptWithdrawn";
case "sharing.accept.not_pending":
return "recordSharing.actionError.acceptNotPending";
case "sharing.accept.not_found":
return "recordSharing.actionError.acceptNotFound";
case "sharing.revoke.already_ended":
case "sharing.renounce.already_ended":
return "recordSharing.actionError.alreadyEnded";
case "sharing.revoke.not_found":
case "sharing.renounce.not_found":
return "recordSharing.actionError.notFound";
default:
break;
}
// Waiting is the recovery, so it must not read as a failure. The accept
// route rate-limits without an error code, hence the status.
if (err.status === 429) return "recordSharing.actionError.rateLimited";
return FAILED_KEY[action];
}
/**
* The refusal, on the row it belongs to.
*
* `role="alert"` because it appears after the act rather than with the page:
* a person who pressed a button and got nothing has already looked away, and
* a screen reader that does not announce it is the same silence the panel had
* before. `text-destructive` because this is content somebody has to act on —
* UI-STANDARDS §3 reserves muted for meta, and a refusal rendered as meta is a
* refusal nobody reads.
*
* `data-grant-error-for` carries the row id so a panel holding two failed rows
* cannot show one row's message against another's, and so a browser journey
* can assert which row was refused rather than that something somewhere went
* red.
*/
export function GrantActionAlert({
grantId,
message,
onRetry,
retrying = false,
}: {
grantId: string;
message: string;
onRetry: () => void;
retrying?: boolean;
}) {
const { t } = useTranslations();
return (
<div
data-slot="grant-action-error"
data-grant-error-for={grantId}
role="alert"
className="text-destructive flex flex-wrap items-center gap-2 text-sm"
>
<span>{message}</span>
<Button
type="button"
size="sm"
variant="outline"
data-slot="grant-action-retry"
disabled={retrying}
onClick={onRetry}
>
{t("recordSharing.actionError.retry")}
</Button>
</div>
);
}
@@ -654,10 +654,15 @@ function useInvitationOwnerTimezone(): string {
* field" and "you already have your own record".
*/
export function inviteErrorKey(err: unknown): string {
// Nothing that is not an `ApiError` ever reached a response, so no
// invitation was refused — the request did not arrive. Said as its own case
// because "the invitation could not be sent" invites somebody to change the
// form, and the form is fine. The typed identifier, the level and the ticked
// sections all survive a failed mutation, so the retry is the same button.
if (!(err instanceof ApiError)) return "recordSharing.invite.errorOffline";
const code =
err instanceof ApiError && typeof err.meta?.errorCode === "string"
? err.meta.errorCode
: null;
typeof err.meta?.errorCode === "string" ? err.meta.errorCode : null;
switch (code) {
case "sharing.invite.self":
return "recordSharing.invite.errorSelf";
@@ -2,6 +2,10 @@
import { Loader2, Users } from "lucide-react";
import {
GrantActionAlert,
grantActionErrorKey,
} from "@/components/settings/access/grant-action-error";
import { GrantRowItem } from "@/components/settings/access/grant-row";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
@@ -75,6 +79,7 @@ export function GrantsGivenCard() {
key={grant.id}
grant={grant}
side="given"
notice={renderFailure(grant)}
actions={renderRevoke(grant)}
/>
))}
@@ -84,6 +89,28 @@ export function GrantsGivenCard() {
</SettingsCard>
);
/**
* A refused revoke, on the row it was refused for.
*
* The confirm dialog has already closed by the time the server answers, so
* without this a refusal is invisible: the list looks the same, and the one
* refusal that matters — a managed profile that would be left with nobody
* looking after it — arrived as silence. Read off the mutation rather than
* copied into local state, so `variables` is what decides which row wears
* it.
*/
function renderFailure(grant: GrantRow) {
if (!revoke.isError || revoke.variables !== grant.id) return null;
return (
<GrantActionAlert
grantId={grant.id}
message={t(grantActionErrorKey(revoke.error, "revoke"))}
retrying={revoke.isPending}
onRetry={() => revoke.mutate(grant.id)}
/>
);
}
function renderRevoke(grant: GrantRow) {
// Only a live grant can be ended. An already-ended row keeps its history
// and loses its button, rather than offering an act that would 409.
@@ -2,6 +2,11 @@
import { FolderOpen, Loader2 } from "lucide-react";
import {
GrantActionAlert,
grantActionErrorKey,
type GrantAction,
} from "@/components/settings/access/grant-action-error";
import { GrantRowItem } from "@/components/settings/access/grant-row";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
@@ -34,6 +39,18 @@ import {
* at the row and should not have to go hunting for the switcher.
* * **Hand back.** The delegate's own way out, attributed as theirs so the
* owner's record can tell "I withdrew it" from "he gave it back".
*
* v1.37.0 — both acts can now be refused in ways the person can do something
* about, and both used to fail in silence. An invitation that lapsed before it
* was opened, and the last Guardian being told they may not hand back a
* profile that would then have nobody looking after it. The refusal renders on
* its own row, announced, with the row's control still there.
*
* The failed state is read off the mutation rather than copied into a
* `useState` from `onError`. TanStack keeps `isError` / `error` / `variables`
* until something resets it, and `variables` is what says which row failed — a
* local copy would be a second version of that fact, and it is the copy that
* goes stale when an unrelated invalidation re-renders the panel.
*/
export function GrantsReceivedCard() {
const { t } = useTranslations();
@@ -78,7 +95,12 @@ export function GrantsReceivedCard() {
key={grant.id}
grant={grant}
side="received"
notice={renderConsent(grant)}
notice={
<>
{renderConsent(grant)}
{renderFailure(grant)}
</>
}
actions={renderActions(grant)}
/>
))}
@@ -88,6 +110,52 @@ export function GrantsReceivedCard() {
</SettingsCard>
);
/**
* The refusal this row is carrying, if it is the row that was refused.
*
* Guarded on `variables` rather than rendered under the list, because a
* panel with two pending invitations would otherwise show one row's failure
* with no way to tell which act it belonged to.
*/
function renderFailure(grant: GrantRow) {
const failure = failedAction(grant.id);
if (!failure) return null;
return (
<GrantActionAlert
grantId={grant.id}
message={t(grantActionErrorKey(failure.error, failure.action))}
retrying={failure.pending}
onRetry={failure.retry}
/>
);
}
/** Which of the two acts failed on this row, and how to run it again. */
function failedAction(grantId: string): {
action: GrantAction;
error: unknown;
pending: boolean;
retry: () => void;
} | null {
if (accept.isError && accept.variables === grantId) {
return {
action: "accept",
error: accept.error,
pending: accept.isPending,
retry: () => accept.mutate(grantId),
};
}
if (renounce.isError && renounce.variables === grantId) {
return {
action: "renounce",
error: renounce.error,
pending: renounce.isPending,
retry: () => renounce.mutate(grantId),
};
}
return null;
}
/**
* The delegate's half of the consent, on the screen where they give it.
*