Skip to content

fix: Introduce zustand state to FacetsPanel to improve maintainability #4769

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

Closed
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4a35b59
Refactor facets
skynetigor May 11, 2025
3847487
add zustand store for facets panel
skynetigor May 12, 2025
91ef44a
refactor: improve facet handling and state management in FacetsPanel …
skynetigor May 12, 2025
1459c52
Merge branch 'main' into 4560-Incidents-page-load-with-resolved-ones-…
skynetigor May 12, 2025
c929232
refactor: streamline facet selection logic and improve button visibil…
skynetigor May 12, 2025
df88bc7
test: enhance incident tests with additional status handling and init…
skynetigor May 12, 2025
6272cca
fix: update default incident status options to include acknowledged s…
skynetigor May 13, 2025
d977096
refactor
skynetigor May 13, 2025
2ed4419
Fixes
skynetigor May 13, 2025
14ac953
test: update filtering logic to include acknowledged status in incide…
skynetigor May 13, 2025
c2574bc
Merge branch 'main' into 4560-Incidents-page-load-with-resolved-ones-…
skynetigor May 13, 2025
aed9611
Merge branch 'main' into 4560-Incidents-page-load-with-resolved-ones-…
skynetigor May 13, 2025
4c028c3
Merge branch '4560-Incidents-page-load-with-resolved-ones-ignoring-fi…
skynetigor May 13, 2025
5e455f2
feat: add useFacetsConfig hook for improved facets configuration hand…
skynetigor May 13, 2025
5560976
refactor
skynetigor May 13, 2025
9dff8d8
Merge branch 'main' into 4560-Incidents-page-load-with-resolved-ones-…
skynetigor May 14, 2025
7270d5e
Merge branch 'main' into 4560-Incidents-page-load-with-resolved-ones-…
skynetigor May 14, 2025
8d199f7
Merge branch 'main' into 4560-Incidents-page-load-with-resolved-ones-…
Matvey-Kuk May 15, 2025
b0ec408
refactor: remove unused buildCel function and related tests
skynetigor May 19, 2025
e055304
refactor: improve type safety in facets panel and store
skynetigor May 19, 2025
a148ae4
Apply suggestions from code review
skynetigor May 19, 2025
315f887
Merge branch 'main' into 4560-Incidents-page-load-with-resolved-ones-…
skynetigor May 19, 2025
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
Prev Previous commit
Next Next commit
add zustand store for facets panel
  • Loading branch information
skynetigor committed May 12, 2025
commit 38474875a84481c66f6cb37f75d8dcead82b2f55
81 changes: 81 additions & 0 deletions keep-ui/features/filter/store.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { createContext, useContext, useRef } from "react";
import { create, createStore, useStore } from "zustand";
import { v4 as uuidV4 } from "uuid";
import { FacetDto } from "./models";

type FacetState = {
facets: FacetDto[] | null;
facetCelState: Record<string, string> | null;
clearFiltersToken: string | null;
setFacets: (facets: FacetDto[]) => void;
setFacetCelState: (facetId: string, cel: string) => void;
clearFilters: () => void;
};

const createFacetStore = () =>
createStore<FacetState>((set, state) => ({
facets: null,
facetCelState: null,
clearFiltersToken: null,
setFacets: (facets: FacetDto[]) => set({ facets }),
setFacetCelState: (facetId: string, cel: string) =>
set({
facetCelState: {
...(state().facetCelState || {}),
[facetId]: cel,
},
}),

clearFilters: () => {
return set({
clearFiltersToken: uuidV4(),
facetCelState: state().facets?.reduce(
(acc, facet) => ({
...acc,
[facet.id]: state().facetCelState?.[facet.id] || "",
}),
{}
),
});
},
}));

export function useNewFacetStore() {
const storeRef = useRef<ReturnType<typeof createFacetStore>>();

if (!storeRef.current) {
storeRef.current = createFacetStore(); // New store per provider
}

return storeRef.current;
}

const FacetStoreContext = createContext<ReturnType<
typeof createFacetStore
> | null>(null);

export const FacetStoreProvider = ({
store,
children,
}: {
store: ReturnType<typeof createFacetStore>;
children: React.ReactNode;
}) => {
return (
<FacetStoreContext.Provider value={store}>
{children}
</FacetStoreContext.Provider>
);
};

// Hook to access the scoped store
export function useExistingFacetStore<T>(
selector: (state: FacetState) => T
): T {
const store = useContext(FacetStoreContext);
if (!store)
throw new Error(
"useExistingFacetStore must be used within FacetStoreProvider"
);
return useStore(store, selector);
}