import type { TFunction } from "i18next"; import { useQueryState } from "nuqs"; import { type ReactNode, useMemo, useRef, useState } from "react"; import { DataTableSkeleton } from "@calcom/features/data-table"; import { downloadAsCsv } from "@calcom/lib/csvUtils"; import { useInViewObserver } from "@calcom/lib/hooks/useInViewObserver"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc"; import classNames from "@calcom/ui/classNames"; import { Avatar } from "@calcom/ui/components/avatar"; import { Badge } from "@calcom/ui/components/badge"; import { Button } from "@calcom/ui/components/button"; import { ToggleGroup, Input } from "@calcom/ui/components/form"; import { HoverCard, HoverCardContent, HoverCardTrigger } from "@calcom/ui/components/hover-card"; import { TableNew, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@calcom/ui/components/table"; import { Tooltip } from "@calcom/ui/components/tooltip"; import { useInsightsParameters } from "../hooks/useInsightsParameters"; interface DownloadButtonProps { teamId?: number; userId?: number; isAll?: boolean; routingFormId?: string; startDate: string; endDate: string; selectedPeriod: string; searchQuery?: string; } function DownloadButton({ userId, teamId, isAll, routingFormId, startDate, endDate, selectedPeriod, searchQuery, }: DownloadButtonProps) { const [isDownloading, setIsDownloading] = useState(false); const utils = trpc.useContext(); const { t } = useLocale(); const handleDownload = async (e: React.MouseEvent) => { e.preventDefault(); // Prevent default form submission try { const result = await utils.viewer.insights.routedToPerPeriodCsv.fetch({ userId, teamId, startDate, endDate, period: selectedPeriod as "perDay" | "perWeek" | "perMonth", isAll, routingFormId, searchQuery: searchQuery || undefined, }); if (!result?.data) { throw new Error("No data received"); } downloadAsCsv(result.data, result.filename || "routing-data.csv"); } catch (error) { console.error("Download failed:", error); } finally { setIsDownloading(false); } }; return (