+
- // 保持原有AI优化的状态
- const [showDiff, setShowDiff] = useState(false);
- const [optimizedCode, setOptimizedCode] = useState("");
- const [isOptimizing, setIsOptimizing] = useState(false);
- const [error, setError] = useState
(null);
-
- // 重用useProblem的状态管理
- const currentCode = currentValue || initialCode;
-
- const handleCodeChange = useCallback((value: string | undefined) => {
- if (value !== undefined) {
- changeValue(value);
- if (onCodeChange) {
- onCodeChange(value);
- }
- }
- }, [onCodeChange, changeValue]);
-
- // 保持原有LSP连接逻辑
- const connectLSP = useCallback(async () => {
- if (!(currentLang && editor)) return;
-
- if (monacoLanguageClientRef.current) {
- monacoLanguageClientRef.current.stop();
- monacoLanguageClientRef.current = null;
- setWebSocket(null);
- }
-
- if (!currentEditorLanguageConfig || !currentLanguageServerConfig) return;
-
- try {
- const { client: monacoLanguageClient, webSocket } = await connectToLanguageServer(
- currentEditorLanguageConfig,
- currentLanguageServerConfig
- );
- monacoLanguageClientRef.current = monacoLanguageClient;
- setWebSocket(webSocket);
- } catch (error) {
- console.error("Failed to connect to LSP:", error);
- }
- }, [
- currentEditorLanguageConfig,
- currentLang,
- currentLanguageServerConfig,
- editor,
- setWebSocket,
- ]);
-
- useEffect(() => {
- connectLSP();
- }, [connectLSP]);
-
- useEffect(() => {
- return () => {
- if (monacoLanguageClientRef.current) {
- monacoLanguageClientRef.current.stop();
- monacoLanguageClientRef.current = null;
- setWebSocket(null);
- }
- };
- }, [setWebSocket]);
-
- const handleEditorWillMount = useCallback((monaco: Monaco) => {
- shikiToMonaco(highlighter, monaco);
- }, []);
-
- const handleOnMount = useCallback(
- async (editor: editor.IStandaloneCodeEditor) => {
- setEditor(editor);
- await connectLSP();
- },
- [setEditor, connectLSP]
- );
-
- const handleEditorValidation = useCallback(
- (markers: editor.IMarker[]) => {
- setMarkers(markers);
- },
- [setMarkers]
- );
-
- const handleOptimizeCode = useCallback(async () => {
- if (!currentCode || !problemId) return;
-
- setIsOptimizing(true);
- setError(null);
-
- try {
- const input: OptimizeCodeInput = {
- code: currentCode,
- problemId
- };
-
- const result = await optimizeCode(input);
- setOptimizedCode(result.optimizedCode);
- setShowDiff(true);
- } catch (err) {
- setError("代码优化失败,请重试");
- console.error(err);
- } finally {
- setIsOptimizing(false);
- }
- }, [currentCode, problemId]);
-
- return (
-
- {/* 保持原有AI优化按钮 */}
-
+ {showDiff && (
+
-
- {showDiff && (
-
- )}
-
-
- {error && (
-
- {error}
-
- )}
-
-
- {showDiff ? (
-
- ) : (
- }
- className="h-full w-full"
- />
- )}
-
+
+
+ )}
- );
-}
\ No newline at end of file
+
+ {error && (
+ {error}
+ )}
+
+
+ {showDiff ? (
+
+ ) : (
+
+ )}
+
+