diff --git a/backend/server.js b/backend/server.js index 891bf2f..c2c5661 100644 --- a/backend/server.js +++ b/backend/server.js @@ -62,8 +62,9 @@ app.use('/proxy', proxyRouter); const PORT = 5000; const server = app.listen(PORT, async () => { console.log(`Server listening on Port ${PORT}`); - if (ChannelService.getCurrentChannel().restream()) { - await streamController.start(ChannelService.getCurrentChannel()); + const currentChannel = ChannelService.getCurrentChannel(); + if (currentChannel && currentChannel.restream()) { + await streamController.start(currentChannel); } PlaylistUpdater.startScheduler(); PlaylistUpdater.registerChannelsPlaylist(ChannelService.getChannels()); diff --git a/backend/services/ChannelService.js b/backend/services/ChannelService.js index f49b2e3..4908270 100644 --- a/backend/services/ChannelService.js +++ b/backend/services/ChannelService.js @@ -88,10 +88,17 @@ class ChannelService { throw new Error('Channel does not exist'); } + // Prevent deleting the last channel + if (this.channels.length === 1) { + throw new Error('Cannot delete the last channel'); + } + const [deletedChannel] = this.channels.splice(channelIndex, 1); + // If we deleted the current channel, switch to another one if (this.currentChannel.id === id) { - await this.setCurrentChannel(0); + const nextChannel = this.channels[0]; + await this.setCurrentChannel(nextChannel.id); } if(save) ChannelStorage.save(this.channels); diff --git a/frontend/src/components/add_channel/ChannelModal.tsx b/frontend/src/components/add_channel/ChannelModal.tsx index 2382d36..ebcaba0 100644 --- a/frontend/src/components/add_channel/ChannelModal.tsx +++ b/frontend/src/components/add_channel/ChannelModal.tsx @@ -155,17 +155,17 @@ function ChannelModal({ onClose, channel }: ChannelModalProps) { const handleDelete = () => { if (channel) { + addToast({ + type: "error", + title: `Deleting ${type}`, + duration: 3000, + }); if (type === 'channel') { socketService.deleteChannel(channel.id); } else if (type === 'playlist') { socketService.deletePlaylist(channel.playlist); } } - addToast({ - type: 'error', - title: `${type} deleted`, - duration: 3000, - }); onClose(); };