Files
supabase/apps/studio/components/ui/AIAssistantPanel/AIAssistantChatSelector.tsx
Joshen Lim c97bc6282d Adjust AIAssistantHeader (#47912)
## Context

As per PR title, just adjusting the Assistant's header a little to
improve the UX

### Before
<img width="442" height="71" alt="image"
src="https://github.com/user-attachments/assets/c714e264-7724-451a-aeaf-7ced456d0639"
/>

### After
<img width="438" height="77" alt="image"
src="https://github.com/user-attachments/assets/11467bf2-7306-4ec9-80e0-2aadd959eff1"
/>

## Changes involved
- Shift permission settings into "More" dropdown
- Chat selection is now "history"
- Added keyboard shortcuts for history and copy chat ID  
<img width="185" height="95" alt="image"
src="https://github.com/user-attachments/assets/fc1c9bdc-9180-48ba-940f-2f39fef53a1d"
/>
<img width="287" height="159" alt="image"
src="https://github.com/user-attachments/assets/8f597306-9ea0-4282-884b-722bab16e4d0"
/>
- Chat name is now clickable to directly edit it
  - Saves on Enter or on blur
  - Resets on Esc
<img width="433" height="70" alt="image"
src="https://github.com/user-attachments/assets/f0c6fb4a-c368-4722-97a2-22ae94cc5511"
/>
<img width="439" height="64" alt="image"
src="https://github.com/user-attachments/assets/1eb21aaf-3979-433a-acc2-378b47b79e94"
/>


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added keyboard shortcuts to toggle the AI Assistant chat history and
copy the active chat ID.
* Added shortcut hint pills to AI Assistant tooltips and the “More
options” menu.
* Enabled inline editing of the active chat name with save/cancel and
blur support.
* **Improvements**
* Refreshed AI Assistant header actions and icons (including “New chat”
and menu controls) for clearer navigation.
  * Updated onboarding header styling with an assistant icon/animation.
  * Standardized shortcut rendering in tooltip pill formatting.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-15 13:35:32 +08:00

221 lines
7.9 KiB
TypeScript

import { Check, Edit, History, Plus, Trash, X } from 'lucide-react'
import { useState } from 'react'
import {
Button,
cn,
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
Input,
Popover,
PopoverContent,
PopoverTrigger,
ScrollArea,
} from 'ui'
import { ShortcutTooltip } from '../ShortcutTooltip'
import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
import { useShortcut } from '@/state/shortcuts/useShortcut'
interface AIAssistantChatSelectorProps {
disabled?: boolean
}
export const AIAssistantChatSelector = ({ disabled = false }: AIAssistantChatSelectorProps) => {
const snap = useAiAssistantStateSnapshot()
const [chatSelectorOpen, setChatSelectorOpen] = useState(false)
const [editingChatId, setEditingChatId] = useState<string | null>(null)
const [editingChatName, setEditingChatName] = useState('')
const chats = Object.entries(snap.chats)
useShortcut(SHORTCUT_IDS.AI_ASSISTANT_TOGGLE_HISTORY, () => setChatSelectorOpen((prev) => !prev))
const handleSelectChat = (id: string) => {
snap.selectChat(id)
setChatSelectorOpen(false)
}
const handleDeleteChat = (id: string, e?: React.MouseEvent) => {
if (e) e.stopPropagation()
snap.deleteChat(id)
}
const handleStartEditChat = (id: string, name: string, e?: React.MouseEvent) => {
if (e) e.stopPropagation()
setEditingChatId(id)
setEditingChatName(name)
}
const handleSaveEditChat = (e?: React.MouseEvent) => {
if (e) e.stopPropagation()
if (editingChatId && editingChatName.trim()) {
snap.renameChat(editingChatId, editingChatName.trim())
setEditingChatId(null)
setEditingChatName('')
}
}
const handleCancelEditChat = (e?: React.MouseEvent | React.FocusEvent) => {
if (e) e.stopPropagation()
setEditingChatId(null)
setEditingChatName('')
}
const handleInputBlur = (e: React.FocusEvent) => {
e.stopPropagation()
const relatedTarget = e.relatedTarget as HTMLElement | null
const isSaveOrCancelButton = relatedTarget?.closest('button')
if (!isSaveOrCancelButton && editingChatId && editingChatName.trim()) {
handleSaveEditChat()
} else if (!isSaveOrCancelButton) {
handleCancelEditChat(e)
}
}
return (
<Popover open={chatSelectorOpen} onOpenChange={setChatSelectorOpen}>
<ShortcutTooltip
side="bottom"
label="History"
shortcutId={SHORTCUT_IDS.AI_ASSISTANT_TOGGLE_HISTORY}
>
<PopoverTrigger asChild>
<Button
aria-label="History"
variant="text"
size="tiny"
className="h-7 w-7 p-0"
icon={<History />}
/>
</PopoverTrigger>
</ShortcutTooltip>
<PopoverContent className="w-[250px] p-0" align="end">
<Command>
<CommandInput className="text-xs" placeholder="Search chats..." />
<CommandList>
<CommandEmpty>No chats found.</CommandEmpty>
<CommandGroup>
<ScrollArea className={chats.length > 4 ? 'h-40' : ''}>
{/* @ts-ignore */}
{chats.map(([id, chat]) => (
<CommandItem
key={id}
value={id}
onSelect={() => handleSelectChat(id)}
className="flex items-center justify-between gap-2 py-1 w-full overflow-hidden group"
keywords={!!chat.name ? [chat.name] : undefined}
disabled={disabled}
>
<div className="flex items-center w-full flex-1 min-w-0">
{editingChatId === id ? (
<div className="flex items-center gap-2 w-full">
<Input
value={editingChatName}
onChange={(e) => setEditingChatName(e.target.value)}
autoFocus
size="tiny"
className="flex-1 w-full"
onClick={(e) => e.stopPropagation()}
onBlur={handleInputBlur}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault()
e.stopPropagation()
handleSaveEditChat()
} else if (e.key === 'Escape') {
e.preventDefault()
e.stopPropagation()
handleCancelEditChat()
}
}}
/>
<div className="flex items-center gap-0">
<Button
aria-label="Save chat name"
variant="text"
size="tiny"
icon={<Check size={14} />}
onClick={(e) => handleSaveEditChat(e)}
className="h-7 w-7"
/>
<Button
aria-label="Cancel edit chat"
variant="text"
size="tiny"
icon={<X size={14} />}
onClick={(e) => handleCancelEditChat(e)}
className="h-7 w-7"
/>
</div>
</div>
) : (
<>
<Check
className={cn(
'mr-2 h-4 w-4 shrink-0',
snap.activeChatId === id ? 'opacity-100' : 'opacity-0'
)}
/>
<span className="truncate flex-1 w-0">{chat.name}</span>
</>
)}
</div>
{editingChatId !== id && (
<div className="flex items-center gap-x-0 shrink-0 opacity-0 group-hover:opacity-100 transition-opacity">
<Button
aria-label="Edit chat name"
variant="text"
size="tiny"
icon={<Edit size={14} />}
onClick={(e) => handleStartEditChat(id, chat.name, e)}
className="h-6 w-6"
/>
{chats.length > 1 && (
<Button
aria-label="Delete chat"
variant="text"
size="tiny"
icon={<Trash size={14} />}
onClick={(e) => handleDeleteChat(id, e)}
className="h-6 w-6"
/>
)}
</div>
)}
</CommandItem>
))}
</ScrollArea>
</CommandGroup>
<CommandSeparator />
<CommandGroup>
<CommandItem
className="cursor-pointer w-full gap-x-2"
onSelect={() => {
snap.newChat()
setChatSelectorOpen(false)
}}
onClick={() => {
snap.newChat()
setChatSelectorOpen(false)
}}
disabled={disabled}
>
<Plus size={14} strokeWidth={1.5} />
<span>Start a new chat</span>
</CommandItem>
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)
}