import React, { type ReactNode } from 'react';
import { TableSkeleton } from './TableSkeleton';
import { type TableContainerProps } from './types';
/**
* State machine wrapper that selects the correct content to render based
* on loading, error, and data conditions.
*
* Resolution order:
* 1. `!loaded` → `skeleton` (defaults to ``)
* 2. `loadError` → error message
* 3. `unfilteredData.length === 0` → `noDataState` (no resources exist)
* 4. `data.length === 0` → `emptyState` (resources exist but filters match nothing)
* 5. Otherwise → `children` (the table)
*
* The `toolbar` is always rendered above the content regardless of state.
*
* @example
* ```tsx
* }
* noDataState={No pipeline runs yet}
* toolbar={}
* >
*
*
* ```
*/
export const TableContainer: React.FC> = ({
data,
unfilteredData,
loaded,
loadError,
skeleton,
emptyState,
noDataState,
toolbar,
children,
}) => {
const renderContent = (): ReactNode => {
if (!loaded) {
return skeleton ?? ;
}
if (loadError) {
return {loadError.message}
;
}
if (unfilteredData.length === 0) {
return noDataState ?? null;
}
if (data.length === 0) {
return emptyState ?? null;
}
return children;
};
return (
{toolbar}
{renderContent()}
);
};