fix(attachments): discard obsolete download and preview completions
This commit is contained in:
@@ -218,15 +218,19 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
e.target.value = "";
|
||||
};
|
||||
|
||||
const getBlobUrl = async (id: number) => {
|
||||
const getBlobUrl = async (id: number, active: () => boolean) => {
|
||||
const res = await api.get(`/attachments/download/${id}`, { responseType: "blob" });
|
||||
if (!active()) return null;
|
||||
const blob: Blob = res.data;
|
||||
return URL.createObjectURL(blob);
|
||||
};
|
||||
|
||||
const download = async (a: AttachmentItem) => {
|
||||
const active = captureAction();
|
||||
if (!active) return;
|
||||
try {
|
||||
const url = await getBlobUrl(a.id);
|
||||
const url = await getBlobUrl(a.id, active);
|
||||
if (!url) return;
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = a.fileName || `attachment_${a.id}`;
|
||||
@@ -235,18 +239,21 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
link.remove();
|
||||
window.setTimeout(() => URL.revokeObjectURL(url), 5000);
|
||||
} catch (error) {
|
||||
toast(getApiErrorMessage(error, t("attachmentsDownloadFailed")), "error");
|
||||
if (active()) toast(getApiErrorMessage(error, currentT.current("attachmentsDownloadFailed")), "error");
|
||||
}
|
||||
};
|
||||
|
||||
const openPreview = async (a: AttachmentItem) => {
|
||||
const active = captureAction();
|
||||
if (!active) return;
|
||||
try {
|
||||
if (preview?.url) URL.revokeObjectURL(preview.url);
|
||||
const url = await getBlobUrl(a.id);
|
||||
const url = await getBlobUrl(a.id, active);
|
||||
if (!url) return;
|
||||
setPreview({ url, type: a.fileType || "", name: a.fileName });
|
||||
setPreviewOpen(true);
|
||||
} catch (error) {
|
||||
toast(getApiErrorMessage(error, t("attachmentsPreviewFailed")), "error");
|
||||
if (active()) toast(getApiErrorMessage(error, currentT.current("attachmentsPreviewFailed")), "error");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user