11"use client" ;
22
3- import { useState , type FormEvent } from "react" ;
3+ import { useState , useSyncExternalStore , type FormEvent } from "react" ;
44import { SolidButton } from "@/src/design-system/Button" ;
55import { Dropdown , DropdownItem } from "@/src/design-system/Dropdown" ;
66import { Input } from "@/src/design-system/Input" ;
@@ -23,7 +23,6 @@ interface FormData {
2323 name : string ;
2424 email : string ;
2525 company : string ;
26- subject : string ;
2726 message : string ;
2827}
2928
@@ -33,15 +32,37 @@ const INITIAL: FormData = {
3332 name : "" ,
3433 email : "" ,
3534 company : "" ,
36- subject : SUBJECTS [ 0 ] ,
3735 message : "" ,
3836} ;
3937
38+ function resolveSubject ( subject : string | null ) : string {
39+ if ( ! subject ) {
40+ return SUBJECTS [ 0 ] ;
41+ }
42+
43+ const match = SUBJECTS . find ( ( s ) => s . toLowerCase ( ) === subject . toLowerCase ( ) ) ;
44+
45+ return match ?? SUBJECTS [ 0 ] ;
46+ }
47+
48+ const subscribe = ( ) => ( ) => { } ;
49+ const getSubjectFromUrl = ( ) =>
50+ resolveSubject ( new URLSearchParams ( window . location . search ) . get ( "subject" ) ) ;
51+ const getServerSubject = ( ) => "" ;
52+
4053export function ContactForm ( ) {
4154 const [ data , setData ] = useState < FormData > ( INITIAL ) ;
4255 const [ errors , setErrors ] = useState < FormErrors > ( { } ) ;
4356 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
4457
58+ const urlSubject = useSyncExternalStore (
59+ subscribe ,
60+ getSubjectFromUrl ,
61+ getServerSubject ,
62+ ) ;
63+ const [ selectedSubject , setSelectedSubject ] = useState < string | null > ( null ) ;
64+ const subject = selectedSubject ?? urlSubject ;
65+
4566 function update < K extends keyof FormData > ( field : K , value : FormData [ K ] ) {
4667 setData ( ( prev ) => ( { ...prev , [ field ] : value } ) ) ;
4768 if ( field in errors ) {
@@ -85,7 +106,7 @@ export function ContactForm() {
85106 Name : data . name ,
86107 Email : data . email ,
87108 Company : data . company ,
88- SupportPlan : data . subject ,
109+ SupportPlan : subject ,
89110 Message : data . message ,
90111 } ) ,
91112 } ) ;
@@ -95,7 +116,7 @@ export function ContactForm() {
95116 }
96117
97118 window . gtag ?.( "event" , "contact_form_submit" , {
98- event_label : data . subject ,
119+ event_label : subject ,
99120 page_path : window . location . pathname ,
100121 } ) ;
101122
@@ -147,14 +168,16 @@ export function ContactForm() {
147168 label = "Subject"
148169 className = { isSubmitting ? "pointer-events-none opacity-60" : undefined }
149170 panelClassName = "p-1"
150- trigger = { < span className = "text-cc-ink text-sm" > { data . subject } </ span > }
171+ trigger = {
172+ < span className = "text-cc-ink text-sm" > { subject || "\u00A0" } </ span >
173+ }
151174 >
152175 < ul className = "m-0 flex list-none flex-col p-0" >
153176 { SUBJECTS . map ( ( s ) => (
154177 < DropdownItem
155178 key = { s }
156- active = { s === data . subject }
157- onClick = { ( ) => update ( "subject" , s ) }
179+ active = { s === subject }
180+ onClick = { ( ) => setSelectedSubject ( s ) }
158181 >
159182 { s }
160183 </ DropdownItem >
0 commit comments