@fabrika/cli 0.3.3 → 0.4.1

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,586 @@
1
+ // What `fabrika app dev` shows a person: the caller picker page, and the toolbar script it injects into every SPA page.
2
+ // Both are self-contained (no font, no CDN) and take their colors from `--fd-*` custom properties, light and dark.
3
+ // Every value here comes from the seed, the vocabulary or the request, so the page escapes it and the script only ever
4
+ // sets `textContent` and `href`.
5
+
6
+ import type { Locale } from '@fabrika/auth-core'
7
+
8
+ /** A label beside a caller: a role it holds (with the scope value it holds it in), or that it is external. */
9
+ export interface Badge {
10
+ readonly text: string
11
+ readonly tone: 'role' | 'external'
12
+ }
13
+
14
+ /** Who the browser is now, as the picker's header and the toolbar's pill name it. */
15
+ export interface CurrentView {
16
+ readonly name: string
17
+ readonly badges: readonly Badge[]
18
+ }
19
+
20
+ export interface PersonaView {
21
+ readonly name: string
22
+ readonly description: string | null
23
+ readonly email: string
24
+ readonly badges: readonly Badge[]
25
+ readonly href: string
26
+ readonly current: boolean
27
+ }
28
+
29
+ /** A role of the vocabulary: chosen with one link when it is app-wide, or with a scope value of its dimension. */
30
+ export interface RoleView {
31
+ readonly role: string
32
+ readonly grants: readonly string[]
33
+ readonly external: boolean
34
+ readonly choose:
35
+ | { readonly kind: 'app'; readonly href: string }
36
+ | {
37
+ readonly kind: 'member'
38
+ readonly action: string
39
+ readonly next: string
40
+ readonly dimension: string
41
+ readonly values: readonly { readonly value: string; readonly label: string }[]
42
+ }
43
+ }
44
+
45
+ export interface LinkView {
46
+ readonly title: string
47
+ readonly description: string | null
48
+ readonly href: string
49
+ }
50
+
51
+ export interface MailView {
52
+ readonly to: string
53
+ readonly subject: string
54
+ /** An invitation's state, or `sent` for a message of the dev outbox. */
55
+ readonly status: 'open' | 'accepted' | 'cancelled' | 'expired' | 'sent'
56
+ readonly action: { readonly label: string; readonly href: string } | null
57
+ }
58
+
59
+ export interface DevView {
60
+ readonly locale: Locale
61
+ /** The application's display name, or its id. */
62
+ readonly title: string
63
+ readonly current: CurrentView | null
64
+ readonly personas: readonly PersonaView[]
65
+ readonly roles: readonly RoleView[]
66
+ readonly anonymousHref: string
67
+ readonly links: readonly LinkView[]
68
+ readonly mail: readonly MailView[]
69
+ readonly pickerHref: string
70
+ }
71
+
72
+ interface Strings {
73
+ readonly preview: string
74
+ readonly choose: string
75
+ readonly lede: string
76
+ readonly now: string
77
+ readonly nobody: string
78
+ readonly personas: string
79
+ readonly noPersonas: string
80
+ readonly another: string
81
+ readonly appWide: string
82
+ readonly inScope: (dimension: string) => string
83
+ readonly external: string
84
+ readonly use: string
85
+ readonly scopePlaceholder: (dimension: string) => string
86
+ readonly anonymous: string
87
+ readonly anonymousText: string
88
+ readonly anonymousButton: string
89
+ readonly links: string
90
+ readonly linksText: string
91
+ readonly openLink: string
92
+ readonly mail: string
93
+ readonly mailText: string
94
+ readonly noMail: string
95
+ readonly invitation: (role: string, scope: string) => string
96
+ readonly accept: string
97
+ readonly continueAs: string
98
+ readonly openMessage: string
99
+ readonly status: Readonly<Record<MailView['status'], string>>
100
+ readonly to: string
101
+ readonly picker: string
102
+ readonly close: string
103
+ readonly back: string
104
+ }
105
+
106
+ const STRINGS: Readonly<Record<Locale, Strings>> = {
107
+ en: {
108
+ preview: 'Development preview',
109
+ choose: 'Choose who you are',
110
+ lede: 'Each choice is a caller of the stand-in IAM. The data lives in a throwaway schema and is gone when app dev stops.',
111
+ now: 'You are',
112
+ nobody: 'Nobody (anonymous)',
113
+ personas: 'Personas',
114
+ noPersonas: 'The seed made no personas. Return them from dev/seed.ts.',
115
+ another: 'Another role',
116
+ appWide: 'app-wide',
117
+ inScope: (dimension) => `in one ${dimension}`,
118
+ external: 'external',
119
+ use: 'Use',
120
+ scopePlaceholder: (dimension) => `${dimension} value`,
121
+ anonymous: 'Anonymous',
122
+ anonymousText: 'A page that needs a sign-in brings you back here.',
123
+ anonymousButton: 'Continue anonymously',
124
+ links: 'Link pages',
125
+ linksText: 'Each opens as the holder of a link the application issued.',
126
+ openLink: 'Open as link holder',
127
+ mail: 'Mail',
128
+ mailText: 'The invitations the stand-in IAM would have e-mailed, and what the application sent to the dev outbox.',
129
+ noMail: 'Nothing has been sent yet.',
130
+ invitation: (role, scope) => `Invitation: ${role} in ${scope}`,
131
+ accept: 'Accept invitation',
132
+ continueAs: 'Continue as this person',
133
+ openMessage: 'Open the message',
134
+ status: { open: 'waiting', accepted: 'accepted', cancelled: 'cancelled', expired: 'expired', sent: 'sent' },
135
+ to: 'To',
136
+ picker: 'Choose another caller',
137
+ close: 'Close',
138
+ back: 'Back to the picker',
139
+ },
140
+ cs: {
141
+ preview: 'Vývojový náhled',
142
+ choose: 'Vyberte, kdo jste',
143
+ lede: 'Každá volba je volající zástupného IAM. Data leží v dočasném schématu a zmizí, když app dev skončí.',
144
+ now: 'Jste',
145
+ nobody: 'Nikdo (anonymně)',
146
+ personas: 'Persony',
147
+ noPersonas: 'Seed nevytvořil žádné persony. Vraťte je z dev/seed.ts.',
148
+ another: 'Jiná role',
149
+ appWide: 'v celé aplikaci',
150
+ inScope: (dimension) => `v jedné hodnotě ${dimension}`,
151
+ external: 'externí',
152
+ use: 'Použít',
153
+ scopePlaceholder: (dimension) => `hodnota ${dimension}`,
154
+ anonymous: 'Anonymně',
155
+ anonymousText: 'Stránka, která vyžaduje přihlášení, vás vrátí sem.',
156
+ anonymousButton: 'Pokračovat anonymně',
157
+ links: 'Stránky přes odkaz',
158
+ linksText: 'Každá se otevře jako držitel odkazu, který aplikace vydala.',
159
+ openLink: 'Otevřít jako držitel odkazu',
160
+ mail: 'Pošta',
161
+ mailText: 'Pozvánky, které by zástupný IAM odeslal e-mailem, a co aplikace poslala do vývojové schránky.',
162
+ noMail: 'Zatím nebylo nic odesláno.',
163
+ invitation: (role, scope) => `Pozvánka: ${role} v ${scope}`,
164
+ accept: 'Přijmout pozvánku',
165
+ continueAs: 'Pokračovat jako tato osoba',
166
+ openMessage: 'Otevřít zprávu',
167
+ status: { open: 'čeká', accepted: 'přijata', cancelled: 'zrušena', expired: 'vypršela', sent: 'odesláno' },
168
+ to: 'Komu',
169
+ picker: 'Vybrat jiného volajícího',
170
+ close: 'Zavřít',
171
+ back: 'Zpět na výběr',
172
+ },
173
+ }
174
+
175
+ export const stringsOf = (locale: Locale): Strings => STRINGS[locale]
176
+
177
+ /** The palette: neutral greys and one accent, redefined for a dark color scheme. */
178
+ const TOKENS = `
179
+ --fd-bg: #f6f6f4;
180
+ --fd-surface: #ffffff;
181
+ --fd-surface-2: #f1f1ee;
182
+ --fd-border: #e2e2dd;
183
+ --fd-border-strong: #cdcdc6;
184
+ --fd-text: #1d1d1b;
185
+ --fd-muted: #686862;
186
+ --fd-accent: #3f51b5;
187
+ --fd-accent-soft: #e9ebf8;
188
+ --fd-on-accent: #ffffff;
189
+ --fd-ring: rgba(63, 81, 181, 0.35);
190
+ --fd-warn: #8a5a00;
191
+ --fd-warn-soft: #fbf0db;
192
+ --fd-shadow: 0 1px 2px rgba(20, 20, 18, 0.06), 0 8px 24px rgba(20, 20, 18, 0.1);
193
+ --fd-font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
194
+ --fd-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
195
+ `
196
+ const DARK_TOKENS = `
197
+ --fd-bg: #131312;
198
+ --fd-surface: #1c1c1b;
199
+ --fd-surface-2: #252524;
200
+ --fd-border: #2f2f2d;
201
+ --fd-border-strong: #42423f;
202
+ --fd-text: #ecece8;
203
+ --fd-muted: #a2a29b;
204
+ --fd-accent: #93a0f2;
205
+ --fd-accent-soft: rgba(147, 160, 242, 0.14);
206
+ --fd-on-accent: #131312;
207
+ --fd-ring: rgba(147, 160, 242, 0.4);
208
+ --fd-warn: #e9bb66;
209
+ --fd-warn-soft: rgba(233, 187, 102, 0.13);
210
+ --fd-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 28px rgba(0, 0, 0, 0.45);
211
+ `
212
+
213
+ const PICKER_CSS = `
214
+ :root {${TOKENS} color-scheme: light dark;
215
+ }
216
+ @media (prefers-color-scheme: dark) {
217
+ :root {${DARK_TOKENS} }
218
+ }
219
+ * { box-sizing: border-box; }
220
+ body { margin: 0; background: var(--fd-bg); color: var(--fd-text); font: 15px/1.5 var(--fd-font); -webkit-font-smoothing: antialiased; }
221
+ a { color: inherit; }
222
+ .bar { border-bottom: 1px solid var(--fd-border); background: var(--fd-surface); }
223
+ .bar-inner, main { max-width: 960px; margin: 0 auto; padding-left: 16px; padding-right: 16px; }
224
+ .bar-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 52px; flex-wrap: wrap; padding-top: 8px; padding-bottom: 8px; }
225
+ .brand { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; letter-spacing: 0.01em; }
226
+ .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--fd-accent); flex: none; }
227
+ .brand .tag { color: var(--fd-muted); font-weight: 500; }
228
+ .now { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--fd-muted); }
229
+ .now strong { color: var(--fd-text); font-weight: 600; }
230
+ main { padding-top: 40px; padding-bottom: 72px; }
231
+ h1 { margin: 0; font-size: 28px; line-height: 1.2; font-weight: 650; letter-spacing: -0.01em; }
232
+ .lede { margin: 8px 0 0; color: var(--fd-muted); max-width: 640px; }
233
+ section { margin-top: 40px; }
234
+ h2 { margin: 0 0 4px; font-size: 13px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--fd-muted); }
235
+ .hint { margin: 0 0 14px; font-size: 14px; color: var(--fd-muted); }
236
+ h2 + .grid, h2 + .rows { margin-top: 14px; }
237
+ .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
238
+ .card { display: flex; flex-direction: column; gap: 4px; padding: 16px; border: 1px solid var(--fd-border); border-radius: 12px; background: var(--fd-surface); color: inherit; text-decoration: none; transition: border-color 0.15s, box-shadow 0.15s; }
239
+ .card:hover { border-color: var(--fd-border-strong); }
240
+ .card[aria-current='true'] { border-color: var(--fd-accent); box-shadow: inset 0 0 0 1px var(--fd-accent); }
241
+ .card .name { font-weight: 600; font-size: 16px; }
242
+ .card .email { font-size: 13px; color: var(--fd-muted); overflow-wrap: anywhere; }
243
+ .card .desc { margin-top: 4px; font-size: 14px; }
244
+ .card .cta { margin-top: 10px; font-size: 13px; font-weight: 600; color: var(--fd-accent); }
245
+ .badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
246
+ .badge { display: inline-flex; align-items: center; height: 22px; padding: 0 9px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--fd-surface-2); color: var(--fd-muted); border: 1px solid var(--fd-border); white-space: nowrap; }
247
+ .badge.role { background: var(--fd-accent-soft); color: var(--fd-accent); border-color: transparent; }
248
+ .badge.external { background: var(--fd-warn-soft); color: var(--fd-warn); border-color: transparent; }
249
+ .rows { border: 1px solid var(--fd-border); border-radius: 12px; background: var(--fd-surface); overflow: hidden; }
250
+ .row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 12px 16px; margin: 0; }
251
+ .row + .row { border-top: 1px solid var(--fd-border); }
252
+ .row .role { font-weight: 600; min-width: 96px; }
253
+ .row .meta { flex: 1 1 220px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 13px; color: var(--fd-muted); }
254
+ .row .meta .grants { font-family: var(--fd-mono); font-size: 12px; overflow-wrap: anywhere; }
255
+ .row .control { display: flex; gap: 8px; align-items: center; }
256
+ .row .meta.stack { flex-direction: column; align-items: flex-start; gap: 2px; }
257
+ .actions { margin-top: 24px; }
258
+ .subject { font-weight: 600; color: var(--fd-text); font-size: 15px; }
259
+ .to { font-size: 13px; color: var(--fd-muted); overflow-wrap: anywhere; }
260
+ input { font: inherit; font-size: 14px; height: 34px; padding: 0 10px; width: 200px; border: 1px solid var(--fd-border-strong); border-radius: 8px; background: var(--fd-bg); color: var(--fd-text); }
261
+ input::placeholder { color: var(--fd-muted); }
262
+ .button { display: inline-flex; align-items: center; justify-content: center; height: 34px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--fd-accent); background: var(--fd-accent); color: var(--fd-on-accent); font: inherit; font-size: 14px; font-weight: 600; text-decoration: none; cursor: pointer; white-space: nowrap; }
263
+ .button.quiet { background: transparent; color: var(--fd-text); border-color: var(--fd-border-strong); }
264
+ .button.quiet:hover { background: var(--fd-surface-2); }
265
+ .empty { padding: 16px; border: 1px dashed var(--fd-border-strong); border-radius: 12px; color: var(--fd-muted); font-size: 14px; }
266
+ a:focus-visible, button:focus-visible, input:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--fd-ring); border-color: var(--fd-accent); }
267
+ @media (max-width: 560px) {
268
+ main { padding-top: 28px; }
269
+ h1 { font-size: 24px; }
270
+ .row .control { flex: 1 1 100%; }
271
+ .row .control input { flex: 1; width: auto; min-width: 0; }
272
+ }
273
+ `
274
+
275
+ const escapeHtml = (text: string): string =>
276
+ text.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;').replaceAll("'", '&#39;')
277
+
278
+ const badges = (list: readonly Badge[]): string =>
279
+ list.length === 0
280
+ ? ''
281
+ : `<div class="badges">${list.map((badge) => `<span class="badge ${badge.tone}">${escapeHtml(badge.text)}</span>`).join('')}</div>`
282
+
283
+ function page(view: Pick<DevView, 'locale' | 'title' | 'current'>, heading: string, body: string): string {
284
+ const text = stringsOf(view.locale)
285
+ const now = view.current === null
286
+ ? `<span>${escapeHtml(text.now)}:</span> <strong>${escapeHtml(text.nobody)}</strong>`
287
+ : `<span>${escapeHtml(text.now)}:</span> <strong>${escapeHtml(view.current.name)}</strong>${
288
+ view.current.badges.map((badge) => `<span class="badge ${badge.tone}">${escapeHtml(badge.text)}</span>`).join('')
289
+ }`
290
+ return `<!doctype html>
291
+ <html lang="${view.locale}">
292
+ <head>
293
+ <meta charset="utf-8">
294
+ <meta name="viewport" content="width=device-width, initial-scale=1">
295
+ <title>${escapeHtml(view.title)} · ${escapeHtml(text.preview)}</title>
296
+ <style>${PICKER_CSS}</style>
297
+ </head>
298
+ <body>
299
+ <header class="bar"><div class="bar-inner"><span class="brand"><span class="dot"></span>fabrika app dev <span class="tag">${
300
+ escapeHtml(text.preview)
301
+ }</span></span><span class="now">${now}</span></div></header>
302
+ <main>
303
+ <h1>${escapeHtml(heading)}</h1>
304
+ ${body}
305
+ </main>
306
+ </body>
307
+ </html>
308
+ `
309
+ }
310
+
311
+ function personaCard(persona: PersonaView): string {
312
+ return `<a class="card" href="${escapeHtml(persona.href)}" aria-current="${persona.current ? 'true' : 'false'}"><span class="name">${
313
+ escapeHtml(persona.name)
314
+ }</span><span class="email">${escapeHtml(persona.email)}</span>${
315
+ persona.description === null ? '' : `<span class="desc">${escapeHtml(persona.description)}</span>`
316
+ }${badges(persona.badges)}</a>`
317
+ }
318
+
319
+ function roleRow(role: RoleView, text: Strings): string {
320
+ const meta = [
321
+ role.choose.kind === 'app' ? text.appWide : text.inScope(role.choose.dimension),
322
+ ...(role.external ? [`<span class="badge external">${escapeHtml(text.external)}</span>`] : []),
323
+ ]
324
+ const describe = `<span class="role">${escapeHtml(role.role)}</span><span class="meta"><span>${escapeHtml(meta[0] ?? '')}</span>${
325
+ meta.slice(1).join('')
326
+ }<span class="grants">${escapeHtml(role.grants.join(', '))}</span></span>`
327
+ if (role.choose.kind === 'app') {
328
+ return `<div class="row">${describe}<span class="control"><a class="button quiet" href="${escapeHtml(role.choose.href)}">${
329
+ escapeHtml(text.use)
330
+ }</a></span></div>`
331
+ }
332
+ const list = `values-${role.role.replaceAll(/[^A-Za-z0-9_-]/g, '_')}`
333
+ const options = role.choose.values.map((value) => `<option value="${escapeHtml(value.value)}">${escapeHtml(value.label)}</option>`).join('')
334
+ return `<form class="row" method="get" action="${
335
+ escapeHtml(role.choose.action)
336
+ }">${describe}<span class="control"><input type="hidden" name="role" value="${escapeHtml(role.role)}"><input type="hidden" name="next" value="${
337
+ escapeHtml(role.choose.next)
338
+ }"><input name="scope" required maxlength="200" list="${list}" aria-label="${
339
+ escapeHtml(text.scopePlaceholder(role.choose.dimension))
340
+ }" placeholder="${
341
+ escapeHtml(text.scopePlaceholder(role.choose.dimension))
342
+ }"><datalist id="${list}">${options}</datalist><button class="button quiet" type="submit">${escapeHtml(text.use)}</button></span></form>`
343
+ }
344
+
345
+ function mailRows(mail: readonly MailView[], text: Strings): string {
346
+ if (mail.length === 0) return `<div class="empty">${escapeHtml(text.noMail)}</div>`
347
+ return `<div class="rows">${
348
+ mail.map((item) =>
349
+ `<div class="row"><span class="meta stack"><span class="subject">${escapeHtml(item.subject)}</span><span class="to">${escapeHtml(text.to)}: ${
350
+ escapeHtml(item.to)
351
+ }</span></span><span class="badge">${escapeHtml(text.status[item.status])}</span>${
352
+ item.action === null
353
+ ? ''
354
+ : `<span class="control"><a class="button quiet" href="${escapeHtml(item.action.href)}">${escapeHtml(item.action.label)}</a></span>`
355
+ }</div>`
356
+ ).join('')
357
+ }</div>`
358
+ }
359
+
360
+ /** The picker: the personas, another role, anonymous, the link pages, and the mail IAM sent. */
361
+ export function pickerPage(view: DevView): string {
362
+ const text = stringsOf(view.locale)
363
+ const personas = view.personas.length === 0
364
+ ? `<div class="empty">${escapeHtml(text.noPersonas)}</div>`
365
+ : `<div class="grid">${view.personas.map(personaCard).join('')}</div>`
366
+ const links = view.links.length === 0
367
+ ? ''
368
+ : `<section aria-labelledby="links"><h2 id="links">${escapeHtml(text.links)}</h2><p class="hint">${
369
+ escapeHtml(text.linksText)
370
+ }</p><div class="grid">${
371
+ view.links.map((link) =>
372
+ `<a class="card" href="${escapeHtml(link.href)}"><span class="name">${escapeHtml(link.title)}</span>${
373
+ link.description === null ? '' : `<span class="desc">${escapeHtml(link.description)}</span>`
374
+ }<span class="cta">${escapeHtml(text.openLink)} →</span></a>`
375
+ ).join('')
376
+ }</div></section>`
377
+ const body = `<p class="lede">${escapeHtml(text.lede)}</p>
378
+ <section aria-labelledby="personas"><h2 id="personas">${escapeHtml(text.personas)}</h2>${personas}</section>
379
+ <section aria-labelledby="roles"><h2 id="roles">${escapeHtml(text.another)}</h2><div class="rows">${
380
+ view.roles.map((role) => roleRow(role, text)).join('')
381
+ }</div></section>
382
+ <section aria-labelledby="anonymous"><h2 id="anonymous">${escapeHtml(text.anonymous)}</h2><p class="hint">${
383
+ escapeHtml(text.anonymousText)
384
+ }</p><a class="button quiet" href="${escapeHtml(view.anonymousHref)}">${escapeHtml(text.anonymousButton)}</a></section>
385
+ ${links}
386
+ <section aria-labelledby="mail"><h2 id="mail">${escapeHtml(text.mail)}</h2><p class="hint">${escapeHtml(text.mailText)}</p>${
387
+ mailRows(view.mail, text)
388
+ }</section>`
389
+ return page(view, `${view.title}: ${text.choose.toLowerCase()}`, body)
390
+ }
391
+
392
+ /** A refusal of a dev route, with the way back to the picker. */
393
+ export function noticePage(view: Pick<DevView, 'locale' | 'title' | 'current' | 'pickerHref'>, heading: string, message: string): string {
394
+ const text = stringsOf(view.locale)
395
+ return page(
396
+ view,
397
+ heading,
398
+ `<p class="lede">${escapeHtml(message)}</p><p class="actions"><a class="button" href="${escapeHtml(view.pickerHref)}">${
399
+ escapeHtml(text.back)
400
+ }</a></p>`,
401
+ )
402
+ }
403
+
404
+ /** What the toolbar renders: its labels and the view, with every caller already put into words. */
405
+ export function toolbarState(view: DevView): unknown {
406
+ const text = stringsOf(view.locale)
407
+ const detail = (list: readonly Badge[]) => list.map((badge) => badge.text).join(', ')
408
+ return {
409
+ labels: {
410
+ now: text.now,
411
+ personas: text.personas,
412
+ links: text.links,
413
+ mail: text.mail,
414
+ noMail: text.noMail,
415
+ picker: text.picker,
416
+ close: text.close,
417
+ preview: text.preview,
418
+ },
419
+ title: view.title,
420
+ current: view.current === null ? { name: text.nobody, detail: '' } : { name: view.current.name, detail: detail(view.current.badges) },
421
+ personas: view.personas.map((persona) => ({ name: persona.name, detail: detail(persona.badges), href: persona.href, current: persona.current })),
422
+ links: view.links.map((link) => ({ title: link.title, detail: link.description ?? '', href: link.href })),
423
+ mail: view.mail.map((item) => ({ subject: item.subject, detail: `${text.to}: ${item.to} · ${text.status[item.status]}`, action: item.action })),
424
+ picker: view.pickerHref,
425
+ }
426
+ }
427
+
428
+ const TOOLBAR_CSS = `
429
+ :host { all: initial; }
430
+ .fd { position: fixed; right: 16px; bottom: 16px; z-index: 2147483647; font: 13px/1.4 var(--fd-font); color: var(--fd-text); text-align: left; }
431
+ .pill { display: inline-flex; align-items: center; gap: 8px; max-width: min(360px, calc(100vw - 32px)); height: 36px; padding: 0 14px 0 12px; border-radius: 999px; border: 1px solid var(--fd-border); background: var(--fd-surface); color: var(--fd-text); box-shadow: var(--fd-shadow); font: inherit; cursor: pointer; }
432
+ .pill:hover { border-color: var(--fd-border-strong); }
433
+ .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--fd-accent); flex: none; }
434
+ .who { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
435
+ .what { color: var(--fd-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
436
+ .panel { position: absolute; right: 0; bottom: 46px; width: 340px; max-width: calc(100vw - 32px); max-height: min(72vh, 600px); overflow: auto; background: var(--fd-surface); border: 1px solid var(--fd-border); border-radius: 14px; box-shadow: var(--fd-shadow); }
437
+ .panel[hidden] { display: none; }
438
+ .head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 12px 12px 16px; border-bottom: 1px solid var(--fd-border); }
439
+ .head .title { font-weight: 600; font-size: 14px; }
440
+ .head .tag { display: block; font-size: 12px; color: var(--fd-muted); }
441
+ .close { width: 28px; height: 28px; border-radius: 8px; border: 1px solid transparent; background: transparent; color: var(--fd-muted); font: inherit; font-size: 18px; line-height: 1; cursor: pointer; }
442
+ .close:hover { background: var(--fd-surface-2); color: var(--fd-text); }
443
+ .section { padding: 10px 8px; }
444
+ .section + .section { border-top: 1px solid var(--fd-border); }
445
+ .label { padding: 2px 8px 6px; font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--fd-muted); }
446
+ .item { display: flex; flex-direction: column; gap: 1px; padding: 8px; border-radius: 8px; color: var(--fd-text); text-decoration: none; }
447
+ a.item:hover { background: var(--fd-surface-2); }
448
+ .item[aria-current='true'] { background: var(--fd-accent-soft); }
449
+ .item .name { font-weight: 600; }
450
+ .item[aria-current='true'] .name { color: var(--fd-accent); }
451
+ .item .sub { font-size: 12px; color: var(--fd-muted); overflow-wrap: anywhere; }
452
+ .item .act { margin-top: 4px; font-size: 12px; font-weight: 600; color: var(--fd-accent); }
453
+ .empty { padding: 4px 8px 6px; color: var(--fd-muted); }
454
+ .more { display: block; margin-top: 2px; padding: 8px; border-radius: 8px; color: var(--fd-accent); font-weight: 600; text-decoration: none; }
455
+ .more:hover { background: var(--fd-surface-2); }
456
+ a:focus-visible, button:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--fd-ring); }
457
+ `
458
+
459
+ /** The toolbar: a pill in the bottom corner, in a shadow root so neither the page's styles nor its layout meet it. */
460
+ export function toolbarScript(statePath: string): string {
461
+ const css = `:host {${TOKENS}}\n@media (prefers-color-scheme: dark) { :host {${DARK_TOKENS}} }\n${TOOLBAR_CSS}`
462
+ return `// fabrika app dev: the dev toolbar. Served by app dev only, never by the kit's tests or a deploy.
463
+ (() => {
464
+ 'use strict'
465
+ if (document.querySelector('fabrika-dev-toolbar') !== null) return
466
+ const CSS = ${JSON.stringify(css)}
467
+ const STATE = ${JSON.stringify(statePath)}
468
+
469
+ const node = (tag, className, text) => {
470
+ const element = document.createElement(tag)
471
+ if (className) element.className = className
472
+ if (text !== undefined) element.textContent = text
473
+ return element
474
+ }
475
+ const link = (href, className) => {
476
+ const element = node('a', className)
477
+ element.href = href
478
+ return element
479
+ }
480
+ const section = (label) => {
481
+ const element = node('div', 'section')
482
+ element.append(node('div', 'label', label))
483
+ return element
484
+ }
485
+ const entry = (href, name, sub, current) => {
486
+ const element = href === null ? node('div', 'item') : link(href, 'item')
487
+ element.append(node('span', 'name', name))
488
+ if (sub) element.append(node('span', 'sub', sub))
489
+ if (current) element.setAttribute('aria-current', 'true')
490
+ return element
491
+ }
492
+
493
+ const mount = (state) => {
494
+ const host = document.createElement('fabrika-dev-toolbar')
495
+ const root = host.attachShadow({ mode: 'open' })
496
+ const style = document.createElement('style')
497
+ style.textContent = CSS
498
+ const frame = node('div', 'fd')
499
+ const pill = node('button', 'pill')
500
+ pill.type = 'button'
501
+ pill.setAttribute('aria-haspopup', 'true')
502
+ pill.setAttribute('aria-expanded', 'false')
503
+ pill.setAttribute('aria-label', 'fabrika app dev · ' + state.labels.now + ': ' + state.current.name)
504
+ pill.append(node('span', 'dot'), node('span', 'who', state.current.name))
505
+ if (state.current.detail) pill.append(node('span', 'what', state.current.detail))
506
+
507
+ const panel = node('div', 'panel')
508
+ panel.hidden = true
509
+ panel.id = 'fabrika-dev-panel'
510
+ panel.setAttribute('role', 'dialog')
511
+ panel.setAttribute('aria-label', 'fabrika app dev')
512
+ pill.setAttribute('aria-controls', panel.id)
513
+ const head = node('div', 'head')
514
+ const heading = node('div')
515
+ heading.append(node('span', 'title', state.title), node('span', 'tag', 'fabrika app dev · ' + state.labels.preview))
516
+ const close = node('button', 'close', '×')
517
+ close.type = 'button'
518
+ close.setAttribute('aria-label', state.labels.close)
519
+ head.append(heading, close)
520
+ panel.append(head)
521
+
522
+ const personas = section(state.labels.personas)
523
+ for (const persona of state.personas) personas.append(entry(persona.href, persona.name, persona.detail, persona.current))
524
+ const more = link(state.picker, 'more')
525
+ more.textContent = state.labels.picker + ' →'
526
+ personas.append(more)
527
+ panel.append(personas)
528
+
529
+ if (state.links.length > 0) {
530
+ const links = section(state.labels.links)
531
+ for (const item of state.links) links.append(entry(item.href, item.title, item.detail, false))
532
+ panel.append(links)
533
+ }
534
+
535
+ const mail = section(state.labels.mail)
536
+ if (state.mail.length === 0) mail.append(node('div', 'empty', state.labels.noMail))
537
+ for (const item of state.mail) {
538
+ const element = entry(item.action === null ? null : item.action.href, item.subject, item.detail, false)
539
+ if (item.action !== null) element.append(node('span', 'act', item.action.label + ' →'))
540
+ mail.append(element)
541
+ }
542
+ panel.append(mail)
543
+
544
+ const open = (opened) => {
545
+ panel.hidden = !opened
546
+ pill.setAttribute('aria-expanded', String(opened))
547
+ }
548
+ pill.addEventListener('click', () => open(panel.hidden))
549
+ close.addEventListener('click', () => {
550
+ open(false)
551
+ pill.focus()
552
+ })
553
+ document.addEventListener('keydown', (event) => {
554
+ if (event.key === 'Escape' && !panel.hidden) {
555
+ open(false)
556
+ pill.focus()
557
+ }
558
+ })
559
+ document.addEventListener('pointerdown', (event) => {
560
+ if (!panel.hidden && !event.composedPath().includes(host)) open(false)
561
+ })
562
+ frame.append(panel, pill)
563
+ root.append(style, frame)
564
+ document.documentElement.append(host)
565
+ }
566
+
567
+ const start = async () => {
568
+ const next = location.pathname + location.search
569
+ const response = await fetch(STATE + '?next=' + encodeURIComponent(next), { credentials: 'same-origin', headers: { Accept: 'application/json' } })
570
+ if (response.ok) mount(await response.json())
571
+ }
572
+ const run = () => {
573
+ start().catch(() => {})
574
+ }
575
+ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run)
576
+ else run()
577
+ })()
578
+ `
579
+ }
580
+
581
+ /** `html` with the toolbar's script tag before its `</body>`, or at its end when it has none. */
582
+ export function withToolbar(html: string, scriptPath: string): string {
583
+ const tag = `<script src="${escapeHtml(scriptPath)}" defer></script>`
584
+ const at = html.toLowerCase().lastIndexOf('</body>')
585
+ return at < 0 ? `${html}${tag}` : `${html.slice(0, at)}${tag}${html.slice(at)}`
586
+ }