'use client' import React, { useState, useEffect } from 'react' import * as statusColors from '../../lib/statusColors' interface Detector { detector_id: number name: string location: string status: string object: string floor: number checked: boolean } interface RawDetector { detector_id: number name: string object: string status: string type: string detector_type: string location: string floor: number notifications: Array<{ id: number type: string message: string timestamp: string acknowledged: boolean priority: string }> } interface DetectorListProps { objectId?: string selectedDetectors: number[] onDetectorSelect: (detectorId: number, selected: boolean) => void initialSearchTerm?: string } const DetectorList: React.FC = ({ objectId, selectedDetectors, onDetectorSelect, initialSearchTerm = '' }) => { const [detectors, setDetectors] = useState([]) const [searchTerm, setSearchTerm] = useState(initialSearchTerm) useEffect(() => { const loadDetectors = async () => { try { const res = await fetch('/api/get-detectors', { cache: 'no-store' }) if (!res.ok) return const payload = await res.json() const detectorsData: Record = payload?.data?.detectors ?? {} const rawArray: RawDetector[] = Object.values(detectorsData).filter( (detector) => (objectId ? detector.object === objectId : true) ) const normalized: Detector[] = rawArray.map((d) => ({ detector_id: d.detector_id, name: d.name, location: d.location, status: d.status, object: d.object, floor: d.floor, checked: false, })) console.log('[DetectorList] Payload:', payload) setDetectors(normalized) } catch (e) { console.error('Failed to load detectors:', e) } } loadDetectors() }, [objectId]) const filteredDetectors = detectors.filter(detector => { const matchesSearch = searchTerm === '' || detector.detector_id.toString() === searchTerm return matchesSearch }) return (
setSearchTerm(e.target.value)} className="bg-[#161824] text-white placeholder-gray-400 px-4 py-2 rounded-lg border border-gray-600 focus:border-blue-500 focus:outline-none w-64 text-sm font-medium" style={{ fontFamily: 'Inter, sans-serif' }} />
{/* Таблица детекторов */}
{filteredDetectors.map((detector) => { const isSelected = selectedDetectors.includes(detector.detector_id) return ( ) })}
0} onChange={(e) => { if (e.target.checked) { filteredDetectors.forEach(detector => { if (!selectedDetectors.includes(detector.detector_id)) { onDetectorSelect(detector.detector_id, true) } }) } else { filteredDetectors.forEach(detector => { if (selectedDetectors.includes(detector.detector_id)) { onDetectorSelect(detector.detector_id, false) } }) } }} className="w-4 h-4 text-blue-600 bg-gray-700 border-gray-600 rounded focus:ring-blue-500 focus:ring-2" /> Детектор Статус Местоположение Проверен
onDetectorSelect(detector.detector_id, e.target.checked)} className="w-4 h-4 text-blue-600 bg-gray-700 border-gray-600 rounded focus:ring-blue-500 focus:ring-2" /> {detector.name}
{detector.status === statusColors.STATUS_COLOR_CRITICAL ? 'Критическое' : detector.status === statusColors.STATUS_COLOR_WARNING ? 'Предупреждение' : 'Норма'}
{detector.location} {detector.checked ? (
Да
) : ( Нет )}
{/* Статы детекторров*/}
{filteredDetectors.length}
Всего
{filteredDetectors.filter(d => d.status === statusColors.STATUS_COLOR_NORMAL).length}
Норма
{filteredDetectors.filter(d => d.status === statusColors.STATUS_COLOR_WARNING).length}
Предупреждения
{filteredDetectors.filter(d => d.status === statusColors.STATUS_COLOR_CRITICAL).length}
Критические
{filteredDetectors.length === 0 && (

Детекторы не найдены

)}
) } export default DetectorList