frigate/web/src/components/TimelineSummary.jsx

121 lines
3.8 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:54:00 +03:00
import PlayIcon from '../icons/Play';
import ExitIcon from '../icons/Exit';
2023-04-21 16:09:21 +03:00
import { Zone } from '../icons/Zone';
import { useState } from 'preact/hooks';
import { useApiHost } from '../api';
import Button from './Button';
2023-04-21 17:33:35 +03:00
import VideoPlayer from './VideoPlayer';
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 17:33:35 +03:00
const eventDuration = event.end_time - event.start_time;
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 17:33:35 +03:00
const onSelectMoment = async (index) => {
setTimeIndex(index);
if (this.player) {
const videoOffset = this.player.duration() - eventDuration;
const startTime = videoOffset + (eventTimeline[index].timestamp - event.start_time);
this.player.currentTime(startTime);
}
};
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)}
2023-04-21 17:33:35 +03:00
onClick={() => onSelectMoment(index)}
2023-04-21 16:46:14 +03:00
>
2023-04-21 16:54:00 +03:00
{item.class_type == 'visible' ? <PlayIcon className="w-8" /> : <ExitIcon className="w-8" />}
2023-04-21 16:09:21 +03:00
</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)}
2023-04-21 17:33:35 +03:00
onClick={() => onSelectMoment(index)}
2023-04-21 16:46:14 +03:00
>
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>
2023-04-21 17:33:35 +03:00
<VideoPlayer
options={{
preload: 'auto',
autoplay: false,
sources: [
{
src: `${apiHost}vod/event/${event.id}/master.m3u8`,
type: 'application/vnd.apple.mpegurl',
},
],
}}
seekOptions={{ forward: 10, backward: 5 }}
onReady={(player) => {
this.player = player;
}}
onDispose={() => {
this.player = null;
}}
/>
2023-04-21 16:09:21 +03:00
</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
}