2 import { ICON_CLASS_DEFAULT } from '$lib/constants/css-classes';
14 } from '@lucide/svelte';
15 import { DropdownMenuActions } from '$lib/components/app';
16 import * as Tooltip from '$lib/components/ui/tooltip';
17 import { Checkbox } from '$lib/components/ui/checkbox';
18 import { FORK_TREE_DEPTH_PADDING } from '$lib/constants';
19 import { RouterService } from '$lib/services/router.service';
20 import { getAllLoadingChats } from '$lib/stores/chat.svelte';
21 import { conversationsStore } from '$lib/stores/conversations.svelte';
22 import { TruncatedText } from '$lib/components/app';
23 import { onMount } from 'svelte';
28 conversation: DatabaseConversation;
29 isSelectionMode?: boolean;
31 onDelete?: (id: string) => void;
32 onEdit?: (id: string) => void;
33 onSelect?: (id: string) => void;
34 onStop?: (id: string) => void;
35 onToggleSelect?: (id: string) => void;
36 onEnterSelectionMode?: (id: string) => void;
37 onSelectionClick?: (id: string, options: { shiftKey: boolean }) => void;
38 onRowMouseDown?: (id: string, event: MouseEvent) => void;
52 isSelectionMode = false,
57 let renderActionsDropdown = $state(false);
58 let dropdownOpen = $state(false);
60 let isLoading = $derived(getAllLoadingChats().includes(conversation.id));
62 function handleEdit(event: Event) {
63 event.stopPropagation();
64 onEdit?.(conversation.id);
67 function handleDelete(event: Event) {
68 event.stopPropagation();
69 onDelete?.(conversation.id);
72 function handleStop(event: Event) {
73 event.stopPropagation();
74 onStop?.(conversation.id);
77 function handleTogglePin() {
78 conversationsStore.toggleConversationPin(conversation.id);
81 function handleEnterSelectionMode(event: Event) {
82 event.stopPropagation();
83 onEnterSelectionMode?.(conversation.id);
86 function handleGlobalEditEvent(event: Event) {
87 const customEvent = event as CustomEvent<{ conversationId: string }>;
89 if (customEvent.detail.conversationId === conversation.id && isActive) {
94 function handleMouseLeave() {
96 renderActionsDropdown = false;
100 function handleMouseOver() {
101 if (isSelectionMode) return;
102 renderActionsDropdown = true;
105 function handleSelect(event: MouseEvent) {
106 if (isSelectionMode) {
107 onSelectionClick?.(conversation.id, { shiftKey: event.shiftKey });
109 onSelect?.(conversation.id);
113 function handleCheckboxClick(event: MouseEvent) {
114 event.stopPropagation();
115 if (isSelectionMode) {
116 onSelectionClick?.(conversation.id, { shiftKey: event.shiftKey });
118 onToggleSelect?.(conversation.id);
122 function handleRowMouseDown(event: MouseEvent) {
123 onRowMouseDown?.(conversation.id, event);
126 function handleCheckboxKeydown(event: KeyboardEvent) {
127 if (event.key !== ' ' && event.key !== 'Enter') return;
128 event.stopPropagation();
129 event.preventDefault();
130 if (isSelectionMode) {
131 onSelectionClick?.(conversation.id, { shiftKey: event.shiftKey });
133 onToggleSelect?.(conversation.id);
139 renderActionsDropdown = false;
144 document.addEventListener('edit-active-conversation', handleGlobalEditEvent as EventListener);
147 document.removeEventListener(
148 'edit-active-conversation',
149 handleGlobalEditEvent as EventListener
155 <!-- svelte-ignore a11y_mouse_events_have_key_events -->
157 class="group flex min-h-9 w-full cursor-pointer items-center justify-between space-x-3 rounded-lg py-1.5 text-left transition-colors hover:bg-foreground/10 {isActive
158 ? 'bg-foreground/5 text-accent-foreground'
159 : ''} {isSelected ? 'bg-primary/10 hover:bg-primary/15' : ''} {isSelectionMode
160 ? 'is-selection-mode'
162 data-conversation-row={conversation.id}
163 onclick={(e) => handleSelect(e)}
164 onmouseover={handleMouseOver}
165 onmouseleave={handleMouseLeave}
166 onmousedown={(e) => handleRowMouseDown(e)}
167 onfocusin={handleMouseOver}
169 if (!e.currentTarget.contains(e.relatedTarget as Node | null)) {
175 class="flex min-w-0 flex-1 items-center gap-2"
176 style:padding-left="{depth * FORK_TREE_DEPTH_PADDING}px"
178 {#if isSelectionMode}
181 onclick={(e) => handleCheckboxClick(e)}
182 onkeydown={handleCheckboxKeydown}
184 aria-checked={isSelected}
185 aria-label={isSelected ? `Deselect ${conversation.name}` : `Select ${conversation.name}`}
190 aria-label={isSelected ? `Deselect ${conversation.name}` : `Select ${conversation.name}`}
198 <!-- prevent another nested button element -->
199 {#snippet child({ props })}
202 href={RouterService.chat(conversation.forkedFromConversationId)}
203 class="flex shrink-0 items-center text-muted-foreground transition-colors hover:text-foreground"
205 <GitBranch class="h-3.5 w-3.5" />
211 <p>See parent conversation</p>
220 class="stop-button flex {ICON_CLASS_DEFAULT} shrink-0 cursor-pointer items-center justify-center rounded text-muted-foreground transition-colors hover:text-foreground"
222 onkeydown={(e) => e.key === 'Enter' && handleStop(e)}
225 aria-label="Stop generation"
227 <Loader2 class="loading-icon h-3.5 w-3.5 animate-spin" />
229 <Square class="stop-icon hidden h-3 w-3 fill-current text-destructive" />
234 <p>Stop generation</p>
239 <TruncatedText text={conversation.name} class="text-sm font-medium" showTooltip={false} />
242 {#if !isSelectionMode && renderActionsDropdown}
243 <div class="actions flex items-center">
245 triggerIcon={MoreHorizontal}
246 triggerTooltip="More actions"
247 bind:open={dropdownOpen}
250 icon: conversation.pinned ? PinOff : Pin,
251 label: conversation.pinned ? 'Unpin' : 'Pin',
252 onclick: (e: Event) => {
261 shortcut: ['shift', 'cmd', 'e']
266 onclick: (e: Event) => {
268 conversationsStore.downloadConversation(conversation.id);
270 shortcut: ['shift', 'cmd', 's']
275 onclick: handleEnterSelectionMode
280 onclick: handleDelete,
281 variant: 'destructive',
282 shortcut: ['shift', 'cmd', 'd'],
293 :global([data-slot='dropdown-menu-trigger']:not([data-state='open'])) {
297 &:is(:hover) :global([data-slot='dropdown-menu-trigger']),
298 &:focus-within :global([data-slot='dropdown-menu-trigger']) {
301 @media (max-width: 768px) {
302 :global([data-slot='dropdown-menu-trigger']) {
303 opacity: 1 !important;
307 &.is-selection-mode :global([data-slot='dropdown-menu-trigger']) {
308 display: none !important;
312 :global(.stop-icon) {
316 :global(.loading-icon) {
321 &:is(:hover) .stop-button {
322 :global(.stop-icon) {
326 :global(.loading-icon) {