import { ClipboardIcon, EditIcon, MaximizeIcon, MessageCirclePlusIcon, MoreVerticalIcon, SettingsIcon, XIcon, } from 'lucide-react' import { KeyboardEvent, useState } from 'react' import { toast } from 'sonner' import { Button, cn, copyToClipboard, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, Input, } from 'ui' import { ButtonTooltip } from '../ButtonTooltip' import { ShortcutPills, ShortcutTooltip } from '../ShortcutTooltip' import { AIAssistantChatSelector } from './AIAssistantChatSelector' import { AIAssistantMetadataWarning } from './AIAssistantMetadataWarning' import { useIsExplorerEnabled } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext' import { useCreateChat } from '@/components/interfaces/Explorer/hooks' import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state' import { SHORTCUT_DEFINITIONS, SHORTCUT_IDS } from '@/state/shortcuts/registry' import { useShortcut } from '@/state/shortcuts/useShortcut' import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state' interface AIAssistantHeaderProps { isChatLoading: boolean shortcutsEnabled?: boolean onNewChat: () => void onCloseAssistant: () => void showMetadataWarning: boolean updatedOptInSinceMCP: boolean aiOptInLevel: 'disabled' | 'schema' | 'full' | string | undefined } export const AIAssistantHeader = ({ isChatLoading, shortcutsEnabled = true, onNewChat, onCloseAssistant, showMetadataWarning, updatedOptInSinceMCP, aiOptInLevel, }: AIAssistantHeaderProps) => { const { openChat } = useCreateChat() const snap = useAiAssistantStateSnapshot() const { isMaximised, toggleMaximise } = useSidebarManagerSnapshot() const isExplorerEnabled = useIsExplorerEnabled() const [value, setValue] = useState(snap.activeChat?.name) const [isEditingName, setIsEditingName] = useState(false) const [isOptInModalOpen, setIsOptInModalOpen] = useState(false) const maximiseLabel = isExplorerEnabled ? 'Open in Explorer' : isMaximised ? 'Minimize' : 'Maximize' const onSelectMaximise = () => { if (isExplorerEnabled) handleOpenInExplorer() else toggleMaximise() } const handleOpenInExplorer = () => { if (!snap.activeChatId) return openChat(snap.activeChatId) onCloseAssistant() } const handleCopyChatId = () => { copyToClipboard(snap.activeChatId ?? '', () => { toast.success(`Copied chat ID for ${snap.activeChat?.name}`) }) } const handleSaveName = () => { if (snap.activeChatId && value?.trim()) { snap.renameChat(snap.activeChatId, value.trim()) } setIsEditingName(false) } const handleKeyDownInput = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.preventDefault() e.stopPropagation() setIsEditingName(false) setValue(snap.activeChat?.name) } else if (e.key === 'Enter') { e.preventDefault() e.stopPropagation() handleSaveName() } } const handleBlurInput = () => { if (isEditingName) handleSaveName() } useShortcut(SHORTCUT_IDS.AI_ASSISTANT_COPY_CHAT_ID, handleCopyChatId, { enabled: shortcutsEnabled && !isChatLoading, }) useShortcut(SHORTCUT_IDS.AI_ASSISTANT_OPEN_PERMISSIONS, () => setIsOptInModalOpen(true), { enabled: shortcutsEnabled && !isChatLoading, }) useShortcut(SHORTCUT_IDS.AI_ASSISTANT_MAXIMIZE, onSelectMaximise, { enabled: shortcutsEnabled && !isChatLoading, }) return (
{isEditingName ? ( setValue(e.target.value)} onKeyDown={handleKeyDownInput} onBlur={handleBlurInput} /> ) : ( )}
) }