diff --git a/web/features/calendar/components/CalendarPage.tsx b/web/features/calendar/components/CalendarPage.tsx index b87ddd2a0..c0e896990 100644 --- a/web/features/calendar/components/CalendarPage.tsx +++ b/web/features/calendar/components/CalendarPage.tsx @@ -2,7 +2,7 @@ import { browserStorage } from '@/shared/storage' import { useEffect, useMemo, useRef, useState } from 'react' -import { CalendarDays, ChevronLeft, ChevronRight, Plus, Search, PanelLeft, X } from 'lucide-react' +import { CalendarDays, ChevronDown, ChevronLeft, ChevronRight, Plus, Search, PanelLeft, X } from 'lucide-react' import { useTranslation } from 'react-i18next' import { PageHeader } from '@/components/layout/FeaturePage' import HeaderActionButton from '@/features/chat/components/HeaderActionButton' @@ -50,6 +50,7 @@ import { } from '../model/items' import { AnchoredPopover, anchorOf, type Anchor } from './AnchoredPopover' import { CalendarRail } from './CalendarRail' +import { MonthYearPicker } from './MiniMonth' import { TimeGrid, type GridDraft } from './TimeGrid' import { MonthView } from './MonthView' import { AgendaView, SearchResults } from './AgendaView' @@ -71,6 +72,7 @@ type Panel = { anchor: Anchor } & ( | { kind: 'delete'; item: EventItem } | { kind: 'calendar-delete'; calendar: api.CalendarEntry } | { kind: 'move'; item: EventItem; start: Date; end: Date } + | { kind: 'jump' } ) const center: Anchor = { top: 110, bottom: 140, left: 280, right: 280 } @@ -367,7 +369,24 @@ export default function CalendarPage() { navigate(-1)} /> navigate(1)} /> -

{heading}

+

+ +

) const tools = ( @@ -557,10 +576,31 @@ export default function CalendarPage() { - {panel.kind === 'edit' ? ( + {panel.kind === 'jump' ? ( +
+ { + const lastDay = new Date(year, month + 1, 0).getDate() + setCursor(new Date(year, month, Math.min(cursor.getDate(), lastDay))) + setQuery('') + setPanel(null) + }} + /> +
+ ) : panel.kind === 'edit' ? ( i) + +/** Compact month & 12-year drill-down picker used by both the toolbar and mini calendar. */ +export function MonthYearPicker({ + activeYear, + activeMonth, + today, + locale, + onSelect, +}: { + activeYear: number + activeMonth: number + today: Date + locale: string + onSelect: (year: number, month: number) => void +}) { + const { t } = useTranslation() + const [year, setYear] = useState(activeYear) + const [followedYear, setFollowedYear] = useState(activeYear) + const [pickingYear, setPickingYear] = useState(false) + const [yearStart, setYearStart] = useState(activeYear - 4) + if (activeYear !== followedYear) { + setFollowedYear(activeYear) + setYear(activeYear) + setYearStart(activeYear - 4) + } + const todayYear = today.getFullYear() + const todayMonth = today.getMonth() + const yearRange = `${yearStart} – ${yearStart + 11}` + return ( +
+
+ + + +
+ {pickingYear ? ( +
+ {MONTHS.map(offset => { + const y = yearStart + offset + return ( + + ) + })} +
+ ) : ( +
+ {MONTHS.map(m => ( + + ))} +
+ )} +
+ ) +} + /** A small month to jump around in; the shown range is shaded. */ export function MiniMonth({ cursor, @@ -34,6 +136,7 @@ export function MiniMonth({ const locale = i18n.language const [shown, setShown] = useState(() => startOfMonth(cursor)) const [followed, setFollowed] = useState(dateKey(startOfMonth(cursor))) + const [pickingMonth, setPickingMonth] = useState(false) // Follow the main view when it leaves the month on display. const cursorMonth = dateKey(startOfMonth(cursor)) if (cursorMonth !== followed) { @@ -45,7 +148,15 @@ export function MiniMonth({ return (
- {formatMonthTitle(shown, locale)} +
-
+ {pickingMonth ? ( + { + setShown(new Date(year, month, 1)) + setPickingMonth(false) + }} + /> + ) : ( +
{weeks[0].map(day => ( @@ -103,7 +226,8 @@ export function MiniMonth({ })}
))} -
+
+ )}
) } diff --git a/web/features/calendar/components/calendar.css b/web/features/calendar/components/calendar.css index 546e6be48..53b689b46 100644 --- a/web/features/calendar/components/calendar.css +++ b/web/features/calendar/components/calendar.css @@ -51,6 +51,20 @@ margin: 0 10px 0 6px; font-variant-numeric: tabular-nums; } +.dt-cal-heading-button { + display: inline-flex; + align-items: center; + gap: 5px; + padding: 3px 8px; + margin: -3px -8px; + border-radius: 7px; + font: inherit; + letter-spacing: inherit; +} +.dt-cal-heading-button:hover, +.dt-cal-heading-button[aria-expanded='true'] { + background: var(--secondary); +} .dt-cal-today { height: 30px; margin-left: 4px; @@ -251,6 +265,70 @@ button.dt-cal-rail-link { } .dt-cal-mini-title { flex: 1; + display: inline-flex; + align-items: center; + gap: 4px; + padding: 2px 6px; + margin-left: -6px; + border-radius: 6px; + font-weight: 600; + text-align: left; +} +.dt-cal-mini-title:hover, +.dt-cal-mini-title[aria-expanded='true'] { + background: var(--secondary); +} +.dt-cal-ym-popover { + padding: 12px; +} +.dt-cal-ym-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 6px; + margin-bottom: 10px; +} +.dt-cal-ym-year { + flex: 1; + height: 28px; + display: inline-flex; + align-items: center; + justify-content: center; + gap: 4px; + padding: 0 8px; + border-radius: 6px; + color: var(--foreground); + font-size: 13px; + font-weight: 600; + font-variant-numeric: tabular-nums; + transition: background-color 120ms; +} +.dt-cal-ym-year:hover, +.dt-cal-ym-year[aria-expanded='true'] { + background: var(--secondary); +} +.dt-cal-ym-grid { + display: grid; + grid-template-columns: repeat(3, 1fr); + gap: 4px; +} +.dt-cal-ym-month { + height: 30px; + border-radius: 6px; + font-size: 12px; + font-variant-numeric: tabular-nums; + color: var(--foreground); + transition: background-color 120ms, color 120ms; +} +.dt-cal-ym-month:hover { + background: var(--accent, var(--secondary)); +} +.dt-cal-ym-month[aria-current='date'] { + box-shadow: inset 0 0 0 1px var(--primary); +} +.dt-cal-ym-month[aria-pressed='true'] { + background: var(--primary); + color: var(--primary-foreground); font-weight: 600; } .dt-cal-icon-button { diff --git a/web/tests/calendar.spec.tsx b/web/tests/calendar.spec.tsx index daaf439b9..4eb185c90 100644 --- a/web/tests/calendar.spec.tsx +++ b/web/tests/calendar.spec.tsx @@ -185,6 +185,55 @@ it('does not trigger calendar shortcuts while typing in the editor', async () => expect(screen.getByRole('tab', { name: 'Week' })).toHaveAttribute('aria-selected', 'true') }) +it('jumps to a chosen month and year from the toolbar heading and toggles closed', async () => { + mount() + await waitFor(() => expect(screen.getByRole('button', { name: 'New event' })).toBeEnabled()) + const heading = screen.getByRole('heading', { level: 2 }) + const toggle = within(heading).getByRole('button') + fireEvent.click(toggle) + expect(screen.getByRole('dialog', { name: 'Calendar' })).toBeInTheDocument() + // Let AnchoredPopover attach its document pointerdown listener before toggling closed. + await act(async () => { + await new Promise(resolve => setTimeout(resolve, 0)) + }) + fireEvent.pointerDown(toggle) + fireEvent.click(toggle) + expect(screen.queryByRole('dialog', { name: 'Calendar' })).not.toBeInTheDocument() + + fireEvent.click(toggle) + const dialog = within(screen.getByRole('dialog', { name: 'Calendar' })) + const currentYear = new Date().getFullYear() + const targetYear = currentYear + 14 + fireEvent.click(dialog.getByRole('button', { name: String(currentYear) })) + fireEvent.click(dialog.getByRole('button', { name: 'Next' })) + fireEvent.click(dialog.getByRole('button', { name: String(targetYear) })) + fireEvent.click(dialog.getByRole('button', { name: 'Feb' })) + expect(screen.queryByRole('dialog', { name: 'Calendar' })).not.toBeInTheDocument() + expect(heading).toHaveTextContent(`February ${targetYear}`) + await waitFor(() => + expect( + vi.mocked(fetch).mock.calls.some(([url]) => String(url).includes(`${targetYear}-`)) + ).toBe(true) + ) +}) + +it('picks a month and year from the mini calendar and keeps year synced when stepping', async () => { + mount() + await waitFor(() => expect(screen.getByRole('button', { name: 'New event' })).toBeEnabled()) + const rail = within(screen.getByRole('complementary', { name: 'Calendar sidebar' })) + const currentYear = new Date().getFullYear() + const prevYear = currentYear - 1 + + fireEvent.click(rail.getByRole('button', { name: new RegExp(String(currentYear)) })) + fireEvent.click(rail.getByRole('button', { name: 'Previous year' })) + fireEvent.click(rail.getByRole('button', { name: 'Jan' })) + expect(rail.getByRole('grid', { name: `January ${prevYear}` })).toBeInTheDocument() + + fireEvent.click(rail.getByRole('button', { name: `January ${prevYear}` })) + fireEvent.click(rail.getByRole('button', { name: 'Previous month' })) + expect(rail.getByRole('button', { name: String(prevYear - 1) })).toBeInTheDocument() +}) + describe('calendar models', () => { it('clamps month navigation and covers the leap day once', () => { expect(dateKey(addMonths(new Date(2024, 0, 31), 1))).toBe('2024-02-29')