@@ -4,7 +4,7 @@ import { useSession } from "next-auth/react";
44import dynamic from "next/dynamic" ;
55import Head from "next/head" ;
66import { useRouter } from "next/router" ;
7- import { useCallback , useEffect , useMemo , useReducer , useState } from "react" ;
7+ import { useEffect , useMemo , useReducer , useState } from "react" ;
88import { useForm , useFormContext } from "react-hook-form" ;
99import { v4 as uuidv4 } from "uuid" ;
1010import { z } from "zod" ;
@@ -15,7 +15,6 @@ import { getEventLocationType, locationKeyToString } from "@calcom/app-store/loc
1515import { createPaymentLink } from "@calcom/app-store/stripepayment/lib/client" ;
1616import { getEventTypeAppData } from "@calcom/app-store/utils" ;
1717import type { LocationObject } from "@calcom/core/location" ;
18- import type { Dayjs } from "@calcom/dayjs" ;
1918import dayjs from "@calcom/dayjs" ;
2019import {
2120 useEmbedNonStylesConfig ,
@@ -36,19 +35,19 @@ import classNames from "@calcom/lib/classNames";
3635import { APP_NAME } from "@calcom/lib/constants" ;
3736import { useLocale } from "@calcom/lib/hooks/useLocale" ;
3837import useTheme from "@calcom/lib/hooks/useTheme" ;
38+ import { useTypedQuery } from "@calcom/lib/hooks/useTypedQuery" ;
3939import { HttpError } from "@calcom/lib/http-error" ;
4040import { getEveryFreqFor } from "@calcom/lib/recurringStrings" ;
4141import { collectPageParameters , telemetryEventTypes , useTelemetry } from "@calcom/lib/telemetry" ;
42- import type { RecurringEvent } from "@calcom/types/Calendar " ;
42+ import { TimeFormat } from "@calcom/lib/timeFormat " ;
4343import { Button , Form , Tooltip } from "@calcom/ui" ;
4444import { FiAlertTriangle , FiCalendar , FiRefreshCw , FiUser } from "@calcom/ui/components/icon" ;
4545
46- import { asStringOrNull } from "@lib/asStringOrNull" ;
4746import { timeZone } from "@lib/clock" ;
4847import useRouterQuery from "@lib/hooks/useRouterQuery" ;
4948import createBooking from "@lib/mutations/bookings/create-booking" ;
5049import createRecurringBooking from "@lib/mutations/bookings/create-recurring-booking" ;
51- import { parseDate , parseRecurringDates } from "@lib/parseDate" ;
50+ import { parseRecurringDates , parseDate } from "@lib/parseDate" ;
5251
5352import type { Gate , GateState } from "@components/Gates" ;
5453import Gates from "@components/Gates" ;
@@ -187,6 +186,22 @@ const BookingFields = ({
187186 ) ;
188187} ;
189188
189+ const routerQuerySchema = z
190+ . object ( {
191+ timeFormat : z . nativeEnum ( TimeFormat ) ,
192+ rescheduleUid : z . string ( ) . optional ( ) ,
193+ date : z
194+ . string ( )
195+ . optional ( )
196+ . transform ( ( date ) => {
197+ if ( date === undefined ) {
198+ return null ;
199+ }
200+ return date ;
201+ } ) ,
202+ } )
203+ . passthrough ( ) ;
204+
190205const BookingPage = ( {
191206 eventType,
192207 booking,
@@ -225,20 +240,6 @@ const BookingPage = ({
225240 duration = Number ( queryDuration ) ;
226241 }
227242
228- // This is a workaround for forcing the same time format for both server side rendering and client side rendering
229- // At initial render, we use the default time format which is 12H
230- const [ withDefaultTimeFormat , setWithDefaultTimeFormat ] = useState ( true ) ;
231- const parseDateFunc = useCallback (
232- ( date : string | null | Dayjs ) => {
233- return parseDate ( date , i18n , withDefaultTimeFormat ) ;
234- } ,
235- [ withDefaultTimeFormat ]
236- ) ;
237- // After intial render on client side, we let parseDateFunc to use the time format from the localStorage
238- useEffect ( ( ) => {
239- setWithDefaultTimeFormat ( false ) ;
240- } , [ ] ) ;
241-
242243 useEffect ( ( ) => {
243244 if ( top !== window ) {
244245 //page_view will be collected automatically by _middleware.ts
@@ -295,9 +296,12 @@ const BookingPage = ({
295296 } ,
296297 } ) ;
297298
298- const rescheduleUid = router . query . rescheduleUid as string ;
299+ const {
300+ data : { timeFormat, rescheduleUid, date } ,
301+ } = useTypedQuery ( routerQuerySchema ) ;
302+
299303 useTheme ( profile . theme ) ;
300- const date = asStringOrNull ( router . query . date ) ;
304+
301305 const querySchema = getBookingResponsesPartialSchema ( {
302306 eventType : {
303307 bookingFields : getBookingFieldsWithSystemFields ( eventType ) ,
@@ -401,26 +405,17 @@ const BookingPage = ({
401405 // Calculate the booking date(s)
402406 let recurringStrings : string [ ] = [ ] ,
403407 recurringDates : Date [ ] = [ ] ;
404- const parseRecurringDatesFunc = useCallback (
405- ( date : string | null | Dayjs , recurringEvent : RecurringEvent , recurringCount : number ) => {
406- return parseRecurringDates (
407- {
408- startDate : date ,
409- timeZone : timeZone ( ) ,
410- recurringEvent : recurringEvent ,
411- recurringCount : recurringCount ,
412- withDefaultTimeFormat : withDefaultTimeFormat ,
413- } ,
414- i18n
415- ) ;
416- } ,
417- [ withDefaultTimeFormat , date , eventType . recurringEvent , recurringEventCount ]
418- ) ;
408+
419409 if ( eventType . recurringEvent ?. freq && recurringEventCount !== null ) {
420- [ recurringStrings , recurringDates ] = parseRecurringDatesFunc (
421- date ,
422- eventType . recurringEvent ,
423- parseInt ( recurringEventCount . toString ( ) )
410+ [ recurringStrings , recurringDates ] = parseRecurringDates (
411+ {
412+ startDate : date ,
413+ timeZone : timeZone ( ) ,
414+ recurringEvent : eventType . recurringEvent ,
415+ recurringCount : parseInt ( recurringEventCount . toString ( ) ) ,
416+ selectedTimeFormat : timeFormat ,
417+ } ,
418+ i18n
424419 ) ;
425420 }
426421
@@ -550,7 +545,7 @@ const BookingPage = ({
550545 < div className = "text-sm font-medium" >
551546 { isClientTimezoneAvailable &&
552547 ( rescheduleUid || ! eventType . recurringEvent ?. freq ) &&
553- `${ parseDateFunc ( date ) } ` }
548+ `${ parseDate ( date , i18n , timeFormat ) } ` }
554549 { isClientTimezoneAvailable &&
555550 ! rescheduleUid &&
556551 eventType . recurringEvent ?. freq &&
@@ -580,7 +575,7 @@ const BookingPage = ({
580575 < FiCalendar className = "ml-[2px] -mt-1 inline-block h-4 w-4 ltr:mr-[10px] rtl:ml-[10px]" />
581576 { isClientTimezoneAvailable &&
582577 typeof booking . startTime === "string" &&
583- parseDateFunc ( dayjs ( booking . startTime ) ) }
578+ parseDate ( dayjs ( booking . startTime ) , i18n , timeFormat ) }
584579 </ p >
585580 </ div >
586581 ) }
0 commit comments