Skip to content

Commit 38dcfa0

Browse files
Fix DropPoint button regression from preview cache effect (#311)
1 parent 1408937 commit 38dcfa0

1 file changed

Lines changed: 37 additions & 34 deletions

File tree

‎src/lib/components/AttachmentField.svelte‎

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { onDestroy } from "svelte";
2+
import { onDestroy, untrack } from "svelte";
33
import DOMPurify from "dompurify";
44
import type {
55
AttachmentDropPointSessionSummary,
@@ -65,7 +65,6 @@
6565
let pollTimer: number | null = null;
6666
let countdownTimer: number | null = null;
6767
let remoteRunId = 0;
68-
let pollInFlight = false;
6968
7069
let isRecorded = $derived(attachments.length > 0);
7170
let hasSelectedFiles = $derived(selectedFiles.length > 0);
@@ -96,14 +95,29 @@
9695
9796
const imageAttachments = attachments.filter(isImageAttachment);
9897
const imagePaths = new Set(imageAttachments.map((file) => file.path));
99-
previewUrls = Object.fromEntries(
100-
Object.entries(previewUrls).filter(([path]) => imagePaths.has(path)),
98+
const currentPreviewUrls = untrack(() => previewUrls);
99+
const retainedPreviewUrls = Object.fromEntries(
100+
Object.entries(currentPreviewUrls).filter(([path]) => imagePaths.has(path)),
101101
);
102+
if (!samePreviewUrls(currentPreviewUrls, retainedPreviewUrls)) {
103+
previewUrls = retainedPreviewUrls;
104+
}
102105
for (const file of imageAttachments) {
103-
if (!previewUrls[file.path]) void loadPreview(previewLoader, file, runId);
106+
if (!currentPreviewUrls[file.path]) void loadPreview(previewLoader, file, runId);
104107
}
105108
});
106109
110+
function samePreviewUrls(
111+
left: Record<string, string>,
112+
right: Record<string, string>,
113+
): boolean {
114+
const leftEntries = Object.entries(left);
115+
return (
116+
leftEntries.length === Object.keys(right).length &&
117+
leftEntries.every(([path, url]) => right[path] === url)
118+
);
119+
}
120+
107121
function normalizedContentType(contentType: string): string {
108122
return contentType.split(";")[0]?.trim().toLowerCase() ?? "";
109123
}
@@ -150,35 +164,27 @@
150164
151165
async function confirmRemoveFile(file: AttachmentState) {
152166
if (!onremovefile) return;
153-
try {
154-
const ok = await confirmDialog(`Remove ${file.filename}?`, {
155-
title: "Remove attachment",
156-
kind: "warning",
157-
okLabel: "Remove",
158-
cancelLabel: "Cancel",
159-
});
160-
if (ok) onremovefile(file.path);
161-
} catch (e) {
162-
remoteError = String(e);
163-
}
167+
const ok = await confirmDialog(`Remove ${file.filename}?`, {
168+
title: "Remove attachment",
169+
kind: "warning",
170+
okLabel: "Remove",
171+
cancelLabel: "Cancel",
172+
});
173+
if (ok) onremovefile(file.path);
164174
}
165175
166176
async function confirmClearAll() {
167177
if (!onclear) return;
168-
try {
169-
const ok = await confirmDialog(
170-
`Remove all ${attachments.length} attachments from ${definition.label}?`,
171-
{
172-
title: "Remove all attachments",
173-
kind: "warning",
174-
okLabel: "Remove all",
175-
cancelLabel: "Cancel",
176-
},
177-
);
178-
if (ok) onclear();
179-
} catch (e) {
180-
remoteError = String(e);
181-
}
178+
const ok = await confirmDialog(
179+
`Remove all ${attachments.length} attachments from ${definition.label}?`,
180+
{
181+
title: "Remove all attachments",
182+
kind: "warning",
183+
okLabel: "Remove all",
184+
cancelLabel: "Cancel",
185+
},
186+
);
187+
if (ok) onclear();
182188
}
183189
184190
async function startRemoteUpload() {
@@ -239,8 +245,7 @@
239245
}
240246
241247
async function pollRemoteOnce() {
242-
if (!remoteSession || !onpolldrop || pollInFlight) return;
243-
pollInFlight = true;
248+
if (!remoteSession || !onpolldrop) return;
244249
const session = remoteSession;
245250
const runId = remoteRunId;
246251
try {
@@ -276,8 +281,6 @@
276281
stopPolling();
277282
remotePhase = "failed";
278283
remoteError = String(e);
279-
} finally {
280-
pollInFlight = false;
281284
}
282285
}
283286

0 commit comments

Comments
 (0)