import React, { useEffect, useState } from 'react'; import { Trash2, X } from 'lucide-react'; import type { AISession } from '../infrastructure/ai/types'; import { useI18n } from '../application/i18n/I18nProvider'; import { cn } from '../lib/utils'; import { ScrollArea } from './ui/scroll-area'; import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip'; import { SESSION_HISTORY_ROW_CLASSNAMES } from './ai/sessionHistoryLayout'; // ------------------------------------------------------------------- // Session History Drawer // ------------------------------------------------------------------- interface SessionHistoryDrawerProps { sessions: AISession[]; activeSessionId: string | null; onSelect: (sessionId: string) => void; onDelete: (e: React.MouseEvent, sessionId: string) => void; onClose: () => void; } const SESSION_RENDER_BATCH = 80; const SESSION_RENDER_STEP = 60; export const SessionHistoryDrawer: React.FC = ({ sessions, activeSessionId, onSelect, onDelete, onClose, }) => { const { t } = useI18n(); const [renderCount, setRenderCount] = useState(SESSION_RENDER_BATCH); useEffect(() => { setRenderCount(SESSION_RENDER_BATCH); }, [sessions]); const displayedSessions = sessions.slice(0, renderCount); const hiddenSessionCount = Math.max(0, sessions.length - renderCount); return (
{t('ai.chat.allSessions')}
{sessions.length === 0 ? (

{t('ai.chat.noSessions')}

) : ( <> {hiddenSessionCount > 0 && ( )} {displayedSessions.map((session) => { const isActive = session.id === activeSessionId; const time = new Date(session.updatedAt); const timeStr = formatRelativeTime(time, t); return (
onSelect(session.id)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') onSelect(session.id); }} className={cn( SESSION_HISTORY_ROW_CLASSNAMES.row, isActive ? 'text-foreground' : 'text-foreground/70 hover:text-foreground', )} > {session.title || t('ai.chat.untitled')}
{timeStr} {t('common.delete')}
); })} )}
); }; // ------------------------------------------------------------------- // Helpers // ------------------------------------------------------------------- export function formatRelativeTime(date: Date, t: (key: string) => string): string { const now = Date.now(); const diff = now - date.getTime(); const minutes = Math.floor(diff / 60_000); const hours = Math.floor(diff / 3_600_000); const days = Math.floor(diff / 86_400_000); if (minutes < 1) return t('ai.chat.justNow'); if (minutes < 60) return t('ai.chat.minutesAgo').replace('{n}', String(minutes)); if (hours < 24) return t('ai.chat.hoursAgo').replace('{n}', String(hours)); if (days < 7) return t('ai.chat.daysAgo').replace('{n}', String(days)); return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }); }