@atelic-action/ui 8.4.0 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +50 -5
  2. package/dist/lib/contactLinks.d.ts +31 -0
  3. package/dist/lib/contactLinks.js +55 -0
  4. package/dist/scripts/check-contacts.d.ts +17 -0
  5. package/dist/scripts/check-contacts.js +162 -0
  6. package/package.json +10 -13
  7. package/src/chrome/Footer.tsx +4 -3
  8. package/src/chrome/SiteHeader.tsx +7 -6
  9. package/src/chrome/SiteMenu.tsx +11 -9
  10. package/src/chrome/StickyCTABar.tsx +3 -2
  11. package/src/components/GateLogin.tsx +2 -1
  12. package/src/components/MultiSelect.tsx +225 -0
  13. package/src/components/index.ts +1 -0
  14. package/src/lib/contactForm.ts +184 -0
  15. package/src/lib/contactLinks.ts +57 -0
  16. package/src/lib/googlePlaces.ts +392 -0
  17. package/src/lib/latestOnly.ts +47 -0
  18. package/src/primitives/AddressSearch.tsx +341 -0
  19. package/src/primitives/EmailLink.tsx +55 -0
  20. package/src/primitives/ExpandableText.tsx +137 -0
  21. package/src/primitives/GoogleMapsAttribution.tsx +14 -0
  22. package/src/primitives/PhoneLink.tsx +61 -0
  23. package/src/primitives/index.ts +18 -0
  24. package/src/scripts/check-base-path.ts +15 -0
  25. package/src/scripts/check-contacts.ts +177 -0
  26. package/src/sections/ContactForm.tsx +683 -87
  27. package/src/sections/ContactInfoCard.tsx +20 -5
  28. package/src/sections/InfoRows.tsx +103 -0
  29. package/src/sections/LocationSection.tsx +2 -1
  30. package/src/sections/Locations.tsx +3 -2
  31. package/src/sections/PageHero.tsx +22 -5
  32. package/src/sections/index.ts +23 -1
  33. package/src/styles/components.css +123 -0
  34. package/src/styles/defaults.css +31 -1
  35. package/src/styles/fields.css +33 -0
  36. package/src/styles/primitives.css +165 -0
  37. package/src/styles/sections.css +306 -19
@@ -0,0 +1,177 @@
1
+ /**
2
+ * The build time check on a site's phone numbers and email addresses: every
3
+ * number the package draws as a link has an `e164` that is a real number
4
+ * for its country, and a `display` that reads as the same number; every
5
+ * address matches the form's pattern. A number that fails ships as a dead or
6
+ * wrong call link on every page, so the build stops and names the field.
7
+ *
8
+ * Google's numbering plan (`libphonenumber-js`, its complete `max` metadata)
9
+ * runs here and in tests only, never in a page: nothing a page bundles
10
+ * imports this file (tst/phone-library-stays-out.test.ts holds that). The
11
+ * browser keeps its light check (`toE164`, `src/lib/contactLinks.ts`).
12
+ *
13
+ * It is sure of what it judges: phones by their shape (an `e164` beside a
14
+ * `display`), addresses only at the config's known paths, and `tel:` and
15
+ * `mailto:` hrefs anywhere, so a site's words are never mistaken for a
16
+ * contact and a valid site never fails.
17
+ *
18
+ * `atelic-check-base-path`, a marketing site's last build step and the one
19
+ * that already reads its config, runs `contactProblems` over the `site`
20
+ * export. A site passing numbers as props outside its config (a contact
21
+ * card's `phones`) calls the same function from its own test:
22
+ *
23
+ * import { contactProblems } from "@atelic-action/ui/scripts/contacts";
24
+ * expect(contactProblems({ phones })).toEqual([]);
25
+ */
26
+ import parsePhoneNumber, {
27
+ type CountryCode,
28
+ isSupportedCountry,
29
+ type PhoneNumber,
30
+ } from "libphonenumber-js/max";
31
+ import { isEmail } from "../lib/contactLinks.js";
32
+
33
+ /** A number read against a country, or undefined when it is not one; never throws. */
34
+ function parse(text: string, country: CountryCode): PhoneNumber | undefined {
35
+ try {
36
+ return parsePhoneNumber(text, country);
37
+ } catch {
38
+ return undefined;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * The country a number written without its + is read against: the
44
+ * config's own (`address.country`, a two letter code), else the US, where
45
+ * every site so far does business.
46
+ */
47
+ function countryOf(value: unknown): CountryCode {
48
+ const country = isRecord(value) && isRecord(value.address) ? value.address.country : undefined;
49
+ const code = typeof country === "string" ? country.trim().toUpperCase() : "";
50
+ return isSupportedCountry(code) ? code : "US";
51
+ }
52
+
53
+ /** The problem with one number, or null when it is sound. */
54
+ function phoneProblem(e164: string, display: string, country: CountryCode): string | null {
55
+ const number = e164.startsWith("+") ? parse(e164, country) : undefined;
56
+ if (!number?.isValid() || number.number !== e164) {
57
+ return `e164 "${e164}" is not a valid number in E.164 (a + and the country code, digits only)`;
58
+ }
59
+ const shown = parse(display, number.country ?? country);
60
+ if (shown?.number !== e164) {
61
+ return `display "${display}" does not read as ${e164}`;
62
+ }
63
+ return null;
64
+ }
65
+
66
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
67
+ typeof value === "object" && value !== null && !Array.isArray(value);
68
+
69
+ /**
70
+ * The config's known email addresses, by path from the walk's root (a
71
+ * `SiteConfig` or an `ArtifactConfig`). A string is judged as an address
72
+ * only here or as a `mailto:` href, never by its key's name alone, so words
73
+ * such as a label reading "Email" are left alone.
74
+ */
75
+ const EMAIL_PATHS = new Set([
76
+ "email",
77
+ "gate.contactEmail",
78
+ "contactEmail",
79
+ "analytics.identify.email",
80
+ ]);
81
+
82
+ /**
83
+ * A `tel:` href's number, decoded, with RFC 3966's visual separators (- .
84
+ * ( ) and spaces) and any parameters (";ext=12") taken out.
85
+ */
86
+ const telNumber = (href: string) =>
87
+ decoded(href.slice("tel:".length))
88
+ .split(";")[0]
89
+ ?.replace(/[-.()\s]/g, "") ?? "";
90
+
91
+ /** A percent encoded address, decoded; one too malformed to decode, as given. */
92
+ function decoded(address: string): string {
93
+ try {
94
+ return decodeURIComponent(address);
95
+ } catch {
96
+ return address;
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Every problem with the numbers and addresses in `value`, each naming its
102
+ * field (`site.header.phones[1].display`), empty when all is sound. Judges
103
+ * only what it is sure is a contact:
104
+ *
105
+ * - a phone is any object holding a string `e164` and `display` (the
106
+ * package's `Phone`, `HeaderPhone`, `ContactPhone`) or `phoneE164` and
107
+ * `phoneDisplay` (a `Locations` entry), wherever it sits: the number must
108
+ * be valid in E.164 for its country and the display must read as it;
109
+ * - an address is a string at one of the config's known address paths
110
+ * (`email`, `gate.contactEmail`, `analytics.identify.email`);
111
+ * - a `tel:` href must reduce, separators aside, to a valid number, and a
112
+ * `mailto:` href must write to an address.
113
+ *
114
+ * A value seen twice (a config that refers to itself) is walked once.
115
+ */
116
+ export function contactProblems(value: unknown, path = "site"): string[] {
117
+ return walk(value, path, "", new WeakSet(), countryOf(value));
118
+ }
119
+
120
+ function walk(
121
+ value: unknown,
122
+ path: string,
123
+ at: string,
124
+ seen: WeakSet<object>,
125
+ country: CountryCode,
126
+ ): string[] {
127
+ if (typeof value === "object" && value !== null) {
128
+ if (seen.has(value)) return [];
129
+ seen.add(value);
130
+ }
131
+ if (Array.isArray(value)) {
132
+ return value.flatMap((item, index) => walk(item, `${path}[${index}]`, at, seen, country));
133
+ }
134
+ // A React element (words a config may hold as JSX) is copy, never a contact.
135
+ if (!isRecord(value) || "$$typeof" in value) return [];
136
+ const problems: string[] = [];
137
+ for (const [e164Key, displayKey] of [
138
+ ["e164", "display"],
139
+ ["phoneE164", "phoneDisplay"],
140
+ ] as const) {
141
+ const e164 = value[e164Key];
142
+ const display = value[displayKey];
143
+ if (typeof e164 === "string" && typeof display === "string") {
144
+ const problem = phoneProblem(e164, display, country);
145
+ if (problem) problems.push(`${path}: ${problem}`);
146
+ }
147
+ }
148
+ for (const [key, item] of Object.entries(value)) {
149
+ const field = `${path}.${key}`;
150
+ const where = at ? `${at}.${key}` : key;
151
+ if (typeof item !== "string") {
152
+ // An array keeps its parent's place: header.phones[0] is under header.phones.
153
+ problems.push(...walk(item, field, where, seen, country));
154
+ continue;
155
+ }
156
+ // An empty address is none (a site without a public one), not a bad one.
157
+ if (EMAIL_PATHS.has(where) && item.trim() && !isEmail(item)) {
158
+ problems.push(`${field}: "${item}" is not an email address`);
159
+ }
160
+ if (item.startsWith("tel:")) {
161
+ // A national number (no +) is read against the config's country, as a
162
+ // dialer reads it; only one that is not a plausible number fails.
163
+ const number = parse(telNumber(item), country);
164
+ if (!number?.isPossible()) problems.push(`${field}: "${item}" does not dial a number`);
165
+ }
166
+ if (item.startsWith("mailto:")) {
167
+ // RFC 6068: one address or several, comma separated, before the query,
168
+ // or in its to= when nothing comes before it.
169
+ const [head = "", query = ""] = item.slice("mailto:".length).split("?");
170
+ const to = decoded(head) || (new URLSearchParams(query).get("to") ?? "");
171
+ if (!to.split(",").every((address) => isEmail(address))) {
172
+ problems.push(`${field}: "${item}" does not write to an address`);
173
+ }
174
+ }
175
+ }
176
+ return problems;
177
+ }