import type { TFunction } from "next-i18next"; import { useQueryState } from "nuqs"; import { type ReactNode, useMemo, useRef, useState } from "react"; import { DataTableSkeleton } from "@calcom/features/data-table"; import classNames from "@calcom/lib/classNames"; 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 { Avatar, ToggleGroup, Badge, Tooltip, HoverCard, HoverCardContent, HoverCardTrigger, Input, Button, } from "@calcom/ui"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@calcom/ui/components/table/TableNew"; 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 (