Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/frontend/src/hooks/useActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ const isDeliveryState = (value: unknown): value is RequestRecord['delivery_state
);
};

const parseHistoryBook = (value: unknown): Book | null => {
/** A History row's download, keeping only the fields it shows. */
export const parseHistoryBook = (value: unknown): Book | null => {
if (!isRecord(value) || Array.isArray(value)) {
return null;
}
Expand Down Expand Up @@ -106,6 +107,11 @@ const parseHistoryBook = (value: unknown): Book | null => {
...(typeof value.content_type === 'string' ? { content_type: value.content_type } : {}),
...(typeof value.book_key === 'string' ? { book_key: value.book_key } : {}),
...(typeof value.info_url === 'string' ? { info_url: value.info_url } : {}),
...(Array.isArray(value.narrators)
? {
narrators: value.narrators.filter((name): name is string => typeof name === 'string'),
}
: {}),
};
};

Expand Down
11 changes: 11 additions & 0 deletions src/frontend/src/tests/useActivity.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect } from 'vitest';

import { parseHistoryBook } from '../hooks/useActivity';
import { getActivityErrorMessage } from '../hooks/useActivity.helpers';

describe('useActivity helpers', () => {
Expand All @@ -15,3 +16,13 @@ describe('useActivity helpers', () => {
expect(getActivityErrorMessage(null, 'Failed to clear item')).toBe('Failed to clear item');
});
});

describe('parseHistoryBook', () => {
it("keeps a download's narrators, to tell audiobooks of one book apart", () => {
const base = { id: 't1', title: 'Good Omens', author: 'Neil Gaiman' };
expect(parseHistoryBook({ ...base, narrators: ['Martin Jarvis', 7] })?.narrators).toEqual([
'Martin Jarvis',
]);
expect(parseHistoryBook(base)?.narrators).toBeUndefined();
});
});
Loading