Skip to content

Commit 24914db

Browse files
refactor(ui): extract appendUtmParams helper for cleaner URL building
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 4e19bb8 commit 24914db

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

packages/ui/src/components/web-navigation.tsx

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,36 @@ interface WebNavigationProps {
4242
buttonVariant?: "ppg" | "orm" | undefined;
4343
}
4444

45+
function appendUtmParams(base: string, utm?: WebNavigationProps["utm"]) {
46+
if (!utm) return base;
47+
const url = new URL(base);
48+
for (const [key, value] of Object.entries(utm)) {
49+
if (key.startsWith("utm_") && value) {
50+
url.searchParams.set(key, value);
51+
}
52+
}
53+
return url.toString();
54+
}
55+
4556
function buildConsoleHref(
4657
pathname: "/login" | "/sign-up",
4758
utm?: WebNavigationProps["utm"],
4859
preserveExactUtm = false,
4960
) {
50-
if (!utm) {
51-
return `https://console.prisma.io${pathname}`;
52-
}
53-
54-
const href = new URL(`https://console.prisma.io${pathname}`);
61+
const href = appendUtmParams(`https://console.prisma.io${pathname}`, utm);
5562

56-
for (const [key, value] of Object.entries(utm)) {
57-
if (key.startsWith("utm_") && value) {
58-
href.searchParams.set(key, value);
63+
if (!preserveExactUtm) {
64+
const url = new URL(href);
65+
if (!url.searchParams.has("utm_campaign")) {
66+
url.searchParams.set(
67+
"utm_campaign",
68+
pathname === "/login" ? "login" : "signup",
69+
);
70+
return url.toString();
5971
}
6072
}
6173

62-
if (!preserveExactUtm && !href.searchParams.has("utm_campaign")) {
63-
href.searchParams.set(
64-
"utm_campaign",
65-
pathname === "/login" ? "login" : "signup",
66-
);
67-
}
68-
69-
return href.toString();
74+
return href;
7075
}
7176

7277
export function WebNavigation({
@@ -78,8 +83,8 @@ export function WebNavigation({
7883
const [mobileView, setMobileView] = useState(false);
7984
const loginHref = buildConsoleHref("/login", utm, preserveExactUtm);
8085
const signupHref = buildConsoleHref("/sign-up", utm, preserveExactUtm);
81-
const logoHref = preserveExactUtm && utm
82-
? `https://www.prisma.io?${new URLSearchParams(Object.entries(utm).filter(([k, v]) => k.startsWith("utm_") && v)).toString()}`
86+
const logoHref = preserveExactUtm
87+
? appendUtmParams("https://www.prisma.io", utm)
8388
: "https://www.prisma.io";
8489

8590
useEffect(() => {

0 commit comments

Comments
 (0)