fix: cancel download fetch when progress toast is closed (#27152)
* fix: cancel download fetch when progress toast is closed - Create new progress-toast utility using @coss/ui toastManager - Add ToastProvider to app providers - Update download components to use AbortController for cancellation - When cancel button is clicked, abort signal stops the fetch loop Co-Authored-By: [email protected] <[email protected]> * fix: track active toasts with Set instead of accessing toastManager.toasts Co-Authored-By: [email protected] <[email protected]> * feat: add AnchoredToastProvider to app providers Co-Authored-By: [email protected] <[email protected]> * refactor: extract common CSV download logic into useCsvDownload hook Co-Authored-By: [email protected] <[email protected]> * refactor: improve progress toast with i18n support and Progress component Co-Authored-By: [email protected] <[email protected]> * refactor: replace useCsvDownload hook with DownloadButton component Co-Authored-By: [email protected] <[email protected]> * refactor: rename DownloadButton to CsvDownloadButton Co-Authored-By: [email protected] <[email protected]> * clean up i18n * fix: properly throw AbortError when download is cancelled Co-Authored-By: [email protected] <[email protected]> * Revert "fix: properly throw AbortError when download is cancelled" This reverts commit 3c64b848140271e4dcd2e45dba5302eef9ca562c. * fix abort controller * fix layout shift * better handle error * fix: address Cubic AI review feedback - Fix typo 'copmlete' -> 'complete' in download_progress i18n string - Treat null batch as failure during CSV download to prevent partial exports Co-Authored-By: unknown <> * remove redundant try - catch --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
7 files changed
+256
-268
No files matched your search
@@ -1,14 +1,12 @@
|
||||
"use client";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { downloadAsCsv } from "@calcom/lib/csvUtils";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { hideProgressToast, showProgressToast, showToast } from "@calcom/ui/components/toast";
|
||||
import { useState } from "react";
|
||||
|
||||
import { CsvDownloadButton } from "@lib/components/CsvDownloadButton";
|
||||
import { useBookingFilters } from "~/bookings/hooks/useBookingFilters";
|
||||
import type { BookingListingStatus } from "../types";
|
||||
|
||||
@@ -38,7 +36,6 @@ function transformBookingToCsv(booking: BookingOutput, t: TranslationFunction) {
|
||||
export function BookingsCsvDownload({ status }: BookingsCsvDownloadProps) {
|
||||
const { t } = useLocale();
|
||||
const { data: user, isPending: isUserPending } = useMeQuery();
|
||||
const [isDownloading, setIsDownloading] = useState(false);
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
const { eventTypeIds, teamIds, userIds, dateRange, attendeeName, attendeeEmail, bookingUid } =
|
||||
@@ -51,75 +48,30 @@ export function BookingsCsvDownload({ status }: BookingsCsvDownloadProps) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const fetchBatch = async (offset: number) => {
|
||||
const result = await utils.viewer.bookings.get.fetch({
|
||||
limit: BATCH_SIZE,
|
||||
offset,
|
||||
filters: {
|
||||
statuses: [status],
|
||||
eventTypeIds,
|
||||
teamIds,
|
||||
userIds,
|
||||
attendeeName,
|
||||
attendeeEmail,
|
||||
bookingUid,
|
||||
afterStartDate: dateRange?.startDate
|
||||
? dayjs(dateRange?.startDate).startOf("day").toISOString()
|
||||
: undefined,
|
||||
beforeEndDate: dateRange?.endDate ? dayjs(dateRange?.endDate).endOf("day").toISOString() : undefined,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
bookings: result.bookings,
|
||||
totalCount: result.totalCount,
|
||||
};
|
||||
};
|
||||
|
||||
const handleDownload = async () => {
|
||||
try {
|
||||
setIsDownloading(true);
|
||||
showProgressToast(0);
|
||||
|
||||
// Fetch first batch to get total count
|
||||
const firstBatch = await fetchBatch(0);
|
||||
let allBookings = firstBatch.bookings;
|
||||
const totalCount = firstBatch.totalCount;
|
||||
|
||||
// Continue fetching remaining batches
|
||||
while (allBookings.length < totalCount) {
|
||||
const offset = allBookings.length;
|
||||
const batch = await fetchBatch(offset);
|
||||
if (batch.bookings.length === 0) break; // Prevent infinite loop if batch returns empty
|
||||
allBookings = [...allBookings, ...batch.bookings];
|
||||
|
||||
const currentProgress = Math.min(Math.round((allBookings.length / totalCount) * 100), 99);
|
||||
showProgressToast(currentProgress);
|
||||
}
|
||||
|
||||
showProgressToast(100);
|
||||
|
||||
// Transform and download
|
||||
const csvData = allBookings.map((booking) => transformBookingToCsv(booking, t));
|
||||
const filename = `${t("bookings").toLowerCase()}-${status}-${dayjs().format("YYYY-MM-DD")}.csv`;
|
||||
downloadAsCsv(csvData, filename);
|
||||
} catch {
|
||||
showToast(t("unexpected_error_try_again"), "error");
|
||||
} finally {
|
||||
setIsDownloading(false);
|
||||
hideProgressToast();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Button
|
||||
color="secondary"
|
||||
StartIcon="download"
|
||||
loading={isDownloading}
|
||||
onClick={handleDownload}
|
||||
size="sm"
|
||||
className="h-full">
|
||||
{t("download")}
|
||||
</Button>
|
||||
<CsvDownloadButton
|
||||
fetchBatch={async (offset) => {
|
||||
const result = await utils.viewer.bookings.get.fetch({
|
||||
limit: BATCH_SIZE,
|
||||
offset,
|
||||
filters: {
|
||||
statuses: [status],
|
||||
eventTypeIds,
|
||||
teamIds,
|
||||
userIds,
|
||||
attendeeName,
|
||||
attendeeEmail,
|
||||
bookingUid,
|
||||
afterStartDate: dateRange?.startDate
|
||||
? dayjs(dateRange?.startDate).startOf("day").toISOString()
|
||||
: undefined,
|
||||
beforeEndDate: dateRange?.endDate ? dayjs(dateRange?.endDate).endOf("day").toISOString() : undefined,
|
||||
},
|
||||
});
|
||||
return { data: result.bookings, total: result.totalCount };
|
||||
}}
|
||||
transformData={(bookings) => bookings.map((booking) => transformBookingToCsv(booking, t))}
|
||||
filename={`${t("bookings").toLowerCase()}-${status}-${dayjs().format("YYYY-MM-DD")}.csv`}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,112 +1,41 @@
|
||||
import { useState } from "react";
|
||||
import posthog from "posthog-js";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { downloadAsCsv } from "@calcom/lib/csvUtils";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import {
|
||||
Dropdown,
|
||||
DropdownItem,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuTrigger,
|
||||
} from "@calcom/ui/components/dropdown";
|
||||
import { showToast, showProgressToast, hideProgressToast } from "@calcom/ui/components/toast";
|
||||
|
||||
import { useInsightsBookingParameters } from "../../../hooks/useInsightsBookingParameters";
|
||||
import { extractDateRangeFromColumnFilters } from "@calcom/features/insights/lib/bookingUtils";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { CsvDownloadButton } from "@lib/components/CsvDownloadButton";
|
||||
import posthog from "posthog-js";
|
||||
import { useInsightsBookingParameters } from "../../../hooks/useInsightsBookingParameters";
|
||||
|
||||
type RawData = RouterOutputs["viewer"]["insights"]["rawData"]["data"][number];
|
||||
|
||||
const BATCH_SIZE = 100;
|
||||
|
||||
const Download = () => {
|
||||
const { t } = useLocale();
|
||||
const insightsBookingParams = useInsightsBookingParameters();
|
||||
const { startDate, endDate } = extractDateRangeFromColumnFilters(insightsBookingParams.columnFilters);
|
||||
const [isDownloading, setIsDownloading] = useState(false);
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
type PaginatedResponse = {
|
||||
data: RawData[];
|
||||
total: number;
|
||||
};
|
||||
|
||||
const fetchBatch = async (offset: number): Promise<PaginatedResponse | null> => {
|
||||
try {
|
||||
const result = await utils.viewer.insights.rawData.fetch({
|
||||
...insightsBookingParams,
|
||||
limit: BATCH_SIZE,
|
||||
offset,
|
||||
});
|
||||
|
||||
if (result && "data" in result && "total" in result) {
|
||||
return result as PaginatedResponse;
|
||||
}
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const handleDownloadClick = async () => {
|
||||
try {
|
||||
posthog.capture("insights_bookings_download_clicked", { teamId: insightsBookingParams.selectedTeamId });
|
||||
setIsDownloading(true);
|
||||
showProgressToast(0);
|
||||
let allData: RawData[] = [];
|
||||
let offset = 0;
|
||||
|
||||
// Get first batch to get total count
|
||||
const firstBatch = await fetchBatch(0);
|
||||
if (!firstBatch) return;
|
||||
|
||||
allData = firstBatch.data;
|
||||
const totalRecords = firstBatch.total;
|
||||
|
||||
// Continue fetching remaining batches
|
||||
while (totalRecords > 0 && allData.length < totalRecords) {
|
||||
offset += BATCH_SIZE;
|
||||
const result = await fetchBatch(offset);
|
||||
if (!result) break;
|
||||
allData = [...allData, ...result.data];
|
||||
|
||||
const currentProgress = Math.min(Math.round((allData.length / totalRecords) * 100), 99);
|
||||
showProgressToast(currentProgress);
|
||||
}
|
||||
|
||||
if (allData.length >= totalRecords) {
|
||||
showProgressToast(100); // Set to 100% before actual download
|
||||
const filename = `Insights-${dayjs(startDate).format("YYYY-MM-DD")}-${dayjs(endDate).format(
|
||||
"YYYY-MM-DD"
|
||||
)}.csv`;
|
||||
downloadAsCsv(allData as Record<string, unknown>[], filename);
|
||||
}
|
||||
} catch {
|
||||
showToast(t("unexpected_error_try_again"), "error");
|
||||
} finally {
|
||||
setIsDownloading(false);
|
||||
hideProgressToast(); // Reset progress
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dropdown modal={false}>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
EndIcon="file-down"
|
||||
color="secondary"
|
||||
loading={isDownloading}
|
||||
className="h-full self-end sm:self-baseline">
|
||||
{t("download")}
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownItem onClick={handleDownloadClick}>{t("as_csv")}</DropdownItem>
|
||||
</DropdownMenuContent>
|
||||
</Dropdown>
|
||||
<CsvDownloadButton<RawData>
|
||||
fetchBatch={async (offset) => {
|
||||
const result = await utils.viewer.insights.rawData.fetch({
|
||||
...insightsBookingParams,
|
||||
limit: BATCH_SIZE,
|
||||
offset,
|
||||
});
|
||||
|
||||
if (result && "data" in result && "total" in result) {
|
||||
return { data: result.data as RawData[], total: result.total };
|
||||
}
|
||||
return null;
|
||||
}}
|
||||
filename={`Insights-${dayjs(startDate).format("YYYY-MM-DD")}-${dayjs(endDate).format("YYYY-MM-DD")}.csv`}
|
||||
onDownloadStart={() => {
|
||||
posthog.capture("insights_bookings_download_clicked", {
|
||||
teamId: insightsBookingParams.selectedTeamId,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
+15
-85
@@ -1,103 +1,33 @@
|
||||
import { useState } from "react";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import type { SortingState } from "@calcom/features/data-table";
|
||||
import { useInsightsRoutingParameters } from "@calcom/web/modules/insights/hooks/useInsightsRoutingParameters";
|
||||
import { downloadAsCsv } from "@calcom/lib/csvUtils";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import {
|
||||
Dropdown,
|
||||
DropdownItem,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuTrigger,
|
||||
} from "@calcom/ui/components/dropdown";
|
||||
import { showToast, showProgressToast, hideProgressToast } from "@calcom/ui/components/toast";
|
||||
import { useInsightsRoutingParameters } from "@calcom/web/modules/insights/hooks/useInsightsRoutingParameters";
|
||||
|
||||
type RoutingData = RouterOutputs["viewer"]["insights"]["routingFormResponsesForDownload"]["data"][number];
|
||||
import { CsvDownloadButton } from "@lib/components/CsvDownloadButton";
|
||||
|
||||
type Props = {
|
||||
sorting: SortingState;
|
||||
};
|
||||
|
||||
const BATCH_SIZE = 100; // Increased batch size for downloads
|
||||
const BATCH_SIZE = 100;
|
||||
|
||||
export const RoutingFormResponsesDownload = ({ sorting }: Props) => {
|
||||
const { t } = useLocale();
|
||||
const [isDownloading, setIsDownloading] = useState(false);
|
||||
const insightsRoutingParameters = useInsightsRoutingParameters();
|
||||
const { startDate, endDate } = insightsRoutingParameters;
|
||||
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
const fetchBatch = async (
|
||||
offset: number
|
||||
): Promise<{
|
||||
data: RoutingData[];
|
||||
total: number;
|
||||
}> => {
|
||||
const result = await utils.viewer.insights.routingFormResponsesForDownload.fetch({
|
||||
...insightsRoutingParameters,
|
||||
sorting,
|
||||
limit: BATCH_SIZE,
|
||||
offset,
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
const handleDownloadClick = async () => {
|
||||
try {
|
||||
setIsDownloading(true);
|
||||
showProgressToast(0); // Reset progress
|
||||
let allData: RoutingData[] = [];
|
||||
let offset = 0;
|
||||
|
||||
// Get first batch to get total count
|
||||
const firstBatch = await fetchBatch(0);
|
||||
allData = [...firstBatch.data];
|
||||
const totalRecords = firstBatch.total;
|
||||
|
||||
// Continue fetching remaining batches
|
||||
while (totalRecords > 0 && allData.length < totalRecords) {
|
||||
offset += BATCH_SIZE;
|
||||
const result = await fetchBatch(offset);
|
||||
allData = [...allData, ...result.data];
|
||||
|
||||
const currentProgress = Math.min(Math.round((allData.length / totalRecords) * 100), 99);
|
||||
showProgressToast(currentProgress);
|
||||
}
|
||||
|
||||
if (allData.length >= totalRecords) {
|
||||
showProgressToast(100); // Set to 100% before actual download
|
||||
const filename = `RoutingFormResponses-${dayjs(startDate).format("YYYY-MM-DD")}-${dayjs(
|
||||
endDate
|
||||
).format("YYYY-MM-DD")}.csv`;
|
||||
downloadAsCsv(allData as Record<string, unknown>[], filename);
|
||||
}
|
||||
} catch (error) {
|
||||
showToast(t("error_downloading_data"), "error");
|
||||
} finally {
|
||||
setIsDownloading(false);
|
||||
hideProgressToast(); // Reset progress
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dropdown modal={false}>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
EndIcon="file-down"
|
||||
color="secondary"
|
||||
className="self-end sm:self-baseline"
|
||||
loading={isDownloading}>
|
||||
{t("download")}
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownItem onClick={handleDownloadClick}>{t("as_csv")}</DropdownItem>
|
||||
</DropdownMenuContent>
|
||||
</Dropdown>
|
||||
<CsvDownloadButton
|
||||
fetchBatch={async (offset) => {
|
||||
const result = await utils.viewer.insights.routingFormResponsesForDownload.fetch({
|
||||
...insightsRoutingParameters,
|
||||
sorting,
|
||||
limit: BATCH_SIZE,
|
||||
offset,
|
||||
});
|
||||
return result;
|
||||
}}
|
||||
filename={`RoutingFormResponses-${dayjs(startDate).format("YYYY-MM-DD")}-${dayjs(endDate).format("YYYY-MM-DD")}.csv`}
|
||||
/>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user