2025-03-20 05:01:18 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import type {
|
|
|
|
EditorLanguage,
|
|
|
|
EditorLanguageConfig,
|
|
|
|
LanguageServerConfig,
|
|
|
|
Template,
|
|
|
|
} from "@prisma/client";
|
2025-03-20 06:18:15 +00:00
|
|
|
import type { editor } from "monaco-editor";
|
2025-03-20 05:01:18 +00:00
|
|
|
import { createStore, StoreApi, useStore } from "zustand";
|
|
|
|
import { persist, createJSONStorage } from "zustand/middleware";
|
|
|
|
import { DEFAULT_EDITOR_LANGUAGE } from "@/config/editor-language";
|
|
|
|
import { createContext, PropsWithChildren, useContext, useState } from "react";
|
|
|
|
|
|
|
|
type ProblemEditorState = {
|
2025-03-20 06:18:15 +00:00
|
|
|
editor: editor.IStandaloneCodeEditor | null;
|
2025-03-20 05:01:18 +00:00
|
|
|
globalLang: EditorLanguage;
|
|
|
|
currentLang: EditorLanguage;
|
|
|
|
currentValue: string;
|
|
|
|
problemId: string;
|
|
|
|
templates: Template[];
|
|
|
|
editorLanguageConfigs: EditorLanguageConfig[];
|
|
|
|
languageServerConfigs: LanguageServerConfig[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type ProblemEditorActions = {
|
2025-03-20 06:18:15 +00:00
|
|
|
setEditor: (editor: editor.IStandaloneCodeEditor) => void;
|
2025-03-20 05:01:18 +00:00
|
|
|
setGlobalLang: (lang: EditorLanguage) => void;
|
|
|
|
setCurrentLang: (lang: EditorLanguage) => void;
|
|
|
|
setCurrentValue: (value: string) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ProblemEditorStore = ProblemEditorState & ProblemEditorActions;
|
|
|
|
|
|
|
|
const ProblemEditorContext = createContext<StoreApi<ProblemEditorStore> | undefined>(undefined);
|
|
|
|
|
|
|
|
type ProblemEditorProviderProps = PropsWithChildren & {
|
|
|
|
problemId: string;
|
|
|
|
templates: Template[];
|
|
|
|
editorLanguageConfigs: EditorLanguageConfig[];
|
|
|
|
languageServerConfigs: LanguageServerConfig[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export function ProblemEditorProvider({
|
|
|
|
children,
|
|
|
|
problemId,
|
|
|
|
templates,
|
|
|
|
editorLanguageConfigs,
|
|
|
|
languageServerConfigs,
|
|
|
|
}: ProblemEditorProviderProps) {
|
|
|
|
const [store] = useState(() =>
|
|
|
|
createStore<ProblemEditorStore>()(
|
|
|
|
persist(
|
|
|
|
(set) => ({
|
2025-03-20 06:18:15 +00:00
|
|
|
editor: null,
|
2025-03-20 05:01:18 +00:00
|
|
|
globalLang: DEFAULT_EDITOR_LANGUAGE,
|
|
|
|
currentLang: DEFAULT_EDITOR_LANGUAGE,
|
|
|
|
currentValue: "",
|
|
|
|
problemId,
|
|
|
|
templates,
|
|
|
|
editorLanguageConfigs,
|
|
|
|
languageServerConfigs,
|
2025-03-20 06:18:15 +00:00
|
|
|
setEditor: (editor) => set({ editor }),
|
2025-03-20 05:01:18 +00:00
|
|
|
setGlobalLang: (lang) => set({ globalLang: lang }),
|
|
|
|
setCurrentLang: (lang) => set({ currentLang: lang }),
|
|
|
|
setCurrentValue: (value) => set({ currentValue: value }),
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: "problem-store",
|
|
|
|
storage: createJSONStorage(() => localStorage),
|
|
|
|
partialize: (state) => ({
|
|
|
|
globalLang: state.globalLang,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2025-03-20 06:18:15 +00:00
|
|
|
return (
|
|
|
|
<ProblemEditorContext.Provider value={store}>
|
|
|
|
{children}
|
|
|
|
</ProblemEditorContext.Provider>
|
|
|
|
);
|
2025-03-20 05:01:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useProblemEditorStore<T>(selector: (state: ProblemEditorStore) => T) {
|
|
|
|
const context = useContext(ProblemEditorContext);
|
|
|
|
if (!context) {
|
|
|
|
throw new Error("ProblemEditorContext.Provider is missing.");
|
|
|
|
}
|
|
|
|
return useStore(context, selector);
|
|
|
|
}
|