import React, { useState, useEffect } from 'react'; import apiService from '../api'; // Import the API service import './Configuration.css'; const Configuration = () => { const [cameras, setCameras] = useState([]); const [newCamera, setNewCamera] = useState({ id: '', source: '0' }); const [referenceImage, setReferenceImage] = useState(null); const [isSearching, setIsSearching] = useState(false); const [loading, setLoading] = useState(true); // Fetch cameras from API const fetchCameras = async () => { try { const response = await apiService.getCameras(); const data = response.data; // Defensive: if backend returns an error or unexpected shape, fall back to empty list setCameras(Array.isArray(data?.cameras) ? data.cameras : []); setLoading(false); } catch (error) { console.error('Error fetching cameras:', error); setLoading(false); } }; // Effect to fetch cameras on component mount useEffect(() => { fetchCameras(); }, []); const handleAddCamera = async (e) => { e.preventDefault(); if (newCamera.id && newCamera.source) { try { const response = await apiService.addCamera(newCamera.id, newCamera.source); const data = response.data; if (data.status === 'success') { setNewCamera({ id: '', source: '0' }); fetchCameras(); // Refresh the camera list } else { alert('Failed to add camera: ' + data.detail); } } catch (error) { console.error('Error adding camera:', error); alert('Failed to add camera'); } } }; const handleRemoveCamera = async (cameraId) => { try { const response = await apiService.removeCamera(cameraId); const data = response.data; if (data.status === 'success') { fetchCameras(); // Refresh the camera list } else { alert('Failed to remove camera: ' + data.detail); } } catch (error) { console.error('Error removing camera:', error); alert('Failed to remove camera'); } }; const handleImageUpload = (e) => { const file = e.target.files[0]; if (file) { setReferenceImage(file); } }; const handleSearchPerson = async () => { if (referenceImage) { setIsSearching(true); try { // Call the search API using our service const response = await apiService.findPerson(referenceImage); const data = response.data; setIsSearching(false); alert(`Person search completed! Found ${data.total_matches} matches across cameras.`); } catch (error) { console.error('Error searching for person:', error); setIsSearching(false); alert('Failed to search for person: ' + error.message); } } }; if (loading) { return (
Loading configuration...
); } return (
{/* Camera Configuration */}

Camera Configuration

setNewCamera({...newCamera, id: e.target.value})} placeholder="Enter camera ID" required />
setNewCamera({...newCamera, source: e.target.value})} placeholder="0 for webcam, RTSP URL for IP camera" required />
{/* Reference Image Upload */}

Person Search - Upload Reference Image

{referenceImage && (
Reference
{referenceImage.name} {(referenceImage.size / 1024).toFixed(1)} KB
)}
{/* Camera Management */}

Camera Management

{cameras.length > 0 ? (
{cameras.map(camera => (
{camera.id}
{camera.type}
{camera.source}
))}
) : (

No cameras configured.

)}
{/* System Controls */}

System Controls

); }; export default Configuration;