umami/src/components/common/DataTable.tsx

93 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-09-29 12:29:22 +00:00
import { ReactNode, Dispatch, SetStateAction } from 'react';
import classNames from 'classnames';
import { Banner, Loading, SearchField } from 'react-basics';
2023-08-25 18:54:44 +00:00
import { useMessages } from 'components/hooks';
import Empty from 'components/common/Empty';
import Pager from 'components/common/Pager';
import styles from './DataTable.module.css';
const DEFAULT_SEARCH_DELAY = 600;
2023-08-25 18:54:44 +00:00
2023-09-29 12:29:22 +00:00
export interface DataTableProps {
2023-10-04 08:46:00 +00:00
queryResult: {
result: {
page: number;
pageSize: number;
count: number;
data: any[];
};
params: {
query: string;
page: number;
};
setParams: Dispatch<SetStateAction<{ query: string; page: number }>>;
isLoading: boolean;
error: unknown;
2023-09-29 12:29:22 +00:00
};
searchDelay?: number;
2023-10-08 05:58:46 +00:00
allowSearch?: boolean;
allowPaging?: boolean;
2023-09-29 12:29:22 +00:00
children: ReactNode | ((data: any) => ReactNode);
}
2023-08-25 18:54:44 +00:00
export function DataTable({
2023-10-04 08:46:00 +00:00
queryResult,
searchDelay = 600,
2023-10-08 05:58:46 +00:00
allowSearch = true,
allowPaging = true,
2023-08-25 18:54:44 +00:00
children,
2023-09-29 12:29:22 +00:00
}: DataTableProps) {
2023-08-25 18:54:44 +00:00
const { formatMessage, labels, messages } = useMessages();
2023-10-04 08:46:00 +00:00
const { result, error, isLoading, params, setParams } = queryResult || {};
const { page, pageSize, count, data } = result || {};
const { query } = params || {};
const hasData = Boolean(!isLoading && data?.length);
const noResults = Boolean(!isLoading && query && !hasData);
2023-08-25 18:54:44 +00:00
const handleSearch = query => {
2023-10-15 22:52:34 +00:00
setParams({ ...params, query, page: params.page ? page : 1 });
2023-08-25 18:54:44 +00:00
};
const handlePageChange = page => {
2023-10-15 22:52:34 +00:00
setParams({ ...params, query, page });
2023-08-25 18:54:44 +00:00
};
if (error) {
return <Banner variant="error">{formatMessage(messages.error)}</Banner>;
}
2023-08-25 18:54:44 +00:00
return (
<>
2023-10-08 05:58:46 +00:00
{allowSearch && (hasData || query) && (
2023-08-25 18:54:44 +00:00
<SearchField
className={styles.search}
value={query}
onChange={handleSearch}
delay={searchDelay || DEFAULT_SEARCH_DELAY}
autoFocus={true}
placeholder={formatMessage(labels.search)}
/>
)}
<div
className={classNames(styles.body, { [styles.status]: isLoading || noResults || !hasData })}
>
2023-09-29 12:29:22 +00:00
{hasData ? (typeof children === 'function' ? children(result) : children) : null}
{isLoading && <Loading icon="dots" />}
2023-10-08 05:58:46 +00:00
{!isLoading && !hasData && !query && <Empty />}
{noResults && <Empty message={formatMessage(messages.noResultsFound)} />}
</div>
2023-10-08 05:58:46 +00:00
{allowPaging && hasData && (
2023-08-25 18:54:44 +00:00
<Pager
className={styles.pager}
page={page}
pageSize={pageSize}
count={count}
onPageChange={handlePageChange}
/>
)}
</>
2023-08-25 18:54:44 +00:00
);
}
export default DataTable;