11import { useMemo , useEffect , startTransition } from "react" ;
2+ import { shallow } from "zustand/shallow" ;
23
34import { trpc } from "@calcom/trpc" ;
45import { SelectField } from "@calcom/ui/components/form" ;
@@ -7,67 +8,80 @@ import { getQueryParam } from "../../bookings/Booker/utils/query-param";
78import { useTroubleshooterStore } from "../store" ;
89
910export function EventTypeSelect ( ) {
10- const { data : eventTypes , isPending } = trpc . viewer . eventTypes . list . useQuery ( ) ;
11- const selectedEventType = useTroubleshooterStore ( ( state ) => state . event ) ;
12- const setSelectedEventType = useTroubleshooterStore ( ( state ) => state . setEvent ) ;
13-
14- const selectedEventQueryParam = getQueryParam ( "eventType" ) ;
11+ const { data : eventTypes , isPending } = trpc . viewer . eventTypes . listWithTeam . useQuery ( ) ;
12+ const { event : selectedEventType , setEvent : setSelectedEventType } = useTroubleshooterStore (
13+ ( state ) => ( {
14+ event : state . event ,
15+ setEvent : state . setEvent ,
16+ } ) ,
17+ shallow
18+ ) ;
1519
1620 const options = useMemo ( ( ) => {
1721 if ( ! eventTypes ) return [ ] ;
1822 return eventTypes . map ( ( e ) => ( {
1923 label : e . title ,
20- value : e . slug ,
24+ value : e . id . toString ( ) ,
2125 id : e . id ,
2226 duration : e . length ,
2327 } ) ) ;
2428 } , [ eventTypes ] ) ;
2529
30+ // Initialize event type from query param or default to first event
2631 useEffect ( ( ) => {
27- if ( ! selectedEventType && eventTypes && eventTypes [ 0 ] && ! selectedEventQueryParam ) {
28- const { id, slug, length } = eventTypes [ 0 ] ;
29- setSelectedEventType ( {
30- id,
31- slug,
32- duration : length ,
33- } ) ;
34- }
35- // eslint-disable-next-line react-hooks/exhaustive-deps
36- } , [ eventTypes ] ) ;
32+ if ( ! eventTypes || eventTypes . length === 0 ) return ;
3733
38- useEffect ( ( ) => {
39- if ( selectedEventQueryParam ) {
40- // ensure that the update is deferred until the Suspense boundary has finished hydrating
34+ const selectedEventIdParam = getQueryParam ( "eventTypeId" ) ;
35+ const eventTypeId = selectedEventIdParam ? parseInt ( selectedEventIdParam , 10 ) : null ;
36+
37+ // If we already have a selected event that matches the query param, don't do anything
38+ if ( selectedEventType ?. id === eventTypeId ) return ;
39+
40+ // If there's a query param, try to find and set that event
41+ if ( eventTypeId && ! isNaN ( eventTypeId ) ) {
4142 startTransition ( ( ) => {
42- const foundEventType = eventTypes ? .find ( ( et ) => et . slug === selectedEventQueryParam ) ;
43+ const foundEventType = eventTypes . find ( ( et ) => et . id === eventTypeId ) ;
4344 if ( foundEventType ) {
44- const { id, slug, length } = foundEventType ;
45- setSelectedEventType ( { id, slug, duration : length } ) ;
46- } else if ( eventTypes && eventTypes [ 0 ] ) {
47- const { id, slug, length } = eventTypes [ 0 ] ;
4845 setSelectedEventType ( {
49- id,
50- slug,
51- duration : length ,
46+ id : foundEventType . id ,
47+ slug : foundEventType . slug ,
48+ duration : foundEventType . length ,
49+ teamId : foundEventType . team ?. id ?? null ,
5250 } ) ;
51+ return ;
5352 }
5453 } ) ;
5554 }
56- } , [ eventTypes , selectedEventQueryParam , setSelectedEventType ] ) ;
55+
56+ // If no event is selected and no valid query param, default to first event
57+ if ( ! selectedEventType && ! eventTypeId ) {
58+ const firstEvent = eventTypes [ 0 ] ;
59+ setSelectedEventType ( {
60+ id : firstEvent . id ,
61+ slug : firstEvent . slug ,
62+ duration : firstEvent . length ,
63+ teamId : firstEvent . team ?. id ?? null ,
64+ } ) ;
65+ }
66+ } , [ eventTypes , selectedEventType , setSelectedEventType ] ) ;
5767
5868 return (
5969 < SelectField
6070 label = "Event Type"
6171 options = { options }
6272 isDisabled = { isPending || options . length === 0 }
63- value = { options . find ( ( option ) => option . value === selectedEventType ?. slug ) || options [ 0 ] }
73+ value = { options . find ( ( option ) => option . id === selectedEventType ?. id ) || options [ 0 ] }
6474 onChange = { ( option ) => {
6575 if ( ! option ) return ;
66- setSelectedEventType ( {
67- slug : option . value ,
68- id : option . id ,
69- duration : option . duration ,
70- } ) ;
76+ const foundEventType = eventTypes ?. find ( ( et ) => et . id === option . id ) ;
77+ if ( foundEventType ) {
78+ setSelectedEventType ( {
79+ id : foundEventType . id ,
80+ slug : foundEventType . slug ,
81+ duration : foundEventType . length ,
82+ teamId : foundEventType . team ?. id ?? null ,
83+ } ) ;
84+ }
7185 } }
7286 />
7387 ) ;
0 commit comments