frigate/web/src/pages/Events.tsx

227 lines
7.4 KiB
TypeScript
Raw Normal View History

2024-02-18 01:43:48 +03:00
import TimeAgo from "@/components/dynamic/TimeAgo";
2024-02-18 02:02:37 +03:00
import PreviewThumbnailPlayer from "@/components/player/PreviewThumbnailPlayer";
2024-02-18 01:43:48 +03:00
import ActivityIndicator from "@/components/ui/activity-indicator";
2024-02-18 01:15:15 +03:00
import { Button } from "@/components/ui/button";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
2024-02-18 01:43:48 +03:00
import { FrigateConfig } from "@/types/frigateConfig";
import { ReviewSegment, ReviewSeverity } from "@/types/review";
import { formatUnixTimestampToDateTime } from "@/utils/dateUtil";
2024-02-18 04:03:51 +03:00
import { getIconForLabel } from "@/utils/iconUtil";
2024-02-18 02:40:04 +03:00
import axios from "axios";
import { useCallback, useMemo, useRef, useState } from "react";
2024-02-18 01:15:15 +03:00
import { LuCalendar, LuFilter, LuVideo } from "react-icons/lu";
import { MdCircle } from "react-icons/md";
2024-02-18 01:43:48 +03:00
import useSWR from "swr";
2024-02-18 02:40:04 +03:00
import useSWRInfinite from "swr/infinite";
2024-02-18 04:03:51 +03:00
const API_LIMIT = 20;
2024-02-18 01:15:15 +03:00
export default function Events() {
2024-02-18 01:43:48 +03:00
const { data: config } = useSWR<FrigateConfig>("config");
2024-02-18 01:15:15 +03:00
const [severity, setSeverity] = useState<ReviewSeverity>("alert");
2024-02-18 02:40:04 +03:00
// review paging
const reviewSearchParams = {};
const reviewSegmentFetcher = useCallback((key: any) => {
const [path, params] = Array.isArray(key) ? key : [key, undefined];
return axios.get(path, { params }).then((res) => res.data);
}, []);
const getKey = useCallback(
(index: number, prevData: ReviewSegment[]) => {
if (index > 0) {
const lastDate = prevData[prevData.length - 1].start_time;
const pagedParams = reviewSearchParams
? { before: lastDate, limit: API_LIMIT, severity: severity }
: {
...reviewSearchParams,
before: lastDate,
limit: API_LIMIT,
};
return ["review", pagedParams];
}
const params = reviewSearchParams
? { limit: API_LIMIT, severity: severity }
: { ...reviewSearchParams, limit: API_LIMIT };
return ["review", params];
},
[reviewSearchParams]
);
const {
data: reviewPages,
mutate: updateSegments,
size,
setSize,
isValidating,
} = useSWRInfinite<ReviewSegment[]>(getKey, reviewSegmentFetcher);
const isDone = useMemo(
() => (reviewPages?.at(-1)?.length ?? 0) < API_LIMIT,
[reviewPages]
);
const observer = useRef<IntersectionObserver | null>();
const lastReviewRef = useCallback(
(node: HTMLElement | null) => {
if (isValidating) return;
if (observer.current) observer.current.disconnect();
try {
observer.current = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && !isDone) {
setSize(size + 1);
}
});
if (node) observer.current.observe(node);
} catch (e) {
// no op
}
},
[isValidating, isDone]
);
// preview videos
2024-02-18 01:43:48 +03:00
const previewTimes = useMemo(() => {
2024-02-18 04:03:51 +03:00
if (
!reviewPages ||
reviewPages.length == 0 ||
reviewPages.at(-1)!!.length == 0
) {
2024-02-18 01:43:48 +03:00
return undefined;
}
const startDate = new Date();
startDate.setMinutes(0, 0, 0);
2024-02-18 02:40:04 +03:00
const endDate = new Date(reviewPages.at(-1)!!.at(-1)!!.end_time);
2024-02-18 01:43:48 +03:00
endDate.setHours(0, 0, 0, 0);
return {
start: startDate.getTime() / 1000,
end: endDate.getTime() / 1000,
};
2024-02-18 02:40:04 +03:00
}, [reviewPages]);
2024-02-18 01:43:48 +03:00
const { data: allPreviews } = useSWR<Preview[]>(
previewTimes
? `preview/all/start/${previewTimes.start}/end/${previewTimes.end}`
: null,
{ revalidateOnFocus: false }
);
if (!config) {
return <ActivityIndicator />;
}
2024-02-18 01:15:15 +03:00
return (
<>
<div className="w-full flex justify-between">
<ToggleGroup
type="single"
defaultValue="alert"
size="sm"
onValueChange={(value: ReviewSeverity) => setSeverity(value)}
>
<ToggleGroupItem
className={`px-3 py-4 rounded-2xl ${
severity == "alert" ? "" : "text-gray-500"
}`}
value="alert"
aria-label="Select alerts"
>
<MdCircle className="w-2 h-2 mr-[10px] text-danger" />
Alerts
</ToggleGroupItem>
<ToggleGroupItem
className={`px-3 py-4 rounded-2xl ${
severity == "detection" ? "" : "text-gray-500"
}`}
value="detection"
aria-label="Select detections"
>
<MdCircle className="w-2 h-2 mr-[10px] text-orange-400" />
Detections
</ToggleGroupItem>
<ToggleGroupItem
className={`px-3 py-4 rounded-2xl ${
severity == "significant_motion" ? "" : "text-gray-500"
}`}
value="significant_motion"
aria-label="Select motion"
>
<MdCircle className="w-2 h-2 mr-[10px] text-yellow-400" />
Motion
</ToggleGroupItem>
</ToggleGroup>
<div>
<Button className="mx-1" variant="secondary">
<LuVideo className=" mr-[10px]" />
All Cameras
</Button>
<Button className="mx-1" variant="secondary">
<LuCalendar className=" mr-[10px]" />
2024-02-18 02:02:37 +03:00
Fab 17
2024-02-18 01:15:15 +03:00
</Button>
<Button className="mx-1" variant="secondary">
<LuFilter className=" mr-[10px]" />
Filter
</Button>
</div>
</div>
2024-02-18 01:43:48 +03:00
<div className="flex flex-wrap gap-2 mt-2">
2024-02-18 02:40:04 +03:00
{reviewPages?.map((reviewSegments, pageIdx) => {
return reviewSegments.map((value, segIdx) => {
const lastRow =
pageIdx == size - 1 && segIdx == reviewSegments.length - 1;
2024-02-18 02:02:37 +03:00
const detectConfig = config.cameras[value.camera].detect;
const relevantPreview = Object.values(allPreviews || []).find(
(preview) =>
preview.camera == value.camera &&
preview.start < value.start_time &&
preview.end > value.end_time
);
2024-02-18 01:43:48 +03:00
return (
2024-02-18 04:03:51 +03:00
<div key={value.id}>
2024-02-18 02:40:04 +03:00
<div
ref={lastRow ? lastReviewRef : null}
className="relative h-[234px] rounded-2xl overflow-hidden"
style={{
aspectRatio: detectConfig.width / detectConfig.height,
}}
>
2024-02-18 04:03:51 +03:00
<PreviewThumbnailPlayer
relevantPreview={relevantPreview}
camera={value.camera}
startTs={value.start_time}
isMobile={false}
eventId=""
/>
{(severity == "alert" || severity == "detection") && (
<div className="absolute top-1 right-1 flex">
{value.data.objects.map((object) => {
return getIconForLabel(object);
})}
2024-02-18 02:40:04 +03:00
</div>
)}
<div className="absolute left-1 right-1 bottom-1 flex justify-between">
<TimeAgo time={value.start_time * 1000} />
{formatUnixTimestampToDateTime(value.start_time, {
strftime_fmt:
config.ui.time_format == "24hour"
? "%b %-d, %H:%M"
: "%b %-d, %I:%M %p",
})}
2024-02-18 02:02:37 +03:00
</div>
2024-02-18 01:43:48 +03:00
</div>
2024-02-18 02:40:04 +03:00
{lastRow && !isDone && <ActivityIndicator />}
2024-02-18 04:03:51 +03:00
</div>
2024-02-18 01:43:48 +03:00
);
2024-02-18 02:40:04 +03:00
});
2024-02-18 01:43:48 +03:00
})}
</div>
2024-02-18 01:15:15 +03:00
</>
);
}