2021-01-09 18:26:46 +01:00
|
|
|
import { h } 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';
|
|
|
|
import { useDetectState, useClipsState, useSnapshotsState } from '../api/mqtt';
|
2021-02-09 20:35:33 +01:00
|
|
|
import { useConfig, FetchStatus } from '../api';
|
2021-02-02 05:28:25 +01:00
|
|
|
import { useMemo } from 'preact/hooks';
|
2021-01-09 18:26:46 +01:00
|
|
|
|
|
|
|
export default function Cameras() {
|
2021-01-26 16:04:03 +01:00
|
|
|
const { data: config, status } = useConfig();
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-02-09 20:35:33 +01:00
|
|
|
return status !== FetchStatus.LOADED ? (
|
|
|
|
<ActivityIndicator />
|
|
|
|
) : (
|
2021-02-04 00:35:28 +01:00
|
|
|
<div className="grid grid-cols-1 3xl:grid-cols-3 md:grid-cols-2 gap-4">
|
2021-01-09 18:26:46 +01:00
|
|
|
{Object.keys(config.cameras).map((camera) => (
|
|
|
|
<Camera name={camera} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Camera({ name }) {
|
2021-02-18 05:53:57 +01:00
|
|
|
const { payload: detectValue, send: sendDetect } = useDetectState(name);
|
|
|
|
const { payload: clipValue, send: sendClips } = useClipsState(name);
|
|
|
|
const { payload: snapshotValue, send: sendSnapshots } = useSnapshotsState(name);
|
2021-01-09 18:26:46 +01:00
|
|
|
const href = `/cameras/${name}`;
|
2021-05-28 19:13:48 +02:00
|
|
|
const buttons = useMemo(() => [
|
|
|
|
{ name: 'Events', href: `/events?camera=${name}` },
|
|
|
|
{ name: 'Recordings', href: `/recordings/${name}` }
|
|
|
|
], [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: () => {
|
|
|
|
sendDetect(detectValue === 'ON' ? 'OFF' : 'ON');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: `Toggle clips ${clipValue === 'ON' ? 'off' : 'on'}`,
|
|
|
|
icon: ClipIcon,
|
|
|
|
color: clipValue === 'ON' ? 'blue' : 'gray',
|
|
|
|
onClick: () => {
|
|
|
|
sendClips(clipValue === '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, clipValue, sendClips, snapshotValue, sendSnapshots]
|
|
|
|
);
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-02-18 05:53:57 +01:00
|
|
|
return (
|
|
|
|
<Card buttons={buttons} href={href} header={name} icons={icons} media={<CameraImage camera={name} stretch />} />
|
|
|
|
);
|
2021-01-09 18:26:46 +01:00
|
|
|
}
|