fix(reports): throttle export actions after queueing

This commit is contained in:
2026-04-27 20:52:17 +03:30
parent 803c3ce629
commit 858aa977f7
2 changed files with 56 additions and 4 deletions

View File

@@ -96,6 +96,10 @@ export default function Reports() {
const [dayDetails, setDayDetails] = useState<DayDetailsResponse | null>(null);
const [openDay, setOpenDay] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [exportState, setExportState] = useState({
excel: { pending: false, cooldownSeconds: 0 },
pdf: { pending: false, cooldownSeconds: 0 },
});
const canSelectUsers = canWorkspace(activeWorkspace?.my_role, WORKSPACE_MEMBERS_VIEW);
@@ -188,12 +192,44 @@ export default function Reports() {
}
};
useEffect(() => {
const interval = window.setInterval(() => {
setExportState((current) => ({
excel: {
pending: current.excel.pending,
cooldownSeconds: Math.max(current.excel.cooldownSeconds - 1, 0),
},
pdf: {
pending: current.pdf.pending,
cooldownSeconds: Math.max(current.pdf.cooldownSeconds - 1, 0),
},
}));
}, 1000);
return () => window.clearInterval(interval);
}, []);
const handleExport = async (type: "excel" | "pdf") => {
if (!apiFilters) return;
if (exportState[type].pending || exportState[type].cooldownSeconds > 0) return;
setExportState((current) => ({
...current,
[type]: { pending: true, cooldownSeconds: 0 },
}));
try {
await createReportExport(apiFilters, type, lang);
setExportState((current) => ({
...current,
[type]: { pending: false, cooldownSeconds: 60 },
}));
toast.success(t.reports?.exportQueued || "Export queued. You will receive a notification with the download link.");
} catch {
setExportState((current) => ({
...current,
[type]: { pending: false, cooldownSeconds: 0 },
}));
toast.error(t.reports?.exportError || "Failed to queue report export.");
}
};
@@ -310,6 +346,7 @@ export default function Reports() {
openDay={openDay}
onToggleDay={(day) => void handleToggleDay(day)}
onExport={(type) => void handleExport(type)}
exportState={exportState}
labels={{
exportExcel: t.reports?.exportExcel || "Export Excel",
exportPdf: t.reports?.exportPdf || "Export PDF",