| 1 |
import { safeParseJson } from '@shared/lib/parsing'; |
| 2 |
import { z } from 'zod'; |
| 3 |
|
| 4 |
// These override user/ai counterparts in get-profile |
| 5 |
export const urlParamsShape = z.object({ |
| 6 |
title: z |
| 7 |
.string() |
| 8 |
.optional() |
| 9 |
.default('') |
| 10 |
.catch(() => ''), |
| 11 |
description: z |
| 12 |
.string() |
| 13 |
.optional() |
| 14 |
.default('') |
| 15 |
.catch(() => ''), |
| 16 |
type: z |
| 17 |
.string() |
| 18 |
.optional() |
| 19 |
.catch(() => ''), |
| 20 |
objective: z |
| 21 |
.string() |
| 22 |
.optional() |
| 23 |
.catch(() => ''), |
| 24 |
category: z |
| 25 |
.string() |
| 26 |
.optional() |
| 27 |
.catch(() => ''), |
| 28 |
structure: z |
| 29 |
.string() |
| 30 |
.optional() |
| 31 |
.catch(() => ''), |
| 32 |
tone: z |
| 33 |
.array(z.string()) |
| 34 |
.optional() |
| 35 |
.catch(() => []), |
| 36 |
products: z |
| 37 |
.union([z.string(), z.literal(false)]) |
| 38 |
.optional() |
| 39 |
.catch(() => false), |
| 40 |
appointments: z |
| 41 |
.boolean() |
| 42 |
.optional() |
| 43 |
.catch(() => false), |
| 44 |
events: z |
| 45 |
.boolean() |
| 46 |
.optional() |
| 47 |
.catch(() => false), |
| 48 |
donations: z |
| 49 |
.boolean() |
| 50 |
.optional() |
| 51 |
.catch(() => false), |
| 52 |
multilingual: z |
| 53 |
.boolean() |
| 54 |
.optional() |
| 55 |
.catch(() => false), |
| 56 |
contact: z |
| 57 |
.boolean() |
| 58 |
.optional() |
| 59 |
.catch(() => false), |
| 60 |
address: z |
| 61 |
.union([z.boolean(), z.string()]) |
| 62 |
.optional() |
| 63 |
.catch(() => false), |
| 64 |
blog: z |
| 65 |
.boolean() |
| 66 |
.optional() |
| 67 |
.catch(() => false), |
| 68 |
'landing-page': z |
| 69 |
.string() |
| 70 |
.optional() |
| 71 |
.catch(() => ''), |
| 72 |
'cta-link': z |
| 73 |
.union([z.boolean(), z.string()]) |
| 74 |
.optional() |
| 75 |
.catch(() => false), |
| 76 |
'build-id': z |
| 77 |
.string() |
| 78 |
.optional() |
| 79 |
.catch(() => ''), |
| 80 |
go: z |
| 81 |
.boolean() |
| 82 |
.optional() |
| 83 |
.catch(() => false), |
| 84 |
}); |
| 85 |
|
| 86 |
export const urlParams = urlParamsShape.parse( |
| 87 |
safeParseJson(window.extLaunchData?.urlParams) || {}, |
| 88 |
); |
| 89 |
// remove them from the url to avoid confusion later |
| 90 |
const url = new URL(window.location.href); |
| 91 |
Object.keys(urlParams).forEach((key) => { |
| 92 |
url.searchParams.delete(key); |
| 93 |
}); |
| 94 |
window.history.replaceState({}, '', url.toString()); |
| 95 |
|
| 96 |
export const overrideWithUrlParams = (urlParams) => { |
| 97 |
const { |
| 98 |
'landing-page': landingPage, |
| 99 |
'cta-link': landingPageCTALink, |
| 100 |
...rest |
| 101 |
} = urlParams; |
| 102 |
const mapped = { |
| 103 |
...rest, |
| 104 |
landingPage: landingPage === 'clickthrough' || undefined, |
| 105 |
landingPageCTALink, |
| 106 |
}; |
| 107 |
|
| 108 |
return Object.fromEntries( |
| 109 |
Object.entries(mapped).filter(([, v]) => v !== undefined && v !== ''), |
| 110 |
); |
| 111 |
}; |
| 112 |
|