Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix review filter #14866

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 93 additions & 97 deletions web/src/components/filter/ReviewFilterGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 && (
Expand Down Expand Up @@ -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<string[] | undefined>(
selectedLabels,
);
const [currentZones, setCurrentZones] = useState<string[] | undefined>(
selectedZones,
);
const [currentFilter, setCurrentFilter] = useState<GeneralFilter>({
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 = (
<Button
Expand All @@ -342,10 +337,18 @@ function GeneralFilterButton({
aria-label="Filter"
>
<FaFilter
className={`${selectedLabels?.length || selectedZones?.length ? "text-selected-foreground" : "text-secondary-foreground"}`}
className={`${
selectedLabels?.length || selectedZones?.length
? "text-selected-foreground"
: "text-secondary-foreground"
}`}
/>
<div
className={`hidden md:block ${selectedLabels?.length || selectedZones?.length ? "text-selected-foreground" : "text-primary"}`}
className={`hidden md:block ${
selectedLabels?.length || selectedZones?.length
? "text-selected-foreground"
: "text-primary"
}`}
>
Filter
</div>
Expand All @@ -355,17 +358,22 @@ function GeneralFilterButton({
<GeneralFilterContent
allLabels={allLabels}
selectedLabels={selectedLabels}
currentLabels={currentLabels}
currentSeverity={currentSeverity}
showAll={showAll}
allZones={allZones}
filter={currentFilter}
selectedZones={selectedZones}
currentZones={currentZones}
setCurrentZones={setCurrentZones}
updateZoneFilter={updateZoneFilter}
setShowAll={setShowAll}
updateLabelFilter={updateLabelFilter}
setCurrentLabels={setCurrentLabels}
onUpdateFilter={setCurrentFilter}
onApply={() => {
if (currentFilter !== filter) {
onUpdateFilter(currentFilter);
}
setOpen(false);
}}
onReset={() => {
const resetFilter: GeneralFilter = {};
setCurrentFilter(resetFilter);
onUpdateFilter(resetFilter);
}}
onClose={() => setOpen(false)}
/>
);
Expand All @@ -377,7 +385,12 @@ function GeneralFilterButton({
open={open}
onOpenChange={(open) => {
if (!open) {
setCurrentLabels(selectedLabels);
setCurrentFilter({
labels: selectedLabels,
zones: selectedZones,
showAll: showAll,
...filter,
});
}

setOpen(open);
Expand All @@ -388,54 +401,50 @@ function GeneralFilterButton({

type GeneralFilterContentProps = {
allLabels: string[];
selectedLabels: string[] | undefined;
currentLabels: string[] | undefined;
allZones: string[];
currentSeverity?: ReviewSeverity;
showAll?: boolean;
allZones?: string[];
filter: GeneralFilter;
selectedLabels?: string[];
selectedZones?: string[];
currentZones?: string[];
setShowAll?: (showAll: boolean) => void;
updateLabelFilter: (labels: string[] | undefined) => void;
setCurrentLabels: (labels: string[] | undefined) => void;
updateZoneFilter?: (zones: string[] | undefined) => void;
setCurrentZones?: (zones: string[] | undefined) => void;
onUpdateFilter: (filter: GeneralFilter) => void;
onApply: () => void;
onReset: () => void;
onClose: () => void;
};
export function GeneralFilterContent({
allLabels,
selectedLabels,
currentLabels,
currentSeverity,
showAll,
allZones,
selectedZones,
currentZones,
setShowAll,
updateLabelFilter,
setCurrentLabels,
updateZoneFilter,
setCurrentZones,
currentSeverity,
filter,
onUpdateFilter,
onApply,
onReset,
onClose,
}: GeneralFilterContentProps) {
return (
<>
<div className="scrollbar-container h-auto max-h-[80dvh] overflow-y-auto overflow-x-hidden">
{currentSeverity && setShowAll && (
{currentSeverity && (
<div className="my-2.5 flex flex-col gap-2.5">
<FilterSwitch
label="Alerts"
disabled={currentSeverity == "alert"}
isChecked={currentSeverity == "alert" ? true : showAll == true}
onCheckedChange={setShowAll}
isChecked={
currentSeverity == "alert" ? true : filter.showAll === true
}
onCheckedChange={(checked) =>
onUpdateFilter({ ...filter, showAll: checked })
}
/>
<FilterSwitch
label="Detections"
disabled={currentSeverity == "detection"}
isChecked={
currentSeverity == "detection" ? true : showAll == true
currentSeverity == "detection" ? true : filter.showAll === true
}
onCheckedChange={(checked) =>
onUpdateFilter({ ...filter, showAll: checked })
}
onCheckedChange={setShowAll}
/>
<DropdownMenuSeparator />
</div>
Expand All @@ -450,10 +459,11 @@ export function GeneralFilterContent({
<Switch
className="ml-1"
id="allLabels"
checked={currentLabels == undefined}
checked={filter.labels === undefined}
onCheckedChange={(isChecked) => {
if (isChecked) {
setCurrentLabels(undefined);
const { labels: _labels, ...rest } = filter;
onUpdateFilter(rest);
}
}}
/>
Expand All @@ -463,28 +473,27 @@ export function GeneralFilterContent({
<FilterSwitch
key={item}
label={item.replaceAll("_", " ")}
isChecked={currentLabels?.includes(item) ?? false}
isChecked={filter.labels?.includes(item) ?? false}
onCheckedChange={(isChecked) => {
if (isChecked) {
const updatedLabels = currentLabels ? [...currentLabels] : [];

const updatedLabels = filter.labels ? [...filter.labels] : [];
updatedLabels.push(item);
setCurrentLabels(updatedLabels);
onUpdateFilter({ ...filter, labels: updatedLabels });
} else {
const updatedLabels = currentLabels ? [...currentLabels] : [];
const updatedLabels = filter.labels ? [...filter.labels] : [];

// can not deselect the last item
if (updatedLabels.length > 1) {
updatedLabels.splice(updatedLabels.indexOf(item), 1);
setCurrentLabels(updatedLabels);
onUpdateFilter({ ...filter, labels: updatedLabels });
}
}
}}
/>
))}
</div>

{allZones && setCurrentZones && (
{allZones && (
<>
<DropdownMenuSeparator />
<div className="mb-5 mt-2.5 flex items-center justify-between">
Expand All @@ -497,10 +506,11 @@ export function GeneralFilterContent({
<Switch
className="ml-1"
id="allZones"
checked={currentZones == undefined}
checked={filter.zones === undefined}
onCheckedChange={(isChecked) => {
if (isChecked) {
setCurrentZones(undefined);
const { zones: _zones, ...rest } = filter;
onUpdateFilter(rest);
}
}}
/>
Expand All @@ -510,24 +520,24 @@ export function GeneralFilterContent({
<FilterSwitch
key={item}
label={item.replaceAll("_", " ")}
isChecked={currentZones?.includes(item) ?? false}
isChecked={filter.zones?.includes(item) ?? false}
onCheckedChange={(isChecked) => {
if (isChecked) {
const updatedZones = currentZones
? [...currentZones]
const updatedZones = filter.zones
? [...filter.zones]
: [];

updatedZones.push(item);
setCurrentZones(updatedZones);
onUpdateFilter({ ...filter, zones: updatedZones });
} else {
const updatedZones = currentZones
? [...currentZones]
const updatedZones = filter.zones
? [...filter.zones]
: [];

// can not deselect the last item
if (updatedZones.length > 1) {
updatedZones.splice(updatedZones.indexOf(item), 1);
setCurrentZones(updatedZones);
onUpdateFilter({ ...filter, zones: updatedZones });
}
}
}}
Expand All @@ -543,27 +553,13 @@ export function GeneralFilterContent({
aria-label="Apply"
variant="select"
onClick={() => {
if (selectedLabels != currentLabels) {
updateLabelFilter(currentLabels);
}

if (updateZoneFilter && selectedZones != currentZones) {
updateZoneFilter(currentZones);
}

onApply();
onClose();
}}
>
Apply
</Button>
<Button
aria-label="Reset"
onClick={() => {
setCurrentLabels(undefined);
setCurrentZones?.(undefined);
updateLabelFilter(undefined);
}}
>
<Button aria-label="Reset" onClick={onReset}>
Reset
</Button>
</div>
Expand Down
Loading