13 } from '@lucide/svelte';
14 import { DropdownMenuActions } from '$lib/components/app';
15 import { TruncatedText } from '$lib/components/app';
16 import { Checkbox } from '$lib/components/ui/checkbox';
17 import * as Tooltip from '$lib/components/ui/tooltip';
18 import { FORK_TREE_DEPTH_PADDING, ICON_CLASS_DEFAULT } 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 { onMount } from 'svelte';
27 conversation: DatabaseConversation;
28 isSelectionMode?: boolean;
30 onDelete?: (id: string) => void;
31 onEdit?: (id: string) => void;
32 onSelect?: (id: string) => void;
33 onStop?: (id: string) => void;
34 onToggleSelect?: (id: string) => void;
35 onEnterSelectionMode?: (id: string) => void;
36 onSelectionClick?: (id: string, options: { shiftKey: boolean }) => void;
37 onRowMouseDown?: (id: string, event: MouseEvent) => void;
45 isSelectionMode = false,
56 let renderActionsDropdown = $state(false);
57 let dropdownOpen = $state(false);
59 let isLoading = $derived(getAllLoadingChats().includes(conversation.id));
61 function handleEdit(event: Event) {
62 event.stopPropagation();
63 onEdit?.(conversation.id);
66 function handleDelete(event: Event) {
67 event.stopPropagation();
68 onDelete?.(conversation.id);
71 function handleStop(event: Event) {
72 event.stopPropagation();
73 onStop?.(conversation.id);
76 function handleTogglePin() {
77 conversationsStore.toggleConversationPin(conversation.id);
80 function handleEnterSelectionMode(event: Event) {
81 event.stopPropagation();
82 onEnterSelectionMode?.(conversation.id);
85 function handleGlobalEditEvent(event: Event) {
86 const customEvent = event as CustomEvent<{ conversationId: string }>;
88 if (customEvent.detail.conversationId === conversation.id && isActive) {
93 function handleMouseLeave() {
95 renderActionsDropdown = false;
99 function handleMouseOver() {
100 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();
116 if (isSelectionMode) {
117 onSelectionClick?.(conversation.id, { shiftKey: event.shiftKey });
119 onToggleSelect?.(conversation.id);
123 function handleRowMouseDown(event: MouseEvent) {
124 onRowMouseDown?.(conversation.id, event);
127 function handleCheckboxKeydown(event: KeyboardEvent) {
128 if (event.key !== ' ' && event.key !== 'Enter') return;
130 event.stopPropagation();
131 event.preventDefault();
133 if (isSelectionMode) {
134 onSelectionClick?.(conversation.id, { shiftKey: event.shiftKey });
136 onToggleSelect?.(conversation.id);
142 renderActionsDropdown = false;
147 document.addEventListener('edit-active-conversation', handleGlobalEditEvent as EventListener);
150 document.removeEventListener(
151 'edit-active-conversation',
152 handleGlobalEditEvent as EventListener
158 <!-- svelte-ignore a11y_mouse_events_have_key_events -->
160 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
161 ? 'bg-foreground/5 text-accent-foreground'
162 : ''} {isSelected ? 'bg-primary/10 hover:bg-primary/15' : ''} {isSelectionMode
163 ? 'is-selection-mode'
165 data-conversation-row={conversation.id}
166 onclick={(e) => handleSelect(e)}
167 onmouseover={handleMouseOver}
168 onmouseleave={handleMouseLeave}
169 onmousedown={(e) => handleRowMouseDown(e)}
170 onfocusin={handleMouseOver}
172 if (!e.currentTarget.contains(e.relatedTarget as Node | null)) {
178 class="flex min-w-0 flex-1 items-center gap-2"
179 style:padding-left="{depth * FORK_TREE_DEPTH_PADDING}px"
181 {#if isSelectionMode}
184 onclick={(e) => handleCheckboxClick(e)}
185 onkeydown={handleCheckboxKeydown}
187 aria-checked={isSelected}
188 aria-label={isSelected ? `Deselect ${conversation.name}` : `Select ${conversation.name}`}
193 aria-label={isSelected ? `Deselect ${conversation.name}` : `Select ${conversation.name}`}
201 <!-- prevent another nested button element -->
202 {#snippet child({ props })}
205 href={RouterService.chat(conversation.forkedFromConversationId)}
206 class="flex shrink-0 items-center text-muted-foreground transition-colors hover:text-foreground"
208 <GitBranch class="h-3.5 w-3.5" />
214 <p>See parent conversation</p>
223 class="stop-button flex {ICON_CLASS_DEFAULT} shrink-0 cursor-pointer items-center justify-center rounded text-muted-foreground transition-colors hover:text-foreground"
225 onkeydown={(e) => e.key === 'Enter' && handleStop(e)}
228 aria-label="Stop generation"
230 <Loader2 class="loading-icon h-3.5 w-3.5 animate-spin" />
232 <Square class="stop-icon hidden h-3 w-3 fill-current text-destructive" />
237 <p>Stop generation</p>
242 <TruncatedText text={conversation.name} class="text-sm font-medium" showTooltip={false} />
245 {#if !isSelectionMode && renderActionsDropdown}
246 <div class="actions flex items-center">
248 triggerIcon={MoreHorizontal}
249 triggerTooltip="More actions"
250 bind:open={dropdownOpen}
253 icon: conversation.pinned ? PinOff : Pin,
254 label: conversation.pinned ? 'Unpin' : 'Pin',
255 onclick: (e: Event) => {
264 shortcut: ['shift', 'cmd', 'e']
269 onclick: (e: Event) => {
271 conversationsStore.downloadConversation(conversation.id);
273 shortcut: ['shift', 'cmd', 's']
278 onclick: handleEnterSelectionMode
283 onclick: handleDelete,
285 shortcut: ['shift', 'cmd', 'd'],
286 variant: 'destructive'
296 :global([data-slot='dropdown-menu-trigger']:not([data-state='open'])) {
300 &:is(:hover) :global([data-slot='dropdown-menu-trigger']),
301 &:focus-within :global([data-slot='dropdown-menu-trigger']) {
304 @media (max-width: 768px) {
305 :global([data-slot='dropdown-menu-trigger']) {
306 opacity: 1 !important;
310 &.is-selection-mode :global([data-slot='dropdown-menu-trigger']) {
311 display: none !important;
315 :global(.stop-icon) {
319 :global(.loading-icon) {
324 &:is(:hover) .stop-button {
325 :global(.stop-icon) {
329 :global(.loading-icon) {