2025-03-20 05:08:00 +00:00
|
|
|
import { getPath } from "@/lib/utils";
|
|
|
|
import { EditorLanguage } from "@prisma/client";
|
|
|
|
import { useCallback, useEffect, useMemo } from "react";
|
2025-03-20 07:11:23 +00:00
|
|
|
import { useMonacoTheme } from "@/hooks/use-monaco-theme";
|
2025-03-21 16:21:51 +00:00
|
|
|
import { useProblemStore } from "@/providers/problem-store-provider";
|
2025-03-20 05:08:00 +00:00
|
|
|
|
|
|
|
/**
|
2025-03-20 07:11:23 +00:00
|
|
|
* Generates a localStorage key for storing the editor language of a problem.
|
|
|
|
* Format: "lang_{problemId}"
|
2025-03-20 05:08:00 +00:00
|
|
|
*/
|
|
|
|
const getProblemLangStorageKey = (problemId: string) => `lang_${problemId}`;
|
|
|
|
|
|
|
|
/**
|
2025-03-20 07:11:23 +00:00
|
|
|
* Generates a localStorage key for storing the editor content of a problem for a specific language.
|
|
|
|
* Format: "{language}_{problemId}"
|
2025-03-20 05:08:00 +00:00
|
|
|
*/
|
2025-03-20 07:11:23 +00:00
|
|
|
const getProblemValueStorageKey = (problemId: string, language: EditorLanguage) =>
|
|
|
|
`${language}_${problemId}`;
|
2025-03-20 05:08:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the stored editor language for a specific problem.
|
2025-03-20 07:11:23 +00:00
|
|
|
* Falls back to the global language if no stored value is found.
|
2025-03-20 05:08:00 +00:00
|
|
|
*/
|
|
|
|
const getStoredProblemLang = (problemId: string, globalLang: EditorLanguage) =>
|
|
|
|
(localStorage.getItem(getProblemLangStorageKey(problemId)) as EditorLanguage) ?? globalLang;
|
|
|
|
|
|
|
|
/**
|
2025-03-20 07:11:23 +00:00
|
|
|
* Retrieves the stored editor content for a specific problem and language.
|
|
|
|
* Falls back to the default template if no stored value is found.
|
2025-03-20 05:08:00 +00:00
|
|
|
*/
|
2025-03-20 07:11:23 +00:00
|
|
|
const getStoredProblemValue = (
|
|
|
|
problemId: string,
|
|
|
|
defaultValue: string,
|
|
|
|
language: EditorLanguage
|
|
|
|
) => localStorage.getItem(getProblemValueStorageKey(problemId, language)) ?? defaultValue;
|
2025-03-20 05:08:00 +00:00
|
|
|
|
2025-03-21 16:21:51 +00:00
|
|
|
export const useProblem = () => {
|
2025-03-20 07:11:23 +00:00
|
|
|
const { currentTheme } = useMonacoTheme();
|
2025-03-21 16:21:51 +00:00
|
|
|
const {
|
|
|
|
hydrated,
|
|
|
|
editor,
|
2025-03-25 05:05:22 +00:00
|
|
|
markers,
|
2025-03-23 15:49:34 +00:00
|
|
|
webSocket,
|
2025-03-21 16:21:51 +00:00
|
|
|
globalLang,
|
|
|
|
currentLang,
|
|
|
|
currentValue,
|
|
|
|
setEditor,
|
2025-03-25 05:05:22 +00:00
|
|
|
setMarkers,
|
2025-03-23 15:49:34 +00:00
|
|
|
setWebSocket,
|
2025-03-21 16:21:51 +00:00
|
|
|
setGlobalLang,
|
|
|
|
setCurrentLang,
|
|
|
|
setCurrentValue,
|
|
|
|
problemId,
|
2025-03-24 02:29:46 +00:00
|
|
|
problem,
|
2025-03-21 16:21:51 +00:00
|
|
|
templates,
|
|
|
|
editorLanguageConfigs,
|
|
|
|
languageServerConfigs,
|
|
|
|
} = useProblemStore((state) => state);
|
2025-03-20 05:08:00 +00:00
|
|
|
|
2025-03-20 07:11:23 +00:00
|
|
|
// Get the default template for the current language from the templates list
|
2025-03-21 16:21:51 +00:00
|
|
|
const currentTemplate = useMemo(
|
|
|
|
() => templates.find((t) => t.language === currentLang)?.template || "",
|
|
|
|
[templates, currentLang]
|
|
|
|
);
|
2025-03-20 05:08:00 +00:00
|
|
|
|
|
|
|
const currentEditorLanguageConfig = useMemo(
|
|
|
|
() => editorLanguageConfigs.find((c) => c.language === currentLang),
|
|
|
|
[editorLanguageConfigs, currentLang]
|
|
|
|
);
|
|
|
|
|
|
|
|
const currentLanguageServerConfig = useMemo(
|
|
|
|
() => languageServerConfigs.find((c) => c.language === currentLang),
|
|
|
|
[languageServerConfigs, currentLang]
|
|
|
|
);
|
|
|
|
|
2025-03-21 16:21:51 +00:00
|
|
|
const currentPath = useMemo(
|
2025-03-23 07:36:53 +00:00
|
|
|
() => (currentEditorLanguageConfig ? getPath(problemId, currentEditorLanguageConfig) : ""),
|
|
|
|
[problemId, currentEditorLanguageConfig]
|
2025-03-21 16:21:51 +00:00
|
|
|
);
|
2025-03-20 05:08:00 +00:00
|
|
|
|
2025-03-20 07:11:23 +00:00
|
|
|
// On initialization, load the stored language and corresponding code content
|
2025-03-20 05:08:00 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const storedLang = getStoredProblemLang(problemId, globalLang);
|
|
|
|
setCurrentLang(storedLang);
|
|
|
|
|
2025-03-20 07:11:23 +00:00
|
|
|
const storedValue = getStoredProblemValue(problemId, currentTemplate, storedLang);
|
2025-03-20 05:08:00 +00:00
|
|
|
setCurrentValue(storedValue);
|
|
|
|
}, [problemId, globalLang, currentTemplate, setCurrentLang, setCurrentValue]);
|
|
|
|
|
2025-03-20 07:11:23 +00:00
|
|
|
// Change the language and update localStorage and state accordingly
|
2025-03-20 05:08:00 +00:00
|
|
|
const changeLang = useCallback(
|
|
|
|
(newLang: EditorLanguage) => {
|
|
|
|
if (!problemId || newLang === currentLang) return;
|
|
|
|
|
2025-03-20 07:11:23 +00:00
|
|
|
// Update the stored language in localStorage
|
2025-03-20 05:08:00 +00:00
|
|
|
localStorage.setItem(getProblemLangStorageKey(problemId), newLang);
|
|
|
|
setCurrentLang(newLang);
|
|
|
|
setGlobalLang(newLang);
|
|
|
|
},
|
|
|
|
[problemId, currentLang, setCurrentLang, setGlobalLang]
|
|
|
|
);
|
|
|
|
|
2025-03-20 07:11:23 +00:00
|
|
|
// Update the stored code content when the editor value changes, specific to the current language
|
2025-03-20 05:08:00 +00:00
|
|
|
const changeValue = useCallback(
|
|
|
|
(newValue: string) => {
|
|
|
|
if (!problemId || newValue === currentValue) return;
|
|
|
|
|
2025-03-20 07:11:23 +00:00
|
|
|
localStorage.setItem(getProblemValueStorageKey(problemId, currentLang), newValue);
|
2025-03-20 05:08:00 +00:00
|
|
|
setCurrentValue(newValue);
|
|
|
|
},
|
2025-03-20 07:11:23 +00:00
|
|
|
[problemId, currentValue, currentLang, setCurrentValue]
|
2025-03-20 05:08:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
2025-03-20 07:11:23 +00:00
|
|
|
hydrated,
|
2025-03-20 06:15:37 +00:00
|
|
|
editor,
|
2025-03-25 05:05:22 +00:00
|
|
|
markers,
|
2025-03-23 15:49:34 +00:00
|
|
|
webSocket,
|
2025-03-20 06:15:37 +00:00
|
|
|
setEditor,
|
2025-03-25 05:05:22 +00:00
|
|
|
setMarkers,
|
2025-03-23 15:49:34 +00:00
|
|
|
setWebSocket,
|
2025-03-20 05:08:00 +00:00
|
|
|
globalLang,
|
|
|
|
currentLang,
|
|
|
|
currentValue,
|
|
|
|
problemId,
|
2025-03-24 02:29:46 +00:00
|
|
|
problem,
|
2025-03-20 05:08:00 +00:00
|
|
|
templates,
|
|
|
|
editorLanguageConfigs,
|
|
|
|
languageServerConfigs,
|
|
|
|
currentTemplate,
|
|
|
|
currentEditorLanguageConfig,
|
|
|
|
currentLanguageServerConfig,
|
|
|
|
currentPath,
|
2025-03-20 07:11:23 +00:00
|
|
|
currentTheme,
|
2025-03-20 05:08:00 +00:00
|
|
|
changeLang,
|
|
|
|
changeValue,
|
|
|
|
};
|
|
|
|
};
|