'use client'; import { useState, useEffect } from 'react'; import Link from 'next/link'; import Dropdown from '@/components/Dropdown'; type Stream = { id: number; name: string; obs_source_name: string; url: string; }; export default function Home() { const [streams, setStreams] = useState([]); type ScreenType = 'large' | 'left' | 'right' | 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight'; const [activeSources, setActiveSources] = useState>({ large: null, left: null, right: null, topLeft: null, topRight: null, bottomLeft: null, bottomRight: null, }); const [isLoadingStreams, setIsLoadingStreams] = useState(true); const [isLoadingActiveSources, setIsLoadingActiveSources] = useState(true); const [openDropdown, setOpenDropdown] = useState(null); // Manage open dropdown useEffect(() => { // Fetch available streams from the database async function fetchStreams() { setIsLoadingStreams(true); try { const res = await fetch('/api/streams'); const data = await res.json(); setStreams(data); } catch (error) { console.error('Error fetching streams:', error); } finally { setIsLoadingStreams(false); } } // Fetch current active sources from files async function fetchActiveSources() { setIsLoadingActiveSources(true); try { const res = await fetch('/api/getActive'); const data = await res.json(); console.log('Fetched activeSources:', data); // Debug log setActiveSources(data); } catch (error) { console.error('Error fetching active sources:', error); } finally { setIsLoadingActiveSources(false); } } fetchStreams(); fetchActiveSources(); }, []); const handleSetActive = async (screen: ScreenType, id: number | null) => { const selectedStream = streams.find((stream) => stream.id === id); // Update local state setActiveSources((prev) => ({ ...prev, [screen]: selectedStream?.obs_source_name || null, })); // Update the backend try { if (id) { console.log('Setting screen ', screen); const response = await fetch('/api/setActive', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ screen, id }), }); if (!response.ok) { throw new Error('Failed to set active stream'); } const data = await response.json(); console.log(data.message); } } catch (error) { console.error('Error setting active stream:', error); } }; const handleToggleDropdown = (screen: string) => { setOpenDropdown((prev) => (prev === screen ? null : screen)); // Toggle dropdown open/close }; return (
{/* Navigation Links */}
Add New Stream | Manage Teams

Manage Streams

{/* Display loading indicator if either streams or active sources are loading */} {isLoadingStreams || isLoadingActiveSources ? (
Loading...
) : ( <> {/* Large Screen on its own line */}

Large

stream.obs_source_name === activeSources.large)?.id || null } onSelect={(id) => handleSetActive('large', id)} label="Select a Stream..." isOpen={openDropdown === 'large'} onToggle={() => handleToggleDropdown('large')} />
{/* Row for Left and Right Screens */}
{/* Left Screen */}

Left

stream.obs_source_name === activeSources.left)?.id || null } onSelect={(id) => handleSetActive('left', id)} label="Select a Stream..." isOpen={openDropdown === 'left'} onToggle={() => handleToggleDropdown('left')} />
{/* Right Screen */}

Right

stream.obs_source_name === activeSources.right)?.id || null } onSelect={(id) => handleSetActive('right', id)} label="Select a Stream..." isOpen={openDropdown === 'right'} onToggle={() => handleToggleDropdown('right')} />
{/* 2x2 Square for Additional Sources */}
{[ { screen: 'topLeft' as const, label: 'Top Left' }, { screen: 'topRight' as const, label: 'Top Right' }, { screen: 'bottomLeft' as const, label: 'Bottom Left' }, { screen: 'bottomRight' as const, label: 'Bottom Right' }, ].map(({ screen, label }) => (

{label}

stream.obs_source_name === activeSources[screen])?.id || null } onSelect={(id) => handleSetActive(screen, id)} label="Select a Stream..." isOpen={openDropdown === screen} onToggle={() => handleToggleDropdown(screen)} />
))}
)}
); }