@12-apps/tenant 1.3.0 → 1.4.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.
@@ -0,0 +1,353 @@
1
+ /**
2
+ * What a person has to publish, and what each state means: the DNS side of
3
+ * the screen. The exact records to paste into a registrar, the word for each
4
+ * stored status, and the warning for the one state that looks like success
5
+ * and is not (verified, ACTIVE, and the name still resolves somewhere else).
6
+ * Keeping both records in one file is what keeps them from drifting apart.
7
+ */
8
+ import { useRef, useState, type JSX } from 'react';
9
+
10
+ import { Alert } from '@12-apps/ui/data-display/Alert';
11
+ import { Button } from '@12-apps/ui/form/Button';
12
+ import { Input } from '@12-apps/ui/form/Input';
13
+ import { Box } from '@12-apps/ui/mui/Box';
14
+ import { Stack } from '@12-apps/ui/mui/Stack';
15
+ import { Text } from '@12-apps/ui/typography/Text';
16
+
17
+ import { dnsZoneOf, isZoneApex, pointingRecordType, relativeDnsName } from '../dns-names';
18
+ import { usePhoneCopy, useDomainsScreen, type TenantDomainItem } from './context';
19
+ import type { TenantDomainPairCopy, TenantDomainRecordsCopy } from './copy';
20
+
21
+ export { pointingRecordType, type PointingRecordType } from '../dns-names';
22
+
23
+ /** The word per stored status: the raw enum never reaches the person; the keys stay the wire's. */
24
+ function statusLabels(copy: TenantDomainRecordsCopy): Record<string, string> {
25
+ return {
26
+ PENDING: copy.awaitingVerification,
27
+ VERIFIED: copy.verified,
28
+ ACTIVE: copy.active,
29
+ DISABLED: copy.disabled,
30
+ FAILED: copy.verificationFailed,
31
+ };
32
+ }
33
+
34
+ /**
35
+ * The status line, which must not claim more than is known. "Active" alone
36
+ * means "this address serves the tenant", true only when the name also
37
+ * resolves here: `dnsPointsHere === false` is the one case known not to;
38
+ * `null` is unknown and stays silent.
39
+ */
40
+ export function statusLabelFor(
41
+ domain: Pick<TenantDomainItem, 'status' | 'dnsPointsHere'>,
42
+ copy: TenantDomainRecordsCopy,
43
+ ): string {
44
+ const base = statusLabels(copy)[domain.status] ?? domain.status;
45
+ if (domain.status !== 'ACTIVE') return base;
46
+ return domain.dnsPointsHere === false ? copy.dnsNotHereYet(base) : base;
47
+ }
48
+
49
+ /** A domain's name on its card: a pair is its root `+ www`, as the host's pack writes it. */
50
+ export function domainName(
51
+ domain: Pick<TenantDomainItem, 'hostname' | 'wwwHostname'>,
52
+ pair: Pick<TenantDomainPairCopy, 'pairName'>,
53
+ ): string {
54
+ return domain.wwwHostname === null ? domain.hostname : pair.pairName(domain.hostname);
55
+ }
56
+
57
+ /**
58
+ * One record value, read-only, and its copy button. A long value (the TXT
59
+ * token) is cut on a phone, so one tap copies it whole; it says so for a
60
+ * moment. Where the browser gives no clipboard or refuses the write, the tap
61
+ * selects the field's text instead, to copy by hand.
62
+ */
63
+ function RecordValue({
64
+ value,
65
+ label,
66
+ copy,
67
+ testId,
68
+ }: {
69
+ value: string;
70
+ label: string;
71
+ copy: TenantDomainRecordsCopy;
72
+ testId: string;
73
+ }): JSX.Element {
74
+ const [copied, setCopied] = useState(false);
75
+ const field = useRef<HTMLDivElement>(null);
76
+ const selectField = () => {
77
+ const input = field.current?.querySelector('input');
78
+ input?.focus();
79
+ input?.select();
80
+ };
81
+ return (
82
+ <Stack direction="row" spacing={1} sx={{ alignItems: 'flex-end' }}>
83
+ <Box ref={field} sx={{ flex: '1 1 0', minWidth: 0 }}>
84
+ <Input size="sm" fullWidth readOnly label={label} value={value} title={value} data-testid={testId} />
85
+ </Box>
86
+ <Button
87
+ variant="outline"
88
+ color="primary"
89
+ size="sm"
90
+ onClick={() => {
91
+ if (typeof navigator.clipboard?.writeText !== 'function') return selectField();
92
+ void navigator.clipboard.writeText(value).then(() => {
93
+ setCopied(true);
94
+ window.setTimeout(() => setCopied(false), 2000);
95
+ }, selectField);
96
+ }}
97
+ aria-label={copy.copyValueLabel(value)}
98
+ dataTestId="domain-record-copy"
99
+ >
100
+ {copied ? copy.copiedValue : copy.copyValue}
101
+ </Button>
102
+ </Stack>
103
+ );
104
+ }
105
+
106
+ /**
107
+ * One record as a provider's form asks for it: type, name, value, each a
108
+ * read-only field to copy. The name is RELATIVE to the zone (`@`, `shop`, the
109
+ * TXT prefix); the full name stays one line below for providers that want it.
110
+ */
111
+ function DnsRecord({
112
+ copy,
113
+ type,
114
+ name,
115
+ values,
116
+ fqdn,
117
+ valueLabel,
118
+ testId,
119
+ }: {
120
+ copy: TenantDomainRecordsCopy;
121
+ type: string;
122
+ name: string;
123
+ values: string[];
124
+ /** The full name, shown as a fallback when it differs from `name`. */
125
+ fqdn?: string;
126
+ /** The value field's label when the plain one would not say which record it is. */
127
+ valueLabel?: string;
128
+ /** Prefix of the name (`-name`) and value (`-value`) fields' test ids. */
129
+ testId: string;
130
+ }): JSX.Element {
131
+ return (
132
+ <Stack spacing={1}>
133
+ <Stack direction="row" spacing={1} sx={{ alignItems: 'flex-end' }}>
134
+ <Box sx={{ flex: '0 0 4.5rem' }}>
135
+ <Input size="sm" fullWidth readOnly label={copy.recordType} value={type} />
136
+ </Box>
137
+ <Box sx={{ flex: '1 1 0', minWidth: 0 }}>
138
+ <Input size="sm" fullWidth readOnly label={copy.recordName} value={name} data-testid={`${testId}-name`} />
139
+ </Box>
140
+ </Stack>
141
+ {values.map((value) => (
142
+ <RecordValue key={value} value={value} label={valueLabel ?? copy.recordValue} copy={copy} testId={`${testId}-value`} />
143
+ ))}
144
+ {fqdn !== undefined && fqdn !== name ? (
145
+ <Text variant="caption" size="xs" style={{ overflowWrap: 'anywhere' }}>
146
+ {copy.fullNameFallback(fqdn)}
147
+ </Text>
148
+ ) : null}
149
+ </Stack>
150
+ );
151
+ }
152
+
153
+ /** The TXT record to publish before a domain can serve. A pair's one record, at the root, proves both names. */
154
+ export function VerificationRecord({ domain }: { domain: TenantDomainItem }): JSX.Element {
155
+ const { copy: pack } = useDomainsScreen();
156
+ const copy = pack.records;
157
+ const phone = usePhoneCopy();
158
+ const step = domain.wwwHostname === null ? copy.txtStep : phone ? pack.pair.txtStepPairPhone : pack.pair.txtStepPair;
159
+ return (
160
+ <Stack spacing={1}>
161
+ <Text variant="caption" size="xs">
162
+ {step}
163
+ </Text>
164
+ <DnsRecord
165
+ copy={copy}
166
+ type="TXT"
167
+ name={relativeDnsName(domain.recordName, domain.hostname)}
168
+ values={domain.dnsRecords.verification.values}
169
+ fqdn={domain.recordName}
170
+ testId="domain-record"
171
+ />
172
+ </Stack>
173
+ );
174
+ }
175
+
176
+ /**
177
+ * Step 2 for a root + `www` pair: both names point here, a record for `@` and
178
+ * one for `www`, as the server built them, then the advice to delete the old
179
+ * site's records. Nothing when the deploy does not know its address.
180
+ */
181
+ export function PairPointingRecord({
182
+ domain,
183
+ dnsTargets,
184
+ numbered,
185
+ }: {
186
+ domain: TenantDomainItem;
187
+ dnsTargets: string[];
188
+ /** Step 2 of a pending pair; unnumbered when no step 1 is on screen (ACTIVE, pointing elsewhere). */
189
+ numbered: boolean;
190
+ }): JSX.Element | null {
191
+ const { copy: pack } = useDomainsScreen();
192
+ const phone = usePhoneCopy();
193
+ const { pointing } = domain.dnsRecords;
194
+ if (dnsTargets.length === 0 || pointing.length === 0) return null;
195
+ const several = new Set(pointing.map((record) => record.type)).size > 1;
196
+ return (
197
+ <Stack spacing={1} data-testid="domain-dns-target">
198
+ <Text variant="caption" size="xs">
199
+ {numbered ? '2. ' : ''}
200
+ {pack.pair.pointBothNames}
201
+ </Text>
202
+ {pointing.map((record) => (
203
+ <DnsRecord
204
+ key={`${record.type}-${record.name}`}
205
+ copy={pack.records}
206
+ type={record.type}
207
+ name={record.name}
208
+ values={record.values}
209
+ valueLabel={several ? pack.records.recordLabel(record.type) : undefined}
210
+ testId="domain-dns-target"
211
+ />
212
+ ))}
213
+ <Text variant="caption" size="xs" data-testid="domain-dns-replace">
214
+ {phone ? pack.pair.replaceOldRecordsPhone : pack.pair.replaceOldRecords}
215
+ </Text>
216
+ </Stack>
217
+ );
218
+ }
219
+
220
+ /**
221
+ * Both kinds of address, every time. A main domain usually still carries the
222
+ * old site's A records, which keep answering beside ours unless deleted; a
223
+ * subdomain is more often a CNAME, which a provider refuses to keep beside an
224
+ * A record. The row's own kind goes first, marked; the other kind is an
225
+ * offer, never an instruction, because telling a subdomain's owner to delete
226
+ * the main domain's A records takes their main site down.
227
+ */
228
+ function DnsKindGuide({ copy, hostname }: { copy: TenantDomainRecordsCopy; hostname?: string }): JSX.Element {
229
+ // No row yet: the field's own placeholder is the example.
230
+ const example = hostname ?? copy.placeholder;
231
+ const zone = dnsZoneOf(example);
232
+ const apex = isZoneApex(example);
233
+ const subdomain = apex ? `${copy.placeholder.split('.')[0]}.${zone}` : example;
234
+ const label = relativeDnsName(subdomain, subdomain);
235
+ // With no row yet both are instructions; with one, only the row's own kind is.
236
+ const teaches = (mine: boolean): boolean => hostname === undefined || mine;
237
+ const kinds = [
238
+ {
239
+ key: 'apex',
240
+ title: copy.kindApex,
241
+ mine: hostname !== undefined && apex,
242
+ text: teaches(hostname !== undefined && apex) ? copy.apexGuide(zone) : copy.otherApexGuide(zone),
243
+ },
244
+ {
245
+ key: 'subdomain',
246
+ title: copy.kindSubdomain,
247
+ mine: hostname !== undefined && !apex,
248
+ text: teaches(hostname !== undefined && !apex)
249
+ ? copy.subdomainGuide(subdomain, label)
250
+ : copy.otherSubdomainGuide(subdomain, label),
251
+ },
252
+ ].sort((left, right) => Number(right.mine) - Number(left.mine));
253
+ return (
254
+ <Stack spacing={0.5} data-testid="domain-dns-kinds">
255
+ {kinds.map((kind) => (
256
+ <Text
257
+ key={kind.key}
258
+ variant="caption"
259
+ size="xs"
260
+ // A long name has no break point but its dots.
261
+ style={{ overflowWrap: 'anywhere' }}
262
+ data-testid={`domain-dns-kind-${kind.key}`}
263
+ >
264
+ <Text as="span" variant="caption" size="xs" weight="semibold">
265
+ {kind.mine ? `${kind.title} (${copy.yourCase})` : kind.title}
266
+ </Text>
267
+ {` — ${kind.text}`}
268
+ </Text>
269
+ ))}
270
+ </Stack>
271
+ );
272
+ }
273
+
274
+ /**
275
+ * Where to point the domain, with the same copy-and-paste treatment as the
276
+ * TXT record: they are two steps of one job. Shown even with no domain yet,
277
+ * when somebody is about to configure DNS. Nothing when the deploy does not
278
+ * know its own address, rather than an empty field that reads as broken.
279
+ */
280
+ export function PointingRecord({
281
+ dnsTargets,
282
+ hostname,
283
+ numbered,
284
+ copy,
285
+ }: {
286
+ dnsTargets: string[];
287
+ /** The domain being pointed; absent in the empty state. */
288
+ hostname?: string;
289
+ numbered: boolean;
290
+ copy: TenantDomainRecordsCopy;
291
+ }): JSX.Element | null {
292
+ if (dnsTargets.length === 0) return null;
293
+ const types = dnsTargets.map(pointingRecordType);
294
+ const single = types.length === 1 ? types[0] : undefined;
295
+ // One record per type, in the deploy's order: an A with its IPv4s, an AAAA with its IPv6s.
296
+ const groups = [...new Set(types)].map((type) => [type, dnsTargets.filter((_, at) => types[at] === type)] as const);
297
+ return (
298
+ <Stack spacing={1} data-testid="domain-dns-target">
299
+ <Text variant="caption" size="xs">
300
+ {numbered ? '2. ' : ''}
301
+ {single ? copy.pointWithRecord(single) : copy.pointWithRecords}
302
+ </Text>
303
+ {groups.map(([type, values], index) => (
304
+ <DnsRecord
305
+ key={type}
306
+ copy={copy}
307
+ type={type}
308
+ name={hostname === undefined ? '@' : relativeDnsName(hostname, hostname)}
309
+ values={values}
310
+ // Two record types share one name: say the full name once.
311
+ fqdn={index === 0 ? hostname : undefined}
312
+ // With A and AAAA side by side, each value names its record.
313
+ valueLabel={single ? undefined : copy.recordLabel(type)}
314
+ testId="domain-dns-target"
315
+ />
316
+ ))}
317
+ <DnsKindGuide copy={copy} hostname={hostname} />
318
+ </Stack>
319
+ );
320
+ }
321
+
322
+ /**
323
+ * The advisory that closes the loop: ownership is proven, the address is not
324
+ * pointed here. Deliberately NOT an error: the domain is legitimately verified
325
+ * and ACTIVE, with one step left in somebody else's control panel.
326
+ */
327
+ export function PointingWarning({
328
+ domain,
329
+ copy,
330
+ }: {
331
+ domain: Pick<TenantDomainItem, 'dnsPointsHere'>;
332
+ copy: TenantDomainRecordsCopy;
333
+ }): JSX.Element | null {
334
+ if (domain.dnsPointsHere !== false) return null;
335
+ return (
336
+ <Alert variant="warning" data-testid="domain-not-pointed">
337
+ {copy.notPointedWarning}
338
+ </Alert>
339
+ );
340
+ }
341
+
342
+ /**
343
+ * What removing this domain costs. A removal archives the domain and frees its
344
+ * name, so both cases say it can be restored while nobody takes the name. A
345
+ * PENDING one never served; an ACTIVE one is the address people bookmarked,
346
+ * and getting it back means verifying the DNS again.
347
+ */
348
+ export function removalConsequence(
349
+ pending: boolean,
350
+ copy: { removeFromList: string; removeConsequence: string },
351
+ ): string {
352
+ return pending ? copy.removeFromList : copy.removeConsequence;
353
+ }
@@ -0,0 +1,133 @@
1
+ /**
2
+ * The removed list: each removed domain, who removed it and when, that its
3
+ * name is free, and the restore button, which works while nobody has taken the
4
+ * name; otherwise the refusal is told in that domain's row. Open, as drawn.
5
+ *
6
+ * The toggle is hand-built rather than the library's `CollapsibleTrigger`,
7
+ * which takes no `sx` (so no 44px row), paints the open state with a selected
8
+ * wash and pushes its children apart, and sets neither `type="button"` nor
9
+ * `aria-controls`.
10
+ */
11
+ import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
12
+ import { useState, type JSX } from 'react';
13
+
14
+ import { Button } from '@12-apps/ui/form/Button';
15
+ import { Collapsible } from '@12-apps/ui/layout/Collapsible';
16
+ import { Box } from '@12-apps/ui/mui/Box';
17
+ import { Stack } from '@12-apps/ui/mui/Stack';
18
+ import { sxRem } from '@12-apps/ui/tokens';
19
+ import { Text } from '@12-apps/ui/typography/Text';
20
+
21
+ import { useDomainsScreen, type TenantDomainItem } from './context';
22
+ import { domainName } from './records';
23
+ import { RowRefusal } from './row';
24
+
25
+ /** The day a domain was removed, as day and month in the pack's language. */
26
+ function removedOn(iso: string | null, locale: string): string {
27
+ if (iso === null) return '';
28
+ return new Intl.DateTimeFormat(locale, { day: '2-digit', month: '2-digit' }).format(new Date(iso));
29
+ }
30
+
31
+ /** One removed domain: its name, who removed it and when, a refused restore's reason, and the restore button. */
32
+ function RemovedRow({
33
+ domain,
34
+ busy,
35
+ refusal,
36
+ onDismissRefusal,
37
+ onRestore,
38
+ }: {
39
+ domain: TenantDomainItem;
40
+ busy: boolean;
41
+ refusal: string | null;
42
+ onDismissRefusal: () => void;
43
+ onRestore: () => void;
44
+ }): JSX.Element {
45
+ const pair = useDomainsScreen().copy.pair;
46
+ const refused = refusal !== null;
47
+ return (
48
+ <Stack
49
+ direction={refused ? 'column' : 'row'}
50
+ useFlexGap
51
+ spacing={refused ? 1 : 2}
52
+ sx={{ alignItems: refused ? 'flex-start' : 'center', justifyContent: 'space-between', flexWrap: 'wrap' }}
53
+ data-testid="domain-removed-row"
54
+ >
55
+ <Stack spacing={0.25} sx={{ minWidth: 0 }}>
56
+ <Text variant="body" size="sm" as="span" style={{ overflowWrap: 'anywhere' }}>
57
+ {domainName(domain, pair)}
58
+ </Text>
59
+ <Text variant="caption" size="xs" as="span">
60
+ {pair.removedLine(removedOn(domain.archivedAt, pair.locale), domain.archivedBy?.name ?? null)}
61
+ </Text>
62
+ </Stack>
63
+ {/* Told here, under the name and above the button tapped. */}
64
+ {refusal !== null ? <RowRefusal message={refusal} testId="domain-restore-error" onDismiss={onDismissRefusal} /> : null}
65
+ <Button variant="outline" color="primary" size="sm" disabled={busy} onClick={onRestore} dataTestId="domain-restore">
66
+ {pair.restore}
67
+ </Button>
68
+ </Stack>
69
+ );
70
+ }
71
+
72
+ export function RemovedList<D extends TenantDomainItem>({
73
+ removed,
74
+ isBusy,
75
+ refusalOf,
76
+ onDismissRefusal,
77
+ onRestore,
78
+ }: {
79
+ removed: D[];
80
+ isBusy: (domain: D) => boolean;
81
+ /** A refused restore's sentence for this domain, or null. */
82
+ refusalOf: (domain: D) => string | null;
83
+ onDismissRefusal: (domain: D) => void;
84
+ onRestore: (domain: D) => void;
85
+ }): JSX.Element | null {
86
+ const pair = useDomainsScreen().copy.pair;
87
+ const [open, setOpen] = useState(true);
88
+ if (removed.length === 0) return null;
89
+ return (
90
+ <Box sx={{ border: 1, borderColor: 'divider', borderRadius: 1, px: 2, py: 0.5 }} data-testid="domains-removed">
91
+ <Box
92
+ component="button"
93
+ type="button"
94
+ aria-expanded={open}
95
+ aria-controls="domains-removed-list"
96
+ onClick={() => setOpen(!open)}
97
+ data-testid="domains-removed-toggle"
98
+ sx={{
99
+ display: 'flex',
100
+ alignItems: 'center',
101
+ gap: 1,
102
+ width: '100%',
103
+ minHeight: sxRem(44),
104
+ p: 0,
105
+ border: 0,
106
+ bgcolor: 'transparent',
107
+ color: 'text.primary',
108
+ font: 'inherit',
109
+ fontWeight: 600,
110
+ cursor: 'pointer',
111
+ textAlign: 'left',
112
+ }}
113
+ >
114
+ <ExpandMoreIcon fontSize="small" sx={{ transform: open ? 'none' : 'rotate(-90deg)', transition: 'transform .15s' }} />
115
+ {pair.removedTitle(removed.length)}
116
+ </Box>
117
+ <Collapsible open={open} keepMounted>
118
+ <Stack id="domains-removed-list" spacing={1.5} sx={{ pb: 1.5 }}>
119
+ {removed.map((domain) => (
120
+ <RemovedRow
121
+ key={domain.id}
122
+ domain={domain}
123
+ busy={isBusy(domain)}
124
+ refusal={refusalOf(domain)}
125
+ onDismissRefusal={() => onDismissRefusal(domain)}
126
+ onRestore={() => onRestore(domain)}
127
+ />
128
+ ))}
129
+ </Stack>
130
+ </Collapsible>
131
+ </Box>
132
+ );
133
+ }