From 68702abc411111d12df53ccf6bd3e943101e7688 Mon Sep 17 00:00:00 2001 From: MK Fain Date: Wed, 3 Dec 2025 19:44:12 -0600 Subject: [PATCH] Add floating global chat feature - Add GlobalChatContext, GlobalChatProvider for managing chat state - Create FloatingChatButton component with unread message indicator - Create GlobalChatPane with desktop (floating panel) and mobile (full screen) views - Features include: - Streaming AI responses with markdown rendering - Auto context trimming when messages exceed ~50k tokens - Easy text copy from messages (hover to reveal copy button) - Pop-out to separate window - Minimize/dismiss functionality - Clear chat history - Mobile-optimized full-screen drawer - Add settings in Preferences page: - Enable/disable floating chat button - Customize chat system prompt - Add translations for all supported languages (en, pt, zh, ha, yo, ig) - Integrate with existing AI provider configuration --- src/App.tsx | 25 +- src/components/FloatingChatButton.tsx | 38 ++ src/components/GlobalChatPane.tsx | 483 ++++++++++++++++++++++++++ src/components/GlobalChatProvider.tsx | 192 ++++++++++ src/contexts/AppContext.ts | 4 + src/contexts/GlobalChatContext.ts | 22 ++ src/hooks/useGlobalChat.ts | 10 + src/locales/en.json | 14 +- src/locales/ha.json | 14 +- src/locales/ig.json | 14 +- src/locales/pt.json | 14 +- src/locales/yo.json | 14 +- src/locales/zh.json | 14 +- src/pages/Preferences.tsx | 126 ++++++- 14 files changed, 967 insertions(+), 17 deletions(-) create mode 100644 src/components/FloatingChatButton.tsx create mode 100644 src/components/GlobalChatPane.tsx create mode 100644 src/components/GlobalChatProvider.tsx create mode 100644 src/contexts/GlobalChatContext.ts create mode 100644 src/hooks/useGlobalChat.ts diff --git a/src/App.tsx b/src/App.tsx index a85f19c1..c8c4dd06 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -28,6 +28,9 @@ import { DynamicFavicon } from '@/components/DynamicFavicon'; import { OfflineIndicator } from '@/components/OfflineIndicator'; import { PWAUpdatePrompt } from '@/components/PWAUpdatePrompt'; import { ErrorBoundary } from '@/components/ErrorBoundary'; +import { GlobalChatProvider } from '@/components/GlobalChatProvider'; +import { FloatingChatButton } from '@/components/FloatingChatButton'; +import { GlobalChatPane } from '@/components/GlobalChatPane'; import AppRouter from './AppRouter'; @@ -177,15 +180,19 @@ export function App() { - - - - - - - - - + + + + + + + + + + + + + diff --git a/src/components/FloatingChatButton.tsx b/src/components/FloatingChatButton.tsx new file mode 100644 index 00000000..84610143 --- /dev/null +++ b/src/components/FloatingChatButton.tsx @@ -0,0 +1,38 @@ +import { MessageCircle } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { useGlobalChat } from '@/hooks/useGlobalChat'; +import { useAppContext } from '@/hooks/useAppContext'; +import { cn } from '@/lib/utils'; + +export function FloatingChatButton() { + const { isOpen, setIsOpen, isPoppedOut, messages } = useGlobalChat(); + const { config } = useAppContext(); + + // Don't render if disabled in settings or if popped out + if (config.globalChatEnabled === false || isPoppedOut) { + return null; + } + + return ( + + ); +} diff --git a/src/components/GlobalChatPane.tsx b/src/components/GlobalChatPane.tsx new file mode 100644 index 00000000..94d85956 --- /dev/null +++ b/src/components/GlobalChatPane.tsx @@ -0,0 +1,483 @@ +import { useState, useRef, useEffect, useCallback } from 'react'; +import { useTranslation } from 'react-i18next'; +import { X, Minimize2, ExternalLink, Trash2, Copy, Check, ChevronDown, ArrowUp, Square, Loader2 } from 'lucide-react'; +import { Streamdown } from 'streamdown'; +import { Button } from '@/components/ui/button'; +import { Textarea } from '@/components/ui/textarea'; +import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'; +import { ModelSelector } from '@/components/ModelSelector'; +import { useGlobalChat } from '@/hooks/useGlobalChat'; +import { useAppContext } from '@/hooks/useAppContext'; +import { useAISettings } from '@/hooks/useAISettings'; +import { useIsMobile } from '@/hooks/useIsMobile'; +import { useTheme } from '@/hooks/useTheme'; +import { cn } from '@/lib/utils'; +import type { GlobalChatMessage } from '@/contexts/GlobalChatContext'; + +// Message component with copy functionality +function ChatMessage({ message }: { message: GlobalChatMessage }) { + const [copied, setCopied] = useState(false); + const { displayTheme } = useTheme(); + const isUser = message.role === 'user'; + + const handleCopy = useCallback(async () => { + try { + await navigator.clipboard.writeText(message.content); + setCopied(true); + setTimeout(() => setCopied(false), 2000); + } catch (error) { + console.error('Failed to copy:', error); + } + }, [message.content]); + + return ( +
+
+ {isUser ? ( +

{message.content}

+ ) : ( +
+ + {message.content || '...'} + +
+ )} + + {/* Copy button - shows on hover */} + +
+
+ ); +} + +export function GlobalChatPane() { + const { t } = useTranslation(); + const { + messages, + isOpen, + isLoading, + sendMessage, + stopGeneration, + clearMessages, + setIsOpen, + setIsPoppedOut, + } = useGlobalChat(); + const { config } = useAppContext(); + const { settings, addRecentlyUsedModel } = useAISettings(); + const isMobile = useIsMobile(); + + const [input, setInput] = useState(''); + const [providerModel, setProviderModel] = useState(() => settings.recentlyUsedModels?.[0] || ''); + const [showScrollToBottom, setShowScrollToBottom] = useState(false); + const scrollAreaRef = useRef(null); + const textareaRef = useRef(null); + + // Sync provider model with settings + useEffect(() => { + if (!providerModel && settings.recentlyUsedModels?.length) { + setProviderModel(settings.recentlyUsedModels[0]); + } + }, [providerModel, settings.recentlyUsedModels]); + + // Auto-scroll to bottom when new messages arrive + useEffect(() => { + if (scrollAreaRef.current) { + const container = scrollAreaRef.current; + const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 100; + + if (isNearBottom || messages.length <= 1) { + container.scrollTop = container.scrollHeight; + } + } + }, [messages]); + + // Handle scroll to show/hide scroll-to-bottom button + const handleScroll = useCallback(() => { + if (scrollAreaRef.current) { + const container = scrollAreaRef.current; + const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 100; + setShowScrollToBottom(!isNearBottom && messages.length > 0); + } + }, [messages.length]); + + const scrollToBottom = useCallback(() => { + if (scrollAreaRef.current) { + scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight; + } + }, []); + + const handleSend = useCallback(async () => { + if (!input.trim() || isLoading || !providerModel) return; + + const messageContent = input.trim(); + setInput(''); + + addRecentlyUsedModel(providerModel); + await sendMessage(messageContent, providerModel); + + // Focus back on textarea + textareaRef.current?.focus(); + }, [input, isLoading, providerModel, addRecentlyUsedModel, sendMessage]); + + const handleKeyDown = useCallback((e: React.KeyboardEvent) => { + if (e.key === 'Enter' && !e.shiftKey && !isMobile) { + e.preventDefault(); + handleSend(); + } + }, [handleSend, isMobile]); + + const handlePopOut = useCallback(() => { + // Open in a new window + const width = 400; + const height = 600; + const left = window.screen.width - width - 50; + const top = 50; + + const popoutWindow = window.open( + '', + 'shakespeare-global-chat', + `width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes` + ); + + if (popoutWindow) { + // Copy styles and render chat in new window + popoutWindow.document.write(` + + + + Shakespeare Chat + + + +
+

Chat window opened!

+

Return to the main Shakespeare window to continue chatting.

+
+ + + `); + + setIsPoppedOut(true); + setIsOpen(false); + + // Handle window close + const checkClosed = setInterval(() => { + if (popoutWindow.closed) { + setIsPoppedOut(false); + clearInterval(checkClosed); + } + }, 500); + } + }, [setIsPoppedOut, setIsOpen]); + + // Don't render if disabled or not open + if (config.globalChatEnabled === false || !isOpen) { + return null; + } + + const hasProviders = settings.providers.length > 0; + + // Mobile: Full screen drawer from bottom + if (isMobile) { + return ( +
+ {/* Header */} +
+

{t('globalChat')}

+
+ {messages.length > 0 && ( + + + + + {t('clearChat')} + + )} + +
+
+ + {/* Messages */} +
+ {messages.length === 0 ? ( +
+
+

{t('globalChatWelcome')}

+

{t('globalChatDescription')}

+
+
+ ) : ( + messages.map((message) => ( + + )) + )} + + {isLoading && messages[messages.length - 1]?.role !== 'assistant' && ( +
+
+ +
+
+ )} +
+ + {/* Scroll to bottom button */} + {showScrollToBottom && ( + + )} + + {/* Input */} +
+
+ +
+
+