frigate/web/src/components/TimelineSummary.jsx

88 lines
2.8 KiB
React
Raw Normal View History

2023-04-21 02:01:13 +03:00
import { h } from 'preact';
import useSWR from 'swr';
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 Button from './Button';
2023-04-21 02:01:13 +03:00
2023-04-21 17:47:48 +03:00
export default function TimelineSummary({ event, onFrameSelected }) {
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 17:47:48 +03:00
const [timeIndex, setTimeIndex] = useState(-1);
2023-04-21 16:09:21 +03:00
2023-04-21 17:33:35 +03:00
const onSelectMoment = async (index) => {
setTimeIndex(index);
2023-04-21 17:47:48 +03:00
onFrameSelected(eventTimeline[index].timestamp);
2023-04-21 17:33:35 +03:00
};
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>
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
}