import { h, Fragment } from 'preact'; import ActivityIndicator from '../components/ActivityIndicator'; import Card from '../components/Card'; import CameraImage from '../components/CameraImage'; import ClipIcon from '../icons/Clip'; import MotionIcon from '../icons/Motion'; import SnapshotIcon from '../icons/Snapshot'; import { useDetectState, useRecordingsState, useSnapshotsState } from '../api/mqtt'; import { useMemo } from 'preact/hooks'; import useSWR from 'swr'; export default function Cameras() { const { data: config } = useSWR('config'); return !config ? ( ) : (
); } function SortedCameras({ unsortedCameras }) { const sortedCameras = useMemo(() => Object.entries(unsortedCameras) .filter(([_, conf]) => conf.ui.dashboard) .sort(([_, aConf], [__, bConf]) => aConf.ui.order - bConf.ui.order), [unsortedCameras]); return ( {sortedCameras.map(([camera, conf]) => ( ))} ); } function Camera({ name }) { const { payload: detectValue, send: sendDetect } = useDetectState(name); const { payload: recordValue, send: sendRecordings } = useRecordingsState(name); const { payload: snapshotValue, send: sendSnapshots } = useSnapshotsState(name); const href = `/cameras/${name}`; const buttons = useMemo(() => { return [ { name: 'Events', href: `/events?camera=${name}` }, { name: 'Recordings', href: `/recording/${name}` }, ]; }, [name]); const icons = useMemo( () => [ { name: `Toggle detect ${detectValue === 'ON' ? 'off' : 'on'}`, icon: MotionIcon, color: detectValue === 'ON' ? 'blue' : 'gray', onClick: () => { sendDetect(detectValue === 'ON' ? 'OFF' : 'ON'); }, }, { name: `Toggle recordings ${recordValue === 'ON' ? 'off' : 'on'}`, icon: ClipIcon, color: recordValue === 'ON' ? 'blue' : 'gray', onClick: () => { sendRecordings(recordValue === 'ON' ? 'OFF' : 'ON'); }, }, { name: `Toggle snapshots ${snapshotValue === 'ON' ? 'off' : 'on'}`, icon: SnapshotIcon, color: snapshotValue === 'ON' ? 'blue' : 'gray', onClick: () => { sendSnapshots(snapshotValue === 'ON' ? 'OFF' : 'ON'); }, }, ], [detectValue, sendDetect, recordValue, sendRecordings, snapshotValue, sendSnapshots] ); return ( } /> ); }