blakeblackshear.frigate/web/src/routes/Cameras.jsx

66 lines
2.2 KiB
React
Raw Normal View History

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';
import ClipIcon from '../icons/Clip';
import MotionIcon from '../icons/Motion';
import SnapshotIcon from '../icons/Snapshot';
import { useDetectState, useClipsState, useSnapshotsState } from '../api/mqtt';
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
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 }) {
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-02-02 05:28:25 +01:00
const buttons = useMemo(() => [{ name: 'Events', href: `/events?camera=${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 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
return (
<Card buttons={buttons} href={href} header={name} icons={icons} media={<CameraImage camera={name} stretch />} />
);
2021-01-09 18:26:46 +01:00
}