frigate/web/src/components/TimelineSummary.jsx

96 lines
3.1 KiB
React
Raw Normal View History

2023-04-21 02:01:13 +03:00
import { h } from 'preact';
import useSWR from 'swr';
import Heading from './Heading';
import ActivityIndicator from './ActivityIndicator';
import { formatUnixTimestampToDateTime } from '../utils/dateUtil';
2023-04-21 16:09:21 +03:00
import MotionIcon from '../icons/Motion';
import SnapshotIcon from '../icons/Snapshot';
import { Zone } from '../icons/Zone';
import { useState } from 'preact/hooks';
import { useApiHost } from '../api';
import Button from './Button';
2023-04-21 02:01:13 +03:00
export default function TimelineSummary({ event }) {
2023-04-21 16:09:21 +03:00
const apiHost = useApiHost();
2023-04-21 02:01:13 +03:00
const { data: eventTimeline } = useSWR([
'timeline',
{
source_id: event.id,
},
]);
2023-04-21 02:16:08 +03:00
const { data: config } = useSWR('config');
2023-04-21 16:09:21 +03:00
const [timeIndex, setTimeIndex] = useState(0);
2023-04-21 02:16:08 +03:00
if (!eventTimeline || !config) {
2023-04-21 02:01:13 +03:00
return <ActivityIndicator />;
}
return (
<div>
2023-04-21 16:09:21 +03:00
<div className="h-14 flex justify-center">
<div className="w-1/4 flex flex-row flex-nowrap justify-between">
{eventTimeline.map((item, index) =>
item.class_type == 'visible' || item.class_type == 'gone' ? (
2023-04-21 16:46:14 +03:00
<Button
className="rounded-full"
type="text"
color={index == timeIndex ? 'blue' : 'gray'}
aria-label={getTimelineItemDescription(config, item, event)}
onClick={() => setTimeIndex(index)}
>
2023-04-21 16:09:21 +03:00
<MotionIcon className="w-8" />
</Button>
) : (
2023-04-21 16:46:14 +03:00
<Button
className="rounded-full"
type="text"
color={index == timeIndex ? 'blue' : 'gray'}
aria-label={getTimelineItemDescription(config, item, event)}
onClick={() => setTimeIndex(index)}
>
2023-04-21 16:09:21 +03:00
<Zone className="w-8" />
</Button>
)
)}
</div>
</div>
<div className="text-center">
2023-04-21 16:46:14 +03:00
<Heading size="sm">{getTimelineItemDescription(config, eventTimeline[timeIndex], event)}</Heading>
<div className="flex justify-center p-2 py-4">
2023-04-21 16:09:21 +03:00
<img
className="flex-grow-0"
src={`${apiHost}/api/${event.camera}/recordings/${eventTimeline[timeIndex].timestamp}/snapshot.png`}
/>
</div>
</div>
2023-04-21 02:01:13 +03:00
</div>
);
}
2023-04-21 16:09:21 +03:00
function getTimelineItemDescription(config, timelineItem, event) {
2023-04-21 02:01:13 +03:00
if (timelineItem.class_type == 'visible') {
2023-04-21 16:09:21 +03:00
return `${event.label} detected at ${formatUnixTimestampToDateTime(timelineItem.timestamp, {
date_style: 'short',
time_style: 'medium',
time_format: config.ui.time_format,
})}`;
2023-04-21 02:01:13 +03:00
} else if (timelineItem.class_type == 'entered_zone') {
2023-04-21 16:46:14 +03:00
return `${event.label.replaceAll('_', ' ')} entered ${timelineItem.data.zones
.join(' and ')
.replaceAll('_', ' ')} at ${formatUnixTimestampToDateTime(timelineItem.timestamp, {
date_style: 'short',
time_style: 'medium',
time_format: config.ui.time_format,
})}`;
2023-04-21 02:01:13 +03:00
}
2023-04-21 16:09:21 +03:00
return `${event.label} left at ${formatUnixTimestampToDateTime(timelineItem.timestamp, {
date_style: 'short',
time_style: 'medium',
time_format: config.ui.time_format,
})}`;
2023-04-21 02:01:13 +03:00
}