diff --git a/src/components/Shakespeare/PreviewPane.tsx b/src/components/Shakespeare/PreviewPane.tsx index 2736683a..53da503c 100644 --- a/src/components/Shakespeare/PreviewPane.tsx +++ b/src/components/Shakespeare/PreviewPane.tsx @@ -8,7 +8,7 @@ import { useBuildProject } from '@/hooks/useBuildProject'; import { Tabs, TabsContent } from '@/components/ui/tabs'; import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area'; import { Button } from '@/components/ui/button'; -import { FolderOpen, ArrowLeft, Bug, Copy, Check, Play, Loader2, MenuIcon, Code, CloudUpload, X } from 'lucide-react'; +import { FolderOpen, ArrowLeft, Bug, Copy, Check, Play, Loader2, MenuIcon, Code, CloudUpload, X, Terminal } from 'lucide-react'; import { useIsMobile } from '@/hooks/useIsMobile'; import { GitStatusIndicator } from '@/components/GitStatusIndicator'; import { BrowserAddressBar } from '@/components/ui/browser-address-bar'; @@ -27,6 +27,7 @@ import { cn } from '@/lib/utils'; import { FileTree } from './FileTree'; import { FileEditor } from './FileEditor'; import { DeployDialog } from '@/components/DeployDialog'; +import { Terminal as TerminalComponent } from '@/components/Terminal'; import { useSearchParams } from 'react-router-dom'; // Get iframe domain from environment variable @@ -76,7 +77,8 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o const [selectedFile, setSelectedFile] = useState(null); const [fileContent, setFileContent] = useState(''); const [isLoading, setIsLoading] = useState(false); - const [mobileCodeView, setMobileCodeView] = useState<'explorer' | 'editor'>('explorer'); + const [mobileCodeView, setMobileCodeView] = useState<'explorer' | 'editor' | 'terminal'>('explorer'); + const [desktopCodeView, setDesktopCodeView] = useState<'files' | 'terminal'>('files'); const isMobile = useIsMobile(); const [hasBuiltProject, setHasBuiltProject] = useState(false); const [currentPath, setCurrentPath] = useState('/'); @@ -455,6 +457,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o setSelectedFile(null); setFileContent(''); setMobileCodeView('explorer'); + setDesktopCodeView('files'); // Reset navigation history and state setCurrentPath('/'); @@ -731,16 +734,47 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o {isMobile ? (
{mobileCodeView === 'explorer' ? ( - - -
- +
+ + +
+ +
+
+ {/* Floating Terminal Button */} + +
+ ) : mobileCodeView === 'terminal' ? ( + <> +
+ +

+ Terminal +

+
- +
+ +
+ ) : ( <>
@@ -803,6 +837,14 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o {t('backToPreview')}
+
)} @@ -810,43 +852,59 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o {!isMobile && !isPreviewable && (
+
)}
-
- - -
- + {desktopCodeView === 'terminal' ? ( +
+ +
+ ) : ( + <> +
+ + +
+ +
+
- -
-
- {selectedFile ? ( - - ) : ( -
-
-

- {t('selectFileFromExplorer')} -

-
+
+ {selectedFile ? ( + + ) : ( +
+
+

+ {t('selectFileFromExplorer')} +

+
+
+ )}
- )} -
+ + )}
)} diff --git a/src/components/Terminal.tsx b/src/components/Terminal.tsx new file mode 100644 index 00000000..3f573701 --- /dev/null +++ b/src/components/Terminal.tsx @@ -0,0 +1,259 @@ +import { useState, useRef, useEffect, useCallback } from 'react'; +import { ScrollArea } from '@/components/ui/scroll-area'; +import { Button } from '@/components/ui/button'; +import { X, Copy, Trash2 } from 'lucide-react'; +import { cn } from '@/lib/utils'; +import { ShellTool } from '@/lib/tools/ShellTool'; +import { useFS } from '@/hooks/useFS'; +import { useGit } from '@/hooks/useGit'; + +interface TerminalProps { + projectId: string; + onClose?: () => void; + className?: string; +} + +interface TerminalLine { + id: string; + type: 'input' | 'output' | 'error'; + content: string; + timestamp: Date; +} + +export function Terminal({ projectId, onClose, className }: TerminalProps) { + const [lines, setLines] = useState([]); + const [currentInput, setCurrentInput] = useState(''); + const [isExecuting, setIsExecuting] = useState(false); + const [commandHistory, setCommandHistory] = useState([]); + const [historyIndex, setHistoryIndex] = useState(-1); + const [currentWorkingDirectory, setCurrentWorkingDirectory] = useState(`/projects/${projectId}`); + + const inputRef = useRef(null); + const scrollRef = useRef(null); + const shellToolRef = useRef(null); + + const { fs } = useFS(); + const { git } = useGit(); + + // Initialize ShellTool + useEffect(() => { + shellToolRef.current = new ShellTool(fs, `/projects/${projectId}`, git); + + // Add welcome message + setLines([{ + id: Date.now().toString(), + type: 'output', + content: `Welcome to Shakespeare Terminal\nProject: ${projectId}\nType 'help' to see available commands.`, + timestamp: new Date() + }]); + }, [projectId, fs, git]); + + // Auto-scroll to bottom when new lines are added + useEffect(() => { + if (scrollRef.current) { + scrollRef.current.scrollTop = scrollRef.current.scrollHeight; + } + }, [lines]); + + // Focus input when component mounts + useEffect(() => { + if (inputRef.current) { + inputRef.current.focus(); + } + }, []); + + const addLine = useCallback((type: TerminalLine['type'], content: string) => { + const newLine: TerminalLine = { + id: Date.now().toString() + Math.random(), + type, + content, + timestamp: new Date() + }; + setLines(prev => [...prev, newLine]); + }, []); + + const executeCommand = useCallback(async (command: string) => { + if (!shellToolRef.current || !command.trim()) return; + + // Add command to history + if (command.trim() && !commandHistory.includes(command.trim())) { + setCommandHistory(prev => [...prev, command.trim()]); + } + setHistoryIndex(-1); + + // Add input line + addLine('input', `$ ${command}`); + + setIsExecuting(true); + + try { + // Handle special help command + if (command.trim().toLowerCase() === 'help') { + const availableCommands = shellToolRef.current.getAvailableCommands(); + const helpText = `Available commands:\n\n${availableCommands.map(cmd => + `${cmd.name.padEnd(12)} - ${cmd.description}` + ).join('\n')}\n\nUse 'which ' for more details about a specific command.`; + addLine('output', helpText); + } else { + const result = await shellToolRef.current.execute({ command }); + + // Update current working directory + const newCwd = shellToolRef.current.getCurrentWorkingDirectory(); + setCurrentWorkingDirectory(newCwd); + + // Add output + if (result && result.trim()) { + addLine('output', result); + } + } + } catch (error) { + addLine('error', `Error: ${error instanceof Error ? error.message : 'Unknown error'}`); + } finally { + setIsExecuting(false); + } + }, [addLine, commandHistory]); + + const handleSubmit = useCallback((e: React.FormEvent) => { + e.preventDefault(); + if (currentInput.trim() && !isExecuting) { + executeCommand(currentInput); + setCurrentInput(''); + } + }, [currentInput, isExecuting, executeCommand]); + + const handleKeyDown = useCallback((e: React.KeyboardEvent) => { + if (e.key === 'ArrowUp') { + e.preventDefault(); + if (commandHistory.length > 0) { + const newIndex = historyIndex === -1 ? commandHistory.length - 1 : Math.max(0, historyIndex - 1); + setHistoryIndex(newIndex); + setCurrentInput(commandHistory[newIndex]); + } + } else if (e.key === 'ArrowDown') { + e.preventDefault(); + if (historyIndex >= 0) { + const newIndex = historyIndex + 1; + if (newIndex >= commandHistory.length) { + setHistoryIndex(-1); + setCurrentInput(''); + } else { + setHistoryIndex(newIndex); + setCurrentInput(commandHistory[newIndex]); + } + } + } else if (e.key === 'Tab') { + e.preventDefault(); + // Basic tab completion could be added here + } + }, [commandHistory, historyIndex]); + + const copyToClipboard = useCallback(async (content: string) => { + try { + await navigator.clipboard.writeText(content); + } catch (error) { + console.error('Failed to copy to clipboard:', error); + } + }, []); + + const clearTerminal = useCallback(() => { + setLines([]); + }, []); + + const getPrompt = () => { + const relativePath = currentWorkingDirectory.replace(`/projects/${projectId}`, '') || '/'; + return `${projectId}:${relativePath}$`; + }; + + return ( +
+ {/* Terminal Header */} +
+
+
+
+
+
+
+ Terminal - {projectId} +
+
+ + {onClose && ( + + )} +
+
+ + {/* Terminal Content */} +
+ +
+ {lines.map((line) => ( +
+
+                  {line.content}
+                
+ +
+ ))} + {isExecuting && ( +
+ Executing... +
+ )} +
+
+ + {/* Command Input */} +
+
+ + {getPrompt()} + + setCurrentInput(e.target.value)} + onKeyDown={handleKeyDown} + disabled={isExecuting} + className="flex-1 bg-transparent border-none outline-none text-green-400 font-mono text-xs placeholder-gray-500" + placeholder="Enter command..." + autoComplete="off" + spellCheck="false" + /> +
+
+
+
+ ); +} \ No newline at end of file