import { useState } from 'react'; import { useConfigStore } from '@/stores/configStore'; import { discoverCameras } from '@/services/api'; import type { CameraConfig } from '@/types/config'; export function CameraSettings() { const config = useConfigStore((s) => s.config); const saveConfig = useConfigStore((s) => s.saveConfig); const [cameras, setCameras] = useState(config?.cameras ?? []); const [saving, setSaving] = useState(false); const [discovering, setDiscovering] = useState(false); if (!config) return null; const handleToggle = (idx: number) => { const updated = [...cameras]; updated[idx] = { ...updated[idx], enabled: !updated[idx].enabled }; setCameras(updated); }; const handleNameChange = (idx: number, display_name: string) => { const updated = [...cameras]; updated[idx] = { ...updated[idx], display_name }; setCameras(updated); }; const handleMoveUp = (idx: number) => { if (idx === 0) return; const updated = [...cameras]; [updated[idx - 1], updated[idx]] = [updated[idx], updated[idx - 1]]; updated.forEach((c, i) => c.order = i); setCameras(updated); }; const handleMoveDown = (idx: number) => { if (idx >= cameras.length - 1) return; const updated = [...cameras]; [updated[idx], updated[idx + 1]] = [updated[idx + 1], updated[idx]]; updated.forEach((c, i) => c.order = i); setCameras(updated); }; const handleRemove = (idx: number) => { setCameras(cameras.filter((_, i) => i !== idx)); }; const handleDiscover = async () => { setDiscovering(true); const streams = await discoverCameras(config.go2rtc.url); const existing = new Set(cameras.map((c) => c.name)); const newCams = streams .filter((s) => !existing.has(s)) .map((name, i) => ({ name, display_name: name.replace(/_/g, ' '), enabled: true, order: cameras.length + i, })); if (newCams.length > 0) { setCameras([...cameras, ...newCams]); } setDiscovering(false); }; const handleSave = async () => { setSaving(true); await saveConfig({ ...config, cameras }); setSaving(false); }; return (

{cameras.length} Cameras

{cameras.map((cam, idx) => (
{/* Toggle */} {/* Stream name */} {cam.name} {/* Display name */} handleNameChange(idx, e.target.value)} /> {/* Reorder */} {/* Remove */}
))}
); }