diff --git a/src/frontend/src/hooks/useActivity.ts b/src/frontend/src/hooks/useActivity.ts index ce8802059..165f7b2d2 100644 --- a/src/frontend/src/hooks/useActivity.ts +++ b/src/frontend/src/hooks/useActivity.ts @@ -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; } @@ -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'), + } + : {}), }; }; diff --git a/src/frontend/src/tests/useActivity.test.ts b/src/frontend/src/tests/useActivity.test.ts index 6b11e579d..15fd4e084 100644 --- a/src/frontend/src/tests/useActivity.test.ts +++ b/src/frontend/src/tests/useActivity.test.ts @@ -1,5 +1,6 @@ import { describe, it, expect } from 'vitest'; +import { parseHistoryBook } from '../hooks/useActivity'; import { getActivityErrorMessage } from '../hooks/useActivity.helpers'; describe('useActivity helpers', () => { @@ -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(); + }); +});