diff --git a/components/calendar/header.tsx b/components/calendar/header.tsx index 514669bc..20e5c5a9 100644 --- a/components/calendar/header.tsx +++ b/components/calendar/header.tsx @@ -8,6 +8,8 @@ import { Callback } from 'lib/model/callback'; import { MeetingsQuery } from 'lib/model/query/meetings'; import { useOrg } from 'lib/context/org'; +import { useCalendarState } from './state'; + export interface CalendarHeaderProps { query: MeetingsQuery; setQuery: Callback; @@ -15,9 +17,11 @@ export interface CalendarHeaderProps { function CalendarHeader({ query, setQuery }: CalendarHeaderProps): JSX.Element { const { org } = useOrg(); + const { display } = useCalendarState(); const { t, lang: locale } = useTranslation(); - const title = useMemo(() => { + const dayTitle = useMemo(() => query.from.toLocaleString(locale, { month: 'long', day: 'numeric', year: 'numeric' }), [query.from, locale]); + const weekTitle = useMemo(() => { const { from, to } = query; if (from.getMonth() !== to.getMonth()) return `${from.toLocaleString(locale, { @@ -30,26 +34,21 @@ function CalendarHeader({ query, setQuery }: CalendarHeaderProps): JSX.Element { return from.toLocaleString(locale, { month: 'long', year: 'numeric' }); }, [query, locale]); - const prevWeek = useCallback(() => { - setQuery((prev) => { - const to = new Date(prev.from); - const from = new Date(to.getFullYear(), to.getMonth(), to.getDate() - 7); - return new MeetingsQuery({ ...prev, from, to }); + const delta = useMemo(() => display === 'Day' ? 1 : 7, [display]); + const prev = useCallback(() => { + setQuery((p) => { + const from = new Date(p.from.getFullYear(), p.from.getMonth(), p.from.getDate() - delta); + const to = new Date(p.to.getFullYear(), p.to.getMonth(), p.to.getDate() - delta); + return new MeetingsQuery({ ...p, from, to }); }); - }, [setQuery]); - - const nextWeek = useCallback(() => { - setQuery((prev) => { - const from = new Date(prev.to); - const to = new Date( - from.getFullYear(), - from.getMonth(), - from.getDate() + 7 - ); - return new MeetingsQuery({ ...prev, from, to }); + }, [setQuery, delta]); + const next = useCallback(() => { + setQuery((p) => { + const from = new Date(p.from.getFullYear(), p.from.getMonth(), p.from.getDate() + delta); + const to = new Date(p.to.getFullYear(), p.to.getMonth(), p.to.getDate() + delta); + return new MeetingsQuery({ ...p, from, to }); }); - }, [setQuery]); - + }, [setQuery, delta]); const today = useCallback(() => { setQuery((prev) => { const { from, to } = new MeetingsQuery(); @@ -60,16 +59,16 @@ function CalendarHeader({ query, setQuery }: CalendarHeaderProps): JSX.Element { return (