Files
Netcatty/components/AIChatSessionHistoryDrawer.tsx
T
陈大猫 abd2557356 fix: dev CJS imports, ligatures empty-line cache crash, and AI sidebar UX (#4310)
* fix: resolve dev CJS imports, ligatures empty-line crash, and streamline AI sidebar UX

* fix(ai): ignore nested control keydown events in session manager row
2026-11-23 15:00:06 +08:00

362 lines
15 KiB
TypeScript

import React, { useEffect, useId, useRef, useState } from 'react';
import { Download, MoreHorizontal, Pencil, Search, Trash2, Upload, X } from 'lucide-react';
import type { AISession } from '../infrastructure/ai/types';
import { useI18n } from '../application/i18n/I18nProvider';
import { cn } from '../lib/utils';
import { MAX_SEARCH_QUERY_LENGTH, truncateQueryForMatch } from '../domain/searchMatcher';
import { Dropdown, DropdownContent, DropdownTrigger } from './ui/dropdown';
import { Input } from './ui/input';
import { ScrollArea } from './ui/scroll-area';
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
import { SESSION_HISTORY_ROW_CLASSNAMES } from './ai/sessionHistoryLayout';
import { useSessionHistorySearch } from '../application/state/useSessionHistorySearch';
// -------------------------------------------------------------------
// Session History Drawer
// -------------------------------------------------------------------
interface SessionHistoryDrawerProps {
sessions: AISession[];
activeSessionId: string | null;
onSelect: (sessionId: string) => void;
onDelete: (e: React.MouseEvent, sessionId: string) => void;
/** Rename a chat inline (issue #4015 R5). Empty titles are ignored. */
onRename?: (sessionId: string, title: string) => void;
/** Manual durability escape hatch: export the whole store to a JSON file. */
onExportSessions?: () => void;
/** Import sessions from a JSON export; merges by id, newer wins. */
onImportSessions?: () => void;
/**
* Global AI session manager slot (issue #4098 phases 4-5). When provided,
* the drawer offers a scoped ⇄ global toggle and renders this node in
* global mode. The parent owns the node so all actions stay panel-scoped.
*/
globalManager?: React.ReactNode;
onClose: () => void;
}
const SESSION_RENDER_BATCH = 80;
const SESSION_RENDER_STEP = 60;
export const SessionHistoryDrawer: React.FC<SessionHistoryDrawerProps> = ({
sessions,
activeSessionId,
onSelect,
onDelete,
onRename,
onExportSessions,
onImportSessions,
globalManager,
onClose,
}) => {
const { t } = useI18n();
const [view, setView] = useState<'scoped' | 'global'>('scoped');
const [headerMenuOpen, setHeaderMenuOpen] = useState(false);
const [renderCount, setRenderCount] = useState(SESSION_RENDER_BATCH);
const [searchQuery, setSearchQuery] = useState('');
const [renamingSessionId, setRenamingSessionId] = useState<string | null>(null);
const [renameDraft, setRenameDraft] = useState('');
const renameInputRef = useRef<HTMLInputElement>(null);
const searchHintId = useId();
const searchInputRef = useRef<HTMLInputElement>(null);
const { filteredSessions, isSearching } = useSessionHistorySearch(
sessions,
searchQuery,
t('ai.chat.untitled'),
);
// Reset the render batch when the list scope or query changes so matching
// sessions are never hidden behind stale paging.
useEffect(() => {
setRenderCount(SESSION_RENDER_BATCH);
}, [sessions, searchQuery]);
const displayedSessions = filteredSessions.slice(0, renderCount);
const hiddenSessionCount = Math.max(0, filteredSessions.length - renderCount);
useEffect(() => {
if (renamingSessionId) renameInputRef.current?.focus();
}, [renamingSessionId]);
const beginRename = (sessionId: string, currentTitle: string) => {
setRenamingSessionId(sessionId);
setRenameDraft(currentTitle);
};
const commitRename = () => {
if (renamingSessionId && renameDraft.trim()) {
onRename?.(renamingSessionId, renameDraft.trim());
}
setRenamingSessionId(null);
setRenameDraft('');
};
return (
<div className="flex-1 flex flex-col min-h-0">
<div className="px-4 py-2.5 flex items-center justify-between shrink-0 border-b border-border/30">
<div className="flex items-center gap-2 min-w-0">
{globalManager ? (
<div className="flex items-center rounded-md border border-border/40 p-0.5 shrink-0" role="tablist">
<button
type="button"
role="tab"
aria-selected={view === 'scoped'}
onClick={() => setView('scoped')}
className={cn(
'px-1.5 py-0.5 rounded text-[11px] transition-colors cursor-pointer',
view === 'scoped'
? 'bg-muted text-foreground'
: 'text-muted-foreground/60 hover:text-muted-foreground',
)}
>
{t('ai.chat.managerScopeTab')}
</button>
<button
type="button"
role="tab"
aria-selected={view === 'global'}
onClick={() => setView('global')}
className={cn(
'px-1.5 py-0.5 rounded text-[11px] transition-colors cursor-pointer',
view === 'global'
? 'bg-muted text-foreground'
: 'text-muted-foreground/60 hover:text-muted-foreground',
)}
>
{t('ai.chat.managerAllChatsTab')}
</button>
</div>
) : (
<span className="text-[13px] font-medium text-foreground/80 truncate">
{t('ai.chat.allSessions')}
</span>
)}
</div>
<div className="flex items-center gap-2">
{(onImportSessions || onExportSessions) && (
<Dropdown open={headerMenuOpen} onOpenChange={setHeaderMenuOpen}>
<DropdownTrigger asChild>
<button
type="button"
className="text-[12px] text-muted-foreground/60 hover:text-muted-foreground transition-colors cursor-pointer"
aria-label={t('ai.chat.managerMoreActions')}
>
<MoreHorizontal size={14} />
</button>
</DropdownTrigger>
<DropdownContent align="end" className="min-w-[152px] p-1" keyboardNavigation>
{onImportSessions && (
<button
type="button"
onClick={(e) => {
e.stopPropagation();
setHeaderMenuOpen(false);
onImportSessions();
}}
className="flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-[12px] text-foreground/85 hover:bg-muted cursor-pointer"
>
<Upload size={12} className="shrink-0 text-muted-foreground" />
<span>{t('ai.chat.importSessions')}</span>
</button>
)}
{onExportSessions && (
<button
type="button"
onClick={(e) => {
e.stopPropagation();
setHeaderMenuOpen(false);
onExportSessions();
}}
className="flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-[12px] text-foreground/85 hover:bg-muted cursor-pointer"
>
<Download size={12} className="shrink-0 text-muted-foreground" />
<span>{t('ai.chat.exportSessions')}</span>
</button>
)}
</DropdownContent>
</Dropdown>
)}
<button
onClick={onClose}
className="text-[12px] text-muted-foreground/60 hover:text-muted-foreground transition-colors cursor-pointer"
>
<X size={14} />
</button>
</div>
</div>
{view === 'global' && globalManager ? (
globalManager
) : (
<>
<div className="px-3 py-2 shrink-0 border-b border-border/30">
<div className="relative">
<Search
size={13}
className="absolute left-2.5 top-1/2 -translate-y-1/2 text-muted-foreground/50 pointer-events-none"
/>
<Input
ref={searchInputRef}
type="text"
value={searchQuery}
maxLength={MAX_SEARCH_QUERY_LENGTH}
placeholder={t('ai.chat.searchSessions')}
aria-label={t('ai.chat.searchSessions')}
aria-describedby={searchHintId}
onChange={(event) => setSearchQuery(truncateQueryForMatch(event.target.value))}
className="h-8 pl-8 pr-7 text-[12px]"
/>
{searchQuery && (
<button
type="button"
aria-label={t('ai.chat.clearSessionSearch')}
onClick={() => {
setSearchQuery('');
searchInputRef.current?.focus();
}}
className="absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground/50 hover:text-muted-foreground transition-colors cursor-pointer"
>
<X size={12} />
</button>
)}
</div>
<p id={searchHintId} className="mt-1.5 text-[11px] text-muted-foreground/60">
{t('ai.chat.searchSessionsHint')}
</p>
</div>
<ScrollArea className="flex-1" aria-busy={isSearching}>
{isSearching && (
<p role="status" className="px-4 py-2 text-[12px] text-muted-foreground/60">
{t('ai.chat.searchingSessions')}
</p>
)}
<div className="px-3">
{sessions.length === 0 ? (
<div className="py-12 text-center">
<p className="text-[13px] text-muted-foreground/40">
{t('ai.chat.noSessions')}
</p>
</div>
) : filteredSessions.length === 0 && !isSearching ? (
<div className="py-12 text-center">
<p className="text-[13px] text-muted-foreground/40">
{t('ai.chat.noMatchingSessions')}
</p>
</div>
) : (
<>
{hiddenSessionCount > 0 && (
<button
type="button"
onClick={() => setRenderCount((count) => count + SESSION_RENDER_STEP)}
className="w-full py-2 text-center text-[12px] text-muted-foreground/50 hover:text-muted-foreground transition-colors cursor-pointer"
>
{t('ai.chat.loadMoreSessions').replace('{n}', String(hiddenSessionCount))}
</button>
)}
{displayedSessions.map((session) => {
const isActive = session.id === activeSessionId;
const time = new Date(session.updatedAt);
const timeStr = formatRelativeTime(time, t);
return (
<div
key={session.id}
role="button"
tabIndex={0}
onClick={() => 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',
)}
>
<span className={SESSION_HISTORY_ROW_CLASSNAMES.title}>
{renamingSessionId === session.id ? (
<Input
ref={renameInputRef}
type="text"
value={renameDraft}
aria-label={t('ai.chat.renameChat')}
onClick={(e) => e.stopPropagation()}
onKeyDown={(e) => {
e.stopPropagation();
if (e.key === 'Enter') {
e.preventDefault();
commitRename();
} else if (e.key === 'Escape') {
e.preventDefault();
setRenamingSessionId(null);
setRenameDraft('');
}
}}
onChange={(e) => setRenameDraft(e.target.value)}
onBlur={commitRename}
className="h-6 text-[12px]"
/>
) : (
session.title || t('ai.chat.untitled')
)}
</span>
<div className={SESSION_HISTORY_ROW_CLASSNAMES.meta}>
<span className={SESSION_HISTORY_ROW_CLASSNAMES.time}>
{timeStr}
</span>
{onRename && renamingSessionId !== session.id && (
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={(e) => {
e.stopPropagation();
beginRename(session.id, session.title || '');
}}
className={SESSION_HISTORY_ROW_CLASSNAMES.deleteButton}
>
<Pencil size={12} />
</button>
</TooltipTrigger>
<TooltipContent>{t('ai.chat.renameChat')}</TooltipContent>
</Tooltip>
)}
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={(e) => onDelete(e, session.id)}
className={SESSION_HISTORY_ROW_CLASSNAMES.deleteButton}
>
<Trash2 size={12} />
</button>
</TooltipTrigger>
<TooltipContent>{t('common.delete')}</TooltipContent>
</Tooltip>
</div>
</div>
);
})}
</>
)}
</div>
</ScrollArea>
</>
)}
</div>
);
};
// -------------------------------------------------------------------
// 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' });
}