frigate/web/src/routes/Camera_V2.jsx

146 lines
4.3 KiB
React
Raw Normal View History

2022-01-13 08:55:18 +03:00
import { h, Fragment } from 'preact';
2022-01-13 01:53:50 +03:00
import AutoUpdatingCameraImage from '../components/AutoUpdatingCameraImage';
import JSMpegPlayer from '../components/JSMpegPlayer';
import Heading from '../components/Heading';
import Link from '../components/Link';
import Switch from '../components/Switch';
import { usePersistence } from '../context';
2022-01-13 08:55:18 +03:00
import { useCallback, useEffect, useMemo, useState } from 'preact/hooks';
import { useConfig } from '../api';
2022-01-13 01:53:50 +03:00
import { Tabs, TextTab } from '../components/Tabs';
import { LiveChip } from '../components/LiveChip';
import HistoryViewer from '../components/HistoryViewer';
2022-01-13 01:53:50 +03:00
const emptyObject = Object.freeze({});
export default function Camera({ camera }) {
const { data: config } = useConfig();
2022-01-13 02:10:59 +03:00
const [playerType, setPlayerType] = useState('live');
2022-01-13 01:53:50 +03:00
const cameraConfig = config?.cameras[camera];
2022-01-13 02:10:59 +03:00
const liveWidth = Math.round(cameraConfig.live.height * (cameraConfig.detect.width / cameraConfig.detect.height));
2022-01-13 01:53:50 +03:00
const [options, setOptions] = usePersistence(`${camera}-feed`, emptyObject);
const handleSetOption = useCallback(
(id, value) => {
const newOptions = { ...options, [id]: value };
setOptions(newOptions);
},
[options, setOptions]
);
const searchParams = useMemo(
() =>
new URLSearchParams(
Object.keys(options).reduce((memo, key) => {
memo.push([key, options[key] === true ? '1' : '0']);
return memo;
}, [])
),
[options]
);
const optionContent = (
<div className='grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4'>
2022-01-13 01:53:50 +03:00
<Switch
checked={options['bbox']}
id='bbox'
2022-01-13 01:53:50 +03:00
onChange={handleSetOption}
label='Bounding box'
labelPosition='after'
2022-01-13 01:53:50 +03:00
/>
<Switch
checked={options['timestamp']}
id='timestamp'
2022-01-13 01:53:50 +03:00
onChange={handleSetOption}
label='Timestamp'
labelPosition='after'
2022-01-13 01:53:50 +03:00
/>
<Switch checked={options['zones']} id='zones' onChange={handleSetOption} label='Zones' labelPosition='after' />
<Switch checked={options['mask']} id='mask' onChange={handleSetOption} label='Masks' labelPosition='after' />
2022-01-13 01:53:50 +03:00
<Switch
checked={options['motion']}
id='motion'
2022-01-13 01:53:50 +03:00
onChange={handleSetOption}
label='Motion boxes'
labelPosition='after'
2022-01-13 01:53:50 +03:00
/>
<Switch
checked={options['regions']}
id='regions'
2022-01-13 01:53:50 +03:00
onChange={handleSetOption}
label='Regions'
labelPosition='after'
2022-01-13 01:53:50 +03:00
/>
<Link href={`/cameras/${camera}/editor`}>Mask & Zone creator</Link>
</div>
2022-01-13 02:10:59 +03:00
);
2022-01-13 01:53:50 +03:00
let renderPlayer;
switch (playerType) {
case 'live':
renderPlayer = (
<Fragment>
<div>
<JSMpegPlayer camera={camera} width={liveWidth} height={cameraConfig.live.height} />
</div>
</Fragment>
2022-01-13 01:53:50 +03:00
);
break;
case 'history':
renderPlayer = <HistoryViewer camera={camera} />;
break;
case 'debug':
renderPlayer = (
<Fragment>
<AutoUpdatingCameraImage camera={camera} searchParams={searchParams} />
{optionContent}
</Fragment>
2022-01-13 02:10:59 +03:00
);
break;
default:
break;
}
2022-01-13 01:53:50 +03:00
const handleTabChange = (index) => {
if (index === 0) {
2022-01-13 02:10:59 +03:00
setPlayerType('history');
} else if (index === 1) {
setPlayerType('live');
2022-01-13 01:53:50 +03:00
} else if (index === 2) {
2022-01-13 02:10:59 +03:00
setPlayerType('debug');
2022-01-13 01:53:50 +03:00
}
2022-01-13 02:10:59 +03:00
};
2022-01-13 01:53:50 +03:00
return (
<div className='flex bg-gray-900 w-full h-full justify-center'>
<div className='relative max-w-screen-md flex-grow w-full'>
<div className='absolute top-0 text-white w-full'>
<div className='flex pt-4 pl-4 items-center w-full h-16 z10'>
2022-01-13 02:10:59 +03:00
{(playerType === 'live' || playerType === 'debug') && (
2022-01-13 01:53:50 +03:00
<Fragment>
<Heading size='xl' className='mr-2'>
2022-01-13 02:10:59 +03:00
{camera}
</Heading>
2022-01-13 01:53:50 +03:00
<LiveChip />
</Fragment>
2022-01-13 02:10:59 +03:00
)}
2022-01-13 01:53:50 +03:00
</div>
</div>
<div className='flex flex-col justify-center h-full'>{renderPlayer}</div>
2022-01-13 01:53:50 +03:00
<div className='absolute flex justify-center bottom-8 w-full'>
<Tabs selectedIndex={1} onChange={handleTabChange} className='justify'>
<TextTab text='History' />
<TextTab text='Live' />
<TextTab text='Debug' />
2022-01-13 01:53:50 +03:00
</Tabs>
</div>
</div>
</div>
);
}