'use client'; /** * SourceSelector - Component for selecting video source in player * Following Liquid Glass design system */ import { useState, useCallback, useEffect, useMemo } from 'react'; import Image from 'next/image'; import { Card } from '@/components/ui/Card'; import { Badge } from '@/components/ui/Badge'; import { Icons } from '@/components/ui/Icon'; import { LatencyBadge } from '@/components/ui/LatencyBadge'; import { Button } from '@/components/ui/Button'; export interface SourceInfo { id: string | number; source: string; sourceName?: string; latency?: number; pic?: string; } interface SourceSelectorProps { sources: SourceInfo[]; currentSource: string; onSourceChange: (source: SourceInfo) => void; className?: string; } export function SourceSelector({ sources, currentSource, onSourceChange, className = '', }: SourceSelectorProps) { const [isLoading, setIsLoading] = useState(false); const [latencies, setLatencies] = useState>({}); // Sort sources by latency const sortedSources = useMemo(() => { return [...sources].sort((a, b) => { const latA = latencies[a.source] ?? a.latency ?? Infinity; const latB = latencies[b.source] ?? b.latency ?? Infinity; return latA - latB; }); }, [sources, latencies]); // Refresh latency for all sources const refreshLatencies = useCallback(async () => { setIsLoading(true); const results = await Promise.all( sources.map(async (source) => { try { // Use the stored baseUrl or extract from source const response = await fetch('/api/ping', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: source.source, // This should be the baseUrl ideally }), }); if (response.ok) { const data = await response.json(); return { source: source.source, latency: data.latency }; } } catch { // Ignore errors } return { source: source.source, latency: undefined }; }) ); const newLatencies: Record = {}; results.forEach(({ source, latency }) => { if (latency !== undefined) { newLatencies[source] = latency; } }); setLatencies(newLatencies); setIsLoading(false); }, [sources]); // Initialize latencies from sources useEffect(() => { const initial: Record = {}; sources.forEach(s => { if (s.latency !== undefined) { initial[s.source] = s.latency; } }); setLatencies(initial); }, [sources]); if (sources.length <= 1) { return null; } return (

{sources.length}

{sortedSources.map((source, index) => { const isCurrent = source.source === currentSource; const latency = latencies[source.source] ?? source.latency; return ( ); })}
); }