From cddcdedfc3cd7314b6e9131e0a966a823858ccd4 Mon Sep 17 00:00:00 2001 From: cesnimda Date: Tue, 17 Nov 2026 07:39:40 +0100 Subject: [PATCH] fix(attachments): discard reads from departed application contexts --- .../src/attachment-action-lifetime.test.tsx | 28 +++++++++++++++++++ job-tracker-ui/src/components/Attachments.tsx | 13 +++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/job-tracker-ui/src/attachment-action-lifetime.test.tsx b/job-tracker-ui/src/attachment-action-lifetime.test.tsx index 296af3f0..b492d733 100644 --- a/job-tracker-ui/src/attachment-action-lifetime.test.tsx +++ b/job-tracker-ui/src/attachment-action-lifetime.test.tsx @@ -46,6 +46,34 @@ async function open(action: "rename" | "delete") { if (action === "rename") fireEvent.change(within(dialog).getByRole("textbox"), { target: { value: "New name.pdf" } }); return { view, dialog }; } + +test.each(['resolve', 'reject'] as const)('old attachment read %s cannot replace the next application', async outcome => { + const pending = deferred(); + const current = { ...file, id: 77, fileName: 'Current application.pdf' }; + mockedApi.get.mockReturnValueOnce(pending.promise).mockResolvedValue({ data: [current] } as any); + const view = render(page()); + await act(async () => { view.rerender(page(77)); }); + expect(await screen.findByText(current.fileName)).toBeVisible(); + await act(async () => { if (outcome === 'resolve') pending.resolve({ data: [file] }); else pending.reject(new Error('obsolete initial read')); }); + expect(screen.getByText(current.fileName)).toBeVisible(); + expect(screen.queryByText(file.fileName)).not.toBeInTheDocument(); + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); +}); + +test.each((['rename', 'delete'] as const).flatMap(action => (['resolve', 'reject'] as const).map(outcome => [action, outcome] as const)))('old post-%s read %s cannot replace the next application', async (action, outcome) => { + const { view, dialog } = await open(action); + const pending = deferred(); + const current = { ...file, id: 77, fileName: 'Current application.pdf' }; + mockedApi.get.mockImplementation((url: any) => url === '/attachments/42' ? pending.promise : Promise.resolve({ data: [current] })); + fireEvent.click(within(dialog).getByRole('button', { name: action === 'rename' ? 'Rename' : 'Delete' })); + await waitFor(() => expect(mockedApi.get.mock.calls.filter(([url]) => url === '/attachments/42')).toHaveLength(2)); + await act(async () => { view.rerender(page(77)); }); + expect(await screen.findByText(current.fileName)).toBeVisible(); + await act(async () => { if (outcome === 'resolve') pending.resolve({ data: [file] }); else pending.reject(new Error('obsolete refresh read')); }); + expect(screen.getByText(current.fileName)).toBeVisible(); + expect(screen.queryByText(file.fileName)).not.toBeInTheDocument(); + expect(screen.queryByText(translations.en.attachmentsLoadFailed)).not.toBeInTheDocument(); +}); test.each([ ["rename", "unmount"], ["rename", "job change"], ["rename", "session"], ["delete", "unmount"], ["delete", "job change"], ["delete", "session"], diff --git a/job-tracker-ui/src/components/Attachments.tsx b/job-tracker-ui/src/components/Attachments.tsx index ed06827b..b9c53f90 100644 --- a/job-tracker-ui/src/components/Attachments.tsx +++ b/job-tracker-ui/src/components/Attachments.tsx @@ -110,7 +110,7 @@ export default function Attachments({ jobId }: { jobId: number }) { return () => { owner.mounted = false; owner.generation += 1; }; }, [jobId]); - const captureAction = () => { + const captureAction = useCallback(() => { checkSessionCookie(); const owner = actionOwner.current; if (!owner.mounted || owner.jobId !== jobId || isSessionInvalidated()) return null; @@ -120,20 +120,23 @@ export default function Attachments({ jobId }: { jobId: number }) { checkSessionCookie(); return owner.mounted && owner.jobId === jobId && owner.generation === generation && !isSessionInvalidated() && getSessionContext() === context; }; - }; + }, [jobId]); const load = useCallback(async () => { + const active = captureAction(); + if (!active) return; setLoading(true); try { const res = await api.get(`/attachments/${jobId}`); + if (!active()) return; setItems(res.data); setLoadError(null); } catch (error) { - setLoadError(getApiErrorMessage(error, currentT.current("attachmentsLoadFailed"))); + if (active()) setLoadError(getApiErrorMessage(error, currentT.current("attachmentsLoadFailed"))); } finally { - setLoading(false); + if (active()) setLoading(false); } - }, [jobId]); + }, [jobId, captureAction]); useEffect(() => { void load();