fix(web): round-trip legacy Artist/MultiCollection block items; fix stale AsyncPicker state

BlocksScreen couldn't load/save blocks containing Artist or
MultiCollection items (creatable upstream; ReplaceBlockItemsHandler
already supports both) - itemFromResponse dropped multiCollectionId
and left selectionName empty for those kinds, and toRequestItem
hardcoded multiCollectionId to null, so re-saving such a block
produced a 422 and made it uneditable. Carry the ids/names through
DraftItem and render them read-only (parity with Blazor, which
doesn't offer these kinds in the creation picker either).

Also key the item AsyncPicker by selection identity + collection
type so switching rows or collection type remounts it with clean
state, instead of carrying over a stale query/options list that
could store a wrong-kind id.
This commit is contained in:
2026-07-07 18:15:21 +02:00
parent 1e34e00e31
commit a20fef6096
2 changed files with 102 additions and 20 deletions
+45
View File
@@ -140,6 +140,51 @@ describe('BlocksScreen', () => {
expect(sent.items.map((i: { searchTitle: string }) => i.searchTitle)).toEqual(['A', 'B']);
});
it('round-trips legacy Artist and MultiCollection items through load and save', async () => {
window.history.pushState({}, '', '/app/blocks/4');
const fetchMock = mockApi({
items: [
blockItem({
id: 1,
collectionType: 'Artist',
mediaItemId: 42,
mediaItemName: 'The Beatles'
}),
blockItem({
id: 2,
collectionType: 'MultiCollection',
multiCollectionId: 99,
multiCollectionName: 'My Multi Collection'
})
]
});
render(<BlocksScreen />);
await waitFor(() => expect(screen.getByDisplayValue('Morning')).toBeInTheDocument());
// Both legacy items should display their carried-over selection name in the table.
expect(await screen.findByText('The Beatles')).toBeInTheDocument();
expect(screen.getByText('My Multi Collection')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /Save block/ }));
await waitFor(() => {
const call = fetchMock.mock.calls.find(([u, init]) => u === '/api/blocks/4' && init?.method === 'PUT');
expect(call).toBeTruthy();
});
const putCall = fetchMock.mock.calls.find(([u, init]) => u === '/api/blocks/4' && init?.method === 'PUT');
const sent = JSON.parse(String(putCall?.[1]?.body));
const artistItem = sent.items.find((i: { collectionType: string }) => i.collectionType === 'Artist');
expect(artistItem.mediaItemId).toBe(42);
const multiCollectionItem = sent.items.find(
(i: { collectionType: string }) => i.collectionType === 'MultiCollection'
);
expect(multiCollectionItem.multiCollectionId).toBe(99);
});
it('renders the preview table from POST preview', async () => {
window.history.pushState({}, '', '/app/blocks/4');
mockApi({ items: [blockItem({ id: 1, searchTitle: 'News', searchQuery: 'news' })] });
+57 -20
View File
@@ -76,6 +76,18 @@ const MINUTE_OPTIONS = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55].map((m) =>
value: String(m)
}));
// Artist and MultiCollection items can exist on a block (created upstream) even though this
// editor's picker doesn't offer them for new items (parity with Blazor, which comments them
// out). Treat them as read-only here so existing items round-trip instead of being clobbered.
const LEGACY_ONLY_TYPE_LABELS: Partial<Record<CollectionType, string>> = {
Artist: 'Artist',
MultiCollection: 'Multi Collection'
};
function isLegacyOnlyType(type: CollectionType): boolean {
return type in LEGACY_ONLY_TYPE_LABELS;
}
// Conditional playback-order options per collection type (mirrors Blazor BlockEditor.razor).
function playbackOrderOptions(type: CollectionType): Array<{ label: string; value: PlaybackOrder }> {
switch (type) {
@@ -107,6 +119,7 @@ interface DraftItem {
key: string;
collectionType: CollectionType;
collectionId: number | null;
multiCollectionId: number | null;
smartCollectionId: number | null;
mediaItemId: number | null;
selectionName: string;
@@ -138,6 +151,7 @@ function newItem(): DraftItem {
key: nextKey(),
collectionType: 'Collection',
collectionId: null,
multiCollectionId: null,
smartCollectionId: null,
mediaItemId: null,
selectionName: '',
@@ -151,13 +165,23 @@ function newItem(): DraftItem {
};
}
// Artist and MultiCollection items aren't creatable from this editor's picker (parity with
// Blazor, which comments them out), but existing blocks can already contain them (created
// upstream, and ReplaceBlockItemsHandler fully supports both) — they must still round-trip
// intact through load -> edit -> save instead of silently losing their selection.
function itemFromResponse(item: BlockItem): DraftItem {
let selectionName = '';
if (item.collectionType === 'Collection') {
selectionName = item.collectionName ?? '';
} else if (item.collectionType === 'SmartCollection') {
selectionName = item.smartCollectionName ?? '';
} else if (item.collectionType === 'TelevisionShow' || item.collectionType === 'TelevisionSeason') {
} else if (item.collectionType === 'MultiCollection') {
selectionName = item.multiCollectionName ?? '';
} else if (
item.collectionType === 'TelevisionShow' ||
item.collectionType === 'TelevisionSeason' ||
item.collectionType === 'Artist'
) {
selectionName = item.mediaItemName ?? '';
}
@@ -165,6 +189,7 @@ function itemFromResponse(item: BlockItem): DraftItem {
key: nextKey(),
collectionType: item.collectionType as CollectionType,
collectionId: item.collectionId ?? null,
multiCollectionId: item.multiCollectionId ?? null,
smartCollectionId: item.smartCollectionId ?? null,
mediaItemId: item.mediaItemId ?? null,
selectionName,
@@ -182,10 +207,12 @@ function toRequestItem(item: DraftItem): BlockItemRequest {
return {
collectionType: item.collectionType,
collectionId: item.collectionType === 'Collection' ? item.collectionId : null,
multiCollectionId: null,
multiCollectionId: item.collectionType === 'MultiCollection' ? item.multiCollectionId : null,
smartCollectionId: item.collectionType === 'SmartCollection' ? item.smartCollectionId : null,
mediaItemId:
item.collectionType === 'TelevisionShow' || item.collectionType === 'TelevisionSeason'
item.collectionType === 'TelevisionShow' ||
item.collectionType === 'TelevisionSeason' ||
item.collectionType === 'Artist'
? item.mediaItemId
: null,
searchTitle: item.searchTitle,
@@ -1038,25 +1065,34 @@ function BlockEditor({ blockId }: { blockId: number }) {
{selected && (
<Card padded={false} title="Block item">
<Row control={360} first label="Collection type">
<Select
onChange={(event) => {
const collectionType = event.target.value as CollectionType;
const orders = playbackOrderOptions(collectionType);
updateItem(selected.key, {
collectionType,
collectionId: null,
smartCollectionId: null,
mediaItemId: null,
selectionName: '',
playbackOrder: orders[0].value
});
}}
options={COLLECTION_TYPE_OPTIONS}
value={selected.collectionType}
/>
{isLegacyOnlyType(selected.collectionType) ? (
<Input disabled size="sm" value={LEGACY_ONLY_TYPE_LABELS[selected.collectionType] ?? selected.collectionType} />
) : (
<Select
onChange={(event) => {
const collectionType = event.target.value as CollectionType;
const orders = playbackOrderOptions(collectionType);
updateItem(selected.key, {
collectionType,
collectionId: null,
multiCollectionId: null,
smartCollectionId: null,
mediaItemId: null,
selectionName: '',
playbackOrder: orders[0].value
});
}}
options={COLLECTION_TYPE_OPTIONS}
value={selected.collectionType}
/>
)}
</Row>
{selected.collectionType === 'SearchQuery' ? (
{isLegacyOnlyType(selected.collectionType) ? (
<Row control={360} label="Selection">
<Badge tone="accent">{selected.selectionName || '(none)'}</Badge>
</Row>
) : selected.collectionType === 'SearchQuery' ? (
<>
<Row control={360} label="Search title">
<Input
@@ -1081,6 +1117,7 @@ function BlockEditor({ blockId }: { blockId: number }) {
>
<AsyncPicker
currentName={selected.selectionName}
key={`${selected.key}-${selected.collectionType}`}
onSelect={(option) => {
if (selected.collectionType === 'Collection') {
updateItem(selected.key, { collectionId: option.id, selectionName: option.name });