refactor(hooks): rename use-problem-editor.ts to use-problem.ts

This commit is contained in:
cfngc4594 2025-03-22 00:21:51 +08:00
parent 695dabe821
commit e5ba5dd51a
11 changed files with 59 additions and 48 deletions

View File

@ -1,8 +1,12 @@
import prisma from "@/lib/prisma"; import prisma from "@/lib/prisma";
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable";
import { notFound } from "next/navigation"; import { notFound } from "next/navigation";
import { ProblemStoreProvider } from "@/providers/problem-store-provider";
import { PlaygroundHeader } from "@/components/features/playground/header"; import { PlaygroundHeader } from "@/components/features/playground/header";
import { ProblemEditorProvider } from "@/providers/problem-editor-provider";
import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable";
interface PlaygroundLayoutProps { interface PlaygroundLayoutProps {
params: Promise<{ id: string }>; params: Promise<{ id: string }>;
@ -38,7 +42,7 @@ export default async function PlaygroundLayout({
return ( return (
<div className="h-screen flex flex-col"> <div className="h-screen flex flex-col">
<ProblemEditorProvider <ProblemStoreProvider
problemId={id} problemId={id}
templates={problemData.templates ?? []} templates={problemData.templates ?? []}
editorLanguageConfigs={editorLanguageConfigs} editorLanguageConfigs={editorLanguageConfigs}
@ -64,7 +68,7 @@ export default async function PlaygroundLayout({
</ResizablePanel> </ResizablePanel>
</ResizablePanelGroup> </ResizablePanelGroup>
</main> </main>
</ProblemEditorProvider> </ProblemStoreProvider>
</div> </div>
); );
} }

View File

@ -10,10 +10,10 @@ import { cn } from "@/lib/utils";
import { useState } from "react"; import { useState } from "react";
import { Check, Copy } from "lucide-react"; import { Check, Copy } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useProblemEditor } from "@/hooks/use-problem-editor"; import { useProblem } from "@/hooks/use-problem";
export function CopyButton() { export function CopyButton() {
const { editor } = useProblemEditor(); const { editor } = useProblem();
const [copied, setCopied] = useState(false); const [copied, setCopied] = useState(false);
const handleCopy = async () => { const handleCopy = async () => {

View File

@ -2,7 +2,7 @@
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useProblemEditor } from "@/hooks/use-problem-editor"; import { useProblem } from "@/hooks/use-problem";
interface WorkspaceEditorFooterProps { interface WorkspaceEditorFooterProps {
className?: string; className?: string;
@ -12,7 +12,7 @@ export function WorkspaceEditorFooter({
className, className,
...props ...props
}: WorkspaceEditorFooterProps) { }: WorkspaceEditorFooterProps) {
const { editor } = useProblemEditor(); const { editor } = useProblem();
const [position, setPosition] = useState<{ lineNumber: number; column: number } | null>(null); const [position, setPosition] = useState<{ lineNumber: number; column: number } | null>(null);
useEffect(() => { useEffect(() => {

View File

@ -8,10 +8,10 @@ import {
} from "@/components/ui/tooltip"; } from "@/components/ui/tooltip";
import { Paintbrush } from "lucide-react"; import { Paintbrush } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useProblemEditor } from "@/hooks/use-problem-editor"; import { useProblem } from "@/hooks/use-problem";
export function FormatButton() { export function FormatButton() {
const { editor } = useProblemEditor(); const { editor } = useProblem();
return ( return (
<TooltipProvider delayDuration={0}> <TooltipProvider delayDuration={0}>

View File

@ -8,14 +8,14 @@ import {
SelectValue, SelectValue,
} from "@/components/ui/select"; } from "@/components/ui/select";
import { Loading } from "@/components/loading"; import { Loading } from "@/components/loading";
import { useProblemEditor } from "@/hooks/use-problem-editor"; import { useProblem } from "@/hooks/use-problem";
import { EditorLanguageIcons } from "@/config/editor-language-icons"; import { EditorLanguageIcons } from "@/config/editor-language-icons";
export function LanguageSelector() { export function LanguageSelector() {
const { hydrated, currentLang, changeLang, editorLanguageConfigs } = useProblemEditor(); const { hydrated, currentLang, changeLang, editorLanguageConfigs } = useProblem();
if (!hydrated) { if (!hydrated) {
return <Loading className="h-6 w-16 p-0" skeletonClassName="rounded-2xl" /> return <Loading className="h-6 w-16 p-0" skeletonClassName="rounded-2xl" />;
} }
return ( return (

View File

@ -8,10 +8,10 @@ import {
} from "@/components/ui/tooltip"; } from "@/components/ui/tooltip";
import { Redo2 } from "lucide-react"; import { Redo2 } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useProblemEditor } from "@/hooks/use-problem-editor"; import { useProblem } from "@/hooks/use-problem";
export function RedoButton() { export function RedoButton() {
const { editor } = useProblemEditor(); const { editor } = useProblem();
return ( return (
<TooltipProvider delayDuration={0}> <TooltipProvider delayDuration={0}>

View File

@ -8,10 +8,10 @@ import {
} from "@/components/ui/tooltip"; } from "@/components/ui/tooltip";
import { RotateCcw } from "lucide-react"; import { RotateCcw } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useProblemEditor } from "@/hooks/use-problem-editor"; import { useProblem } from "@/hooks/use-problem";
export function ResetButton() { export function ResetButton() {
const { editor, currentTemplate } = useProblemEditor(); const { editor, currentTemplate } = useProblem();
const handleReset = () => { const handleReset = () => {
if (editor) { if (editor) {

View File

@ -8,10 +8,10 @@ import {
} from "@/components/ui/tooltip"; } from "@/components/ui/tooltip";
import { Undo2 } from "lucide-react"; import { Undo2 } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useProblemEditor } from "@/hooks/use-problem-editor"; import { useProblem } from "@/hooks/use-problem";
export function UndoButton() { export function UndoButton() {
const { editor } = useProblemEditor(); const { editor } = useProblem();
return ( return (
<TooltipProvider delayDuration={0}> <TooltipProvider delayDuration={0}>

View File

@ -5,8 +5,8 @@ import { highlighter } from "@/lib/shiki";
import type { editor } from "monaco-editor"; import type { editor } from "monaco-editor";
import { Loading } from "@/components/loading"; import { Loading } from "@/components/loading";
import { shikiToMonaco } from "@shikijs/monaco"; import { shikiToMonaco } from "@shikijs/monaco";
import { useProblem } from "@/hooks/use-problem";
import type { Monaco } from "@monaco-editor/react"; import type { Monaco } from "@monaco-editor/react";
import { useProblemEditor } from "@/hooks/use-problem-editor";
import { DefaultEditorOptionConfig } from "@/config/editor-option"; import { DefaultEditorOptionConfig } from "@/config/editor-option";
// Dynamically import Monaco Editor with SSR disabled // Dynamically import Monaco Editor with SSR disabled
@ -25,8 +25,7 @@ const Editor = dynamic(
); );
export function CodeEditor() { export function CodeEditor() {
const { setEditor, currentLang, currentPath, currentTheme, currentValue, changeValue } = useProblem();
const { setEditor, currentLang, currentPath, currentTheme, currentValue, changeValue } = useProblemEditor();
const handleBeforeMount = (monaco: Monaco) => { const handleBeforeMount = (monaco: Monaco) => {
shikiToMonaco(highlighter, monaco); shikiToMonaco(highlighter, monaco);
@ -34,7 +33,7 @@ export function CodeEditor() {
const handleOnMount = (editor: editor.IStandaloneCodeEditor) => { const handleOnMount = (editor: editor.IStandaloneCodeEditor) => {
setEditor(editor); setEditor(editor);
} };
const handleOnChange = (value: string | undefined) => { const handleOnChange = (value: string | undefined) => {
if (value === undefined) return; if (value === undefined) return;

View File

@ -1,13 +1,18 @@
"use client"; "use client";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { useState } from "react"; import { useState } from "react";
import { judge } from "@/app/actions/judge"; import { judge } from "@/app/actions/judge";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useProblem } from "@/hooks/use-problem";
import { LoaderCircleIcon, PlayIcon } from "lucide-react"; import { LoaderCircleIcon, PlayIcon } from "lucide-react";
import { useProblemEditor } from "@/hooks/use-problem-editor";
import { showExitCodeToast } from "@/lib/show-exit-code-toast"; import { showExitCodeToast } from "@/lib/show-exit-code-toast";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
interface RunCodeProps { interface RunCodeProps {
className?: string; className?: string;
@ -17,7 +22,7 @@ export function RunCode({
className, className,
...props ...props
}: RunCodeProps) { }: RunCodeProps) {
const { currentLang, editor } = useProblemEditor(); const { currentLang, editor } = useProblem();
const [isLoading, setIsLoading] = useState<boolean>(false); const [isLoading, setIsLoading] = useState<boolean>(false);
const handleJudge = async () => { const handleJudge = async () => {

View File

@ -2,7 +2,7 @@ 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 { useMonacoTheme } from "@/hooks/use-monaco-theme"; import { useMonacoTheme } from "@/hooks/use-monaco-theme";
import { useProblemEditorStore } from "@/providers/problem-editor-provider"; import { useProblemStore } from "@/providers/problem-store-provider";
/** /**
* Generates a localStorage key for storing the editor language of a problem. * Generates a localStorage key for storing the editor language of a problem.
@ -34,27 +34,29 @@ const getStoredProblemValue = (
language: EditorLanguage language: EditorLanguage
) => localStorage.getItem(getProblemValueStorageKey(problemId, language)) ?? defaultValue; ) => localStorage.getItem(getProblemValueStorageKey(problemId, language)) ?? defaultValue;
export const useProblemEditor = () => { export const useProblem = () => {
const { currentTheme } = useMonacoTheme(); const { currentTheme } = useMonacoTheme();
const {
const hydrated = useProblemEditorStore((state) => state.hydrated); hydrated,
const editor = useProblemEditorStore((state) => state.editor); editor,
const globalLang = useProblemEditorStore((state) => state.globalLang); globalLang,
const currentLang = useProblemEditorStore((state) => state.currentLang); currentLang,
const currentValue = useProblemEditorStore((state) => state.currentValue); currentValue,
const setEditor = useProblemEditorStore((state) => state.setEditor); setEditor,
const setGlobalLang = useProblemEditorStore((state) => state.setGlobalLang); setGlobalLang,
const setCurrentLang = useProblemEditorStore((state) => state.setCurrentLang); setCurrentLang,
const setCurrentValue = useProblemEditorStore((state) => state.setCurrentValue); setCurrentValue,
const problemId = useProblemEditorStore((state) => state.problemId); problemId,
const templates = useProblemEditorStore((state) => state.templates); templates,
const editorLanguageConfigs = useProblemEditorStore((state) => state.editorLanguageConfigs); editorLanguageConfigs,
const languageServerConfigs = useProblemEditorStore((state) => state.languageServerConfigs); languageServerConfigs,
} = useProblemStore((state) => state);
// Get the default template for the current language from the templates list // Get the default template for the current language from the templates list
const currentTemplate = useMemo(() => { const currentTemplate = useMemo(
return templates.find((t) => t.language === currentLang)?.template || ""; () => templates.find((t) => t.language === currentLang)?.template || "",
}, [templates, currentLang]); [templates, currentLang]
);
const currentEditorLanguageConfig = useMemo( const currentEditorLanguageConfig = useMemo(
() => editorLanguageConfigs.find((c) => c.language === currentLang), () => editorLanguageConfigs.find((c) => c.language === currentLang),
@ -66,9 +68,10 @@ export const useProblemEditor = () => {
[languageServerConfigs, currentLang] [languageServerConfigs, currentLang]
); );
const currentPath = useMemo(() => { const currentPath = useMemo(
return currentEditorLanguageConfig ? getPath(currentEditorLanguageConfig) : ""; () => (currentEditorLanguageConfig ? getPath(currentEditorLanguageConfig) : ""),
}, [currentEditorLanguageConfig]); [currentEditorLanguageConfig]
);
// On initialization, load the stored language and corresponding code content // On initialization, load the stored language and corresponding code content
useEffect(() => { useEffect(() => {