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 (
{label}
{Array.from({ length: rows }, (_, i) => )}
);
}
/** 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 void q.refetch?.() : undefined} />;
if (q.isLoading) return <>{loading ?? }>;
if (isEmpty) return <>{empty ?? }>;
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 (), 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 ? void q.refetch?.() : undefined} /> : null}
{/* a focusable, named region: keyboard users can scroll a wide table sideways */}
{cols.map((c, i) => (
{c.hidden ? {c.label} : c.label}
))}
{loading
? Array.from({ length: 3 }, (_, r) => (
{cols.map((_, i) => )}
))
: rowCount === 0 && !q?.error
? {empty}
: children}
>
);
}