import { Box, Pagination, Table, Tile } from '@rocket.chat/fuselage'; import { useDebouncedValue } from '@rocket.chat/fuselage-hooks'; import React, { useState, useEffect, useCallback, forwardRef, ReactNode, ReactElement, Key, RefAttributes, } from 'react'; import flattenChildren from 'react-keyed-flatten-children'; import { useTranslation } from '../../contexts/TranslationContext'; import ScrollableContentWrapper from '../ScrollableContentWrapper'; import LoadingRow from './LoadingRow'; const defaultParamsValue = { text: '', current: 0, itemsPerPage: 25 } as const; const defaultSetParamsValue = (): void => undefined; type Params = { text?: string; current?: number; itemsPerPage?: 25 | 50 | 100 }; type GenericTableProps< FilterProps extends { onChange?: (params: Params) => void }, ResultProps extends { _id?: Key }, > = { fixed?: boolean; header?: ReactNode; params?: Params; setParams?: (params: Params) => void; children?: (props: ResultProps, key: number) => ReactElement; renderFilter?: (props: FilterProps) => ReactElement; renderRow?: (props: ResultProps) => ReactElement; results?: ResultProps[]; total?: number; pagination?: boolean; } & FilterProps; const GenericTable: { void }, ResultProps extends { _id?: Key }>( props: GenericTableProps & RefAttributes, ): ReactElement | null; } = forwardRef(function GenericTable( { children, fixed = true, header, params: paramsDefault = defaultParamsValue, setParams = defaultSetParamsValue, renderFilter, renderRow: RenderRow, results, total, pagination = true, ...props }, ref, ) { const t = useTranslation(); const [filter, setFilter] = useState(paramsDefault); const [itemsPerPage, setItemsPerPage] = useState<25 | 50 | 100>(25); const [current, setCurrent] = useState(0); const params = useDebouncedValue(filter, 500); useEffect(() => { setParams({ ...params, current, itemsPerPage }); }, [params, current, itemsPerPage, setParams]); const Loading = useCallback(() => { const headerCells = flattenChildren(header); return ( <> {Array.from({ length: 10 }, (_, i) => ( ))} ); }, [header]); const showingResultsLabel = useCallback( ({ count, current, itemsPerPage }) => t('Showing_results_of', current + 1, Math.min(current + itemsPerPage, count), count), [t], ); const itemsPerPageLabel = useCallback(() => t('Items_per_page:'), [t]); return ( <> {typeof renderFilter === 'function' ? renderFilter({ ...props, onChange: setFilter } as any) // TODO: ugh : null} {results && !results.length ? ( {t('No_data_found')} ) : ( <> {header && ( {header} )} {RenderRow && (results ? ( results.map((props, index) => ( )) ) : ( ))} {children && (results ? results.map(children) : )}
{pagination && ( )} )} ); }); export default GenericTable;