add floating chat

This commit is contained in:
filemon committed 2026-01-20 13:09:38 -03:00
1 parent 13ea7c192e
commit 6b47557f40
2 files changed
+443 -97

No files matched your search

@@ -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<ChatPaneRef>(null);
const [size, setSize] = useLocalStorage<ChatSize>('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 (
<div className="fixed right-4 bottom-4 z-50">
<Button
onClick={onToggleMinimize}
size="icon"
className="h-14 w-14 rounded-full shadow-lg hover:shadow-xl transition-all duration-200"
aria-label="Open chat"
>
<MessageSquare className="h-6 w-6" />
</Button>
</div>
);
}
return (
<div
className={cn(
"fixed right-4 bottom-4 z-50",
"rounded-lg border shadow-2xl bg-background",
"flex flex-col overflow-hidden",
isResizing && "select-none"
)}
style={{
width: `${size.width}px`,
height: `${size.height}px`,
maxWidth: '92vw',
maxHeight: '70vh',
}}
>
{/* Header - Styled like ProjectSidebar */}
<div className="h-12 px-4 border-b flex-shrink-0 flex items-center justify-between bg-gradient-to-r from-primary/10 via-accent/5 to-primary/10 backdrop-blur">
<div className="flex items-center gap-2">
<ShakespeareLogo className="w-5 h-5" />
<h2 className="text-base font-semibold bg-gradient-to-r from-primary to-accent bg-clip-text text-transparent">
Shakespeare
</h2>
</div>
<div className="flex items-center gap-1">
<Button
onClick={onToggleMinimize}
variant="ghost"
size="icon"
className="h-8 w-8"
aria-label="Minimize chat"
>
<Minus className="h-4 w-4" />
</Button>
<Button
onClick={onClose}
variant="ghost"
size="icon"
className="h-8 w-8"
aria-label="Close floating chat"
>
<X className="h-4 w-4" />
</Button>
</div>
</div>
{/* Chat Body */}
<div className="flex-1 min-h-0 overflow-hidden">
<div className="h-full">
<ChatPane
ref={chatPaneRef}
projectId={projectId}
onNewChat={onNewChat}
onFirstInteraction={onFirstInteraction}
onLoadingChange={onLoadingChange}
isLoading={isLoading}
isBuildLoading={isBuildLoading}
consoleError={consoleError}
onDismissConsoleError={onDismissConsoleError}
/>
</div>
</div>
{/* Resize Handles */}
{/* Left edge (appears as "right" from widget perspective, but it's on the left side) */}
<div
className="absolute top-0 left-0 h-full w-2 cursor-ew-resize hover:bg-primary/10 active:bg-primary/20 transition-colors"
onPointerDown={(e) => startResize(e, 'right')}
style={{ touchAction: 'none' }}
/>
{/* Top edge */}
<div
className="absolute top-0 left-0 w-full h-2 cursor-ns-resize hover:bg-primary/10 active:bg-primary/20 transition-colors"
onPointerDown={(e) => startResize(e, 'top')}
style={{ touchAction: 'none' }}
/>
{/* Bottom edge */}
<div
className="absolute bottom-0 left-0 w-full h-2 cursor-ns-resize hover:bg-primary/10 active:bg-primary/20 transition-colors"
onPointerDown={(e) => startResize(e, 'bottom')}
style={{ touchAction: 'none' }}
/>
{/* Bottom-left corner */}
<div
className="absolute bottom-0 left-0 w-4 h-4 cursor-nesw-resize hover:bg-primary/20 active:bg-primary/30 transition-colors"
onPointerDown={(e) => startResize(e, 'bottomLeft')}
style={{ touchAction: 'none' }}
/>
{/* Top-right corner */}
<div
className="absolute top-0 right-0 w-4 h-4 cursor-nesw-resize hover:bg-primary/20 active:bg-primary/30 transition-colors"
onPointerDown={(e) => startResize(e, 'topRight')}
style={{ touchAction: 'none' }}
/>
</div>
);
}
+197 -97
View File
@@ -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() {
</div>
</div>
<div className="flex items-center gap-4 px-3">
<div className="flex items-center gap-2 px-3">
{project ? (
<>
<GitSyncButton
@@ -239,6 +252,24 @@ export function ProjectView() {
projectName={project.name}
disabled={isAnyLoading}
/>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
className="h-8 w-8"
onClick={() => {
setIsFloatingChatEnabled(true);
setIsFloatingChatMinimized(false);
}}
aria-label="Floating chat"
aria-pressed={isFloatingChatEnabled}
>
<MessageSquare className="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>Floating chat</TooltipContent>
</Tooltip>
</>
) : (
<>
@@ -389,6 +420,23 @@ export function ProjectView() {
/>
</>
)}
{/* Floating Chat Widget - Mobile */}
{isFloatingChatEnabled && project && (
<FloatingChatWidget
projectId={project.id}
isMinimized={isFloatingChatMinimized}
onToggleMinimize={() => setIsFloatingChatMinimized(!isFloatingChatMinimized)}
onClose={() => setIsFloatingChatEnabled(false)}
onNewChat={handleNewChat}
onFirstInteraction={handleFirstInteraction}
onLoadingChange={handleAILoadingChange}
isLoading={isAILoading}
isBuildLoading={build.isPending}
consoleError={consoleError}
onDismissConsoleError={handleDismissConsoleError}
/>
)}
</div>
);
}
@@ -415,123 +463,175 @@ export function ProjectView() {
{/* Main Content Area */}
<div className="flex-1 flex flex-col overflow-hidden bg-background">
<div className="flex-1 overflow-hidden">
<ResizablePanelGroup direction="horizontal" className="h-full">
{/* Chat Panel */}
<ResizablePanel defaultSize={35} minSize={25}>
<div className="h-full flex flex-col">
{/* Chat Header */}
<div className="h-12 px-4 border-b flex-shrink-0">
<div className="flex items-center justify-between h-12">
{/* Left side - Sidebar toggle (when collapsed) */}
<div className="flex items-center gap-3 overflow-hidden">
{!isSidebarVisible && (
<Button
variant="ghost"
size="sm"
onClick={() => setIsSidebarVisible(true)}
className="h-8 w-8 p-0"
aria-label="Open sidebar"
>
<Menu className="h-4 w-4" />
</Button>
)}
{isFloatingChatEnabled ? (
// Full-width preview/code when floating chat is enabled
<div className="h-full">
{project ? (
<PreviewPane
projectId={project.id}
activeTab={isPreviewable ? activeTab : 'code'}
onToggleView={isPreviewable ? () => setActiveTab(activeTab === 'preview' ? 'code' : 'preview') : undefined}
isPreviewable={isPreviewable}
/>
) : (
<div className="h-full p-4 space-y-4">
<Skeleton className="h-8 w-32" />
<Skeleton className="h-full w-full" />
</div>
)}
</div>
) : (
// Original resizable panel layout
<ResizablePanelGroup direction="horizontal" className="h-full">
{/* Chat Panel */}
<ResizablePanel defaultSize={35} minSize={25}>
<div className="h-full flex flex-col">
{/* Chat Header */}
<div className="h-12 px-4 border-b flex-shrink-0">
<div className="flex items-center justify-between h-12">
{/* Left side - Sidebar toggle (when collapsed) */}
<div className="flex items-center gap-3 overflow-hidden">
{!isSidebarVisible && (
<Button
variant="ghost"
size="sm"
onClick={() => setIsSidebarVisible(true)}
className="h-8 w-8 p-0"
aria-label="Open sidebar"
>
<Menu className="h-4 w-4" />
</Button>
)}
{/* Project title */}
<div className="flex flex-1 min-w-0 truncate">
{/* Project title */}
<div className="flex flex-1 min-w-0 truncate">
{project ? (
<ProjectTitleMenu
projectId={project.id}
projectName={project.name}
onNewChat={handleNewChat}
onGitHistory={() => setGitHistoryOpen(true)}
onDuplicate={() => setDuplicateDialogOpen(true)}
onDelete={() => setDeleteDialogOpen(true)}
onProjectDetails={() => setIsProjectDetailsOpen(true)}
isAILoading={isAILoading}
isAnyLoading={isAnyLoading || isDeleting}
className="text-lg"
/>
) : (
<Skeleton className="h-6 w-40" />
)}
</div>
</div>
{/* Right side - Deploy and Sync buttons */}
<div className="flex items-center gap-2">
{project ? (
<ProjectTitleMenu
projectId={project.id}
projectName={project.name}
onNewChat={handleNewChat}
onGitHistory={() => setGitHistoryOpen(true)}
onDuplicate={() => setDuplicateDialogOpen(true)}
onDelete={() => setDeleteDialogOpen(true)}
onProjectDetails={() => setIsProjectDetailsOpen(true)}
isAILoading={isAILoading}
isAnyLoading={isAnyLoading || isDeleting}
className="text-lg"
/>
<>
<GitSyncButton
projectId={project.id}
className="h-8 w-8"
/>
<DeployButton
projectId={project.id}
projectName={project.name}
disabled={isAnyLoading}
/>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
className="h-8 w-8"
onClick={() => setIsFloatingChatEnabled(true)}
aria-label="Floating chat"
aria-pressed={isFloatingChatEnabled}
>
<MessageSquare className="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>Floating chat</TooltipContent>
</Tooltip>
</>
) : (
<Skeleton className="h-6 w-40" />
<>
<Skeleton className="h-8 w-8 rounded" />
<Skeleton className="h-8 w-8 rounded" />
</>
)}
</div>
</div>
</div>
{/* Right side - Deploy and Sync buttons */}
<div className="flex items-center gap-2">
{project ? (
<>
<GitSyncButton
projectId={project.id}
className="h-8 w-8"
/>
<DeployButton
projectId={project.id}
projectName={project.name}
disabled={isAnyLoading}
/>
</>
) : (
<>
<Skeleton className="h-8 w-8 rounded" />
<Skeleton className="h-8 w-8 rounded" />
</>
)}
</div>
{/* Chat Content */}
<div className="flex-1 overflow-hidden">
{project ? (
<ChatPane
ref={chatPaneRef}
projectId={project.id}
onNewChat={handleNewChat}
onFirstInteraction={handleFirstInteraction}
onLoadingChange={handleAILoadingChange}
isLoading={isAILoading}
isBuildLoading={build.isPending}
consoleError={consoleError}
onDismissConsoleError={handleDismissConsoleError}
/>
) : (
<div className="h-full p-4 space-y-4">
<Skeleton className="h-12 w-full" />
<Skeleton className="h-8 w-3/4" />
<Skeleton className="h-8 w-1/2" />
<Skeleton className="h-32 w-full" />
</div>
)}
</div>
</div>
</ResizablePanel>
{/* Chat Content */}
<div className="flex-1 overflow-hidden">
{/* Resizable Handle */}
<ResizableHandle withHandle />
{/* Preview/Code Panel */}
<ResizablePanel defaultSize={65} minSize={30}>
<div className="h-full">
{project ? (
<ChatPane
ref={chatPaneRef}
<PreviewPane
projectId={project.id}
onNewChat={handleNewChat}
onFirstInteraction={handleFirstInteraction}
onLoadingChange={handleAILoadingChange}
isLoading={isAILoading}
isBuildLoading={build.isPending}
consoleError={consoleError}
onDismissConsoleError={handleDismissConsoleError}
activeTab={isPreviewable ? activeTab : 'code'}
onToggleView={isPreviewable ? () => setActiveTab(activeTab === 'preview' ? 'code' : 'preview') : undefined}
isPreviewable={isPreviewable}
/>
) : (
<div className="h-full p-4 space-y-4">
<Skeleton className="h-12 w-full" />
<Skeleton className="h-8 w-3/4" />
<Skeleton className="h-8 w-1/2" />
<Skeleton className="h-32 w-full" />
<Skeleton className="h-8 w-32" />
<Skeleton className="h-full w-full" />
</div>
)}
</div>
</div>
</ResizablePanel>
{/* Resizable Handle */}
<ResizableHandle withHandle />
{/* Preview/Code Panel */}
<ResizablePanel defaultSize={65} minSize={30}>
<div className="h-full">
{project ? (
<PreviewPane
projectId={project.id}
activeTab={isPreviewable ? activeTab : 'code'}
onToggleView={isPreviewable ? () => setActiveTab(activeTab === 'preview' ? 'code' : 'preview') : undefined}
isPreviewable={isPreviewable}
/>
) : (
<div className="h-full p-4 space-y-4">
<Skeleton className="h-8 w-32" />
<Skeleton className="h-full w-full" />
</div>
)}
</div>
</ResizablePanel>
</ResizablePanelGroup>
</ResizablePanel>
</ResizablePanelGroup>
)}
</div>
</div>
{/* Floating Chat Widget - Desktop */}
{isFloatingChatEnabled && project && (
<FloatingChatWidget
projectId={project.id}
isMinimized={isFloatingChatMinimized}
onToggleMinimize={() => setIsFloatingChatMinimized(!isFloatingChatMinimized)}
onClose={() => setIsFloatingChatEnabled(false)}
onNewChat={handleNewChat}
onFirstInteraction={handleFirstInteraction}
onLoadingChange={handleAILoadingChange}
isLoading={isAILoading}
isBuildLoading={build.isPending}
consoleError={consoleError}
onDismissConsoleError={handleDismissConsoleError}
/>
)}
{/* Dialogs */}
{project && (
<>