File size: 4,000 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Skeleton, Stack, Table } from '@mantine/core';
import type { ReactNode } from 'react';
import { ErrorAlert } from './common';
import { EmptyState } from './shell/EmptyState';

export { EmptyState };

/** Content is on its way: grey placeholder lines, announced once to screen readers ("Loading the users"). */
export function LoadingState({ label = 'Loading', rows = 3, height = 18 }: { label?: string; rows?: number; height?: number }) {
  return (
    <Stack gap="xs" role="status" aria-live="polite" data-testid="loading-state">

      <span className="fpt-sr-only">{label}</span>

      {Array.from({ length: rows }, (_, i) => <Skeleton key={i} height={height} width={i === rows - 1 ? '60%' : '100%'} radius="sm" />)}

    </Stack>
  );
}

/** The minimum a query-backed block needs, from TanStack Query (or anything shaped like it). */
export interface QueryLike { isLoading?: boolean; isPending?: boolean; error?: unknown; refetch?: () => unknown }

/**

 * One place for the four states of a block that loads data: loading, failed (with "Try again"), empty, and the content.

 * `isEmpty` decides the empty case; `empty` is what to show then (an EmptyState, usually).

 */
export function QueryState({ q, isEmpty = false, empty, loading, errorTitle, children }: {

  q: QueryLike; isEmpty?: boolean; empty?: ReactNode; loading?: ReactNode; errorTitle?: string; children: ReactNode;

}) {
  if (q.error) return <ErrorAlert error={q.error} title={errorTitle} onRetry={q.refetch ? () => void q.refetch?.() : undefined} />;
  if (q.isLoading) return <>{loading ?? <LoadingState />}</>;
  if (isEmpty) return <>{empty ?? <EmptyState title="Nothing here yet" />}</>;
  return <>{children}</>;
}

export interface Column { label: ReactNode; hidden?: boolean; width?: number | string }

/**

 * A data table with its states built in: a scroll region reachable by keyboard (named after the table), placeholder

 * rows while loading, the error with "Try again", and one explanatory row when there is nothing to show.

 * Rows are passed as children (<Table.Tr>), so each page keeps full control of its cells.

 */
export function DataTable({ label, columns, q, rowCount, empty = 'Nothing to show yet.', minWidth, className = 'fpt-table', fz, children }: {

  label: string; columns: (Column | string)[]; q?: QueryLike; rowCount: number; empty?: ReactNode; minWidth?: number;

  className?: string; fz?: 'xs' | 'sm'; children: ReactNode;

}) {
  const cols = columns.map((c) => (typeof c === 'string' ? { label: c } : c));
  const loading = !!q && (q.isLoading ?? false);
  return (
    <>

      {q?.error ? <ErrorAlert error={q.error} onRetry={q.refetch ? () => void q.refetch?.() : undefined} /> : null}

      {/* a focusable, named region: keyboard users can scroll a wide table sideways */}

      <div className="fpt-tablewrap" role="region" aria-label={label} tabIndex={0}>

        <Table aria-label={label} className={className} fz={fz} style={minWidth ? { minWidth } : undefined}>

          <Table.Thead>

            <Table.Tr>

              {cols.map((c, i) => (

                <Table.Th key={i} style={c.width ? { width: c.width } : undefined}>{c.hidden ? <span className="fpt-sr-only">{c.label}</span> : c.label}</Table.Th>

              ))}

            </Table.Tr>

          </Table.Thead>

          <Table.Tbody aria-busy={loading || undefined}>

            {loading

              ? Array.from({ length: 3 }, (_, r) => (

                <Table.Tr key={r} data-testid="table-loading">

                  {cols.map((_, i) => <Table.Td key={i}><Skeleton height={12} radius="sm" /></Table.Td>)}

                </Table.Tr>

              ))

              : rowCount === 0 && !q?.error

                ? <Table.Tr><Table.Td colSpan={cols.length} className="fpt-table-empty" data-testid="table-empty">{empty}</Table.Td></Table.Tr>

                : children}

          </Table.Tbody>

        </Table>

      </div>

    </>
  );
}