mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-03-10 02:29:19 +03:00
optimize calendar rendering with Set lookups and remove unnecessary remount key
The old code built Date[] arrays with a TZDate object for every day in recording history (365+ timezone-aware date constructions). react-day-picker then did O(visible × history) date comparisons to match each of the displayed days against these arrays. Now we build Set<string> from the raw keys (zero date construction), and pass matcher functions that do O(1) Set.has() lookups. react-day-picker only calls these for visible days
This commit is contained in:
parent
9b2bd14db6
commit
bcf0705132
@ -3,7 +3,7 @@ import { Calendar } from "../ui/calendar";
|
|||||||
import { ButtonHTMLAttributes, useEffect, useMemo, useRef } from "react";
|
import { ButtonHTMLAttributes, useEffect, useMemo, useRef } from "react";
|
||||||
import { FaCircle } from "react-icons/fa";
|
import { FaCircle } from "react-icons/fa";
|
||||||
import { getUTCOffset } from "@/utils/dateUtil";
|
import { getUTCOffset } from "@/utils/dateUtil";
|
||||||
import { type DayButtonProps, TZDate } from "react-day-picker";
|
import { type DayButtonProps } from "react-day-picker";
|
||||||
import { LAST_24_HOURS_KEY } from "@/types/filter";
|
import { LAST_24_HOURS_KEY } from "@/types/filter";
|
||||||
import { useUserPersistence } from "@/hooks/use-user-persistence";
|
import { useUserPersistence } from "@/hooks/use-user-persistence";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
@ -38,60 +38,46 @@ export default function ReviewActivityCalendar({
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const modifiers = useMemo(() => {
|
const modifiers = useMemo(() => {
|
||||||
const recordings: Date[] = [];
|
const recordingsSet = new Set<string>();
|
||||||
const alerts: Date[] = [];
|
const alertsSet = new Set<string>();
|
||||||
const detections: Date[] = [];
|
const detectionsSet = new Set<string>();
|
||||||
|
|
||||||
// Handle recordings
|
|
||||||
if (recordingsSummary) {
|
if (recordingsSummary) {
|
||||||
Object.keys(recordingsSummary).forEach((date) => {
|
for (const date of Object.keys(recordingsSummary)) {
|
||||||
if (date === LAST_24_HOURS_KEY) {
|
if (date !== LAST_24_HOURS_KEY) {
|
||||||
return;
|
recordingsSet.add(date);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const parts = date.split("-");
|
|
||||||
const cal = new TZDate(date + "T00:00:00", timezone);
|
|
||||||
|
|
||||||
cal.setFullYear(
|
|
||||||
parseInt(parts[0]),
|
|
||||||
parseInt(parts[1]) - 1,
|
|
||||||
parseInt(parts[2]),
|
|
||||||
);
|
|
||||||
|
|
||||||
recordings.push(cal);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle reviews if present
|
|
||||||
if (reviewSummary) {
|
if (reviewSummary) {
|
||||||
Object.entries(reviewSummary).forEach(([date, data]) => {
|
for (const [date, data] of Object.entries(reviewSummary)) {
|
||||||
if (date === LAST_24_HOURS_KEY) {
|
if (date === LAST_24_HOURS_KEY) continue;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const parts = date.split("-");
|
|
||||||
const cal = new TZDate(date + "T00:00:00", timezone);
|
|
||||||
|
|
||||||
cal.setFullYear(
|
|
||||||
parseInt(parts[0]),
|
|
||||||
parseInt(parts[1]) - 1,
|
|
||||||
parseInt(parts[2]),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (data.total_alert > data.reviewed_alert) {
|
if (data.total_alert > data.reviewed_alert) {
|
||||||
alerts.push(cal);
|
alertsSet.add(date);
|
||||||
} else if (data.total_detection > data.reviewed_detection) {
|
} else if (data.total_detection > data.reviewed_detection) {
|
||||||
detections.push(cal);
|
detectionsSet.add(date);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { alerts, detections, recordings };
|
const formatDay = (day: Date) => {
|
||||||
}, [reviewSummary, recordingsSummary, timezone]);
|
const y = day.getFullYear();
|
||||||
|
const m = String(day.getMonth() + 1).padStart(2, "0");
|
||||||
|
const d = String(day.getDate()).padStart(2, "0");
|
||||||
|
return `${y}-${m}-${d}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
recordings: (day: Date) => recordingsSet.has(formatDay(day)),
|
||||||
|
alerts: (day: Date) => alertsSet.has(formatDay(day)),
|
||||||
|
detections: (day: Date) => detectionsSet.has(formatDay(day)),
|
||||||
|
};
|
||||||
|
}, [reviewSummary, recordingsSummary]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Calendar
|
<Calendar
|
||||||
key={selectedDay ? selectedDay.toISOString() : "reset"}
|
|
||||||
mode="single"
|
mode="single"
|
||||||
disabled={disabledDates}
|
disabled={disabledDates}
|
||||||
showOutsideDays={false}
|
showOutsideDays={false}
|
||||||
@ -223,7 +209,6 @@ export function TimezoneAwareCalendar({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Calendar
|
<Calendar
|
||||||
key={selectedDay ? selectedDay.toISOString() : "reset"}
|
|
||||||
mode="single"
|
mode="single"
|
||||||
disabled={disabledDates}
|
disabled={disabledDates}
|
||||||
showOutsideDays={false}
|
showOutsideDays={false}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user