Skip to content

Commit e76a97c

Browse files
authored
fix: empty invoke URL
When the modal auto-opens via the ?contact= query (your useEffect with isCalled), you never call onOpen or set meta, so invokedOnUrl: meta?.url ends up undefined which is why it’s empty on /.
1 parent ae38457 commit e76a97c

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

src/services/contact.tsx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,34 @@ export const ContactModalProvider: React.FC<ContactModalDrawerProps> = ({ childr
3838
const [meta, setMeta] = React.useState<Record<string, any> | null>(null)
3939
const [isOpen, setIsOpen] = React.useState(false)
4040

41-
// When the query parameter is present, open the modal.
41+
const toast = useToast()
42+
const authentication = useAuth()
43+
44+
// Helper: always get a full URL (incl. protocol, domain, path, search, hash) and guard SSR
45+
const getCurrentUrl = React.useCallback(() => {
46+
if (typeof window !== "undefined" && window.location) {
47+
// Prefer the full href; this includes search and hash even on root "/"
48+
return window.location.href
49+
}
50+
// SSR-safe fallback using @reach/router location
51+
const pathname = location?.pathname ?? "/"
52+
const search = location?.search ?? ""
53+
const hash = location?.hash ?? ""
54+
return `${pathname}${search}${hash}`
55+
}, [location])
56+
57+
// When the query parameter is present, open the modal AND set meta.url
4258
React.useEffect(() => {
4359
if (isCalled) {
60+
setMeta(prev => ({ ...prev, url: getCurrentUrl() }))
4461
setIsOpen(true)
4562
}
46-
}, [isCalled])
47-
48-
const toast = useToast()
49-
const authentication = useAuth()
63+
}, [isCalled, getCurrentUrl])
5064

5165
const onOpen: ContactModalContextProps["onOpen"] = (args) => {
5266
const updatedMeta = {
5367
...meta,
54-
url: window.location.href,
68+
url: getCurrentUrl(), // ensure we always store a URL when opening
5569
...args?.meta,
5670
}
5771
setMeta(updatedMeta)
@@ -60,16 +74,20 @@ export const ContactModalProvider: React.FC<ContactModalDrawerProps> = ({ childr
6074

6175
const onClose = () => {
6276
// Remove the "contact" query parameter from the URL without reloading the page.
63-
const url = new URL(window.location.href)
64-
url.searchParams.delete("contact")
65-
window.history.replaceState({}, '', url.toString())
66-
77+
if (typeof window !== "undefined") {
78+
const url = new URL(window.location.href)
79+
url.searchParams.delete("contact")
80+
window.history.replaceState({}, "", url.toString())
81+
}
6782
setIsOpen(false)
6883
}
6984

7085
const onSubmit = async (data: ContactFormValues): Promise<void> => {
86+
// Never empty: prefer meta.url, else current url, else "unknown"
87+
const invokedOnUrl = meta?.url ?? getCurrentUrl() ?? "unknown"
88+
7189
const { errors } = await sendTemplateMail(
72-
'68d4c136-7d75-40cc-ba74-079a0dca4044', // replace with your actual template ID
90+
"9c919b15-02f9-46ef-8fe8-db0b04abfc40", // replace with your actual template ID
7391
{
7492
envelope: {
7593
replyTo: data.email,
@@ -78,9 +96,9 @@ export const ContactModalProvider: React.FC<ContactModalDrawerProps> = ({ childr
7896
firstName: data.firstName,
7997
lastName: data.lastName,
8098
email: data.email,
81-
phone: data.phone || '',
99+
phone: data.phone || "",
82100
message: data.message,
83-
invokedOnUrl: meta?.url,
101+
invokedOnUrl, // <-- full href incl. search + hash; never empty
84102
},
85103
}
86104
)

0 commit comments

Comments
 (0)