ErrorState
A view that failed to load, what to do about it, a retry that can count down, and a tracking code.
'use client';
import { ErrorState } from '@nyxon/nasim';
import { useState } from 'react';
const copy = { loaded: 'دوباره بارگذاری شد.' };
// The defaults: «بارگذاری انجام نشد» and a connection hint. The retry is the one action.
export default function Retry() {
const [retried, setRetried] = useState(false);
if (retried) return <p className="text-center text-muted">{copy.loaded}</p>;
return <ErrorState onRetry={() => setRetried(true)} />;
}Import
import { ErrorState } from '@nyxon/nasim';Usage
With no props it says «بارگذاری انجام نشد» and a connection hint; pass title and message for a
specific case. onRetry adds the retry button, supportHref a link to support beside it, and
requestId a tracking code under them, so support can find the failure. Children are detail under the
message (the fields a server refused), announced with it. It is a client component.
<ErrorState
onRetry={refetch}
supportHref={supportUrl}
requestId={error.requestId}
/>Examples
Retry
Shown above: the defaults, with the retry as the one action.
Come back later
retryIn (seconds) keeps the retry disabled and counts down, for a server that said when to come back
(a 429 with Retry-After). A new retryIn starts the count again.
'use client';
import { ErrorState } from '@nyxon/nasim';
const copy = {
title: 'درخواستها زیاد بود',
message: 'چند لحظه صبر کنید؛ دکمهٔ تلاش دوباره خودش باز میشود.',
};
// Sample values: a real request id and support link come from the API error and the app's config.
const error = { requestId: 'req-7f3a-1042', retryAfter: 30 };
const support = 'https://t.me/example_support';
// A server that said when to come back (a 429 with Retry-After): the button counts down.
export default function RateLimited() {
return (
<ErrorState
title={copy.title}
message={copy.message}
onRetry={() => {}}
retryIn={error.retryAfter}
supportHref={support}
requestId={error.requestId}
/>
);
}Rules
- Say what happened and what to do, in the screen's words when the defaults are too vague.
- Pass
requestIdwhenever the API gives one: it is what support asks for.
Accessibility
What happened (the title, the message and the children) is a role="alert" region, announced when it
appears; the buttons are outside it. The title is an h2; pass as="h1" when the state is the whole page,
and titleRef to move the focus to it, as for EmptyState. The tracking code is isolated (<bdi>) in
monospace, so it reads in order inside a right-to-left line.
API
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | — | Defaults to «بارگذاری انجام نشد». |
message | string | — | What went wrong and what to do; defaults to a connection hint. |
onRetry | (() => void) | — | |
retryIn | number | — | Seconds before the retry works, e.g. a 429's Retry-After: the button counts them down. |
supportHref | string | — | A support link (Telegram or in-app); shown next to the retry button. |
requestId | string | — | The request id from the API error, so support can find the failure. |
children | ReactNode | — | Detail under the message (the fields the server refused), announced with it. |
as | "h1" | "h2" | — | Heading level: "h1" when the state is the whole page, "h2" (default) inside one. |
titleRef | Ref<HTMLHeadingElement> | — | A ref to the title, which takes focus when a screen swaps this state in. |
Related
EmptyState (nothing to show) and Notice (a failure inside a view that still works).