fix(reports): throttle export actions after queueing
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user