fix(activity): Clear Completed leaves out requests that are still pending (#1438)

Kept small on purpose so it's easy to review. Same kind of small fix as
#1429, so see the question there about separate PRs versus combined.

"Clear Completed" sends each finished download plus the request linked
to it. The server only dismisses finished requests, and if one request
in the batch is still pending it answers 409 "Only terminal requests can
be dismissed" and nothing gets cleared. A request can be pending while
its download has ended, for example when the startup sweep from #1409
reopens the request of an interrupted download. On my instance 25
finished downloads were linked to 21 pending requests, and the button
failed every time.

The button now leaves out the request target when the request list shows
it as pending, and still dismisses the download. A request the list has
no record of is sent as before. The target logic moved into a small
`getClearCompletedTargets` helper so it can be tested without rendering
the sidebar. The pending-request test fails on the old logic.

Vitest (225 passing), tsc, oxlint and oxfmt are clean. I reproduced the
409 from the server log and the data, but I haven't clicked through it
in the UI with this build.

The one red check is the existing clock-dependent test
`test_a_queued_torrent_asks_for_a_grace_once`, which fails on any runner
that booted recently. It is fixed in #1426 and is not caused by this
change.

Co-authored-by: CaliBrain <calibrain@l4n.xyz>
This commit is contained in:
splitsec2andCaliBrain authored and GitHub committed 2026-10-20 01:05:55 -04:00
1 parent b5e47b2593
commit 95ed00ce78
3 files changed
+156 -32

No files matched your search

@@ -9,6 +9,7 @@ import { ActivityCard } from './ActivityCard';
import type { DownloadStatusKey } from './activityMappers';
import { downloadToActivityItem, linkedDownloadIdForRequest } from './activityMappers';
import type { ActivityItem } from './activityTypes';
import { getClearCompletedTargets } from './clearCompleted';
interface ActivitySidebarProps {
isOpen: boolean;
@@ -483,38 +484,10 @@ export const ActivitySidebar = ({
const hasUserFilter = isAdmin && availableUsers.length > 1;
const clearCompletedTargets = useMemo(() => {
const targets: ActivityDismissTarget[] = [];
const seen = new Set<string>();
visibleItems.forEach((item) => {
const isTerminalDownload =
item.kind === 'download' &&
(item.visualStatus === 'complete' ||
item.visualStatus === 'error' ||
item.visualStatus === 'cancelled');
if (!isTerminalDownload || !item.downloadBookId) {
return;
}
const downloadKey = `download:${item.downloadBookId}`;
if (!seen.has(downloadKey)) {
seen.add(downloadKey);
targets.push({ itemType: 'download', itemKey: downloadKey });
}
if (item.requestId) {
const requestKey = `request:${item.requestId}`;
if (!seen.has(requestKey)) {
seen.add(requestKey);
targets.push({ itemType: 'request', itemKey: requestKey });
}
}
});
return targets;
}, [visibleItems]);
const clearCompletedTargets = useMemo(
() => getClearCompletedTargets(visibleItems, visibleRequestItems),
[visibleItems, visibleRequestItems],
);
const visibleCategoryOrder = useMemo(
() => getVisibleCategoryOrder(effectiveActiveTab),
@@ -0,0 +1,50 @@
import type { ActivityDismissTarget } from './ActivitySidebar';
import type { ActivityItem } from './activityTypes';
/**
* Items the "Clear Completed" button dismisses: every finished download, plus the request
* linked to it once that request is itself finished. The server only dismisses finished
* requests and rejects the whole batch if one is still pending, which a restart can cause
* by reopening the request of a download that already ended.
*/
export const getClearCompletedTargets = (
visibleItems: ActivityItem[],
requestItems: ActivityItem[],
): ActivityDismissTarget[] => {
const pendingRequestIds = new Set<number>();
requestItems.forEach((item) => {
if (typeof item.requestId === 'number' && item.requestRecord?.status === 'pending') {
pendingRequestIds.add(item.requestId);
}
});
const targets: ActivityDismissTarget[] = [];
const seen = new Set<string>();
visibleItems.forEach((item) => {
const isTerminalDownload =
item.kind === 'download' &&
(item.visualStatus === 'complete' ||
item.visualStatus === 'error' ||
item.visualStatus === 'cancelled');
if (!isTerminalDownload || !item.downloadBookId) {
return;
}
const downloadKey = `download:${item.downloadBookId}`;
if (!seen.has(downloadKey)) {
seen.add(downloadKey);
targets.push({ itemType: 'download', itemKey: downloadKey });
}
if (item.requestId && !pendingRequestIds.has(item.requestId)) {
const requestKey = `request:${item.requestId}`;
if (!seen.has(requestKey)) {
seen.add(requestKey);
targets.push({ itemType: 'request', itemKey: requestKey });
}
}
});
return targets;
};
@@ -0,0 +1,101 @@
import { describe, it, expect } from 'vitest';
import type { ActivityItem } from '../components/activity/activityTypes';
import { getClearCompletedTargets } from '../components/activity/clearCompleted';
import type { RequestRecord } from '../types';
const makeRequestRecord = (id: number, status: RequestRecord['status']): RequestRecord => ({
id,
user_id: 7,
status,
source_hint: 'prowlarr',
content_type: 'ebook',
request_level: 'book',
policy_mode: 'request_book',
book_data: { title: 'Title', author: 'Author' },
release_data: null,
note: null,
admin_note: null,
reviewed_by: null,
reviewed_at: null,
created_at: '2026-02-13T12:00:00Z',
updated_at: '2026-02-13T12:00:00Z',
username: 'alice',
});
const makeDownloadItem = (
downloadBookId: string,
requestId: number | undefined,
overrides: Partial<ActivityItem> = {},
): ActivityItem => ({
id: `download-${downloadBookId}`,
kind: 'download',
visualStatus: 'complete',
title: 'Title',
author: 'Author',
metaLine: 'EPUB',
statusLabel: 'Complete',
timestamp: 1,
downloadBookId,
requestId,
...overrides,
});
const makeRequestItem = (requestId: number, status: RequestRecord['status']): ActivityItem => ({
id: `request-${requestId}`,
kind: 'request',
visualStatus: status,
title: 'Title',
author: 'Author',
metaLine: 'Book request',
statusLabel: status,
timestamp: 1,
requestId,
requestRecord: makeRequestRecord(requestId, status),
});
describe('getClearCompletedTargets', () => {
it('dismisses a finished download together with its finished request', () => {
const targets = getClearCompletedTargets(
[makeDownloadItem('a', 5)],
[makeRequestItem(5, 'fulfilled')],
);
expect(targets).toEqual([
{ itemType: 'download', itemKey: 'download:a' },
{ itemType: 'request', itemKey: 'request:5' },
]);
});
it('dismisses the download but not a request that is still pending', () => {
// The server refuses to dismiss a pending request, and one refusal fails the whole batch.
const targets = getClearCompletedTargets(
[makeDownloadItem('a', 5), makeDownloadItem('b', 6)],
[makeRequestItem(5, 'pending'), makeRequestItem(6, 'fulfilled')],
);
expect(targets).toEqual([
{ itemType: 'download', itemKey: 'download:a' },
{ itemType: 'download', itemKey: 'download:b' },
{ itemType: 'request', itemKey: 'request:6' },
]);
});
it('still dismisses the request when the list has no record of it', () => {
const targets = getClearCompletedTargets([makeDownloadItem('a', 9)], []);
expect(targets).toEqual([
{ itemType: 'download', itemKey: 'download:a' },
{ itemType: 'request', itemKey: 'request:9' },
]);
});
it('leaves unfinished downloads alone', () => {
const targets = getClearCompletedTargets(
[makeDownloadItem('a', undefined, { visualStatus: 'downloading' })],
[],
);
expect(targets).toEqual([]);
});
});