From ae30ac6e3cbfe6a08e33c03c8a08cda7dacd0978 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Fri, 8 Nov 2024 07:45:00 -0600 Subject: [PATCH] Refactor general review filter to only call the update function once (#14866) --- .../components/filter/ReviewFilterGroup.tsx | 190 +++++++++--------- .../overlay/MobileReviewSettingsDrawer.tsx | 38 ++-- web/src/types/filter.ts | 6 + 3 files changed, 117 insertions(+), 117 deletions(-) diff --git a/web/src/components/filter/ReviewFilterGroup.tsx b/web/src/components/filter/ReviewFilterGroup.tsx index 004fe5527..bea816203 100644 --- a/web/src/components/filter/ReviewFilterGroup.tsx +++ b/web/src/components/filter/ReviewFilterGroup.tsx @@ -14,7 +14,7 @@ import MobileReviewSettingsDrawer, { } from "../overlay/MobileReviewSettingsDrawer"; import useOptimisticState from "@/hooks/use-optimistic-state"; import FilterSwitch from "./FilterSwitch"; -import { FilterList } from "@/types/filter"; +import { FilterList, GeneralFilter } from "@/types/filter"; import CalendarFilterButton from "./CalendarFilterButton"; import { CamerasFilterButton } from "./CamerasFilterButton"; import PlatformAwareDialog from "../overlay/dialog/PlatformAwareDialog"; @@ -214,15 +214,7 @@ export default function ReviewFilterGroup({ showAll={filter?.showAll == true} allZones={filterValues.zones} selectedZones={filter?.zones} - setShowAll={(showAll) => { - onUpdateFilter({ ...filter, showAll }); - }} - updateLabelFilter={(newLabels) => { - onUpdateFilter({ ...filter, labels: newLabels }); - }} - updateZoneFilter={(newZones) => - onUpdateFilter({ ...filter, zones: newZones }) - } + onUpdateFilter={onUpdateFilter} /> )} {isMobile && mobileSettingsFeatures.length > 0 && ( @@ -300,37 +292,40 @@ type GeneralFilterButtonProps = { showAll: boolean; allZones: string[]; selectedZones?: string[]; - setShowAll: (showAll: boolean) => void; - updateLabelFilter: (labels: string[] | undefined) => void; - updateZoneFilter: (zones: string[] | undefined) => void; + filter?: GeneralFilter; + onUpdateFilter: (filter: ReviewFilter) => void; }; + function GeneralFilterButton({ allLabels, selectedLabels, + filter, currentSeverity, showAll, allZones, selectedZones, - setShowAll, - updateLabelFilter, - updateZoneFilter, + onUpdateFilter, }: GeneralFilterButtonProps) { const [open, setOpen] = useState(false); - const [currentLabels, setCurrentLabels] = useState( - selectedLabels, - ); - const [currentZones, setCurrentZones] = useState( - selectedZones, - ); + const [currentFilter, setCurrentFilter] = useState({ + labels: selectedLabels, + zones: selectedZones, + showAll: showAll, + ...filter, + }); - // ui + // Update local state when props change useEffect(() => { - setCurrentLabels(selectedLabels); - setCurrentZones(selectedZones); + setCurrentFilter({ + labels: selectedLabels, + zones: selectedZones, + showAll: showAll, + ...filter, + }); // only refresh when state changes // eslint-disable-next-line react-hooks/exhaustive-deps - }, [selectedLabels, selectedZones]); + }, [selectedLabels, selectedZones, showAll, filter]); const trigger = ( - diff --git a/web/src/components/overlay/MobileReviewSettingsDrawer.tsx b/web/src/components/overlay/MobileReviewSettingsDrawer.tsx index d58d485b9..3eeb639cd 100644 --- a/web/src/components/overlay/MobileReviewSettingsDrawer.tsx +++ b/web/src/components/overlay/MobileReviewSettingsDrawer.tsx @@ -4,7 +4,7 @@ import { Button } from "../ui/button"; import { FaArrowDown, FaCalendarAlt, FaCog, FaFilter } from "react-icons/fa"; import { TimeRange } from "@/types/timeline"; import { ExportContent, ExportPreviewDialog } from "./ExportDialog"; -import { ExportMode } from "@/types/filter"; +import { ExportMode, GeneralFilter } from "@/types/filter"; import ReviewActivityCalendar from "./ReviewActivityCalendar"; import { SelectSeparator } from "../ui/select"; import { ReviewFilter, ReviewSeverity, ReviewSummary } from "@/types/review"; @@ -114,12 +114,12 @@ export default function MobileReviewSettingsDrawer({ // filters - const [currentLabels, setCurrentLabels] = useState( - filter?.labels, - ); - const [currentZones, setCurrentZones] = useState( - filter?.zones, - ); + const [currentFilter, setCurrentFilter] = useState({ + labels: filter?.labels, + zones: filter?.zones, + showAll: filter?.showAll, + ...filter, + }); if (!isMobile) { return; @@ -260,23 +260,21 @@ export default function MobileReviewSettingsDrawer({ - onUpdateFilter({ ...filter, zones: newZones }) - } - setShowAll={(showAll) => { - onUpdateFilter({ ...filter, showAll }); + onUpdateFilter={setCurrentFilter} + onApply={() => { + if (currentFilter !== filter) { + onUpdateFilter(currentFilter); + } + }} + onReset={() => { + const resetFilter: GeneralFilter = {}; + setCurrentFilter(resetFilter); + onUpdateFilter(resetFilter); }} - setCurrentLabels={setCurrentLabels} - updateLabelFilter={(newLabels) => - onUpdateFilter({ ...filter, labels: newLabels }) - } onClose={() => setDrawerMode("select")} /> diff --git a/web/src/types/filter.ts b/web/src/types/filter.ts index 09ff5b99a..b7d2223c0 100644 --- a/web/src/types/filter.ts +++ b/web/src/types/filter.ts @@ -10,3 +10,9 @@ export type FilterList = { }; export const LAST_24_HOURS_KEY = "last24Hours"; + +export type GeneralFilter = { + showAll?: boolean; + labels?: string[]; + zones?: string[]; +};