From db40b71108edb07f2fa597eeb7613a579a813cc2 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Sun, 18 Oct 2026 19:27:03 -0400 Subject: [PATCH] feat(header): move mobile reports into the More menu and compact the header --- .../components/navigation/MoreDrawer.test.tsx | 57 +++++++++++++ .../src/components/navigation/MoreDrawer.tsx | 17 ++++ .../navigation/SessionMoreButton.test.tsx | 42 ++++++++++ .../navigation/SessionMoreButton.tsx | 10 ++- .../notifications/ScheduleReportsBadge.tsx | 23 ++++++ .../ScheduleReportsBell.test.tsx | 6 +- .../notifications/ScheduleReportsBell.tsx | 81 ++++++------------- .../session/ContextUsageIndicator.test.tsx | 52 ++++++++++++ .../session/ContextUsageIndicator.tsx | 10 ++- 9 files changed, 232 insertions(+), 66 deletions(-) create mode 100644 frontend/src/components/navigation/SessionMoreButton.test.tsx create mode 100644 frontend/src/components/notifications/ScheduleReportsBadge.tsx create mode 100644 frontend/src/components/session/ContextUsageIndicator.test.tsx diff --git a/frontend/src/components/navigation/MoreDrawer.test.tsx b/frontend/src/components/navigation/MoreDrawer.test.tsx index 34752591..a402a49e 100644 --- a/frontend/src/components/navigation/MoreDrawer.test.tsx +++ b/frontend/src/components/navigation/MoreDrawer.test.tsx @@ -7,6 +7,7 @@ import { useAuth } from '@/hooks/useAuth' import { useServerHealth } from '@/hooks/useServerHealth' import { getRepo } from '@/api/repos' import type { ActiveWorkItem } from '@/lib/activeWork' +import type { UnreadScheduleRun } from '@/api/schedules' vi.mock('@/hooks/useAuth') vi.mock('@/hooks/useServerHealth') @@ -32,6 +33,16 @@ vi.mock('@/hooks/useActiveWork', () => ({ useRepoActivityState: () => null, })) +const scheduleMocks = vi.hoisted(() => ({ + useUnreadScheduleRuns: vi.fn(), + useMarkAllScheduleRunsViewed: vi.fn(), +})) + +vi.mock('@/hooks/useSchedules', () => ({ + useUnreadScheduleRuns: scheduleMocks.useUnreadScheduleRuns, + useMarkAllScheduleRunsViewed: scheduleMocks.useMarkAllScheduleRunsViewed, +})) + function createActiveWorkItem(): ActiveWorkItem { const now = Date.now() return { @@ -61,6 +72,33 @@ function createActiveWorkItem(): ActiveWorkItem { } } +function createUnreadScheduleRun(): UnreadScheduleRun { + const now = Date.now() + return { + id: 1, + jobId: 1, + repoId: 1, + triggerSource: 'schedule', + status: 'completed', + startedAt: now - 60000, + finishedAt: now - 30000, + viewedAt: null, + createdAt: now - 60000, + sessionId: null, + sessionTitle: null, + logText: null, + responseText: null, + errorText: null, + runBranch: null, + commitHash: null, + worktreePath: null, + jobName: 'Nightly report', + repoName: 'repo1', + repoPath: '/path/to/repo1', + preview: 'Report ready', + } +} + function setActivityGroups(groups: { needsInput?: ActiveWorkItem[] running?: ActiveWorkItem[] @@ -158,6 +196,8 @@ describe('MoreDrawer', () => { }) activityMocks.useActivityWindowMinutes.mockReturnValue(30) setActivityGroups({}) + scheduleMocks.useUnreadScheduleRuns.mockReturnValue({ data: { runs: [], total: 0, failed: 0 } }) + scheduleMocks.useMarkAllScheduleRunsViewed.mockReturnValue({ mutate: vi.fn(), isPending: false }) }) it('renders Settings and Logout menu items', () => { @@ -360,4 +400,21 @@ describe('MoreDrawer', () => { expect(navigateMock).toHaveBeenCalledWith('/repos/1/sessions/s1') expect(handleClose).not.toHaveBeenCalled() }) + + it('shows unread reports and marks them all read', () => { + const markAllReportsViewed = vi.fn() + mockAuth() + mockServerHealth() + scheduleMocks.useUnreadScheduleRuns.mockReturnValue({ + data: { runs: [createUnreadScheduleRun()], total: 1, failed: 0 }, + }) + scheduleMocks.useMarkAllScheduleRunsViewed.mockReturnValue({ mutate: markAllReportsViewed, isPending: false }) + renderMoreDrawer({ initialEntry: '/repos/1/sessions/session-1', routePath: '/repos/:id/sessions/:sessionId' }) + + expect(screen.getByText('Nightly report')).toBeInTheDocument() + + fireEvent.click(screen.getByRole('button', { name: 'Mark all read' })) + + expect(markAllReportsViewed).toHaveBeenCalledTimes(1) + }) }) diff --git a/frontend/src/components/navigation/MoreDrawer.tsx b/frontend/src/components/navigation/MoreDrawer.tsx index fd82c588..48f1c26f 100644 --- a/frontend/src/components/navigation/MoreDrawer.tsx +++ b/frontend/src/components/navigation/MoreDrawer.tsx @@ -9,6 +9,8 @@ import { getRepo } from '@/api/repos' import { useRefreshOnOpen } from '@/hooks/useRefreshOnOpen' import { SideDrawer, SideDrawerContent } from '@/components/ui/side-drawer' import { CompactActivityList } from '@/components/activity/CompactActivityList' +import { ScheduleReportsList } from '@/components/notifications/ScheduleReportsBell' +import { useMarkAllScheduleRunsViewed, useUnreadScheduleRuns } from '@/hooks/useSchedules' import { buildMoreItems, buildNavModel, isSessionDetailPath, type MoreDrawerItem } from './moreDrawerItems' import { useSwipeBack } from '@/hooks/useMobile' import { cn, getRepoDisplayName } from '@/lib/utils' @@ -77,6 +79,9 @@ export function MoreDrawer({ isOpen, onClose }: MoreDrawerProps) { const isAssistantRoute = isAssistantPath(location.pathname) const isAssistantSession = isSessionDetail && searchParams.get('assistant') === '1' const { open: openMobileSheet } = useMobileTabBar() + const { data: unreadReports } = useUnreadScheduleRuns() + const markAllReportsViewed = useMarkAllScheduleRunsViewed() + const total = unreadReports?.total ?? 0 useEffect(() => { if (isOpen && swipeRef.current) { @@ -184,6 +189,18 @@ export function MoreDrawer({ isOpen, onClose }: MoreDrawerProps) { )} + {total > 0 && ( +
+ markAllReportsViewed.mutate()} + markAllPending={markAllReportsViewed.isPending} + maxHeightClass="" + /> +
+ )} {isOpen && ( navigate(path)} /> )} diff --git a/frontend/src/components/navigation/SessionMoreButton.test.tsx b/frontend/src/components/navigation/SessionMoreButton.test.tsx new file mode 100644 index 00000000..6cea1161 --- /dev/null +++ b/frontend/src/components/navigation/SessionMoreButton.test.tsx @@ -0,0 +1,42 @@ +import { fireEvent, render, screen } from '@testing-library/react' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { SessionMoreButton } from './SessionMoreButton' + +const mocks = vi.hoisted(() => ({ + open: vi.fn(), + useUnreadScheduleRuns: vi.fn(), +})) + +vi.mock('@/hooks/useMobileTabBar', () => ({ + useMobileTabBar: () => ({ open: mocks.open, openSheet: null, close: vi.fn() }), +})) + +vi.mock('@/hooks/useSchedules', () => ({ + useUnreadScheduleRuns: mocks.useUnreadScheduleRuns, +})) + +describe('SessionMoreButton', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('shows the unread report count and opens the drawer', () => { + mocks.useUnreadScheduleRuns.mockReturnValue({ data: { runs: [], total: 4, failed: 0 } }) + render() + + const button = screen.getByRole('button', { name: 'More, 4 unread reports' }) + expect(screen.getByText('4')).toBeInTheDocument() + + fireEvent.click(button) + + expect(mocks.open).toHaveBeenCalledWith('more') + }) + + it('shows no count when there are no unread reports', () => { + mocks.useUnreadScheduleRuns.mockReturnValue({ data: { runs: [], total: 0, failed: 0 } }) + render() + + expect(screen.getByRole('button', { name: 'More' })).toBeInTheDocument() + expect(screen.queryByText('0')).not.toBeInTheDocument() + }) +}) diff --git a/frontend/src/components/navigation/SessionMoreButton.tsx b/frontend/src/components/navigation/SessionMoreButton.tsx index 41a56b8f..16d42e3f 100644 --- a/frontend/src/components/navigation/SessionMoreButton.tsx +++ b/frontend/src/components/navigation/SessionMoreButton.tsx @@ -1,19 +1,25 @@ import { MoreVertical } from 'lucide-react' import { Button } from '@/components/ui/button' +import { ScheduleReportsBadge } from '@/components/notifications/ScheduleReportsBadge' +import { useUnreadScheduleRuns } from '@/hooks/useSchedules' import { useMobileTabBar } from '@/hooks/useMobileTabBar' export function SessionMoreButton() { const { open } = useMobileTabBar() + const { data } = useUnreadScheduleRuns() + const total = data?.total ?? 0 + const failed = data?.failed ?? 0 return ( ) } diff --git a/frontend/src/components/notifications/ScheduleReportsBadge.tsx b/frontend/src/components/notifications/ScheduleReportsBadge.tsx new file mode 100644 index 00000000..360478cb --- /dev/null +++ b/frontend/src/components/notifications/ScheduleReportsBadge.tsx @@ -0,0 +1,23 @@ +import { cn } from '@/lib/utils' + +interface ScheduleReportsBadgeProps { + total: number + failed: number + className?: string +} + +export function ScheduleReportsBadge({ total, failed, className }: ScheduleReportsBadgeProps) { + if (total <= 0) return null + + return ( + 0 ? 'bg-destructive' : 'bg-primary', + className, + )} + > + {total > 9 ? '9+' : total} + + ) +} diff --git a/frontend/src/components/notifications/ScheduleReportsBell.test.tsx b/frontend/src/components/notifications/ScheduleReportsBell.test.tsx index fdf7cb3d..7a797f27 100644 --- a/frontend/src/components/notifications/ScheduleReportsBell.test.tsx +++ b/frontend/src/components/notifications/ScheduleReportsBell.test.tsx @@ -14,10 +14,6 @@ vi.mock('@/hooks/useSchedules', () => ({ useMarkAllScheduleRunsViewed: mocks.useMarkAllScheduleRunsViewed, })) -vi.mock('@/hooks/useMobile', () => ({ - useMobile: vi.fn(() => false), -})) - function renderBell() { return render( @@ -50,7 +46,7 @@ describe('ScheduleReportsBell', () => { mocks.useUnreadScheduleRuns.mockReturnValue({ data: { runs: [], total: 3, failed: 1 } }) renderBell() - expect(screen.getByTestId('schedule-reports-count')).toHaveClass('bg-destructive') + expect(screen.getByText('3')).toHaveClass('bg-destructive') }) it('renders nothing when there are no unread runs', () => { diff --git a/frontend/src/components/notifications/ScheduleReportsBell.tsx b/frontend/src/components/notifications/ScheduleReportsBell.tsx index 576aad79..a28900b4 100644 --- a/frontend/src/components/notifications/ScheduleReportsBell.tsx +++ b/frontend/src/components/notifications/ScheduleReportsBell.tsx @@ -4,28 +4,11 @@ import { CheckCircle2, Inbox, XCircle } from 'lucide-react' import { formatDistanceToNow } from 'date-fns' import { Button } from '@/components/ui/button' import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@/components/ui/dropdown-menu' -import { BottomSheet, BottomSheetContent, BottomSheetHeader } from '@/components/ui/bottom-sheet' -import { useMobile } from '@/hooks/useMobile' +import { ScheduleReportsBadge } from '@/components/notifications/ScheduleReportsBadge' import { useMarkAllScheduleRunsViewed, useUnreadScheduleRuns } from '@/hooks/useSchedules' import { cn } from '@/lib/utils' import type { UnreadScheduleRun } from '@/api/schedules' -function ScheduleReportsCount({ total, failed }: { total: number; failed: number }) { - if (total <= 0) return null - - return ( - 0 ? 'bg-destructive' : 'bg-primary', - )} - > - {total > 9 ? '9+' : total} - - ) -} - function ScheduleReportRow({ run, rowClassName, onClick }: { run: UnreadScheduleRun; rowClassName?: string; onClick: () => void }) { const failed = run.status === 'failed' const StatusIcon = failed ? XCircle : CheckCircle2 @@ -66,9 +49,19 @@ interface ScheduleReportsListProps { markAllPending: boolean onNavigate?: () => void showTitle?: boolean + maxHeightClass?: string } -function ScheduleReportsList({ runs, total, rowClassName, onMarkAllRead, markAllPending, onNavigate, showTitle = true }: ScheduleReportsListProps) { +export function ScheduleReportsList({ + runs, + total, + rowClassName, + onMarkAllRead, + markAllPending, + onNavigate, + showTitle = true, + maxHeightClass = 'max-h-[60vh]', +}: ScheduleReportsListProps) { const navigate = useNavigate() const openRun = (runId: number) => { @@ -99,7 +92,7 @@ function ScheduleReportsList({ runs, total, rowClassName, onMarkAllRead, markAll

New scheduled reports will show up here.

) : ( -
+
{runs.map((run) => ( openRun(run.id)} /> ))} @@ -117,7 +110,6 @@ function ScheduleReportsList({ runs, total, rowClassName, onMarkAllRead, markAll } export function ScheduleReportsBell() { - const isMobile = useMobile() const { data } = useUnreadScheduleRuns() const markAllViewed = useMarkAllScheduleRunsViewed() const [open, setOpen] = useState(false) @@ -132,44 +124,19 @@ export function ScheduleReportsBell() { if (total === 0 && !open) return null - const trigger = ( - - ) - - if (isMobile) { - return ( - <> - {trigger} - setOpen(false)} ariaLabel="Reports"> - - - setOpen(false)} - showTitle={false} - /> - - - - ) - } - return ( - {trigger} + + + ({ + useContextUsage: vi.fn(), + useMobile: vi.fn(), +})) + +vi.mock('@/hooks/useContextUsage', () => ({ + useContextUsage: mocks.useContextUsage, +})) + +vi.mock('@/hooks/useMobile', () => ({ + useMobile: mocks.useMobile, +})) + +function renderIndicator() { + return render() +} + +describe('ContextUsageIndicator', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('renders tokens and percentage on desktop', () => { + mocks.useMobile.mockReturnValue(false) + mocks.useContextUsage.mockReturnValue({ usage: { totalTokens: 1234, usagePercentage: 42 }, isLoading: false }) + renderIndicator() + + expect(screen.getByText('1,234 (42%)')).toBeInTheDocument() + expect(screen.queryByText('42%')).not.toBeInTheDocument() + }) + + it('renders only the percentage on mobile', () => { + mocks.useMobile.mockReturnValue(true) + mocks.useContextUsage.mockReturnValue({ usage: { totalTokens: 1234, usagePercentage: 42 }, isLoading: false }) + renderIndicator() + + expect(screen.getByText('42%')).toBeInTheDocument() + expect(screen.queryByText('1,234 (42%)')).not.toBeInTheDocument() + }) + + it('renders only the token count on mobile without a percentage', () => { + mocks.useMobile.mockReturnValue(true) + mocks.useContextUsage.mockReturnValue({ usage: { totalTokens: 1234, usagePercentage: null }, isLoading: false }) + renderIndicator() + + expect(screen.getByText('1,234')).toBeInTheDocument() + }) +}) diff --git a/frontend/src/components/session/ContextUsageIndicator.tsx b/frontend/src/components/session/ContextUsageIndicator.tsx index c11bd1f9..64d4b354 100644 --- a/frontend/src/components/session/ContextUsageIndicator.tsx +++ b/frontend/src/components/session/ContextUsageIndicator.tsx @@ -1,4 +1,5 @@ import { useContextUsage } from '@/hooks/useContextUsage' +import { useMobile } from '@/hooks/useMobile' interface ContextUsageIndicatorProps { directory?: string @@ -22,6 +23,7 @@ export function ContextUsageIndicator({ revertMessageID, }: ContextUsageIndicatorProps) { const { usage, isLoading } = useContextUsage(sessionID, directory, revertMessageID) + const isMobile = useMobile() if (isLoading) { return ( @@ -43,14 +45,18 @@ export function ContextUsageIndicator({ return null } - const tokenText = usage.usagePercentage === null + const compactText = usage.usagePercentage === null + ? usage.totalTokens.toLocaleString() + : `${usage.usagePercentage}%` + + const fullText = usage.usagePercentage === null ? usage.totalTokens.toLocaleString() : `${usage.totalTokens.toLocaleString()} (${usage.usagePercentage}%)` return (
- {tokenText} + {isMobile ? compactText : fullText}
)