Skip to content

Commit

Permalink
Improve refreshing of review items (#10675)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 authored Mar 26, 2024
1 parent 7b7d3c5 commit 30d93e1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
39 changes: 11 additions & 28 deletions web/src/components/dynamic/NewReviewData.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
import { useFrigateReviews } from "@/api/ws";
import { ReviewSeverity } from "@/types/review";
import { ReviewSegment } from "@/types/review";
import { Button } from "../ui/button";
import { LuRefreshCcw } from "react-icons/lu";
import { MutableRefObject, useEffect, useMemo, useState } from "react";
import { MutableRefObject, useMemo } from "react";

type NewReviewDataProps = {
className: string;
contentRef: MutableRefObject<HTMLDivElement | null>;
severity: ReviewSeverity;
hasUpdate: boolean;
setHasUpdate: (update: boolean) => void;
reviewItems?: ReviewSegment[] | null;
itemsToReview?: number;
pullLatestData: () => void;
};
export default function NewReviewData({
className,
contentRef,
severity,
hasUpdate,
setHasUpdate,
reviewItems,
itemsToReview,
pullLatestData,
}: NewReviewDataProps) {
const { payload: review } = useFrigateReviews();

const startCheckTs = useMemo(() => Date.now() / 1000, []);
const [reviewTs, setReviewTs] = useState(startCheckTs);

useEffect(() => {
if (!review) {
return;
const hasUpdate = useMemo(() => {
if (!reviewItems || !itemsToReview) {
return false;
}

if (review.type == "end" && review.review.severity == severity) {
setReviewTs(review.review.start_time);
}
}, [review, severity]);

useEffect(() => {
if (reviewTs > startCheckTs) {
setHasUpdate(true);
}
}, [startCheckTs, reviewTs, setHasUpdate]);
return reviewItems.length != itemsToReview;
}, [reviewItems, itemsToReview]);

return (
<div className={className}>
Expand All @@ -52,7 +36,6 @@ export default function NewReviewData({
} text-center mt-5 mx-auto bg-gray-400 text-white`}
variant="secondary"
onClick={() => {
setHasUpdate(false);
pullLatestData();
if (contentRef.current) {
contentRef.current.scrollTo({
Expand Down
27 changes: 19 additions & 8 deletions web/src/pages/Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { useCallback, useEffect, useMemo, useState } from "react";
import useSWR from "swr";

export default function Events() {
const { data: config } = useSWR<FrigateConfig>("config");
const { data: config } = useSWR<FrigateConfig>("config", {
revalidateOnFocus: false,
});
const timezone = useTimezone(config);

// recordings viewer
Expand Down Expand Up @@ -66,6 +68,9 @@ export default function Events() {
};
}, [last24Hours, reviewSearchParams]);

// we want to update the items whenever the severity changes
useEffect(() => setBeforeTs(Date.now() / 1000), [severity]);

const reviewSegmentFetcher = useCallback((key: Array<string> | string) => {
const [path, params] = Array.isArray(key) ? key : [key, undefined];
return axios.get(path, { params }).then((res) => res.data);
Expand Down Expand Up @@ -93,15 +98,21 @@ export default function Events() {

// review summary

const { data: reviewSummary, mutate: updateSummary } = useSWR<ReviewSummary>([
"review/summary",
const { data: reviewSummary, mutate: updateSummary } = useSWR<ReviewSummary>(
[
"review/summary",
{
timezone: timezone,
cameras: reviewSearchParams["cameras"] ?? null,
labels: reviewSearchParams["labels"] ?? null,
},
],
{
timezone: timezone,
cameras: reviewSearchParams["cameras"] ?? null,
labels: reviewSearchParams["labels"] ?? null,
revalidateOnFocus: true,
refreshInterval: 30000,
revalidateOnReconnect: false,
},
{ revalidateOnFocus: false, revalidateOnReconnect: false },
]);
);

const reloadData = useCallback(() => {
setBeforeTs(Date.now() / 1000);
Expand Down
10 changes: 2 additions & 8 deletions web/src/views/events/EventView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,6 @@ function DetectionReview({
[previewTime, setPreviewTime],
);

// review interaction

const [hasUpdate, setHasUpdate] = useState(false);

// timeline interaction

const timelineDuration = useMemo(
Expand Down Expand Up @@ -498,9 +494,8 @@ function DetectionReview({
<NewReviewData
className="absolute w-full z-30 pointer-events-none"
contentRef={contentRef}
severity={severity}
hasUpdate={hasUpdate}
setHasUpdate={setHasUpdate}
reviewItems={currentItems}
itemsToReview={itemsToReview}
pullLatestData={pullLatestData}
/>
)}
Expand Down Expand Up @@ -555,7 +550,6 @@ function DetectionReview({
className="text-white"
variant="select"
onClick={() => {
setHasUpdate(false);
markAllItemsAsReviewed(currentItems ?? []);
}}
>
Expand Down

0 comments on commit 30d93e1

Please sign in to comment.