Files
HealthLog/src/components/insights/overview-section-boundary.tsx
T
Marc-André Bombeck 3cf67d05f9 fix(ui): let the read-failure primitives decide by the error
About forty compact rows and seventy cards still offered a retry on a
spent read budget and drew an error card on a refusal, while a third
primitive (ReadFailureRow) did the right thing at eight places.

QueryErrorRow and QueryErrorCard now take the query's `error` and read
it through readFailureKind, which moves to src/lib/api next to
ApiError: a refusal (403) draws the surface's refused state or nothing,
a spent budget (429) says so without a retry, an ended session (401)
says so and offers sign-in instead of going blank, and only a real
failure offers the retry. ReadFailureRow is gone and every site passes
its error; a guard test fails on a site that does not.

The coach read strip keeps its card in the error state and holds one
body height across loading, failure and a one-line read.
2026-10-20 03:38:57 +02:00

90 lines
2.5 KiB
TypeScript

"use client";
import { Component, type ReactNode } from "react";
import { QueryErrorRow } from "@/components/ui/query-error-row";
import { useTranslations } from "@/lib/i18n/context";
/**
* One Insights overview section, fenced.
*
* The overview is a stack of independent sections, each with its own read.
* A section that fails to load already shows its own error row; this fence
* covers the other failure, a section that throws while rendering (a payload
* shape it did not expect, a chunk that no longer exists after a deploy).
* Without it the throw reaches the route boundary and replaces the whole
* page, so one broken card takes the hero, the scores and every other
* section down with it. Inside the fence the failed section becomes one row
* with a retry, and the rest of the page stays.
*
* Retry clears the fence and renders the section again; a section whose
* failure was transient comes back, one whose failure is permanent shows the
* row again.
*/
interface FenceProps {
sectionId: string;
message: string;
retryLabel: string;
children: ReactNode;
}
interface FenceState {
failed: boolean;
error?: unknown;
}
class SectionFence extends Component<FenceProps, FenceState> {
state: FenceState = { failed: false };
static getDerivedStateFromError(error: unknown): FenceState {
return { failed: true, error };
}
componentDidCatch(error: Error): void {
// Same hand-off the chart boundary uses: the section degrades quietly,
// the failure still reaches the client error tracker.
if (typeof window !== "undefined") {
const g = window as typeof window & {
__healthlog_onError?: (err: Error) => void;
};
g.__healthlog_onError?.(error);
}
}
render(): ReactNode {
if (!this.state.failed) return this.props.children;
return (
<section
data-slot="insights-overview-section-error"
data-section={this.props.sectionId}
>
<QueryErrorRow
error={this.state.error}
message={this.props.message}
retryLabel={this.props.retryLabel}
onRetry={() => this.setState({ failed: false, error: undefined })}
/>
</section>
);
}
}
export function OverviewSectionBoundary({
sectionId,
children,
}: {
sectionId: string;
children: ReactNode;
}) {
const { t } = useTranslations();
return (
<SectionFence
sectionId={sectionId}
message={t("insights.sectionLoadError")}
retryLabel={t("common.retry")}
>
{children}
</SectionFence>
);
}