EnergyVenom's picture
Patchwise Agent Studio: Oracle AI Agent Studio cycles
af1bee9 verified
Raw History Blame Contribute Delete
4 kB
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>
</>
);
}