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