import { useCallback, useRef, useState } from 'react' import type { ComponentProps, ReactNode } from 'react' import { motion } from 'motion/react' import { Resizable } from 'react-resizable' import { AgentBlobatar } from '@/client/components/shared/agent-blobatar' import { Popover, PopoverContent, PopoverTrigger } from '@/client/components/ui/popover' import { cn } from '@/client/lib/cn' import { useUiStore } from '@/client/store/ui' import type { AgentTheme } from '@/lib/types' type ChatPopupProps = { agent: AgentTheme loading: boolean open: boolean onOpenChange: (open: boolean) => void onOpenChangeComplete: (open: boolean) => void children: (onClose: () => void) => ReactNode } export function ChatPopup({ agent, loading, open, onOpenChange, onOpenChangeComplete, children }: ChatPopupProps) { const popupRef = useRef(null) const popupChatWidth = useUiStore(state => state.popupChatWidth) const setPopupChatWidth = useUiStore(state => state.setPopupChatWidth) const [size, setSize] = useState({ width: popupChatWidth, min: 0, max: popupChatWidth }) const setPopupRef = useCallback( (popup: HTMLDivElement | null) => { popupRef.current = popup if (!popup) return popup.style.setProperty('--chat-popup-width', `${popupChatWidth}px`) // Report the rendered width, including viewport clamping of the saved preference. const observer = new ResizeObserver(([entry]) => { if (!entry) return const { minWidth, maxWidth } = getComputedStyle(popup) setSize({ width: Math.round(entry.contentRect.width), min: parseFloat(minWidth), max: parseFloat(maxWidth) }) }) observer.observe(popup) return () => { observer.disconnect() popupRef.current = null } }, [popupChatWidth] ) const resize = (width: number) => { if (!popupRef.current) return const { minWidth, maxWidth } = getComputedStyle(popupRef.current) setPopupChatWidth( Math.round(Math.min(parseFloat(maxWidth), Math.max(parseFloat(minWidth), width))) ) } const onClose = () => onOpenChange(false) const handleOpenChange: NonNullable['onOpenChange']> = ( nextOpen, eventDetails ) => { // Applet actions move focus outside before their click handler runs. // Keep chat open so adding context only refocuses the existing composer. if ( !nextOpen && (eventDetails.reason === 'outside-press' || eventDetails.reason === 'focus-out') ) { eventDetails.cancel() return } onOpenChange(nextOpen) } return ( {/* Floating over a themed workspace — use the card pair so it stays legible regardless of the active theme background. */} } /> setPopupChatWidth(Math.round(size.width))} handle={
{ const width = popupRef.current?.offsetWidth ?? popupChatWidth const step = event.shiftKey ? 32 : 16 if (event.key === 'ArrowLeft') resize(width + step) else if (event.key === 'ArrowRight') resize(width - step) else if (event.key === 'Home') resize(-Infinity) else if (event.key === 'End') resize(Infinity) else return event.preventDefault() }} /> } > -anchor.height} align="end" className="relative flex h-[calc(100vh-2rem)] w-(--chat-popup-width) max-w-[calc(100vw-1.5rem)] min-w-[min(440px,calc(100vw-1.5rem))] flex-col gap-0 rounded-3xl p-0 transition-[opacity,transform] sm:h-[calc(100vh-8rem)]" keepMounted > {children(onClose)} ) }