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.
This commit is contained in:
2026-07-11 00:16:09 +02:00
parent 5d0ad8b0d7
commit f459e74645
2 changed files with 189 additions and 0 deletions
+118
View File
@@ -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(<App />);
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(<App />);
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));
}
+71
View File
@@ -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<Set<number>>(() => new Set());
const [mutationError, setMutationError] = useState<string | null>(null);
const [mutating, setMutating] = useState(false);
const [creatingBlank, setCreatingBlank] = useState(false);
if (query.status === 'loading') {
return <ChannelsLoadingState />;
@@ -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 <ChannelsEmptyState />;
}
@@ -1424,6 +1485,16 @@ function ChannelsScreen() {
</div>
<span className="ctv-channels-spacer" />
<span className="ctv-channels-live"><StatusDot status="live" size={7} /><code>{onAirCount}</code> on air</span>
<Button
disabled={creatingBlank}
loading={creatingBlank}
onClick={() => void createBlankChannel()}
size="sm"
startIcon={<Plus aria-hidden="true" size={14} />}
variant="secondary"
>
New blank channel
</Button>
<Button size="sm" startIcon={<Plus aria-hidden="true" size={14} />} onClick={() => navigateToPath('/app/new-channel')}>Add Channel</Button>
</>
)}