refactor(useProblemEditor): refactor to use useProblemEditorStore for state management

This commit is contained in:
cfngc4594 2025-03-20 14:15:37 +08:00
parent 298d2c1c86
commit 69dfb8cb12

View File

@ -1,7 +1,7 @@
import { getPath } from "@/lib/utils"; import { getPath } from "@/lib/utils";
import { EditorLanguage } from "@prisma/client"; import { EditorLanguage } from "@prisma/client";
import { useCallback, useEffect, useMemo } from "react"; import { useCallback, useEffect, useMemo } from "react";
import { useProblemEditorStoreInstance } from "@/providers/problem-editor-provider"; import { useProblemEditorStore } from "@/providers/problem-editor-provider";
/** /**
* Generates a unique localStorage key for storing the editor language of a problem. * Generates a unique localStorage key for storing the editor language of a problem.
@ -28,18 +28,18 @@ const getStoredProblemValue = (problemId: string, defaultValue: string) =>
localStorage.getItem(getProblemValueStorageKey(problemId)) ?? defaultValue; localStorage.getItem(getProblemValueStorageKey(problemId)) ?? defaultValue;
export const useProblemEditor = () => { export const useProblemEditor = () => {
const { const editor = useProblemEditorStore((state) => state.editor);
globalLang, const globalLang = useProblemEditorStore((state) => state.globalLang);
currentLang, const currentLang = useProblemEditorStore((state) => state.currentLang);
currentValue, const currentValue = useProblemEditorStore((state) => state.currentValue);
setGlobalLang, const setEditor = useProblemEditorStore((state) => state.setEditor);
setCurrentLang, const setGlobalLang = useProblemEditorStore((state) => state.setGlobalLang);
setCurrentValue, const setCurrentLang = useProblemEditorStore((state) => state.setCurrentLang);
problemId, const setCurrentValue = useProblemEditorStore((state) => state.setCurrentValue);
templates, const problemId = useProblemEditorStore((state) => state.problemId);
editorLanguageConfigs, const templates = useProblemEditorStore((state) => state.templates);
languageServerConfigs, const editorLanguageConfigs = useProblemEditorStore((state) => state.editorLanguageConfigs);
} = useProblemEditorStoreInstance().getState(); const languageServerConfigs = useProblemEditorStore((state) => state.languageServerConfigs);
const currentTemplate = useMemo(() => { const currentTemplate = useMemo(() => {
return templates.find((t) => t.language === currentLang)?.template || ""; return templates.find((t) => t.language === currentLang)?.template || "";
@ -89,6 +89,8 @@ export const useProblemEditor = () => {
); );
return { return {
editor,
setEditor,
globalLang, globalLang,
currentLang, currentLang,
currentValue, currentValue,