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: Fix buggy incident facets #3284

Merged
merged 4 commits into from
Feb 3, 2025
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
30 changes: 21 additions & 9 deletions keep-ui/features/filter/facets-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,24 @@ export const FacetsPanel: React.FC<FacetsPanelProps> = ({
return facetsState[facetId] || new Set<string>();
}

useEffect(() => {
const newFacetsState: FacetState = {};

facets.forEach((facet) => {
newFacetsState[facet.id] = getFacetState(facet.id);
});

setFacetsState(newFacetsState);
// we need to run this effect only once on mount
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const isOptionSelected = (facet_id: string, option_id: string) => {
return !facetsState[facet_id] || !facetsState[facet_id].has(option_id);
};

function calculateFacetsState(newFacetsState: FacetState): void {
setFacetsState(newFacetsState);
var cel = buildCel(facets, facetOptions, newFacetsState);
useEffect(() => {
var cel = buildCel(facets, facetOptions, facetsState);

if (cel !== celState) {
setCelState(cel);
Expand All @@ -152,12 +163,13 @@ export const FacetsPanel: React.FC<FacetsPanelProps> = ({
facetOptionQueries[facet.id] = buildCel(
otherFacets,
facetOptions,
newFacetsState
facetsState
);
});

onReloadFacetOptions && onReloadFacetOptions(facetOptionQueries);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [facetsState]);

function toggleFacetOption(facetId: string, value: string) {
setClickedFacetId(facetId);
Expand All @@ -169,7 +181,7 @@ export const FacetsPanel: React.FC<FacetsPanelProps> = ({
facetState.delete(value);
}

calculateFacetsState({ ...facetsState, [facetId]: facetState });
setFacetsState({ ...facetsState, [facetId]: facetState });
}

function selectOneFacetOption(facetId: string, optionValue: string): void {
Expand All @@ -185,7 +197,7 @@ export const FacetsPanel: React.FC<FacetsPanelProps> = ({
facetState.add(facetOption.display_name);
});

calculateFacetsState({
setFacetsState({
...facetsState,
[facetId]: facetState,
});
Expand All @@ -199,14 +211,14 @@ export const FacetsPanel: React.FC<FacetsPanelProps> = ({
facetState.delete(option.display_name)
);

calculateFacetsState({
setFacetsState({
...facetsState,
[facetId]: facetState,
});
}

function clearFilters(): void {
calculateFacetsState({});
setFacetsState({});
}

useEffect(
Expand Down
18 changes: 15 additions & 3 deletions keep-ui/utils/hooks/useIncidents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,28 @@ export const useIncidents = (

const filtersParams = new URLSearchParams();

filtersParams.set("confirmed", confirmed.toString());

if (limit !== undefined) {
filtersParams.set("limit", limit.toString());
}

if (offset !== undefined) {
filtersParams.set("offset", offset.toString());
}

if (sorting) {
filtersParams.set("sorting", sorting.desc ? `-${sorting.id}` : sorting.id);
}

if (cel) {
filtersParams.set("cel", cel);
}

const swrValue = useSWR<PaginatedIncidentsDto>(
() =>
api.isReady()
? `/incidents?confirmed=${confirmed}&limit=${limit}&offset=${offset}&sorting=${
sorting.desc ? "-" : ""
}${sorting.id}&${filtersParams.toString()}`
? `/incidents${filtersParams.size ? `?${filtersParams.toString()}` : ""}`
: null,
(url) => api.get(url),
options
Expand Down
Loading