import { useState, type HTMLAttributes, type ReactNode } from 'react'; import type { Except } from 'type-fest'; import { getLipsumObjectArray } from '../mock-data/lipsum'; import { type Props as DataListProps } from '../components/data-list'; import type { Props as DataTableProps, SortableColumn, NonSortableColumn, } from '../components/data-table'; import type { WrapperProps } from '../components/data-loader'; import { ExternalLink } from '../components'; export type DataType = Record; export type CommonProps = DataListProps | DataTableProps; const Column5 = () => ( <> Column 5 FC ); const totalNumberDataPoints = 50; const getIdKey = (datum: DataType) => datum.id; export const columns: Array< SortableColumn | NonSortableColumn > = [ { label: 'Column 1', name: 'content1', tooltip: 'Some content for the tooltip', render: (row) => row.content1, sortable: true, sorted: 'descend', }, { label: 'Column 2', name: 'content2', tooltip: ( <> Some richer content for the tooltip with a{' '} link ), render: (row) => row.content2, }, { label: 'Column 3', name: 'content3', tooltip: ( <> Some richer content for the tooltip with a{' '} link ), render: (row) => row.content3, sortable: true, }, { label: 'Column 4', name: 'content4', render: (row) => row.content4, width: '30vw', sortable: true, }, { label: , name: 'content5', render: (row) => row.content5, }, ]; const generateData = (numberElements: number) => getLipsumObjectArray({ keys: columns.map((column) => column.name), numberElements, }); type Args

= { children: (props: P) => ReactNode; } & Except, 'children'>; export const DataLoaderDecorator = ({ children, ...props }: Args>) => { let loadingData = false; const numberDataPointsPerRequest = 6; const sleepDuration = 0.75; const numberInitialDataPoints = 1; const [data, setData] = useState( numberInitialDataPoints > 0 ? generateData(numberInitialDataPoints) : [] ); function onLoadMoreItems() { if (loadingData) { return null; } loadingData = true; const numberDataPoints = Math.min( numberDataPointsPerRequest, totalNumberDataPoints - data.length ); const moreData = generateData(numberDataPoints); return setTimeout(() => { loadingData = false; setData([...data, ...moreData]); }, sleepDuration * 1000); } const hasMoreData = data.length < totalNumberDataPoints; return (

{children({ data, getIdKey, columns, hasMoreData, onLoadMoreItems, })}
); }; export const DataDecorator = ({ children, ...props }: Args) => (
{children({ data: generateData(totalNumberDataPoints), getIdKey, columns, })}
);