diff --git a/src/components/Shakespeare/FloatingChatWidget.tsx b/src/components/Shakespeare/FloatingChatWidget.tsx new file mode 100644 index 00000000..ca2dcf2b --- /dev/null +++ b/src/components/Shakespeare/FloatingChatWidget.tsx @@ -0,0 +1,246 @@ +import { useEffect, useRef, useState, useCallback } from 'react'; +import { MessageSquare, Minus, X } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { ChatPane, type ChatPaneRef } from '@/components/Shakespeare/ChatPane'; +import { ShakespeareLogo } from '@/components/ShakespeareLogo'; +import { ProjectPreviewConsoleError } from '@/lib/consoleMessages'; +import { useLocalStorage } from '@/hooks/useLocalStorage'; +import { cn } from '@/lib/utils'; + +interface FloatingChatWidgetProps { + projectId: string; + isMinimized: boolean; + onToggleMinimize: () => void; + onClose: () => void; + onNewChat: () => void; + onFirstInteraction?: () => void; + onLoadingChange?: (isLoading: boolean) => void; + isLoading?: boolean; + isBuildLoading?: boolean; + consoleError?: ProjectPreviewConsoleError | null; + onDismissConsoleError?: () => void; +} + +interface ChatSize { + width: number; + height: number; +} + +const MIN_WIDTH = 320; +const MIN_HEIGHT = 380; +const MAX_WIDTH = 720; +const MAX_HEIGHT = 900; +const DEFAULT_WIDTH = 420; +const DEFAULT_HEIGHT = 520; + +export function FloatingChatWidget({ + projectId, + isMinimized, + onToggleMinimize, + onClose, + onNewChat, + onFirstInteraction, + onLoadingChange, + isLoading, + isBuildLoading, + consoleError, + onDismissConsoleError, +}: FloatingChatWidgetProps) { + const chatPaneRef = useRef(null); + const [size, setSize] = useLocalStorage('shk:floatingChatSize', { + width: DEFAULT_WIDTH, + height: DEFAULT_HEIGHT, + }); + const [isResizing, setIsResizing] = useState(false); + const resizeStartRef = useRef<{ x: number; y: number; width: number; height: number } | null>(null); + + // Handle keyboard escape to minimize + useEffect(() => { + const handleKeyDown = (e: KeyboardEvent) => { + if (e.key === 'Escape' && !isMinimized) { + onToggleMinimize(); + } + }; + + if (!isMinimized) { + window.addEventListener('keydown', handleKeyDown); + return () => window.removeEventListener('keydown', handleKeyDown); + } + }, [isMinimized, onToggleMinimize]); + + // Resize handlers + const startResize = useCallback((e: React.PointerEvent, direction: 'right' | 'bottom' | 'top' | 'bottomLeft' | 'topRight') => { + e.preventDefault(); + e.stopPropagation(); + + const target = e.currentTarget as HTMLElement; + target.setPointerCapture(e.pointerId); + + setIsResizing(true); + document.body.style.userSelect = 'none'; + + resizeStartRef.current = { + x: e.clientX, + y: e.clientY, + width: size.width, + height: size.height, + }; + + const handlePointerMove = (moveEvent: PointerEvent) => { + if (!resizeStartRef.current) return; + + const maxW = Math.min(MAX_WIDTH, window.innerWidth - 16); + const maxH = Math.min(MAX_HEIGHT, window.innerHeight - 16); + + let newWidth = resizeStartRef.current.width; + let newHeight = resizeStartRef.current.height; + + // Handle horizontal resizing (left edge for bottom-right anchored widget) + if (direction === 'right' || direction === 'bottomLeft' || direction === 'topRight') { + const deltaX = resizeStartRef.current.x - moveEvent.clientX; + newWidth = Math.max(MIN_WIDTH, Math.min(maxW, resizeStartRef.current.width + deltaX)); + } + + // Handle vertical resizing from bottom (grows downward) + if (direction === 'bottom' || direction === 'bottomLeft') { + const deltaY = resizeStartRef.current.y - moveEvent.clientY; + newHeight = Math.max(MIN_HEIGHT, Math.min(maxH, resizeStartRef.current.height + deltaY)); + } + + // Handle vertical resizing from top (grows upward, widget anchored at bottom) + if (direction === 'top' || direction === 'topRight') { + const deltaY = moveEvent.clientY - resizeStartRef.current.y; + newHeight = Math.max(MIN_HEIGHT, Math.min(maxH, resizeStartRef.current.height - deltaY)); + } + + setSize({ width: newWidth, height: newHeight }); + }; + + const handlePointerUp = () => { + setIsResizing(false); + document.body.style.userSelect = ''; + resizeStartRef.current = null; + + window.removeEventListener('pointermove', handlePointerMove); + window.removeEventListener('pointerup', handlePointerUp); + }; + + window.addEventListener('pointermove', handlePointerMove); + window.addEventListener('pointerup', handlePointerUp); + }, [size, setSize]); + + if (isMinimized) { + return ( +
+ +
+ ); + } + + return ( +
+ {/* Header - Styled like ProjectSidebar */} +
+
+ +

+ Shakespeare +

+
+
+ + +
+
+ + {/* Chat Body */} +
+
+ +
+
+ + {/* Resize Handles */} + {/* Left edge (appears as "right" from widget perspective, but it's on the left side) */} +
startResize(e, 'right')} + style={{ touchAction: 'none' }} + /> + + {/* Top edge */} +
startResize(e, 'top')} + style={{ touchAction: 'none' }} + /> + + {/* Bottom edge */} +
startResize(e, 'bottom')} + style={{ touchAction: 'none' }} + /> + + {/* Bottom-left corner */} +
startResize(e, 'bottomLeft')} + style={{ touchAction: 'none' }} + /> + + {/* Top-right corner */} +
startResize(e, 'topRight')} + style={{ touchAction: 'none' }} + /> +
+ ); +} diff --git a/src/pages/ProjectView.tsx b/src/pages/ProjectView.tsx index 132c9c62..e7240835 100644 --- a/src/pages/ProjectView.tsx +++ b/src/pages/ProjectView.tsx @@ -31,6 +31,9 @@ import { useGitAutosync } from '@/hooks/useGitAutosync'; import { useQueryClient } from '@tanstack/react-query'; import { cn } from '@/lib/utils'; import { useProjectSessionStatus } from '@/hooks/useProjectSessionStatus'; +import { useLocalStorage } from '@/hooks/useLocalStorage'; +import { FloatingChatWidget } from '@/components/Shakespeare/FloatingChatWidget'; +import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'; export function ProjectView() { const { projectId } = useParams<{ projectId: string }>(); @@ -59,6 +62,16 @@ export function ProjectView() { const [isSidebarVisible, setIsSidebarVisible] = useState(!isMobile); + // Floating chat state + const [isFloatingChatEnabled, setIsFloatingChatEnabled] = useLocalStorage( + 'shk:floatingChatEnabled', + false + ); + const [isFloatingChatMinimized, setIsFloatingChatMinimized] = useLocalStorage( + 'shk:floatingChatMinimized', + false + ); + const build = useBuildProject(projectId!); const { data: isPreviewable = false } = useIsProjectPreviewable(projectId!); const { hasRunningSessions } = useProjectSessionStatus(projectId || ''); @@ -227,7 +240,7 @@ export function ProjectView() {
-
+
{project ? ( <> + + + + + Floating chat + ) : ( <> @@ -389,6 +420,23 @@ export function ProjectView() { /> )} + + {/* Floating Chat Widget - Mobile */} + {isFloatingChatEnabled && project && ( + setIsFloatingChatMinimized(!isFloatingChatMinimized)} + onClose={() => setIsFloatingChatEnabled(false)} + onNewChat={handleNewChat} + onFirstInteraction={handleFirstInteraction} + onLoadingChange={handleAILoadingChange} + isLoading={isAILoading} + isBuildLoading={build.isPending} + consoleError={consoleError} + onDismissConsoleError={handleDismissConsoleError} + /> + )}
); } @@ -415,123 +463,175 @@ export function ProjectView() { {/* Main Content Area */}
- - {/* Chat Panel */} - -
- {/* Chat Header */} -
-
- {/* Left side - Sidebar toggle (when collapsed) */} -
- {!isSidebarVisible && ( - - )} + {isFloatingChatEnabled ? ( + // Full-width preview/code when floating chat is enabled +
+ {project ? ( + setActiveTab(activeTab === 'preview' ? 'code' : 'preview') : undefined} + isPreviewable={isPreviewable} + /> + ) : ( +
+ + +
+ )} +
+ ) : ( + // Original resizable panel layout + + {/* Chat Panel */} + +
+ {/* Chat Header */} +
+
+ {/* Left side - Sidebar toggle (when collapsed) */} +
+ {!isSidebarVisible && ( + + )} - {/* Project title */} -
+ {/* Project title */} +
+ {project ? ( + setGitHistoryOpen(true)} + onDuplicate={() => setDuplicateDialogOpen(true)} + onDelete={() => setDeleteDialogOpen(true)} + onProjectDetails={() => setIsProjectDetailsOpen(true)} + isAILoading={isAILoading} + isAnyLoading={isAnyLoading || isDeleting} + className="text-lg" + /> + ) : ( + + )} +
+
+ + {/* Right side - Deploy and Sync buttons */} +
{project ? ( - setGitHistoryOpen(true)} - onDuplicate={() => setDuplicateDialogOpen(true)} - onDelete={() => setDeleteDialogOpen(true)} - onProjectDetails={() => setIsProjectDetailsOpen(true)} - isAILoading={isAILoading} - isAnyLoading={isAnyLoading || isDeleting} - className="text-lg" - /> + <> + + + + + + + Floating chat + + ) : ( - + <> + + + )}
+
- {/* Right side - Deploy and Sync buttons */} -
- {project ? ( - <> - - - - ) : ( - <> - - - - )} -
+ {/* Chat Content */} +
+ {project ? ( + + ) : ( +
+ + + + +
+ )}
+ - {/* Chat Content */} -
+ {/* Resizable Handle */} + + + {/* Preview/Code Panel */} + +
{project ? ( - setActiveTab(activeTab === 'preview' ? 'code' : 'preview') : undefined} + isPreviewable={isPreviewable} /> ) : (
- - - - + +
)}
-
- - - {/* Resizable Handle */} - - - {/* Preview/Code Panel */} - -
- {project ? ( - setActiveTab(activeTab === 'preview' ? 'code' : 'preview') : undefined} - isPreviewable={isPreviewable} - /> - ) : ( -
- - -
- )} -
-
- + + + )}
+ {/* Floating Chat Widget - Desktop */} + {isFloatingChatEnabled && project && ( + setIsFloatingChatMinimized(!isFloatingChatMinimized)} + onClose={() => setIsFloatingChatEnabled(false)} + onNewChat={handleNewChat} + onFirstInteraction={handleFirstInteraction} + onLoadingChange={handleAILoadingChange} + isLoading={isAILoading} + isBuildLoading={build.isPending} + consoleError={consoleError} + onDismissConsoleError={handleDismissConsoleError} + /> + )} + {/* Dialogs */} {project && ( <>