import { ReactNode, useMemo, useState } from 'react'; import { Button, Column, Icon, Row, SearchField, Text, Grid } from '@umami/react-zen'; import { LinkButton } from '@/components/common/LinkButton'; import { LoadingPanel } from '@/components/common/LoadingPanel'; import { useFormat, useMessages, useNavigation, useWebsiteExpandedMetricsQuery, useWebsiteMetricsQuery, } from '@/components/hooks'; import { Close, Maximize } from '@/components/icons'; import { DownloadButton } from '@/components/input/DownloadButton'; import { DEFAULT_ANIMATION_DURATION } from '@/lib/constants'; import { percentFilter } from '@/lib/filters'; import { ListExpandedTable, ListExpandedTableProps } from './ListExpandedTable'; import { ListTable, ListTableProps } from './ListTable'; export interface MetricsTableProps extends ListTableProps { websiteId: string; type?: string; dataFilter?: (data: any) => any; limit?: number; delay?: number; onSearch?: (search: string) => void; allowSearch?: boolean; searchFormattedValues?: boolean; showMore?: boolean; params?: { [key: string]: any }; allowDownload?: boolean; isExpanded?: boolean; onClose?: () => void; children?: ReactNode; } export function MetricsTable({ websiteId, type, dataFilter, limit, delay = null, allowSearch = false, searchFormattedValues = false, showMore = true, params, allowDownload = true, isExpanded = false, onClose, children, ...props }: MetricsTableProps) { const [search, setSearch] = useState(''); const { formatValue } = useFormat(); const { updateParams } = useNavigation(); const { formatMessage, labels } = useMessages(); const expandedQuery = useWebsiteExpandedMetricsQuery( websiteId, { type, search: searchFormattedValues ? undefined : search, ...params, }, { retryDelay: delay || DEFAULT_ANIMATION_DURATION, enabled: isExpanded, }, ); const query = useWebsiteMetricsQuery( websiteId, { type, limit, search: searchFormattedValues ? undefined : search, ...params, }, { retryDelay: delay || DEFAULT_ANIMATION_DURATION, enabled: !isExpanded, }, ); const { data, isLoading, isFetching, error } = isExpanded ? expandedQuery : query; const filteredData = useMemo(() => { if (data) { let items = data as any[]; if (dataFilter) { if (Array.isArray(dataFilter)) { items = dataFilter.reduce((arr, filter) => { return filter(arr); }, items); } else { items = dataFilter(items); } } if (searchFormattedValues && search) { items = items.filter(({ x, ...data }) => { const value = formatValue(x, type, data); return value?.toLowerCase().includes(search.toLowerCase()); }); } items = percentFilter(items); return items; } return []; }, [data, dataFilter, search, limit, formatValue, type]); const downloadData = isExpanded ? data : filteredData; return ( {allowSearch && } {children} {allowDownload && } {onClose && ( )} {data && (isExpanded ? ( ) : ( ))} {showMore && limit && ( {formatMessage(labels.more)} )} ); }