Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions src/frontend/src/components/activity/ActivitySidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
50 changes: 50 additions & 0 deletions src/frontend/src/components/activity/clearCompleted.ts
Original file line number Diff line number Diff line change
@@ -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;
};
101 changes: 101 additions & 0 deletions src/frontend/src/tests/clearCompleted.test.ts
Original file line number Diff line number Diff line change
@@ -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([]);
});
});
Loading