Skip to content

Commit 4ce0a36

Browse files
skvostclaude
andcommitted
feat(checkout): add useCheckoutPrefill hook for query-param prefill
Parses first_name/last_name/email/lock from the URL, validates email, returns a stable { prefill, lock } for the checkout details form. 🤖 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b551d55 commit 4ce0a36

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {useMemo} from "react";
2+
import {useSearchParams} from "react-router";
3+
4+
export interface CheckoutPrefill {
5+
first_name?: string;
6+
last_name?: string;
7+
email?: string;
8+
}
9+
10+
export interface UseCheckoutPrefillResult {
11+
prefill: CheckoutPrefill;
12+
lock: boolean;
13+
}
14+
15+
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
16+
17+
export const useCheckoutPrefill = (): UseCheckoutPrefillResult => {
18+
const [searchParams] = useSearchParams();
19+
20+
const firstNameParam = searchParams.get("first_name");
21+
const lastNameParam = searchParams.get("last_name");
22+
const emailParam = searchParams.get("email");
23+
const lockParam = searchParams.get("lock");
24+
25+
return useMemo(() => {
26+
const prefill: CheckoutPrefill = {};
27+
28+
const firstName = firstNameParam?.trim();
29+
if (firstName) {
30+
prefill.first_name = firstName;
31+
}
32+
33+
const lastName = lastNameParam?.trim();
34+
if (lastName) {
35+
prefill.last_name = lastName;
36+
}
37+
38+
const email = emailParam?.trim();
39+
if (email && EMAIL_REGEX.test(email)) {
40+
prefill.email = email;
41+
}
42+
43+
const hasPrefill = Object.keys(prefill).length > 0;
44+
const lock = hasPrefill && (lockParam === "true" || lockParam === "1");
45+
46+
return {prefill, lock};
47+
}, [firstNameParam, lastNameParam, emailParam, lockParam]);
48+
};

0 commit comments

Comments
 (0)