'use client'; import DeleteChat from '@/components/DeleteChat'; import { formatTimeDifference } from '@/lib/utils'; import { ClockIcon, FileText, Globe2Icon, MessagesSquare, Search, Filter, } from 'lucide-react'; import Link from 'next/link'; import { useEffect, useMemo, useState } from 'react'; import { useTranslation } from '@/lib/localization/context'; export interface Chat { id: string; title: string; createdAt: string; sources: string[]; files: { fileId: string; name: string }[]; } const SOURCE_KEYS = ['web', 'academic', 'discussions'] as const; type SourceKey = (typeof SOURCE_KEYS)[number]; type FilesFilter = 'all' | 'with' | 'without'; const Page = () => { const { t } = useTranslation(); const [chats, setChats] = useState([]); const [loading, setLoading] = useState(true); const [searchQuery, setSearchQuery] = useState(''); const [sourceFilter, setSourceFilter] = useState('all'); const [filesFilter, setFilesFilter] = useState('all'); useEffect(() => { const fetchChats = async () => { setLoading(true); const token = typeof window !== 'undefined' ? localStorage.getItem('auth_token') ?? localStorage.getItem('access_token') : null; try { if (token) { const res = await fetch('/api/v1/library/threads', { headers: { Authorization: `Bearer ${token}` }, }); if (res.ok) { const data = await res.json(); setChats(data.chats ?? []); setLoading(false); return; } } const { getGuestChats } = await import('@/lib/guest-storage'); const guestChats = getGuestChats(); setChats( guestChats.map((c) => ({ id: c.id, title: c.title, createdAt: c.createdAt, sources: c.sources, files: c.files, })), ); } catch { setChats([]); } finally { setLoading(false); } }; fetchChats(); }, []); useEffect(() => { const onMigrated = () => { const token = typeof window !== 'undefined' ? localStorage.getItem('auth_token') ?? localStorage.getItem('access_token') : null; if (token) { fetch('/api/v1/library/threads', { headers: { Authorization: `Bearer ${token}` }, }) .then((r) => r.json()) .then((data) => setChats(data.chats ?? [])) .catch(() => setChats([])); } }; window.addEventListener('gooseek:guest-migrated', onMigrated); return () => window.removeEventListener('gooseek:guest-migrated', onMigrated); }, []); const filteredChats = useMemo(() => { return chats.filter((chat) => { const matchesSearch = !searchQuery.trim() || chat.title.toLowerCase().includes(searchQuery.trim().toLowerCase()); const matchesSource = sourceFilter === 'all' || chat.sources.some((s) => s === sourceFilter); const matchesFiles = filesFilter === 'all' || (filesFilter === 'with' && chat.files.length > 0) || (filesFilter === 'without' && chat.files.length === 0); return matchesSearch && matchesSource && matchesFiles; }); }, [chats, searchQuery, sourceFilter, filesFilter]); return (

{t('nav.messageHistory')}

Past chats, sources, and uploads.
{loading && chats.length === 0 ? '—' : `${chats.length} ${chats.length === 1 ? 'chat' : 'chats'}`}
{(chats.length > 0 || loading) && (
setSearchQuery(e.target.value)} placeholder={t('app.searchPlaceholder')} className="w-full pl-10 pr-4 py-2.5 text-sm bg-light-secondary dark:bg-dark-secondary border border-light-200 dark:border-dark-200 rounded-xl placeholder:text-black/40 dark:placeholder:text-white/40 text-black dark:text-white focus:outline-none focus:border-light-300 dark:focus:border-dark-300 transition-colors" />
)} {loading && chats.length === 0 ? (
{[1, 2, 3, 4].map((i) => (
))}
) : chats.length === 0 ? (

No chats found.

Start a new chat {' '} to see it listed here.

) : filteredChats.length === 0 ? (

{t('library.noResults')}

) : (
{filteredChats.map((chat, index) => { const sourcesLabel = chat.sources.length === 0 ? null : chat.sources.length <= 2 ? chat.sources .map((s) => s.charAt(0).toUpperCase() + s.slice(1)) .join(', ') : `${chat.sources .slice(0, 2) .map((s) => s.charAt(0).toUpperCase() + s.slice(1)) .join(', ')} + ${chat.sources.length - 2}`; return (
{chat.title}
{formatTimeDifference(new Date(), chat.createdAt)} Ago {sourcesLabel && ( {sourcesLabel} )} {chat.files.length > 0 && ( {chat.files.length}{' '} {chat.files.length === 1 ? 'file' : 'files'} )}
); })}
)}
); }; export default Page;