From 7ab30af944ead5f504456a6e888914dcbbb9f345 Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Thu, 25 Sep 2025 04:46:47 +0000 Subject: [PATCH] Simplify console message handling and reduce code complexity - Remove unused useErrorDetection.ts hook (154 lines) - Remove separate console.ts type file, consolidate into ReadConsoleMessagesTool - Simplify console message state management in PreviewPane - Remove redundant console clearing logic in ProjectView - Streamline copy functionality in console UI - Clean up imports and formatting Preserves all functionality while reducing code complexity and maintenance burden. --- src/components/Shakespeare/ChatPane.tsx | 5 +- src/components/Shakespeare/PreviewPane.tsx | 38 ++--- src/hooks/useErrorDetection.ts | 154 --------------------- src/lib/tools/ReadConsoleMessagesTool.ts | 8 +- src/pages/ProjectView.tsx | 9 +- src/types/console.ts | 6 - 6 files changed, 19 insertions(+), 201 deletions(-) delete mode 100644 src/hooks/useErrorDetection.ts delete mode 100644 src/types/console.ts diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 223cca21..a08fb8a3 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -34,11 +34,10 @@ import { NostrReadNipsIndexTool } from '@/lib/tools/NostrReadNipsIndexTool'; import { NostrGenerateKindTool } from '@/lib/tools/NostrGenerateKindTool'; import { ShellTool } from '@/lib/tools/ShellTool'; import { TypecheckTool } from '@/lib/tools/TypecheckTool'; - +import { ReadConsoleMessagesTool } from '@/lib/tools/ReadConsoleMessagesTool'; import { toolToOpenAI } from '@/lib/tools/openai-adapter'; import { Tool } from '@/lib/tools/Tool'; import OpenAI from 'openai'; -import { ReadConsoleMessagesTool } from '@/lib/tools/ReadConsoleMessagesTool'; import { saveFileToTmp } from '@/lib/fileUtils'; import { useGit } from '@/hooks/useGit'; import { Quilly } from '@/components/Quilly'; @@ -105,8 +104,6 @@ export const ChatPane = forwardRef(({ const { user } = useCurrentUser(); const { models } = useProviderModels(); - - // Initialize AI chat with tools const cwd = `/projects/${projectId}`; const customTools = useMemo(() => { diff --git a/src/components/Shakespeare/PreviewPane.tsx b/src/components/Shakespeare/PreviewPane.tsx index 64dc2389..f7059e8e 100644 --- a/src/components/Shakespeare/PreviewPane.tsx +++ b/src/components/Shakespeare/PreviewPane.tsx @@ -3,7 +3,7 @@ import { useState, useEffect, useCallback, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { useFS } from '@/hooks/useFS'; -import { addConsoleMessage, getConsoleMessages } from '@/lib/tools/ReadConsoleMessagesTool'; +import { addConsoleMessage, getConsoleMessages, type ConsoleMessage } from '@/lib/tools/ReadConsoleMessagesTool'; import { useBuildProject } from '@/hooks/useBuildProject'; import { Tabs, TabsContent } from '@/components/ui/tabs'; import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area'; @@ -24,7 +24,6 @@ import { PopoverTrigger, } from '@/components/ui/popover'; import { cn } from '@/lib/utils'; -import type { ConsoleMessage } from '@/types/console'; import { FileTree } from './FileTree'; import { FileEditor } from './FileEditor'; @@ -73,8 +72,6 @@ interface JSONRPCResponse { id: number; } - - export function PreviewPane({ projectId, activeTab, onToggleView, projectName, onFirstInteraction, isPreviewable = true }: PreviewPaneProps) { const { t } = useTranslation(); const [selectedFile, setSelectedFile] = useState(null); @@ -88,12 +85,10 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o const [historyIndex, setHistoryIndex] = useState(0); const [deployDialogOpen, setDeployDialogOpen] = useState(false); - const iframeRef = useRef(null); const { fs } = useFS(); const projectsManager = useProjectsManager(); - const [consoleMessages, setConsoleMessages] = useState([]); const { mutate: buildProject, isPending: isBuildLoading } = useBuildProject(projectId); const [searchParams, setSearchParams] = useSearchParams(); @@ -119,10 +114,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o } }, [searchParams, setSearchParams]); - // Load console messages when project changes - useEffect(() => { - setConsoleMessages(getConsoleMessages()); - }, []); + // Build automatically if "build" parameter was present useEffect(() => { @@ -226,9 +218,8 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o normalizedLevel = params.level as ConsoleMessage['level']; } - // Add to project-specific console messages + // Add to console messages addConsoleMessage(normalizedLevel, params.message); - setConsoleMessages(getConsoleMessages()); // Log to parent console for debugging with appropriate level console[normalizedLevel](`[IFRAME ${params.level.toUpperCase()}] ${params.message}`); @@ -520,28 +511,19 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o const ConsoleDropdown = () => { const [isOpen, setIsOpen] = useState(false); - const copiedMessageRef = useRef(null); - const [_, setCopyUpdate] = useState(0); + const [copiedMessageId, setCopiedMessageId] = useState(null); + const copyMessageToClipboard = async (msg: ConsoleMessage) => { try { await navigator.clipboard.writeText(msg.message); - copiedMessageRef.current = msg.id; - setCopyUpdate(prev => prev + 1); - - // Reset the copied state after 2 seconds - setTimeout(() => { - copiedMessageRef.current = null; - setCopyUpdate(prev => prev + 1); - }, 2000); + setCopiedMessageId(msg.id); + setTimeout(() => setCopiedMessageId(null), 2000); } catch (error) { console.error('Failed to copy message to clipboard:', error); } }; - const closePanel = () => { - setIsOpen(false); - }; - + const consoleMessages = getConsoleMessages(); const hasErrors = consoleMessages.some(msg => msg.level === 'error'); const messageCount = consoleMessages.length; @@ -602,7 +584,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o }} className="h-3 w-3 p-0 opacity-0 group-hover:opacity-100 transition-all duration-200 absolute right-1 top-1 hover:bg-muted-foreground/10 text-muted-foreground hover:text-foreground bg-black/50 rounded" > - {copiedMessageRef.current === msg.id ? ( + {copiedMessageId === msg.id ? ( ) : ( @@ -616,7 +598,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o {/* Close button */}