@@ -8,16 +8,20 @@ import { Callback } from 'lib/model/callback';
88import { MeetingsQuery } from 'lib/model/query/meetings' ;
99import { useOrg } from 'lib/context/org' ;
1010
11+ import { useCalendarState } from './state' ;
12+
1113export interface CalendarHeaderProps {
1214 query : MeetingsQuery ;
1315 setQuery : Callback < MeetingsQuery > ;
1416}
1517
1618function CalendarHeader ( { query, setQuery } : CalendarHeaderProps ) : JSX . Element {
1719 const { org } = useOrg ( ) ;
20+ const { display } = useCalendarState ( ) ;
1821 const { t, lang : locale } = useTranslation ( ) ;
1922
20- const title = useMemo ( ( ) => {
23+ const dayTitle = useMemo ( ( ) => query . from . toLocaleString ( locale , { month : 'long' , day : 'numeric' , year : 'numeric' } ) , [ query . from , locale ] ) ;
24+ const weekTitle = useMemo ( ( ) => {
2125 const { from, to } = query ;
2226 if ( from . getMonth ( ) !== to . getMonth ( ) )
2327 return `${ from . toLocaleString ( locale , {
@@ -30,26 +34,21 @@ function CalendarHeader({ query, setQuery }: CalendarHeaderProps): JSX.Element {
3034 return from . toLocaleString ( locale , { month : 'long' , year : 'numeric' } ) ;
3135 } , [ query , locale ] ) ;
3236
33- const prevWeek = useCallback ( ( ) => {
34- setQuery ( ( prev ) => {
35- const to = new Date ( prev . from ) ;
36- const from = new Date ( to . getFullYear ( ) , to . getMonth ( ) , to . getDate ( ) - 7 ) ;
37- return new MeetingsQuery ( { ...prev , from, to } ) ;
37+ const delta = useMemo ( ( ) => display === 'Day' ? 1 : 7 , [ display ] ) ;
38+ const prev = useCallback ( ( ) => {
39+ setQuery ( ( p ) => {
40+ const from = new Date ( p . from . getFullYear ( ) , p . from . getMonth ( ) , p . from . getDate ( ) - delta ) ;
41+ const to = new Date ( p . to . getFullYear ( ) , p . to . getMonth ( ) , p . to . getDate ( ) - delta ) ;
42+ return new MeetingsQuery ( { ...p , from, to } ) ;
3843 } ) ;
39- } , [ setQuery ] ) ;
40-
41- const nextWeek = useCallback ( ( ) => {
42- setQuery ( ( prev ) => {
43- const from = new Date ( prev . to ) ;
44- const to = new Date (
45- from . getFullYear ( ) ,
46- from . getMonth ( ) ,
47- from . getDate ( ) + 7
48- ) ;
49- return new MeetingsQuery ( { ...prev , from, to } ) ;
44+ } , [ setQuery , delta ] ) ;
45+ const next = useCallback ( ( ) => {
46+ setQuery ( ( p ) => {
47+ const from = new Date ( p . from . getFullYear ( ) , p . from . getMonth ( ) , p . from . getDate ( ) + delta ) ;
48+ const to = new Date ( p . to . getFullYear ( ) , p . to . getMonth ( ) , p . to . getDate ( ) + delta ) ;
49+ return new MeetingsQuery ( { ...p , from, to } ) ;
5050 } ) ;
51- } , [ setQuery ] ) ;
52-
51+ } , [ setQuery , delta ] ) ;
5352 const today = useCallback ( ( ) => {
5453 setQuery ( ( prev ) => {
5554 const { from, to } = new MeetingsQuery ( ) ;
@@ -60,16 +59,16 @@ function CalendarHeader({ query, setQuery }: CalendarHeaderProps): JSX.Element {
6059
6160 return (
6261 < Header
63- header = { title }
62+ header = { display === 'Day' ? dayTitle : weekTitle }
6463 body = { t ( 'calendar:subtitle' , { name : org ? `${ org . name } 's` : 'your' } ) }
6564 actions = { [
6665 {
67- label : 'Previous week' ,
68- onClick : prevWeek ,
66+ label : display === 'Day' ? 'Previous day' : 'Previous week' ,
67+ onClick : prev ,
6968 } ,
7069 {
71- label : 'Next week' ,
72- onClick : nextWeek ,
70+ label : display === 'Day' ? 'Next day' : 'Next week' ,
71+ onClick : next ,
7372 } ,
7473 {
7574 label : 'Today' ,
0 commit comments