2024-02-27 17:05:28 +01:00
|
|
|
import { useFrigateReviews } from "@/api/ws";
|
|
|
|
import { ReviewSeverity } from "@/types/review";
|
|
|
|
import { Button } from "../ui/button";
|
|
|
|
import { LuRefreshCcw } from "react-icons/lu";
|
2024-02-28 23:23:56 +01:00
|
|
|
import { MutableRefObject, useEffect, useMemo, useState } from "react";
|
2024-02-27 17:05:28 +01:00
|
|
|
|
|
|
|
type NewReviewDataProps = {
|
|
|
|
className: string;
|
|
|
|
contentRef: MutableRefObject<HTMLDivElement | null>;
|
|
|
|
severity: ReviewSeverity;
|
|
|
|
pullLatestData: () => void;
|
|
|
|
};
|
|
|
|
export default function NewReviewData({
|
|
|
|
className,
|
|
|
|
contentRef,
|
|
|
|
severity,
|
|
|
|
pullLatestData,
|
|
|
|
}: NewReviewDataProps) {
|
|
|
|
const { payload: review } = useFrigateReviews();
|
|
|
|
|
2024-02-28 23:23:56 +01:00
|
|
|
const startCheckTs = useMemo(() => Date.now() / 1000, []);
|
|
|
|
const [reviewTs, setReviewTs] = useState(startCheckTs);
|
2024-02-27 17:05:28 +01:00
|
|
|
const [hasUpdate, setHasUpdate] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!review) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (review.type == "end" && review.review.severity == severity) {
|
2024-02-28 23:23:56 +01:00
|
|
|
setReviewTs(review.review.start_time);
|
2024-02-27 17:05:28 +01:00
|
|
|
}
|
2024-02-28 23:23:56 +01:00
|
|
|
}, [review, severity]);
|
2024-02-27 17:05:28 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-02-28 23:23:56 +01:00
|
|
|
if (reviewTs > startCheckTs) {
|
2024-02-27 17:05:28 +01:00
|
|
|
setHasUpdate(true);
|
|
|
|
}
|
2024-02-28 23:23:56 +01:00
|
|
|
}, [startCheckTs, reviewTs]);
|
2024-02-27 17:05:28 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={className}>
|
2024-02-27 17:55:14 +01:00
|
|
|
<div className="flex justify-center items-center md:mr-[100px]">
|
2024-02-27 17:05:28 +01:00
|
|
|
<Button
|
|
|
|
className={`${
|
|
|
|
hasUpdate
|
|
|
|
? "animate-in slide-in-from-top duration-500"
|
|
|
|
: "invisible"
|
|
|
|
} text-center mt-5 mx-auto bg-gray-400 text-white`}
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => {
|
|
|
|
setHasUpdate(false);
|
|
|
|
pullLatestData();
|
|
|
|
if (contentRef.current) {
|
|
|
|
contentRef.current.scrollTo({
|
|
|
|
top: 0,
|
|
|
|
behavior: "smooth",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<LuRefreshCcw className="w-4 h-4 mr-2" />
|
|
|
|
New Items To Review
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|