# ErrorState

> A view that failed to load, what to do about it, a retry that can count down, and a tracking code.

- Import: `import { ErrorState } from '@nyxon/nasim';`
- Server Component: no, it needs a client component
- Since: 0.1.0; status: stable
- Source: libs/nasim/src/composites/ErrorState.tsx
- Describes: @nyxon/nasim unreleased, after 0.12.0
- Page: https://nasim.nyxon.tech/docs/components/feedback/error-state

<Example name="error-state/retry">
  ```tsx title="error-state/retry.tsx"
  '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)} />;
  }
  ```
</Example>

## Import [#import]

```tsx
import { ErrorState } from '@nyxon/nasim';
```

## Usage [#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.

```tsx
<ErrorState
  onRetry={refetch}
  supportHref={supportUrl}
  requestId={error.requestId}
/>
```

## Examples [#examples]

### Retry [#retry]

Shown above: the defaults, with the retry as the one action.

### Come back later [#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.

<Example name="error-state/rate-limited">
  ```tsx title="error-state/rate-limited.tsx"
  '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}
      />
    );
  }
  ```
</Example>

## Rules [#rules]

* Say what happened and what to do, in the screen's words when the defaults are too vague.
* Pass `requestId` whenever the API gives one: it is what support asks for.

## Accessibility [#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 [#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 [#related]

`EmptyState` (nothing to show) and `Notice` (a failure inside a view that still works).