Spaces:
Sleeping
Sleeping
File size: 20,319 Bytes
bf8b26e 2f73768 bf8b26e 2f73768 bf8b26e 2f73768 bf8b26e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
import { motion, type Variants } from 'framer-motion';
import { useCallback, useEffect, useRef, useState } from 'react';
import { toast } from 'react-toastify';
import { Dialog, DialogButton, DialogDescription, DialogRoot, DialogTitle } from '~/components/ui/Dialog';
import { Button } from '~/components/ui/Button';
import { db, deleteById, getAll, chatId, type ChatHistoryItem, useChatHistory } from '~/lib/persistence';
import { cubicEasingFn } from '~/utils/easings';
import { HistoryItem } from './HistoryItem';
import { binDates } from './date-binning';
import { useSearchFilter } from '~/lib/hooks/useSearchFilter';
import { classNames } from '~/utils/classNames';
import { useStore } from '@nanostores/react';
import { profileStore } from '~/lib/stores/profile';
const menuVariants = {
closed: {
opacity: 0,
visibility: 'hidden',
left: '-340px',
transition: {
duration: 0.2,
ease: cubicEasingFn,
},
},
open: {
opacity: 1,
visibility: 'initial',
left: 0,
transition: {
duration: 0.2,
ease: cubicEasingFn,
},
},
} satisfies Variants;
type DialogContent =
| { type: 'delete'; item: ChatHistoryItem }
| { type: 'bulkDelete'; items: ChatHistoryItem[] }
| null;
function CurrentDateTime() {
const [dateTime, setDateTime] = useState(new Date());
useEffect(() => {
const timer = setInterval(() => {
setDateTime(new Date());
}, 60000);
return () => clearInterval(timer);
}, []);
return (
<div className="flex items-center gap-2 px-4 py-2 text-sm text-gray-600 dark:text-gray-400 border-b border-gray-100 dark:border-gray-800/50">
<div className="h-4 w-4 i-ph:clock opacity-80" />
<div className="flex gap-2">
<span>{dateTime.toLocaleDateString()}</span>
<span>{dateTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}</span>
</div>
</div>
);
}
export const Menu = () => {
const { duplicateCurrentChat, exportChat } = useChatHistory();
const menuRef = useRef<HTMLDivElement>(null);
const [list, setList] = useState<ChatHistoryItem[]>([]);
const [open, setOpen] = useState(false);
const [dialogContent, setDialogContent] = useState<DialogContent>(null);
const profile = useStore(profileStore);
const [selectionMode, setSelectionMode] = useState(false);
const [selectedItems, setSelectedItems] = useState<string[]>([]);
const { filteredItems: filteredList, handleSearchChange } = useSearchFilter({
items: list,
searchFields: ['description'],
});
const loadEntries = useCallback(() => {
if (db) {
getAll(db)
.then((list) => list.filter((item) => item.urlId && item.description))
.then(setList)
.catch((error) => toast.error(error.message));
}
}, []);
const deleteChat = useCallback(
async (id: string): Promise<void> => {
if (!db) {
throw new Error('Database not available');
}
// Delete chat snapshot from localStorage
try {
const snapshotKey = `snapshot:${id}`;
localStorage.removeItem(snapshotKey);
console.log('Removed snapshot for chat:', id);
} catch (snapshotError) {
console.error(`Error deleting snapshot for chat ${id}:`, snapshotError);
}
// Delete the chat from the database
await deleteById(db, id);
console.log('Successfully deleted chat:', id);
},
[db],
);
const deleteItem = useCallback(
(event: React.UIEvent, item: ChatHistoryItem) => {
event.preventDefault();
event.stopPropagation();
// Log the delete operation to help debugging
console.log('Attempting to delete chat:', { id: item.id, description: item.description });
deleteChat(item.id)
.then(() => {
toast.success('Chat deleted successfully', {
position: 'bottom-right',
autoClose: 3000,
});
// Always refresh the list
loadEntries();
if (chatId.get() === item.id) {
// hard page navigation to clear the stores
console.log('Navigating away from deleted chat');
window.location.pathname = '/';
}
})
.catch((error) => {
console.error('Failed to delete chat:', error);
toast.error('Failed to delete conversation', {
position: 'bottom-right',
autoClose: 3000,
});
// Still try to reload entries in case data has changed
loadEntries();
});
},
[loadEntries, deleteChat],
);
const deleteSelectedItems = useCallback(
async (itemsToDeleteIds: string[]) => {
if (!db || itemsToDeleteIds.length === 0) {
console.log('Bulk delete skipped: No DB or no items to delete.');
return;
}
console.log(`Starting bulk delete for ${itemsToDeleteIds.length} chats`, itemsToDeleteIds);
let deletedCount = 0;
const errors: string[] = [];
const currentChatId = chatId.get();
let shouldNavigate = false;
// Process deletions sequentially using the shared deleteChat logic
for (const id of itemsToDeleteIds) {
try {
await deleteChat(id);
deletedCount++;
if (id === currentChatId) {
shouldNavigate = true;
}
} catch (error) {
console.error(`Error deleting chat ${id}:`, error);
errors.push(id);
}
}
// Show appropriate toast message
if (errors.length === 0) {
toast.success(`${deletedCount} chat${deletedCount === 1 ? '' : 's'} deleted successfully`);
} else {
toast.warning(`Deleted ${deletedCount} of ${itemsToDeleteIds.length} chats. ${errors.length} failed.`, {
autoClose: 5000,
});
}
// Reload the list after all deletions
await loadEntries();
// Clear selection state
setSelectedItems([]);
setSelectionMode(false);
// Navigate if needed
if (shouldNavigate) {
console.log('Navigating away from deleted chat');
window.location.pathname = '/';
}
},
[deleteChat, loadEntries, db],
);
const closeDialog = () => {
setDialogContent(null);
};
const toggleSelectionMode = () => {
setSelectionMode(!selectionMode);
if (selectionMode) {
// If turning selection mode OFF, clear selection
setSelectedItems([]);
}
};
const toggleItemSelection = useCallback((id: string) => {
setSelectedItems((prev) => {
const newSelectedItems = prev.includes(id) ? prev.filter((itemId) => itemId !== id) : [...prev, id];
console.log('Selected items updated:', newSelectedItems);
return newSelectedItems; // Return the new array
});
}, []); // No dependencies needed
const handleBulkDeleteClick = useCallback(() => {
if (selectedItems.length === 0) {
toast.info('Select at least one chat to delete');
return;
}
const selectedChats = list.filter((item) => selectedItems.includes(item.id));
if (selectedChats.length === 0) {
toast.error('Could not find selected chats');
return;
}
setDialogContent({ type: 'bulkDelete', items: selectedChats });
}, [selectedItems, list]); // Keep list dependency
const selectAll = useCallback(() => {
const allFilteredIds = filteredList.map((item) => item.id);
setSelectedItems((prev) => {
const allFilteredAreSelected = allFilteredIds.length > 0 && allFilteredIds.every((id) => prev.includes(id));
if (allFilteredAreSelected) {
// Deselect only the filtered items
const newSelectedItems = prev.filter((id) => !allFilteredIds.includes(id));
console.log('Deselecting all filtered items. New selection:', newSelectedItems);
return newSelectedItems;
} else {
// Select all filtered items, adding them to any existing selections
const newSelectedItems = [...new Set([...prev, ...allFilteredIds])];
console.log('Selecting all filtered items. New selection:', newSelectedItems);
return newSelectedItems;
}
});
}, [filteredList]); // Depends only on filteredList
useEffect(() => {
if (open) {
loadEntries();
}
}, [open, loadEntries]);
// Exit selection mode when sidebar is closed
useEffect(() => {
if (!open && selectionMode) {
/*
* Don't clear selection state anymore when sidebar closes
* This allows the selection to persist when reopening the sidebar
*/
console.log('Sidebar closed, preserving selection state');
}
}, [open, selectionMode]);
useEffect(() => {
const enterThreshold = 40;
const exitThreshold = 40;
function onMouseMove(event: MouseEvent) {
if (event.pageX < enterThreshold) {
setOpen(true);
}
if (menuRef.current && event.clientX > menuRef.current.getBoundingClientRect().right + exitThreshold) {
setOpen(false);
}
}
window.addEventListener('mousemove', onMouseMove);
return () => {
window.removeEventListener('mousemove', onMouseMove);
};
}, []);
const handleDuplicate = async (id: string) => {
await duplicateCurrentChat(id);
loadEntries(); // Reload the list after duplication
};
const setDialogContentWithLogging = useCallback((content: DialogContent) => {
console.log('Setting dialog content:', content);
setDialogContent(content);
}, []);
return (
<>
<motion.div
ref={menuRef}
initial="closed"
animate={open ? 'open' : 'closed'}
variants={menuVariants}
style={{ width: '340px' }}
className={classNames(
'flex selection-accent flex-col side-menu fixed top-0 h-full',
'bg-white dark:bg-gray-950 border-r border-gray-100 dark:border-gray-800/50',
'shadow-sm text-sm z-sidebar',
)}
>
<div className="h-12 flex items-center justify-between px-4 border-b border-gray-100 dark:border-gray-800/50 bg-gray-50/50 dark:bg-gray-900/50">
<div className="text-gray-900 dark:text-white font-medium"></div>
<div className="flex items-center gap-3">
<span className="font-medium text-sm text-gray-900 dark:text-white truncate">
{profile?.username || 'Guest User'}
</span>
<div className="flex items-center justify-center w-[32px] h-[32px] overflow-hidden bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-500 rounded-full shrink-0">
{profile?.avatar ? (
<img
src={profile.avatar}
alt={profile?.username || 'User'}
className="w-full h-full object-cover"
loading="eager"
decoding="sync"
/>
) : (
<div className="i-ph:user-fill text-lg" />
)}
</div>
</div>
</div>
<CurrentDateTime />
<div className="flex-1 flex flex-col h-full w-full overflow-hidden">
<div className="p-4 space-y-3">
<div className="flex gap-2">
<a
href="/"
className="flex-1 flex gap-2 items-center bg-purple-50 dark:bg-purple-500/10 text-purple-700 dark:text-purple-300 hover:bg-purple-100 dark:hover:bg-purple-500/20 rounded-lg px-4 py-2 transition-colors"
>
<span className="inline-block i-ph:plus-circle h-4 w-4" />
<span className="text-sm font-medium">Start new chat</span>
</a>
<button
onClick={toggleSelectionMode}
className={classNames(
'flex gap-1 items-center rounded-lg px-3 py-2 transition-colors',
selectionMode
? 'bg-purple-600 dark:bg-purple-500 text-white border border-purple-700 dark:border-purple-600'
: 'bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 border border-gray-200 dark:border-gray-700',
)}
aria-label={selectionMode ? 'Exit selection mode' : 'Enter selection mode'}
>
<span className={selectionMode ? 'i-ph:x h-4 w-4' : 'i-ph:check-square h-4 w-4'} />
</button>
</div>
<div className="relative w-full">
<div className="absolute left-3 top-1/2 -translate-y-1/2">
<span className="i-ph:magnifying-glass h-4 w-4 text-gray-400 dark:text-gray-500" />
</div>
<input
className="w-full bg-gray-50 dark:bg-gray-900 relative pl-9 pr-3 py-2 rounded-lg focus:outline-none focus:ring-1 focus:ring-purple-500/50 text-sm text-gray-900 dark:text-gray-100 placeholder-gray-500 dark:placeholder-gray-500 border border-gray-200 dark:border-gray-800"
type="search"
placeholder="Search chats..."
onChange={handleSearchChange}
aria-label="Search chats"
/>
</div>
</div>
<div className="flex items-center justify-between text-sm px-4 py-2">
<div className="font-medium text-gray-600 dark:text-gray-400">Your Chats</div>
{selectionMode && (
<div className="flex items-center gap-2">
<Button variant="ghost" size="sm" onClick={selectAll}>
{selectedItems.length === filteredList.length ? 'Deselect all' : 'Select all'}
</Button>
<Button
variant="destructive"
size="sm"
onClick={handleBulkDeleteClick}
disabled={selectedItems.length === 0}
>
Delete selected
</Button>
</div>
)}
</div>
<div className="flex-1 overflow-auto px-3 pb-3">
{filteredList.length === 0 && (
<div className="px-4 text-gray-500 dark:text-gray-400 text-sm">
{list.length === 0 ? 'No previous conversations' : 'No matches found'}
</div>
)}
<DialogRoot open={dialogContent !== null}>
{binDates(filteredList).map(({ category, items }) => (
<div key={category} className="mt-2 first:mt-0 space-y-1">
<div className="text-xs font-medium text-gray-500 dark:text-gray-400 sticky top-0 z-1 bg-white dark:bg-gray-950 px-4 py-1">
{category}
</div>
<div className="space-y-0.5 pr-1">
{items.map((item) => (
<HistoryItem
key={item.id}
item={item}
exportChat={exportChat}
onDelete={(event) => {
event.preventDefault();
event.stopPropagation();
console.log('Delete triggered for item:', item);
setDialogContentWithLogging({ type: 'delete', item });
}}
onDuplicate={() => handleDuplicate(item.id)}
selectionMode={selectionMode}
isSelected={selectedItems.includes(item.id)}
onToggleSelection={toggleItemSelection}
/>
))}
</div>
</div>
))}
<Dialog onBackdrop={closeDialog} onClose={closeDialog}>
{dialogContent?.type === 'delete' && (
<>
<div className="p-6 bg-white dark:bg-gray-950">
<DialogTitle className="text-gray-900 dark:text-white">Delete Chat?</DialogTitle>
<DialogDescription className="mt-2 text-gray-600 dark:text-gray-400">
<p>
You are about to delete{' '}
<span className="font-medium text-gray-900 dark:text-white">
{dialogContent.item.description}
</span>
</p>
<p className="mt-2">Are you sure you want to delete this chat?</p>
</DialogDescription>
</div>
<div className="flex justify-end gap-3 px-6 py-4 bg-gray-50 dark:bg-gray-900 border-t border-gray-100 dark:border-gray-800">
<DialogButton type="secondary" onClick={closeDialog}>
Cancel
</DialogButton>
<DialogButton
type="danger"
onClick={(event) => {
console.log('Dialog delete button clicked for item:', dialogContent.item);
deleteItem(event, dialogContent.item);
closeDialog();
}}
>
Delete
</DialogButton>
</div>
</>
)}
{dialogContent?.type === 'bulkDelete' && (
<>
<div className="p-6 bg-white dark:bg-gray-950">
<DialogTitle className="text-gray-900 dark:text-white">Delete Selected Chats?</DialogTitle>
<DialogDescription className="mt-2 text-gray-600 dark:text-gray-400">
<p>
You are about to delete {dialogContent.items.length}{' '}
{dialogContent.items.length === 1 ? 'chat' : 'chats'}:
</p>
<div className="mt-2 max-h-32 overflow-auto border border-gray-100 dark:border-gray-800 rounded-md bg-gray-50 dark:bg-gray-900 p-2">
<ul className="list-disc pl-5 space-y-1">
{dialogContent.items.map((item) => (
<li key={item.id} className="text-sm">
<span className="font-medium text-gray-900 dark:text-white">{item.description}</span>
</li>
))}
</ul>
</div>
<p className="mt-3">Are you sure you want to delete these chats?</p>
</DialogDescription>
</div>
<div className="flex justify-end gap-3 px-6 py-4 bg-gray-50 dark:bg-gray-900 border-t border-gray-100 dark:border-gray-800">
<DialogButton type="secondary" onClick={closeDialog}>
Cancel
</DialogButton>
<DialogButton
type="danger"
onClick={() => {
/*
* Pass the current selectedItems to the delete function.
* This captures the state at the moment the user confirms.
*/
const itemsToDeleteNow = [...selectedItems];
console.log('Bulk delete confirmed for', itemsToDeleteNow.length, 'items', itemsToDeleteNow);
deleteSelectedItems(itemsToDeleteNow);
closeDialog();
}}
>
Delete
</DialogButton>
</div>
</>
)}
</Dialog>
</DialogRoot>
</div>
<div className="flex items-center justify-center border-t border-gray-200 dark:border-gray-800 px-4 py-3">
<a
href="/api"
className="flex items-center justify-center w-8 h-8 rounded-lg bg-purple-50 dark:bg-purple-500/10 text-purple-600 dark:text-purple-400 hover:bg-purple-100 dark:hover:bg-purple-500/20 transition-colors"
>
<span className="i-ph:brain h-5 w-5" />
</a>
</div>
</div>
</motion.div>
</>
);
};
|