From f459e74645335c49be5ed6cc49e290f29a79cf53 Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 11 Jul 2026 00:16:09 +0200 Subject: [PATCH] feat(spa): bare-channel create from the channels list (#212) Adds a "New blank channel" action next to "Add Channel" on ChannelsScreen (web/src/App.tsx) that POSTs CreateChannelRequest with Blazor's add-mode defaults (ChannelEditor.razor's else branch) via the new createChannel client, then navigates to the channel's editor. Distinct from "Add Channel", which remains the library-to-lineup ChannelBuilder flow and is untouched. --- web/src/App.test.tsx | 118 +++++++++++++++++++++++++++++++++++++++++++ web/src/App.tsx | 71 ++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) diff --git a/web/src/App.test.tsx b/web/src/App.test.tsx index 9d6bb692e..02970b99e 100644 --- a/web/src/App.test.tsx +++ b/web/src/App.test.tsx @@ -742,6 +742,113 @@ describe('ChicoryTV SPA scaffold', () => { ); }); + it('creates a blank channel with Blazor-parity defaults and navigates to its editor', async () => { + mockDashboardApi({ + channels: [ + { + ffmpegProfile: 'HLS Direct', + group: 'Kids', + id: 1, + isEnabled: true, + language: 'en', + name: 'Retro Cartoons', + number: '5.1', + showInEpg: true, + sortNumber: 5.1, + streamingMode: 'HLS Direct' + }, + { + ffmpegProfile: 'MPEG-TS', + group: 'News', + id: 2, + isEnabled: true, + language: 'fr', + name: 'News 24', + number: '24', + showInEpg: false, + sortNumber: 24, + streamingMode: 'MPEG-TS' + } + ], + createChannelResponse: { id: 42 }, + ffmpegSettings: defaultFfmpegSettings({ defaultFFmpegProfileId: 7 }) + }); + + render(); + + fireEvent.click(screen.getByRole('link', { name: 'Channels' })); + expect(await screen.findByText('Retro Cartoons')).toBeInTheDocument(); + + fireEvent.click(screen.getByRole('button', { name: 'New blank channel' })); + + await waitFor(() => + expect(window.fetch).toHaveBeenCalledWith('/api/channels', expect.objectContaining({ method: 'POST' })) + ); + + const body = requestBodyFor('/api/channels'); + expect(body).toMatchObject({ + categories: null, + fallbackFillerId: null, + ffmpegProfileId: 7, + group: 'ErsatzTV', + idleBehavior: 'StopOnDisconnect', + isEnabled: true, + logo: { path: null, contentType: null }, + mirrorSourceChannelId: null, + musicVideoCreditsMode: 'None', + musicVideoCreditsTemplate: null, + name: 'New Channel', + number: '25', + playoutMode: 'Continuous', + playoutOffset: null, + playoutSource: 'Generated', + preferredAudioLanguageCode: null, + preferredAudioTitle: null, + preferredSubtitleLanguageCode: null, + showInEpg: true, + slugSeconds: null, + songVideoMode: 'Default', + streamingMode: 'TransportStreamHybrid', + streamSelector: null, + streamSelectorMode: 'Default', + subtitleMode: 'None', + transcodeMode: 'OnDemand', + watermarkId: null + }); + + await waitFor(() => expect(window.location.pathname).toBe('/app/edit-channel/42')); + }); + + it('surfaces a 422 error from bare-channel create without navigating', async () => { + mockDashboardApi({ + channels: [ + { + ffmpegProfile: 'HLS Direct', + group: 'Kids', + id: 1, + isEnabled: true, + language: 'en', + name: 'Retro Cartoons', + number: '5.1', + showInEpg: true, + sortNumber: 5.1, + streamingMode: 'HLS Direct' + } + ], + createChannelFailure: { detail: 'A channel with that number already exists', status: 422, title: 'Validation failed' } + }); + + render(); + + fireEvent.click(screen.getByRole('link', { name: 'Channels' })); + expect(await screen.findByText('Retro Cartoons')).toBeInTheDocument(); + + fireEvent.click(screen.getByRole('button', { name: 'New blank channel' })); + + expect(await screen.findByText('A channel with that number already exists')).toBeInTheDocument(); + expect(window.location.pathname).toBe('/app/channels'); + }); + it('clears the selection when the channel view filter changes', async () => { mockDashboardApi({ channels: [ @@ -3750,6 +3857,8 @@ function mockDashboardApi({ channels = [], channelStates = [], channelTemplates = [], + createChannelFailure = null, + createChannelResponse = null, createTemplateResponse = null, defaultChannelTemplate = null, playoutAlternateSchedules = [], @@ -3817,6 +3926,8 @@ function mockDashboardApi({ channels?: unknown[]; channelStates?: unknown[]; channelTemplates?: unknown[]; + createChannelFailure?: { detail?: string; status?: number; title?: string } | null; + createChannelResponse?: unknown; createTemplateResponse?: unknown; defaultChannelTemplate?: unknown; playoutAlternateSchedules?: unknown[]; @@ -3903,6 +4014,13 @@ function mockDashboardApi({ vi.spyOn(window, 'fetch').mockImplementation((input: RequestInfo | URL, init?: RequestInit) => { const path = input.toString(); + if (path === '/api/channels' && (init?.method ?? 'GET') === 'POST') { + if (createChannelFailure) { + return Promise.resolve(jsonResponse(createChannelFailure, createChannelFailure.status ?? 422)); + } + return Promise.resolve(jsonResponse(createChannelResponse ?? { id: 99 }, 201)); + } + if (path === '/api/channels') { return Promise.resolve(jsonResponse(channels)); } diff --git a/web/src/App.tsx b/web/src/App.tsx index d79019e78..dcc3daf05 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -132,8 +132,10 @@ import { erasePlayoutItemsAndHistory, getPlayoutItemSchedulingContext, resetChannelPlayout, + createChannel, deleteChannel, getDecos, + getFfmpegSettings, messageFromError, addScheduleItem, deleteScheduleItem, @@ -155,6 +157,7 @@ import { type ChannelSummary, type ChannelGuideChannel, type ChannelGuideProgramme, + type CreateChannelRequest, type CreatePlayoutRequest, type DashboardChannel, type DecoListItem, @@ -1268,6 +1271,7 @@ function ChannelsScreen() { const [selectedIds, setSelectedIds] = useState>(() => new Set()); const [mutationError, setMutationError] = useState(null); const [mutating, setMutating] = useState(false); + const [creatingBlank, setCreatingBlank] = useState(false); if (query.status === 'loading') { return ; @@ -1398,6 +1402,63 @@ function ChannelsScreen() { void refreshAfterMutation(() => deleteChannel(channel.id)); }; + // Blazor parity (ChannelEditor.razor "add" branch): a blank channel is created with these + // defaults, then the editor opens for the rest of the fields. Distinct from "Add Channel", + // which is the library-to-lineup ChannelBuilder flow (untouched here). + const createBlankChannel = async () => { + if (creatingBlank) { + return; + } + + setMutationError(null); + setCreatingBlank(true); + + try { + const ffmpegSettings = await getFfmpegSettings(); + const maxNumber = channels.reduce((max, channel) => { + const parsed = Number.parseInt(channel.number.split('.')[0] ?? '', 10); + return Number.isFinite(parsed) ? Math.max(max, parsed) : max; + }, 0); + + const body: CreateChannelRequest = { + categories: null, + fallbackFillerId: null, + ffmpegProfileId: ffmpegSettings.defaultFFmpegProfileId, + group: 'ErsatzTV', + idleBehavior: 'StopOnDisconnect', + isEnabled: true, + logo: { path: null, contentType: null }, + mirrorSourceChannelId: null, + musicVideoCreditsMode: 'None', + musicVideoCreditsTemplate: null, + name: 'New Channel', + number: String(maxNumber + 1), + playoutMode: 'Continuous', + playoutOffset: null, + playoutSource: 'Generated', + preferredAudioLanguageCode: null, + preferredAudioTitle: null, + preferredSubtitleLanguageCode: null, + showInEpg: true, + slugSeconds: null, + songVideoMode: 'Default', + streamingMode: 'TransportStreamHybrid', + streamSelector: null, + streamSelectorMode: 'Default', + subtitleMode: 'None', + transcodeMode: 'OnDemand', + watermarkId: null + }; + + const created = await createChannel(body); + navigateToPath(`/app/edit-channel/${created.id}`); + } catch (error: unknown) { + setMutationError(messageFromError(error)); + } finally { + setCreatingBlank(false); + } + }; + if (channels.length === 0) { return ; } @@ -1424,6 +1485,16 @@ function ChannelsScreen() { {onAirCount} on air + )}