# Bloco: Vazio, erro e carregando

Os quatro finais de uma listagem na mesma tela: primeira vez, filtro sem resultado, erro com nova tentativa e carregando.

Uma tela inteira, montada só com peças do @rivocode/ui e as regras da skill. Copie o arquivo, troque os dados de exemplo pelos da sua consulta e mantenha os quatro finais: dados, carregando, erro e vazio. Ele importa só de `@rivocode/ui`, `@rivocode/ui/illustrations`, `lucide-react` e `react`.

Peças: [EmptyState](https://ds.rivocode.com.br/componentes/empty-state.md), [Illustration](https://ds.rivocode.com.br/componentes/illustration.md), [DataTable](https://ds.rivocode.com.br/componentes/data-table.md), [ToggleGroup](https://ds.rivocode.com.br/componentes/toggle-group.md), [PageHeader](https://ds.rivocode.com.br/componentes/page-header.md).

Padrões de UX que ele aplica: [Espera](https://ds.rivocode.com.br/padroes/espera.md), [O que fazer quando dá errado](https://ds.rivocode.com.br/padroes/erros.md).

Ao vivo, no desktop e no celular: https://ds.rivocode.com.br/blocos#vazio-e-erro

## Código

```tsx
import {
  Badge,
  Button,
  Card,
  DataTable,
  EmptyState,
  PageHeader,
  Toggle,
  ToggleGroup,
  currency,
  type Column,
} from '@rivocode/ui'
import { EmptyList } from '@rivocode/ui/illustrations'
import { Plus, SearchX } from 'lucide-react'
import { useState } from 'react'

type Order = { id: string; customer: string; amount: number; shipped: boolean }

type Situation = 'first-run' | 'filtered' | 'error' | 'loading' | 'data'

const SITUATIONS: Array<{ value: Situation; label: string }> = [
  { value: 'first-run', label: 'Primeira vez' },
  { value: 'filtered', label: 'Filtro sem resultado' },
  { value: 'error', label: 'Erro' },
  { value: 'loading', label: 'Carregando' },
  { value: 'data', label: 'Com dados' },
]

const ORDERS: Order[] = [
  { id: '20481', customer: 'Marina Duarte', amount: 189.9, shipped: false },
  { id: '20480', customer: 'Rafael Nogueira', amount: 64.5, shipped: true },
  { id: '20479', customer: 'Beatriz Lima', amount: 412, shipped: true },
]

const COLUMNS: Column<Order>[] = [
  { key: 'id', header: 'Pedido', cell: (order) => <span className="tabular-nums">#{order.id}</span> },
  { key: 'customer', header: 'Cliente' },
  {
    key: 'amount',
    header: 'Total',
    align: 'right',
    cell: (order) => <span className="tabular-nums">{currency(order.amount)}</span>,
  },
  {
    key: 'shipped',
    header: 'Situação',
    hideOnMobile: true,
    cell: (order) => (
      <Badge tone={order.shipped ? 'success' : 'info'} size="sm">
        {order.shipped ? 'Enviado' : 'Separando'}
      </Badge>
    ),
  },
]

export default function OrderStatesPage() {
  const [situation, setSituation] = useState<Situation>('first-run')

  return (
    <div className="w-full space-y-4 bg-bg p-4 sm:p-6">
      <PageHeader
        title="Pedidos"
        description="Toda listagem tem quatro finais: dados, carregando, erro e vazio."
        actions={
          situation === 'first-run' ? undefined : (
            <Button>
              <Plus size={16} aria-hidden="true" />
              Novo pedido
            </Button>
          )
        }
      />

      <ToggleGroup
        aria-label="Ver a listagem no estado"
        value={[situation]}
        onValueChange={(value) => {
          const next = value[0] as Situation | undefined
          if (next) setSituation(next)
        }}
        className="flex-wrap"
      >
        {SITUATIONS.map((option) => (
          <Toggle key={option.value} value={option.value}>
            {option.label}
          </Toggle>
        ))}
      </ToggleGroup>

      {situation === 'first-run' ? (
        <Card>
          <EmptyState
            illustration={<EmptyList />}
            title="Nenhum pedido por aqui"
            description="Quando o primeiro cliente comprar, o pedido aparece nesta lista, com a situação da entrega."
            action={
              <Button size="sm">
                <Plus size={14} aria-hidden="true" />
                Cadastrar um pedido
              </Button>
            }
          />
        </Card>
      ) : (
        <DataTable
          data={situation === 'data' ? ORDERS : situation === 'filtered' ? [] : undefined}
          isLoading={situation === 'loading'}
          isError={situation === 'error'}
          onRetry={() => setSituation('loading')}
          errorTitle="Não foi possível carregar os pedidos"
          errorMessage="O servidor não respondeu. Tente de novo em alguns instantes."
          labels={{ retry: 'Tentar de novo' }}
          columns={COLUMNS}
          rowKey={(order) => order.id}
          caption="Pedidos de agosto"
          skeletonRows={3}
          empty={{
            icon: <SearchX aria-hidden="true" />,
            title: 'Nenhum pedido em separação em agosto',
            description:
              'O filtro está em "separando" e "agosto". Mude o período para ver os outros meses.',
            action: (
              <Button variant="secondary" size="sm" onClick={() => setSituation('data')}>
                Limpar filtros
              </Button>
            ),
          }}
        />
      )}
    </div>
  )
}
```
