2022-04-15 14:23:02 +02:00
|
|
|
import { h, Fragment } from 'preact';
|
2021-02-07 22:46:05 +01:00
|
|
|
import ActivityIndicator from '../components/ActivityIndicator';
|
|
|
|
import Card from '../components/Card';
|
|
|
|
import CameraImage from '../components/CameraImage';
|
2021-02-18 05:53:57 +01:00
|
|
|
import ClipIcon from '../icons/Clip';
|
|
|
|
import MotionIcon from '../icons/Motion';
|
|
|
|
import SnapshotIcon from '../icons/Snapshot';
|
2022-11-24 03:03:20 +01:00
|
|
|
import { useDetectState, useRecordingsState, useSnapshotsState } from '../api/ws';
|
2021-02-02 05:28:25 +01:00
|
|
|
import { useMemo } from 'preact/hooks';
|
2022-02-26 20:11:00 +01:00
|
|
|
import useSWR from 'swr';
|
2021-01-09 18:26:46 +01:00
|
|
|
|
|
|
|
export default function Cameras() {
|
2022-02-26 20:11:00 +01:00
|
|
|
const { data: config } = useSWR('config');
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2022-02-26 20:11:00 +01:00
|
|
|
return !config ? (
|
2021-02-09 20:35:33 +01:00
|
|
|
<ActivityIndicator />
|
|
|
|
) : (
|
2022-02-27 15:04:12 +01:00
|
|
|
<div className="grid grid-cols-1 3xl:grid-cols-3 md:grid-cols-2 gap-4 p-2 px-4">
|
2022-04-15 14:23:02 +02:00
|
|
|
<SortedCameras unsortedCameras={config.cameras} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function SortedCameras({ unsortedCameras }) {
|
2022-04-25 13:59:52 +02:00
|
|
|
const sortedCameras = useMemo(
|
|
|
|
() =>
|
|
|
|
Object.entries(unsortedCameras)
|
|
|
|
.filter(([_, conf]) => conf.ui.dashboard)
|
|
|
|
.sort(([_, aConf], [__, bConf]) => aConf.ui.order - bConf.ui.order),
|
|
|
|
[unsortedCameras]
|
|
|
|
);
|
2022-04-15 14:23:02 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{sortedCameras.map(([camera, conf]) => (
|
2022-02-26 20:11:00 +01:00
|
|
|
<Camera key={camera} name={camera} conf={conf} />
|
2021-01-09 18:26:46 +01:00
|
|
|
))}
|
2022-04-15 14:23:02 +02:00
|
|
|
</Fragment>
|
2021-01-09 18:26:46 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-26 20:11:00 +01:00
|
|
|
function Camera({ name }) {
|
2021-02-18 05:53:57 +01:00
|
|
|
const { payload: detectValue, send: sendDetect } = useDetectState(name);
|
2021-07-11 21:49:10 +02:00
|
|
|
const { payload: recordValue, send: sendRecordings } = useRecordingsState(name);
|
2021-02-18 05:53:57 +01:00
|
|
|
const { payload: snapshotValue, send: sendSnapshots } = useSnapshotsState(name);
|
2021-01-09 18:26:46 +01:00
|
|
|
const href = `/cameras/${name}`;
|
2021-06-06 02:40:52 +02:00
|
|
|
const buttons = useMemo(() => {
|
2022-02-26 20:11:00 +01:00
|
|
|
return [
|
2023-01-04 02:32:56 +01:00
|
|
|
{ name: 'Events', href: `/events?cameras=${name}` },
|
2022-02-26 20:11:00 +01:00
|
|
|
{ name: 'Recordings', href: `/recording/${name}` },
|
|
|
|
];
|
2022-02-12 20:51:28 +01:00
|
|
|
}, [name]);
|
2022-08-25 13:44:34 +02:00
|
|
|
const cleanName = useMemo(
|
|
|
|
() => { return `${name.replaceAll('_', ' ')}` },
|
|
|
|
[name]
|
|
|
|
);
|
2021-02-18 05:53:57 +01:00
|
|
|
const icons = useMemo(
|
|
|
|
() => [
|
|
|
|
{
|
|
|
|
name: `Toggle detect ${detectValue === 'ON' ? 'off' : 'on'}`,
|
|
|
|
icon: MotionIcon,
|
|
|
|
color: detectValue === 'ON' ? 'blue' : 'gray',
|
|
|
|
onClick: () => {
|
2022-04-25 13:59:52 +02:00
|
|
|
sendDetect(detectValue === 'ON' ? 'OFF' : 'ON', true);
|
2021-02-18 05:53:57 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-07-11 21:49:10 +02:00
|
|
|
name: `Toggle recordings ${recordValue === 'ON' ? 'off' : 'on'}`,
|
2021-02-18 05:53:57 +01:00
|
|
|
icon: ClipIcon,
|
2021-07-11 21:49:10 +02:00
|
|
|
color: recordValue === 'ON' ? 'blue' : 'gray',
|
2021-02-18 05:53:57 +01:00
|
|
|
onClick: () => {
|
2022-04-25 13:59:52 +02:00
|
|
|
sendRecordings(recordValue === 'ON' ? 'OFF' : 'ON', true);
|
2021-02-18 05:53:57 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: `Toggle snapshots ${snapshotValue === 'ON' ? 'off' : 'on'}`,
|
|
|
|
icon: SnapshotIcon,
|
|
|
|
color: snapshotValue === 'ON' ? 'blue' : 'gray',
|
|
|
|
onClick: () => {
|
2022-04-25 13:59:52 +02:00
|
|
|
sendSnapshots(snapshotValue === 'ON' ? 'OFF' : 'ON', true);
|
2021-02-18 05:53:57 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2021-07-11 21:49:10 +02:00
|
|
|
[detectValue, sendDetect, recordValue, sendRecordings, snapshotValue, sendSnapshots]
|
2021-02-18 05:53:57 +01:00
|
|
|
);
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-02-18 05:53:57 +01:00
|
|
|
return (
|
2022-08-25 13:44:34 +02:00
|
|
|
<Card buttons={buttons} href={href} header={cleanName} icons={icons} media={<CameraImage camera={name} stretch />} />
|
2021-02-18 05:53:57 +01:00
|
|
|
);
|
2021-01-09 18:26:46 +01:00
|
|
|
}
|