mirror of
https://github.com/cfngc4594/monaco-editor-lsp-next.git
synced 2025-05-18 15:26:36 +00:00
feat(store): add hydration logic and state management to new problem store
This commit is contained in:
parent
54ac7b3cd5
commit
0d074a556d
@ -2,18 +2,34 @@ import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
import { ProblemSchema } from "@/components/features/dashboard/admin/problemset/new/schema";
|
||||
|
||||
type NewProblemState = Partial<ProblemSchema> & {
|
||||
interface NewProblemActions {
|
||||
setHydrated: (value: boolean) => void;
|
||||
setData: (data: Partial<ProblemSchema>) => void;
|
||||
};
|
||||
}
|
||||
|
||||
type NewProblemState = Partial<ProblemSchema> & {
|
||||
hydrated: boolean;
|
||||
} & NewProblemActions;
|
||||
|
||||
export const useNewProblemStore = create<NewProblemState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
hydrated: false,
|
||||
setHydrated: (value) => set({ hydrated: value }),
|
||||
setData: (data) => set(data),
|
||||
}),
|
||||
{
|
||||
name: "new-problem-storage",
|
||||
name: "zustand:new-problem",
|
||||
storage: createJSONStorage(() => localStorage),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
partialize: ({ hydrated, ...rest }) => rest,
|
||||
onRehydrateStorage: () => (state, error) => {
|
||||
if (error) {
|
||||
console.error("An error happened during hydration", error);
|
||||
} else if (state) {
|
||||
state.setHydrated(true);
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user