fix(forms): give sign-in, forbidden and retry-later save failures their own state (08-01)

Every non-input save failure became "unavailable" under the "review the entered
information" summary, with no sign-in button. useAppForm now reports
reauth-required, forbidden and retry-later with the failure, and
FormFailureStatus shows each one's copy and action outside ErrorSummary.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:22:58 +09:00
co-authored by Claude Opus 5.5
parent 2afee67150
commit 9894a70f04
12 changed files with 217 additions and 4 deletions
@@ -1,5 +1,7 @@
import { useId, type FormHTMLAttributes, type ReactNode } from "react";
import type { AppFailure } from "../../contracts/errors.ts";
import { TerminalErrorSurface } from "../components/async-surface.tsx";
import { TextField } from "../components/ui/text-field.ts";
import { useLocale } from "../i18n/index.ts";
import type {
@@ -78,6 +80,33 @@ export function ErrorSummary<Values extends FormValues>(props: Readonly<{
);
}
/**
* 08-01. A save that failed for a reason other than the input (sign-in,
* permission, retry later) is announced here with its own action, not in
* ErrorSummary. Pass `useAppForm().failure`.
*/
export function FormFailureStatus(props: Readonly<{
failure: AppFailure | null;
onAction?: () => void;
}>) {
const { message } = useLocale();
const { failure, onAction } = props;
if (!failure) return null;
return (
<TerminalErrorSurface
userMessageKey={failure.userMessageKey}
action={failure.action}
onAction={onAction}
// Signing in remounts the scope, so the entered values do not survive.
description={
failure.action === "reauth"
? message("form.reauthLosesInput")
: undefined
}
/>
);
}
export function FormActions(props: Readonly<{
children: ReactNode;
sticky?: boolean;
+4
View File
@@ -19,6 +19,10 @@ export type FormResultState =
| "validation-error"
| "conflict"
| "effect-unknown"
/** 08-01. Not input errors: shown by `FormFailureStatus`, never ErrorSummary. */
| "reauth-required"
| "forbidden"
| "retry-later"
| "unavailable";
export type MappedValidationFailure<Values extends FormValues> = Readonly<{
+26
View File
@@ -9,6 +9,7 @@ import {
} from "react";
import { flushSync } from "react-dom";
import type { ZodType, ZodIssue } from "zod";
import type { AppFailure } from "../../contracts/errors.ts";
import {
useLocale,
type ParameterlessMessageKey,
@@ -64,6 +65,7 @@ export function useAppForm<
const [formErrors, setFormErrors] = useState<readonly string[]>([]);
const [pending, setPending] = useState(false);
const [result, setResult] = useState<FormResultState>("idle");
const [statusFailure, setStatusFailure] = useState<AppFailure | null>(null);
const pendingRef = useRef<Promise<FormResult<Output> | null> | null>(null);
const unknownSubmissionRef = useRef<Values | null>(null);
@@ -192,6 +194,12 @@ export function useAppForm<
setFormErrors(mapped.formErrors);
setResult("validation-error");
focusFirstError(mapped.fieldErrors);
} else if (outcome.error.action === "reauth") {
// D-2. Signing in again comes first, whatever the effect.
unknownSubmissionRef.current = null;
setFormErrors([]);
setStatusFailure(outcome.error);
setResult("reauth-required");
} else if (outcome.error.effect === "MAYBE_APPLIED") {
unknownSubmissionRef.current = submittedValues;
setFormErrors([]);
@@ -204,6 +212,16 @@ export function useAppForm<
message("form.conflict"),
]);
setResult("conflict");
} else if (
outcome.error.kind === "FORBIDDEN" ||
outcome.error.action === "retry"
) {
unknownSubmissionRef.current = null;
setFormErrors([]);
setStatusFailure(outcome.error);
setResult(
outcome.error.kind === "FORBIDDEN" ? "forbidden" : "retry-later",
);
} else {
unknownSubmissionRef.current = null;
setFormErrors([message("form.unavailable")]);
@@ -317,6 +335,8 @@ export function useAppForm<
formErrors,
pending,
result,
/** The save failure behind `reauth-required`, `forbidden` or `retry-later`. */
failure: STATUS_RESULTS.has(result) ? statusFailure : null,
field,
fieldId,
focusField,
@@ -329,6 +349,12 @@ export function useAppForm<
});
}
const STATUS_RESULTS: ReadonlySet<FormResultState> = new Set([
"reauth-required",
"forbidden",
"retry-later",
]);
function issuesToFieldErrors<Values extends FormValues>(
issues: readonly ZodIssue[],
allowedFields: readonly FieldName<Values>[],