'use client'; import React, { useEffect, useState, useCallback } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; import { useToast } from '@/hooks/use-toast'; import { useAudioPlayer } from '@/app/components/AudioPlayerContext'; import { getNavidromeAPI, RadioStation } from '@/lib/navidrome'; import { FaWifi, FaPlay, FaPlus, FaTrash } from 'react-icons/fa6'; const RadioStationsPage = () => { const [stations, setStations] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isAddDialogOpen, setIsAddDialogOpen] = useState(false); const [newStation, setNewStation] = useState({ name: '', streamUrl: '', homePageUrl: '' }); const { toast } = useToast(); const { playTrack } = useAudioPlayer(); const loadRadioStations = useCallback(async () => { setIsLoading(true); try { const api = getNavidromeAPI(); if (!api) { throw new Error('Navidrome API not available'); } const stationList = await api.getInternetRadioStations(); setStations(stationList); } catch (error) { console.error('Failed to load radio stations:', error); toast({ title: "Error", description: "Failed to load radio stations. Please check your Navidrome connection.", variant: "destructive" }); } finally { setIsLoading(false); } }, [toast]); useEffect(() => { loadRadioStations(); }, [loadRadioStations]); const addRadioStation = async () => { if (!newStation.name || !newStation.streamUrl) { toast({ title: "Missing Information", description: "Please provide both name and stream URL.", variant: "destructive" }); return; } try { const api = getNavidromeAPI(); if (!api) { throw new Error('Navidrome API not available'); } await api.createInternetRadioStation( newStation.name, newStation.streamUrl, newStation.homePageUrl || undefined ); toast({ title: "Success", description: "Radio station added successfully.", }); setNewStation({ name: '', streamUrl: '', homePageUrl: '' }); setIsAddDialogOpen(false); await loadRadioStations(); } catch (error) { console.error('Failed to add radio station:', error); toast({ title: "Error", description: "Failed to add radio station.", variant: "destructive" }); } }; const deleteRadioStation = async (stationId: string) => { try { const api = getNavidromeAPI(); if (!api) { throw new Error('Navidrome API not available'); } await api.deleteInternetRadioStation(stationId); toast({ title: "Success", description: "Radio station deleted successfully.", }); await loadRadioStations(); } catch (error) { console.error('Failed to delete radio station:', error); toast({ title: "Error", description: "Failed to delete radio station.", variant: "destructive" }); } }; const playRadioStation = (station: RadioStation) => { const radioTrack = { id: `radio-${station.id}`, name: station.name, url: station.streamUrl, artist: 'Internet Radio', album: 'Live Stream', duration: 0, // Radio streams don't have duration albumId: '', artistId: '' }; playTrack(radioTrack); toast({ title: "Playing Radio", description: `Now playing: ${station.name}`, }); }; if (isLoading) { return (
Loading radio stations...
); } return (

Radio Stations

Listen to internet radio stations.

Add Radio Station Add a new internet radio station to your collection.
setNewStation(prev => ({ ...prev, name: e.target.value }))} />
setNewStation(prev => ({ ...prev, streamUrl: e.target.value }))} />
setNewStation(prev => ({ ...prev, homePageUrl: e.target.value }))} />
{stations.length === 0 ? (

No Radio Stations

You haven't added any radio stations yet. Click the "Add Station" button to get started.

) : (
{stations.map((station) => ( {station.name} {station.homePageUrl && ( Visit Website )}
))}
)}
); }; export default RadioStationsPage;