diff --git a/web/src/components/SessionList.mark-all-read.test.tsx b/web/src/components/SessionList.mark-all-read.test.tsx new file mode 100644 index 00000000..bbdbea00 --- /dev/null +++ b/web/src/components/SessionList.mark-all-read.test.tsx @@ -0,0 +1,224 @@ +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react' +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { ReactNode } from 'react' +import type { SessionSummary } from '@/types/api' +import { I18nProvider } from '@/lib/i18n-context' +import { ToastProvider } from '@/lib/toast-context' +import { SessionList } from './SessionList' + +afterEach(() => cleanup()) + +function makeSession(overrides: Partial & { id: string }): SessionSummary { + return { + active: false, + thinking: false, + activeAt: 0, + updatedAt: 0, + metadata: null, + metadataVersion: 0, + agentStateVersion: 0, + todosUpdatedAt: 0, + todoProgress: null, + pendingRequestsCount: 0, + pendingRequestKinds: [], + pendingRequests: [], + backgroundTaskCount: 0, + futureScheduledMessageCount: 0, + nextScheduledAt: null, + model: null, + effort: null, + ...overrides + } +} + +function renderWithProviders(children: ReactNode) { + return render( + + + {children} + + + ) +} + +function renderSessionList(sessions: SessionSummary[]) { + return renderWithProviders( + + ) +} + +describe('SessionList mark all as read', () => { + beforeEach(() => { + localStorage.clear() + }) + + it('only shows the independent action when unread sessions exist', () => { + localStorage.setItem('hapi.sessionLastSeen.v1', JSON.stringify({ read: 1000 })) + renderSessionList([ + makeSession({ id: 'read', updatedAt: 1000, metadata: { path: '/work/read', name: 'Read' } }) + ]) + + expect(screen.queryByRole('button', { name: 'Mark all as read (1)' })).toBeNull() + + cleanup() + localStorage.setItem('hapi.sessionLastSeen.v1', JSON.stringify({ unread: 1000 })) + renderSessionList([ + makeSession({ id: 'unread', updatedAt: 2000, metadata: { path: '/work/unread', name: 'Unread' } }) + ]) + + const action = screen.getByRole('button', { name: 'Mark all as read (1)' }) + expect(action).toBeTruthy() + expect(action).toHaveClass('text-[var(--app-hint)]', 'hover:text-[var(--app-fg)]') + expect(action.textContent).toBe('') + expect(action.querySelector('svg')).toBeTruthy() + }) + + it('requires confirmation before marking every unread session read', async () => { + localStorage.setItem('hapi.sessionLastSeen.v1', JSON.stringify({ + unreadA: 1000, + unreadB: 2000, + read: 3000 + })) + renderSessionList([ + makeSession({ id: 'unreadA', updatedAt: 1100, metadata: { path: '/work/a', name: 'Unread A' } }), + makeSession({ id: 'unreadB', updatedAt: 2200, metadata: { path: '/work/b', name: 'Unread B' } }), + makeSession({ id: 'read', updatedAt: 3000, metadata: { path: '/work/read', name: 'Read' } }) + ]) + + fireEvent.click(screen.getByRole('button', { name: 'Mark all as read (2)' })) + const dialog = screen.getByRole('dialog') + const title = within(dialog).getByRole('heading', { name: 'Mark all as read?' }) + expect(title.parentElement).toHaveClass('pr-0') + expect(title).toHaveClass('min-h-6', 'px-10', 'text-center', 'leading-6') + expect(screen.getByText('This will mark 2 unread sessions as read on this device.')).toBeTruthy() + expect(within(dialog).getByRole('button', { name: 'Confirm' })).toHaveClass('bg-red-600', 'text-white') + expect(JSON.parse(localStorage.getItem('hapi.sessionLastSeen.v1')!)).toMatchObject({ unreadA: 1000, unreadB: 2000 }) + + fireEvent.click(screen.getByRole('button', { name: 'Confirm' })) + + await waitFor(() => { + expect(screen.queryByRole('button', { name: 'Mark all as read (2)' })).toBeNull() + }) + expect(JSON.parse(localStorage.getItem('hapi.sessionLastSeen.v1')!)).toMatchObject({ + unreadA: 1100, + unreadB: 2200, + read: 3000 + }) + expect(screen.queryByRole('dialog')).toBeNull() + }) + + it('leaves unread state unchanged when the confirmation is cancelled', () => { + localStorage.setItem('hapi.sessionLastSeen.v1', JSON.stringify({ unread: 1000 })) + renderSessionList([ + makeSession({ id: 'unread', updatedAt: 2000, metadata: { path: '/work/unread', name: 'Unread' } }) + ]) + + fireEvent.click(screen.getByRole('button', { name: 'Mark all as read (1)' })) + fireEvent.click(screen.getByRole('button', { name: 'Cancel' })) + + expect(screen.getByRole('button', { name: 'Mark all as read (1)' })).toBeTruthy() + expect(JSON.parse(localStorage.getItem('hapi.sessionLastSeen.v1')!)).toEqual({ unread: 1000 }) + }) + + it('marks hidden duplicate session IDs so a dedup winner change stays read', async () => { + localStorage.setItem('hapi.sessionLastSeen.v1', JSON.stringify({ + visibleWinner: 2000, + hiddenDuplicate: 0, + })) + const queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + mutations: { retry: false }, + } + }) + const renderTree = (sessions: SessionSummary[]) => ( + + + + + + + + ) + const initialSessions = [ + makeSession({ + id: 'visibleWinner', + updatedAt: 2000, + metadata: { path: '/work/shared', name: 'Visible winner', agentSessionId: 'native-1', flavor: 'codex' } + }), + makeSession({ + id: 'hiddenDuplicate', + updatedAt: 1000, + metadata: { path: '/work/shared', name: 'Hidden duplicate', agentSessionId: 'native-1', flavor: 'codex' } + }), + ] + const view = render(renderTree(initialSessions)) + + fireEvent.click(screen.getByRole('button', { name: 'Mark all as read (1)' })) + fireEvent.click(screen.getByRole('button', { name: 'Confirm' })) + await waitFor(() => { + expect(screen.queryByRole('button', { name: 'Mark all as read (1)' })).toBeNull() + }) + + view.rerender(renderTree(initialSessions.map(session => ( + session.id === 'hiddenDuplicate' ? { ...session, active: true } : session + )))) + + expect(screen.queryByRole('button', { name: 'Mark all as read (1)' })).toBeNull() + expect(JSON.parse(localStorage.getItem('hapi.sessionLastSeen.v1')!)).toMatchObject({ + visibleWinner: 2000, + hiddenDuplicate: 1000, + }) + }) + + it('does not count unread inactive empty session stubs', () => { + localStorage.setItem('hapi.sessionLastSeen.v1', JSON.stringify({ + visible: 2000, + emptyStub: 0, + })) + renderSessionList([ + makeSession({ + id: 'visible', + updatedAt: 2000, + metadata: { path: '/work/visible', name: 'Visible' } + }), + makeSession({ id: 'emptyStub', updatedAt: 3000 }) + ]) + + expect(screen.queryByRole('button', { name: 'Mark all as read (1)' })).toBeNull() + }) + + it('uses the requested Chinese label', () => { + localStorage.setItem('hapi-lang', 'zh-CN') + localStorage.setItem('hapi.sessionLastSeen.v1', JSON.stringify({ unread: 1000 })) + renderSessionList([ + makeSession({ id: 'unread', updatedAt: 2000, metadata: { path: '/work/unread', name: 'Unread' } }) + ]) + + expect(screen.getByRole('button', { name: '全部标为已读(1)' })).toBeTruthy() + }) +}) diff --git a/web/src/components/SessionList.tsx b/web/src/components/SessionList.tsx index d54ed2f6..be03b779 100644 --- a/web/src/components/SessionList.tsx +++ b/web/src/components/SessionList.tsx @@ -10,7 +10,7 @@ import { SessionActionMenu } from '@/components/SessionActionMenu' import { SessionExportDialog } from '@/components/SessionExportDialog' import { RenameSessionDialog } from '@/components/RenameSessionDialog' import { ConfirmDialog } from '@/components/ui/ConfirmDialog' -import { CopyIcon, CheckIcon } from '@/components/icons' +import { CopyIcon, CheckIcon, MarkAllReadIcon } from '@/components/icons' function PinnedSectionIcon(props: { className?: string }) { return ( @@ -32,6 +32,8 @@ import { getSessionLastSeenAt, getSessionLastSeenSnapshot, getSessionManualUnreadAt, + getUnreadSessionCount, + markAllSessionsSeen, markSessionUnread, useSessionLastSeenVersion } from '@/lib/sessionLastSeen' @@ -1209,6 +1211,7 @@ export function SessionList(props: { const [searchExpanded, setSearchExpanded] = useState(false) const [customStart, setCustomStart] = useState('') const [customEnd, setCustomEnd] = useState('') + const [markAllReadOpen, setMarkAllReadOpen] = useState(false) const [, setCodexImportedSessionsVersion] = useState(0) const normalizedQuery = normalizeSearch(searchQuery) const timeRange = getSessionTimeRange(customStart, customEnd) @@ -1231,14 +1234,23 @@ export function SessionList(props: { return t('machine.unknown') } + const sidebarSessions = useMemo( + () => prepareSidebarSessions(props.sessions, selectedSessionId), + [props.sessions, selectedSessionId] + ) + const readableSessions = useMemo( + () => props.sessions.filter(session => shouldShowSessionInSidebar(session, selectedSessionId)), + [props.sessions, selectedSessionId] + ) const allSessions = useMemo( - () => { - const prepared = prepareSidebarSessions(props.sessions, selectedSessionId) - return showActiveSessionsOnly - ? filterActiveSessionsOnly(prepared, selectedSessionId) - : prepared - }, - [props.sessions, selectedSessionId, showActiveSessionsOnly] + () => showActiveSessionsOnly + ? filterActiveSessionsOnly(sidebarSessions, selectedSessionId) + : sidebarSessions, + [sidebarSessions, selectedSessionId, showActiveSessionsOnly] + ) + const unreadSessionCount = useMemo( + () => getUnreadSessionCount(readableSessions), + [lastSeenVersion, readableSessions] ) const sessionActivityDates = useMemo( () => new Set(allSessions.map(session => formatDateValue(new Date(session.updatedAt)))), @@ -1894,6 +1906,17 @@ export function SessionList(props: { onChange={setMachineFilter} /> ) : null} + {unreadSessionCount > 0 ? ( + + ) : null}