@milesroxas/design-hub 0.13.0 → 0.15.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 (101) hide show
  1. package/README.md +38 -33
  2. package/core/catalog.test.ts +28 -1
  3. package/core/catalog.ts +21 -4
  4. package/core/prototype.test.ts +12 -1
  5. package/core/prototype.ts +21 -2
  6. package/core/review/model.ts +71 -6
  7. package/core/review/service.test.ts +536 -1
  8. package/core/review/service.ts +452 -25
  9. package/core/review/store.ts +45 -4
  10. package/dist/core/catalog.d.ts +8 -1
  11. package/dist/core/catalog.js +12 -3
  12. package/dist/core/prototype.d.ts +20 -2
  13. package/dist/core/prototype.js +17 -2
  14. package/dist/core/review/model.d.ts +42 -3
  15. package/dist/core/review/model.js +52 -3
  16. package/dist/core/review/service.d.ts +81 -7
  17. package/dist/core/review/service.js +407 -23
  18. package/dist/core/review/store.d.ts +29 -2
  19. package/dist/core/review/store.js +4 -0
  20. package/dist/scripts/client.d.ts +1 -1
  21. package/dist/scripts/client.js +1 -1
  22. package/dist/scripts/lib/catalog.d.ts +1 -1
  23. package/dist/scripts/lib/catalog.js +3 -3
  24. package/dist/scripts/lib/dns.d.ts +16 -2
  25. package/dist/scripts/lib/dns.js +66 -3
  26. package/dist/scripts/lib/fake-host.d.ts +16 -1
  27. package/dist/scripts/lib/fake-host.js +36 -1
  28. package/dist/scripts/lib/host.d.ts +2 -0
  29. package/dist/scripts/lib/resend.d.ts +36 -0
  30. package/dist/scripts/lib/resend.js +66 -0
  31. package/dist/scripts/lib/studio-deps.d.ts +7 -0
  32. package/dist/scripts/lib/studio-deps.js +6 -1
  33. package/dist/scripts/lib/studios.d.ts +2 -2
  34. package/dist/scripts/lib/studios.js +5 -5
  35. package/dist/scripts/lib/vercel-host.js +6 -0
  36. package/dist/scripts/new.d.ts +8 -3
  37. package/dist/scripts/new.js +15 -4
  38. package/dist/scripts/people.d.ts +16 -4
  39. package/dist/scripts/people.js +67 -17
  40. package/dist/scripts/preflight.js +1 -1
  41. package/dist/scripts/share.js +3 -3
  42. package/dist/scripts/studio.d.ts +19 -3
  43. package/dist/scripts/studio.js +327 -6
  44. package/dist/server/api.d.ts +25 -2
  45. package/dist/server/api.js +77 -17
  46. package/dist/server/dev-studio.d.ts +36 -0
  47. package/dist/server/dev-studio.js +181 -0
  48. package/dist/server/dev.d.ts +16 -1
  49. package/dist/server/dev.js +23 -3
  50. package/dist/server/gate.d.ts +26 -3
  51. package/dist/server/gate.js +323 -52
  52. package/dist/server/mailer.d.ts +60 -0
  53. package/dist/server/mailer.js +104 -0
  54. package/dist/server/mailers/fake.d.ts +8 -0
  55. package/dist/server/mailers/fake.js +16 -0
  56. package/dist/server/mailers/resend.d.ts +21 -0
  57. package/dist/server/mailers/resend.js +49 -0
  58. package/dist/server/session.js +4 -1
  59. package/dist/vercel/proxy.js +10 -3
  60. package/dist/vite/plugin.js +6 -1
  61. package/kit/ViewTools.tsx +9 -0
  62. package/kit/prefs.ts +3 -4
  63. package/kit/ui/Menu.tsx +29 -18
  64. package/kit/ui/layer.ts +41 -0
  65. package/package.json +1 -1
  66. package/shell/Command.tsx +142 -0
  67. package/shell/Index.tsx +902 -400
  68. package/shell/Milestone.tsx +302 -0
  69. package/shell/Sidebar.tsx +236 -0
  70. package/shell/index.css +224 -0
  71. package/shell/milestones.test.ts +157 -0
  72. package/shell/milestones.ts +144 -0
  73. package/shell/registry.ts +4 -3
  74. package/shell/review/Bar.tsx +1 -0
  75. package/shell/review/Card.tsx +17 -7
  76. package/shell/review/People.tsx +908 -134
  77. package/shell/review/Review.tsx +2 -2
  78. package/shell/review/anchor.ts +6 -1
  79. package/shell/review/client.ts +41 -7
  80. package/shell/review/menu-place.test.ts +41 -0
  81. package/shell/review/place.test.ts +26 -1
  82. package/shell/review/place.ts +17 -0
  83. package/shell/review/review.css +97 -11
  84. package/shell/review/view.test.ts +115 -1
  85. package/shell/review/view.ts +92 -2
  86. package/shell/shell.css +8 -175
  87. package/shell/sidebar.css +86 -0
  88. package/shell/studio/ClientHub.tsx +21 -10
  89. package/shell/studio/StudioHub.tsx +121 -45
  90. package/shell/studio/hub.css +118 -27
  91. package/shell/studio/page.tsx +28 -17
  92. package/shell/system.css +12 -0
  93. package/skills/miles-deploy/SKILL.md +25 -4
  94. package/skills/miles-new-feature/SKILL.md +2 -1
  95. package/skills/miles-prototype/SKILL.md +1 -1
  96. package/templates/app/README.md +1 -1
  97. package/templates/brand/README.md +1 -1
  98. package/templates/site/README.md +1 -1
  99. package/templates/space/README.md +1 -1
  100. package/templates/studio/.env.example +6 -0
  101. package/templates/studio/package.json +1 -1
@@ -0,0 +1,36 @@
1
+ import type { Plugin } from 'vite';
2
+ import type { Person } from '../core/review/model.ts';
3
+ import { type Review } from '../core/review/service.ts';
4
+ type Sample = {
5
+ slug: string;
6
+ name: string;
7
+ /** Proven: added and open. Otherwise still waiting for its address, as a half-run `client add` leaves it. */
8
+ proven: boolean;
9
+ projects: {
10
+ slug: string;
11
+ name: string;
12
+ built: boolean;
13
+ }[];
14
+ people: {
15
+ name: string;
16
+ email: string | null;
17
+ }[];
18
+ };
19
+ /** What a studio with a little history holds: two open clients with projects and people, and one still being added. */
20
+ export declare const SAMPLE_CLIENTS: Sample[];
21
+ export declare const SAMPLE_STUDIO_PEOPLE: {
22
+ name: string;
23
+ email: string;
24
+ }[];
25
+ /** Fills an empty dev studio store with the samples; a store that has a client already is left as it is. */
26
+ export declare function seedDevStudio(review: Review, actor: Person): Promise<void>;
27
+ /** Which dev site a page is: the studio's host, a client's (by slug), or neither (the project's own pages). */
28
+ export declare function devSiteOf(page: string | undefined): {
29
+ studio: true;
30
+ } | {
31
+ client: string;
32
+ } | null;
33
+ /** Where Open on the studio hub lands in dev: the client's dev hub, or, for a project, this project's index. */
34
+ export declare function devHandoffTarget(form: URLSearchParams): string;
35
+ export declare function devStudio(): Plugin;
36
+ export {};
@@ -0,0 +1,181 @@
1
+ /// <reference types="node" />
2
+ // The studio site's own pages in `design-hub dev`, so they can be seen and
3
+ // used before a studio exists: the studio hub at /_studio/ and each client's
4
+ // hub at /_clients/<client>/. They run on a store of their own,
5
+ // .design-hub/studio.json, filled on first use with sample clients, projects
6
+ // and people, and never on the project's review store or a studio's. Whoever
7
+ // runs the dev server is the local Studio person, as on the index.
8
+ //
9
+ // Their review API is the dev server's own path: the page that asks
10
+ // (the request's Referer) says which site it is, the studio's host or a
11
+ // client's. Open on the studio hub goes to the client's dev hub, and every
12
+ // project opens this project's index, the one project dev has.
13
+ import { readFileSync } from 'node:fs';
14
+ import { join } from 'node:path';
15
+ import { HANDOFF_START } from '../core/prototype.js';
16
+ import { createReview } from '../core/review/service.js';
17
+ import { keyOf } from '../core/review/store.js';
18
+ import { API_PATH, handleReview, handleStudioReview } from './api.js';
19
+ import { devMailer, mailUnknown, send, toRequest } from './dev.js';
20
+ import { fileKV } from './file-kv.js';
21
+ import { localStudio } from './local.js';
22
+ import { SHELL, STATE_DIR } from './paths.js';
23
+ const DEV_STUDIO_STORE = join(STATE_DIR, 'studio.json');
24
+ const DEV_STUDIO = '/_studio/';
25
+ const DEV_CLIENTS = '/_clients/';
26
+ /** The host the sample studio names in its links: no request ever reaches it. */
27
+ const STUDIO_HOST = 'studio.localhost';
28
+ /** What a studio with a little history holds: two open clients with projects and people, and one still being added. */
29
+ export const SAMPLE_CLIENTS = [
30
+ {
31
+ slug: 'northwind',
32
+ name: 'Northwind Health',
33
+ proven: true,
34
+ projects: [
35
+ { slug: 'portal', name: 'Patient portal', built: true },
36
+ { slug: 'site', name: 'Marketing site', built: true },
37
+ { slug: 'brand', name: 'Brand refresh', built: false },
38
+ ],
39
+ people: [
40
+ { name: 'Priya Shah', email: 'priya@northwind.example' },
41
+ { name: 'Tom Reyes', email: null },
42
+ ],
43
+ },
44
+ {
45
+ slug: 'acme',
46
+ name: 'Acme Freight',
47
+ proven: true,
48
+ projects: [{ slug: 'driver-app', name: 'Driver app', built: true }],
49
+ people: [{ name: 'Sam Ortiz', email: 'sam@acme.example' }],
50
+ },
51
+ { slug: 'harbor', name: 'Harbor Bank', proven: false, projects: [], people: [] },
52
+ ];
53
+ export const SAMPLE_STUDIO_PEOPLE = [{ name: 'Dana Lee', email: 'dana@studio.example' }];
54
+ /** A build id for a sample project: the store asks only that one was started before it goes live. */
55
+ const sampleBuild = (client, project) => `dev${client}${project}0000000000000000`.replace(/[^a-z0-9]/g, '').slice(0, 64);
56
+ async function seedClient(review, actor, c) {
57
+ await review.addClient(actor, { slug: c.slug, name: c.name, host: `${c.slug}.localhost` });
58
+ if (!c.proven)
59
+ return;
60
+ const at = { client: c.slug };
61
+ await review.endProof(at, actor);
62
+ for (const p of c.projects) {
63
+ await review.addProject(at, actor, { slug: p.slug, name: p.name });
64
+ if (!p.built)
65
+ continue;
66
+ const scope = { ...at, project: p.slug };
67
+ const build = sampleBuild(c.slug, p.slug);
68
+ await review.startBuild(scope, actor, build);
69
+ await review.setBuild(scope, actor, build);
70
+ }
71
+ for (const person of c.people)
72
+ await review.addPerson(at, actor, { ...person, kind: 'client' });
73
+ }
74
+ /** Fills an empty dev studio store with the samples; a store that has a client already is left as it is. */
75
+ export async function seedDevStudio(review, actor) {
76
+ if ((await review.listClients(actor)).length)
77
+ return;
78
+ for (const c of SAMPLE_CLIENTS)
79
+ await seedClient(review, actor, c);
80
+ for (const person of SAMPLE_STUDIO_PEOPLE)
81
+ await review.addStudioPerson(actor, { ...person, kind: 'studio', team: actor.team });
82
+ }
83
+ /** Which dev site a page is: the studio's host, a client's (by slug), or neither (the project's own pages). */
84
+ export function devSiteOf(page) {
85
+ if (!page)
86
+ return null;
87
+ let path;
88
+ try {
89
+ path = new URL(page).pathname;
90
+ }
91
+ catch {
92
+ return null;
93
+ }
94
+ if (path.startsWith(DEV_STUDIO))
95
+ return { studio: true };
96
+ const client = path.startsWith(DEV_CLIENTS) ? path.slice(DEV_CLIENTS.length).split('/')[0] : '';
97
+ return client ? { client } : null;
98
+ }
99
+ /** Where Open on the studio hub lands in dev: the client's dev hub, or, for a project, this project's index. */
100
+ export function devHandoffTarget(form) {
101
+ if (form.get('project'))
102
+ return '/';
103
+ const client = form.get('client') ?? '';
104
+ return /^[a-z0-9-]+$/.test(client) ? `${DEV_CLIENTS}${client}/` : DEV_STUDIO;
105
+ }
106
+ /** A page of the studio site's, its script served from the shell like the index's. */
107
+ const page = (file, script) => readFileSync(join(SHELL, 'studio', file), 'utf8').replace(/src="\.\/[\w.]+"/, `src="/studio/${script}"`);
108
+ export function devStudio() {
109
+ return {
110
+ name: 'dev-studio',
111
+ apply: 'serve',
112
+ configureServer(server) {
113
+ const kv = fileKV(DEV_STUDIO_STORE);
114
+ const review = createReview(kv);
115
+ const actor = localStudio();
116
+ const mailer = devMailer(review);
117
+ // The local Studio person is among the studio's people, so Studio people lists them.
118
+ const ready = kv
119
+ .hset(keyOf('studio', 'people'), actor.id, JSON.stringify(actor))
120
+ .then(() => seedDevStudio(review, actor))
121
+ // A sample that does not go in leaves the pages emptier, never the dev server down.
122
+ .catch((error) => console.error('design-hub: the dev studio samples did not load:', error));
123
+ // The API first: a request from one of these pages is the studio site's; any other goes on to the project's.
124
+ server.middlewares.use(API_PATH, (req, res, next) => {
125
+ const site = devSiteOf(req.headers.referer);
126
+ if (!site)
127
+ return next();
128
+ ready
129
+ .then(() => toRequest(req))
130
+ .then((request) => 'studio' in site
131
+ ? handleStudioReview(request, { review, actor, host: STUDIO_HOST, mailer })
132
+ : handleReview(request, {
133
+ review,
134
+ actor,
135
+ scope: { client: site.client },
136
+ mailer,
137
+ host: `${site.client}.localhost`,
138
+ mailUnknown: mailUnknown(),
139
+ }))
140
+ .then((response) => send(res, response))
141
+ .catch(next);
142
+ });
143
+ server.middlewares.use((req, res, next) => {
144
+ const url = new URL(req.url ?? '/', 'http://dev');
145
+ const path = url.pathname;
146
+ if (path === HANDOFF_START && req.method === 'POST') {
147
+ toRequest(req)
148
+ .then((request) => request.text())
149
+ .then((form) => {
150
+ res.statusCode = 303;
151
+ res.setHeader('location', devHandoffTarget(new URLSearchParams(form)));
152
+ res.end();
153
+ })
154
+ .catch(next);
155
+ return;
156
+ }
157
+ const html = path === DEV_STUDIO
158
+ ? page('studio.html', 'studio.tsx')
159
+ : /^\/_clients\/[a-z0-9-]+\/$/.test(path)
160
+ ? page('index.html', 'main.tsx')
161
+ : null;
162
+ // A project's row on a client's dev hub opens this project's index: dev has no other.
163
+ if (html === null && path.startsWith(DEV_CLIENTS)) {
164
+ res.statusCode = 302;
165
+ res.setHeader('location', '/');
166
+ res.end();
167
+ return;
168
+ }
169
+ if (html === null)
170
+ return next();
171
+ server
172
+ .transformIndexHtml(req.url ?? path, html)
173
+ .then((out) => {
174
+ res.setHeader('content-type', 'text/html; charset=utf-8');
175
+ res.end(out);
176
+ })
177
+ .catch(next);
178
+ });
179
+ },
180
+ };
181
+ }
@@ -1,2 +1,17 @@
1
- import type { Plugin } from 'vite';
1
+ import type { ServerResponse } from 'node:http';
2
+ import type { Connect, Plugin } from 'vite';
3
+ import { type Review } from '../core/review/service.ts';
4
+ import type { Mailer } from './mailer.ts';
5
+ export declare function toRequest(req: Connect.IncomingMessage): Promise<Request>;
6
+ export declare function send(res: ServerResponse, response: Response): Promise<void>;
7
+ /**
8
+ * A mailer that keeps mail in memory, for seeing the People tab's mail lines
9
+ * in dev: `DESIGN_HUB_DEV_MAIL=on` sends, `failing` refuses, each recorded
10
+ * once at start so the tab opens in that state, and `off` has none. Unset,
11
+ * there is none and the tab shows no sign-in line: dev cannot know how the
12
+ * project's studio sends mail. Nothing leaves the machine.
13
+ */
14
+ export declare function devMailer(review: Review): Mailer | null;
15
+ /** Unset, dev says nothing of sign-in mail; `off`, `on` and `failing` show each state. */
16
+ export declare const mailUnknown: () => boolean;
2
17
  export declare function devReview(): Plugin;
@@ -6,9 +6,10 @@ import { checks } from './checks.js';
6
6
  import { readEnv } from './env.js';
7
7
  import { fileKV } from './file-kv.js';
8
8
  import { LOCAL_SCOPE, LOCAL_STORE, localStudio } from './local.js';
9
+ import { fakeMailer } from './mailers/fake.js';
9
10
  import { ROOT } from './paths.js';
10
11
  // Connect strips the mount path from req.url; originalUrl keeps the query the handler reads.
11
- async function toRequest(req) {
12
+ export async function toRequest(req) {
12
13
  const chunks = [];
13
14
  for await (const chunk of req)
14
15
  chunks.push(chunk);
@@ -23,13 +24,31 @@ async function toRequest(req) {
23
24
  body: req.method === 'GET' || req.method === 'HEAD' ? undefined : body,
24
25
  });
25
26
  }
26
- async function send(res, response) {
27
+ export async function send(res, response) {
27
28
  res.statusCode = response.status;
28
29
  response.headers.forEach((value, key) => {
29
30
  res.setHeader(key, value);
30
31
  });
31
32
  res.end(Buffer.from(await response.arrayBuffer()));
32
33
  }
34
+ /**
35
+ * A mailer that keeps mail in memory, for seeing the People tab's mail lines
36
+ * in dev: `DESIGN_HUB_DEV_MAIL=on` sends, `failing` refuses, each recorded
37
+ * once at start so the tab opens in that state, and `off` has none. Unset,
38
+ * there is none and the tab shows no sign-in line: dev cannot know how the
39
+ * project's studio sends mail. Nothing leaves the machine.
40
+ */
41
+ export function devMailer(review) {
42
+ const mode = process.env.DESIGN_HUB_DEV_MAIL;
43
+ if (mode !== 'on' && mode !== 'failing')
44
+ return null;
45
+ const mailer = fakeMailer('Dev Studio');
46
+ mailer.answer = mode === 'on' ? 'sent' : 'refused';
47
+ void review.recordMail(mailer.answer);
48
+ return mailer;
49
+ }
50
+ /** Unset, dev says nothing of sign-in mail; `off`, `on` and `failing` show each state. */
51
+ export const mailUnknown = () => !['off', 'on', 'failing'].includes(process.env.DESIGN_HUB_DEV_MAIL ?? '');
33
52
  export function devReview() {
34
53
  return {
35
54
  name: 'dev-review',
@@ -41,11 +60,12 @@ export function devReview() {
41
60
  triage: jevTriager(readEnv(ROOT).TYPESAFE_API_KEY ?? '', checks),
42
61
  });
43
62
  const actor = localStudio();
63
+ const mailer = devMailer(review);
44
64
  // The local Studio person is in the people list like anyone else, so the people panel shows them.
45
65
  void kv.hset(keyOf('studio', 'people'), actor.id, JSON.stringify(actor));
46
66
  server.middlewares.use(API_PATH, (req, res, nextMiddleware) => {
47
67
  toRequest(req)
48
- .then((request) => handleReview(request, { review, actor, scope: LOCAL_SCOPE }))
68
+ .then((request) => handleReview(request, { review, actor, scope: LOCAL_SCOPE, mailer, mailUnknown: mailUnknown() }))
49
69
  .then((response) => send(res, response))
50
70
  .catch(nextMiddleware);
51
71
  });
@@ -2,6 +2,7 @@ import { GATE_FONT, type Route } from '../core/prototype.ts';
2
2
  import { type Client, type Person, type PersonKind, type Project, type Scope } from '../core/review/model.ts';
3
3
  import { type Review } from '../core/review/service.ts';
4
4
  import { type HostLookup } from './hosts.ts';
5
+ import { type Mailer } from './mailer.ts';
5
6
  import { type Gate } from './session.ts';
6
7
  export declare const DEFAULT_SITE_NAME = "Prototypes";
7
8
  /**
@@ -11,7 +12,7 @@ export declare const DEFAULT_SITE_NAME = "Prototypes";
11
12
  * (`next`) or to another URL of the site (`rewrite`, a function), each with
12
13
  * the headers to add to the answer.
13
14
  */
14
- export type Decision = Response | {
15
+ export type Decision = Response | Later | {
15
16
  pass: 'next';
16
17
  headers?: Record<string, string>;
17
18
  } | {
@@ -19,6 +20,15 @@ export type Decision = Response | {
19
20
  to: URL;
20
21
  headers: Record<string, string>;
21
22
  };
23
+ /**
24
+ * A Response with work to finish after it is sent (a code's mail): the
25
+ * adapter answers at once and keeps the work running (on Vercel, waitUntil).
26
+ * The work never rejects, and the answer never waits for it (E7).
27
+ */
28
+ export type Later = {
29
+ answer: Response;
30
+ after: Promise<void>;
31
+ };
22
32
  export { GATE_FONT };
23
33
  export declare function gatePreview(page: string | null, siteName?: string): Promise<{
24
34
  html: string;
@@ -37,17 +47,30 @@ export declare const plain: (request: Request, status: number, text: string, ext
37
47
  export declare const notFound: (request: Request) => Response;
38
48
  /** The store did not answer: a 503 that passes, so it says when to try again. */
39
49
  export declare const storeDown: (request: Request) => Response;
40
- /** One request at one moment: `at` is the gate's clock, so freshness is tested without waiting. */
50
+ /**
51
+ * One request at one moment: `at` is the gate's clock, so freshness is tested
52
+ * without waiting. `mailer`: the studio's, or none (no email sign-in).
53
+ * `address`: the network address the host's adapter gives, never a header
54
+ * the gate reads itself; none is not counted against the cap.
55
+ */
41
56
  export type Ctx = {
42
57
  siteName: string;
43
58
  request: Request;
44
59
  at: Date;
60
+ mailer?: Mailer | null;
61
+ address?: string | null;
45
62
  };
46
63
  export type ProxyOptions = {
47
64
  /** The gate pages' title, the same on every client's host. */
48
65
  siteName?: string;
49
66
  /** The gate's clock; tests move it instead of waiting. */
50
67
  now?: () => Date;
68
+ /** The studio's mailer, or null for none; absent, it is chosen from the env on each request (chooseMailer). */
69
+ mailer?: Mailer | null;
70
+ };
71
+ /** What the host's adapter knows of a request that no header the caller sends can say: its network address. */
72
+ export type From = {
73
+ address?: string | null;
51
74
  };
52
75
  /** Who a request's host is: its client, and the gate and the client's scope built from that one lookup. */
53
76
  export type Place = {
@@ -94,4 +117,4 @@ export declare function admit(found: Found, request: Request, at: Date): Promise
94
117
  /** The answer for a refusal: the same from the gate and from the files function. */
95
118
  export declare function refuse(ctx: Ctx, refusal: Refusal): Promise<Response> | Response;
96
119
  /** The gate a host's adapter runs on every request (vercel/proxy.ts on Vercel). Its sign-in page names no client (S10). */
97
- export declare function createGate({ siteName, now }?: ProxyOptions): (request: Request) => Promise<Decision>;
120
+ export declare function createGate(options?: ProxyOptions): (request: Request, { address }?: From) => Promise<Decision>;