import { Button, Column, DataColumn, DataTable, Icon, Row, SearchField } from '@umami/react-zen';
import { type ReactNode, useState } from 'react';
import { LoadingPanel } from '@/components/common/LoadingPanel';
import { useMessages, useWebsiteExpandedMetricsQuery } from '@/components/hooks';
import { X } from '@/components/icons';
import { DownloadButton } from '@/components/input/DownloadButton';
import { MetricLabel } from '@/components/metrics/MetricLabel';
import { SESSION_COLUMNS } from '@/lib/constants';
import { formatShortTime } from '@/lib/format';
export interface MetricsExpandedTableProps {
websiteId: string;
type?: string;
title?: string;
dataFilter?: (data: any) => any;
onSearch?: (search: string) => void;
params?: { [key: string]: any };
allowSearch?: boolean;
allowDownload?: boolean;
renderLabel?: (row: any, index: number) => ReactNode;
onClose?: () => void;
children?: ReactNode;
}
export function MetricsExpandedTable({
websiteId,
type,
title,
params,
allowSearch = true,
allowDownload = true,
onClose,
children,
}: MetricsExpandedTableProps) {
const [search, setSearch] = useState('');
const { formatMessage, labels } = useMessages();
const isType = ['browser', 'country', 'device', 'os'].includes(type);
const showBounceDuration = SESSION_COLUMNS.includes(type);
const { data, isLoading, isFetching, error } = useWebsiteExpandedMetricsQuery(websiteId, {
type,
search: isType ? undefined : search,
...params,
});
const items = data?.map(({ name, ...props }) => ({ label: name, ...props }));
return (
<>
{allowSearch && }
{children}
{allowDownload && }
{onClose && (
)}
{items && (
{row => (
)}
{row => row?.visitors?.toLocaleString()}
{row => row?.visits?.toLocaleString()}
{row => row?.pageviews?.toLocaleString()}
{showBounceDuration && [
{row => {
const n = (Math.min(row?.visits, row?.bounces) / row?.visits) * 100;
return `${Math.round(+n)}%`;
}}
,
{row => {
const n = row?.totaltime / row?.visits;
return `${+n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}`;
}}
,
]}
)}
>
);
}