Simplify ws updating (#12390)

* Simplify ws updating

* Simplify return values
This commit is contained in:
Nicolas Mowen 2024-07-11 09:25:33 -06:00 committed by GitHub
parent fe4a737421
commit e416e44998
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 14 additions and 18 deletions

View File

@ -11,6 +11,7 @@ import {
import { FrigateStats } from "@/types/stats"; import { FrigateStats } from "@/types/stats";
import useSWR from "swr"; import useSWR from "swr";
import { createContainer } from "react-tracked"; import { createContainer } from "react-tracked";
import useDeepMemo from "@/hooks/use-deep-memo";
type Update = { type Update = {
topic: string; topic: string;
@ -206,18 +207,18 @@ export function useFrigateEvents(): { payload: FrigateEvent } {
return { payload: JSON.parse(payload as string) }; return { payload: JSON.parse(payload as string) };
} }
export function useFrigateReviews(): { payload: FrigateReview } { export function useFrigateReviews(): FrigateReview {
const { const {
value: { payload }, value: { payload },
} = useWs("reviews", ""); } = useWs("reviews", "");
return { payload: JSON.parse(payload as string) }; return useDeepMemo(JSON.parse(payload as string));
} }
export function useFrigateStats(): { payload: FrigateStats } { export function useFrigateStats(): FrigateStats {
const { const {
value: { payload }, value: { payload },
} = useWs("stats", ""); } = useWs("stats", "");
return { payload: JSON.parse(payload as string) }; return useDeepMemo(JSON.parse(payload as string));
} }
export function useInitialCameraState( export function useInitialCameraState(
@ -230,7 +231,8 @@ export function useInitialCameraState(
value: { payload }, value: { payload },
send: sendCommand, send: sendCommand,
} = useWs("camera_activity", "onConnect"); } = useWs("camera_activity", "onConnect");
const data = JSON.parse(payload as string);
const data = useDeepMemo(JSON.parse(payload as string));
useEffect(() => { useEffect(() => {
let listener = undefined; let listener = undefined;

View File

@ -48,7 +48,7 @@ function StatusAlertNav({ className }: StatusAlertNavProps) {
const { data: initialStats } = useSWR<FrigateStats>("stats", { const { data: initialStats } = useSWR<FrigateStats>("stats", {
revalidateOnFocus: false, revalidateOnFocus: false,
}); });
const { payload: latestStats } = useFrigateStats(); const latestStats = useFrigateStats();
const { messages, addMessage, clearMessages } = useContext( const { messages, addMessage, clearMessages } = useContext(
StatusBarMessagesContext, StatusBarMessagesContext,

View File

@ -27,13 +27,10 @@ export function useCameraActivity(
// init camera activity // init camera activity
const { payload: initialCameraState } = useInitialCameraState( const { payload: updatedCameraState } = useInitialCameraState(
camera.name, camera.name,
revalidateOnFocus, revalidateOnFocus,
); );
const updatedCameraState = useDeepMemo(initialCameraState);
useEffect(() => { useEffect(() => {
if (updatedCameraState) { if (updatedCameraState) {
setObjects(updatedCameraState.objects); setObjects(updatedCameraState.objects);
@ -140,7 +137,7 @@ export function useCameraActivity(
activeTracking: hasActiveObjects, activeTracking: hasActiveObjects,
activeMotion: detectingMotion activeMotion: detectingMotion
? detectingMotion === "ON" ? detectingMotion === "ON"
: initialCameraState?.motion === true, : updatedCameraState?.motion === true,
objects, objects,
offline, offline,
}; };

View File

@ -97,7 +97,7 @@ export function useAutoFrigateStats() {
const { data: initialStats } = useSWR<FrigateStats>("stats", { const { data: initialStats } = useSWR<FrigateStats>("stats", {
revalidateOnFocus: false, revalidateOnFocus: false,
}); });
const { payload: latestStats } = useFrigateStats(); const latestStats = useFrigateStats();
const stats = useMemo(() => { const stats = useMemo(() => {
if (latestStats) { if (latestStats) {

View File

@ -31,7 +31,6 @@ import { cn } from "@/lib/utils";
import { LivePlayerError, LivePlayerMode } from "@/types/live"; import { LivePlayerError, LivePlayerMode } from "@/types/live";
import { FaCompress, FaExpand } from "react-icons/fa"; import { FaCompress, FaExpand } from "react-icons/fa";
import { useResizeObserver } from "@/hooks/resize-observer"; import { useResizeObserver } from "@/hooks/resize-observer";
import useDeepMemo from "@/hooks/use-deep-memo";
type LiveDashboardViewProps = { type LiveDashboardViewProps = {
cameras: CameraConfig[]; cameras: CameraConfig[];
@ -64,14 +63,12 @@ export default function LiveDashboardView({
// recent events // recent events
const { payload: reviewTopic } = useFrigateReviews(); const eventUpdate = useFrigateReviews();
const { data: allEvents, mutate: updateEvents } = useSWR<ReviewSegment[]>([ const { data: allEvents, mutate: updateEvents } = useSWR<ReviewSegment[]>([
"review", "review",
{ limit: 10, severity: "alert" }, { limit: 10, severity: "alert" },
]); ]);
const eventUpdate = useDeepMemo(reviewTopic);
useEffect(() => { useEffect(() => {
if (!eventUpdate) { if (!eventUpdate) {
return; return;

View File

@ -26,7 +26,7 @@ export default function CameraMetrics({
); );
const [statsHistory, setStatsHistory] = useState<FrigateStats[]>([]); const [statsHistory, setStatsHistory] = useState<FrigateStats[]>([]);
const { payload: updatedStats } = useFrigateStats(); const updatedStats = useFrigateStats();
useEffect(() => { useEffect(() => {
if (initialStats == undefined || initialStats.length == 0) { if (initialStats == undefined || initialStats.length == 0) {

View File

@ -40,7 +40,7 @@ export default function GeneralMetrics({
); );
const [statsHistory, setStatsHistory] = useState<FrigateStats[]>([]); const [statsHistory, setStatsHistory] = useState<FrigateStats[]>([]);
const { payload: updatedStats } = useFrigateStats(); const updatedStats = useFrigateStats();
useEffect(() => { useEffect(() => {
if (initialStats == undefined || initialStats.length == 0) { if (initialStats == undefined || initialStats.length == 0) {