mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 09:27:50 +00:00
fix: resolved infinite rendering issue caused by zustand
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { createContext, ReactNode, useContext, useEffect, useRef } from "react";
|
import { createContext, ReactNode, useContext, useEffect, useRef } from "react";
|
||||||
import { useRouter } from "@tanstack/react-router";
|
import { useRouter } from "@tanstack/react-router";
|
||||||
import { createStore, StateCreator, StoreApi, useStore } from "zustand";
|
import { createStore, StateCreator, StoreApi, useStore } from "zustand";
|
||||||
|
import { useShallow } from "zustand/react/shallow";
|
||||||
|
|
||||||
import { SecretV3RawSanitized } from "@app/hooks/api/secrets/types";
|
import { SecretV3RawSanitized } from "@app/hooks/api/secrets/types";
|
||||||
|
|
||||||
@@ -60,14 +61,13 @@ const createPopUpStore: StateCreator<PopUpState> = (set) => ({
|
|||||||
type CombinedState = SelectedSecretState & PopUpState;
|
type CombinedState = SelectedSecretState & PopUpState;
|
||||||
const StoreContext = createContext<StoreApi<CombinedState> | null>(null);
|
const StoreContext = createContext<StoreApi<CombinedState> | null>(null);
|
||||||
export const StoreProvider = ({ children }: { children: ReactNode }) => {
|
export const StoreProvider = ({ children }: { children: ReactNode }) => {
|
||||||
const storeRef = useRef<StoreApi<CombinedState>>();
|
const storeRef = useRef<StoreApi<CombinedState>>(
|
||||||
const router = useRouter();
|
createStore<CombinedState>((...a) => ({
|
||||||
if (!storeRef.current) {
|
|
||||||
storeRef.current = createStore<CombinedState>((...a) => ({
|
|
||||||
...createSelectedSecretStore(...a),
|
...createSelectedSecretStore(...a),
|
||||||
...createPopUpStore(...a)
|
...createPopUpStore(...a)
|
||||||
}));
|
}))
|
||||||
}
|
);
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onRouteChangeStart = () => {
|
const onRouteChangeStart = () => {
|
||||||
@@ -91,10 +91,11 @@ const useStoreContext = <T extends object>(selector: (state: CombinedState) => T
|
|||||||
};
|
};
|
||||||
|
|
||||||
// selected secret context
|
// selected secret context
|
||||||
export const useSelectedSecrets = () => useStoreContext((state) => state.selectedSecret);
|
export const useSelectedSecrets = () =>
|
||||||
export const useSelectedSecretActions = () => useStoreContext((state) => state.action);
|
useStoreContext(useShallow((state) => state.selectedSecret));
|
||||||
|
export const useSelectedSecretActions = () => useStoreContext(useShallow((state) => state.action));
|
||||||
|
|
||||||
// popup context
|
// popup context
|
||||||
export const usePopUpState = (id: PopUpNames) =>
|
export const usePopUpState = (id: PopUpNames) =>
|
||||||
useStoreContext((state) => state.popUp?.[id] || { isOpen: false });
|
useStoreContext(useShallow((state) => state.popUp?.[id] || { isOpen: false }));
|
||||||
export const usePopUpAction = () => useStoreContext((state) => state.popUpActions);
|
export const usePopUpAction = () => useStoreContext(useShallow((state) => state.popUpActions));
|
||||||
|
|||||||
Reference in New Issue
Block a user