@milesroxas/design-hub 0.14.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 (44) hide show
  1. package/README.md +37 -32
  2. package/core/catalog.test.ts +28 -1
  3. package/core/catalog.ts +21 -4
  4. package/dist/core/catalog.d.ts +8 -1
  5. package/dist/core/catalog.js +12 -3
  6. package/dist/scripts/lib/catalog.d.ts +1 -1
  7. package/dist/scripts/lib/catalog.js +3 -3
  8. package/dist/scripts/new.d.ts +8 -3
  9. package/dist/scripts/new.js +15 -4
  10. package/dist/scripts/preflight.js +1 -1
  11. package/dist/scripts/share.js +3 -3
  12. package/dist/server/api.d.ts +11 -0
  13. package/dist/server/api.js +6 -0
  14. package/dist/server/dev-studio.d.ts +36 -0
  15. package/dist/server/dev-studio.js +181 -0
  16. package/dist/server/dev.d.ts +16 -1
  17. package/dist/server/dev.js +4 -4
  18. package/dist/vite/plugin.js +3 -0
  19. package/kit/ViewTools.tsx +9 -0
  20. package/kit/prefs.ts +3 -4
  21. package/package.json +1 -1
  22. package/shell/Command.tsx +142 -0
  23. package/shell/Index.tsx +902 -400
  24. package/shell/Milestone.tsx +302 -0
  25. package/shell/Sidebar.tsx +236 -0
  26. package/shell/index.css +224 -0
  27. package/shell/milestones.test.ts +157 -0
  28. package/shell/milestones.ts +144 -0
  29. package/shell/registry.ts +4 -3
  30. package/shell/shell.css +8 -175
  31. package/shell/sidebar.css +86 -0
  32. package/shell/studio/ClientHub.tsx +21 -10
  33. package/shell/studio/StudioHub.tsx +120 -45
  34. package/shell/studio/hub.css +20 -15
  35. package/shell/studio/page.tsx +28 -17
  36. package/shell/system.css +12 -0
  37. package/skills/miles-deploy/SKILL.md +1 -1
  38. package/skills/miles-new-feature/SKILL.md +2 -1
  39. package/skills/miles-prototype/SKILL.md +1 -1
  40. package/templates/app/README.md +1 -1
  41. package/templates/brand/README.md +1 -1
  42. package/templates/site/README.md +1 -1
  43. package/templates/space/README.md +1 -1
  44. package/templates/studio/package.json +1 -1
@@ -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;
@@ -9,7 +9,7 @@ import { LOCAL_SCOPE, LOCAL_STORE, localStudio } from './local.js';
9
9
  import { fakeMailer } from './mailers/fake.js';
10
10
  import { ROOT } from './paths.js';
11
11
  // Connect strips the mount path from req.url; originalUrl keeps the query the handler reads.
12
- async function toRequest(req) {
12
+ export async function toRequest(req) {
13
13
  const chunks = [];
14
14
  for await (const chunk of req)
15
15
  chunks.push(chunk);
@@ -24,7 +24,7 @@ async function toRequest(req) {
24
24
  body: req.method === 'GET' || req.method === 'HEAD' ? undefined : body,
25
25
  });
26
26
  }
27
- async function send(res, response) {
27
+ export async function send(res, response) {
28
28
  res.statusCode = response.status;
29
29
  response.headers.forEach((value, key) => {
30
30
  res.setHeader(key, value);
@@ -38,7 +38,7 @@ async function send(res, response) {
38
38
  * there is none and the tab shows no sign-in line: dev cannot know how the
39
39
  * project's studio sends mail. Nothing leaves the machine.
40
40
  */
41
- function devMailer(review) {
41
+ export function devMailer(review) {
42
42
  const mode = process.env.DESIGN_HUB_DEV_MAIL;
43
43
  if (mode !== 'on' && mode !== 'failing')
44
44
  return null;
@@ -48,7 +48,7 @@ function devMailer(review) {
48
48
  return mailer;
49
49
  }
50
50
  /** Unset, dev says nothing of sign-in mail; `off`, `on` and `failing` show each state. */
51
- const mailUnknown = () => !['off', 'on', 'failing'].includes(process.env.DESIGN_HUB_DEV_MAIL ?? '');
51
+ export const mailUnknown = () => !['off', 'on', 'failing'].includes(process.env.DESIGN_HUB_DEV_MAIL ?? '');
52
52
  export function devReview() {
53
53
  return {
54
54
  name: 'dev-review',
@@ -12,6 +12,7 @@ import react from '@vitejs/plugin-react';
12
12
  import { build, searchForWorkspaceRoot } from 'vite';
13
13
  import { isProjectSlug, STUDIO_DIR, STUDIO_PAGE } from '../core/prototype.js';
14
14
  import { devReview } from '../server/dev.js';
15
+ import { devStudio } from '../server/dev-studio.js';
15
16
  import { devPort, readEnv } from '../server/env.js';
16
17
  import { GATE_FONT, gatePreview } from '../server/gate.js';
17
18
  import { PACKAGE, PACKAGE_KIT, SHELL } from '../server/paths.js';
@@ -66,6 +67,8 @@ export function designHub({ root }) {
66
67
  kitResolver(join(root, 'kit'), PACKAGE_KIT),
67
68
  lapse(root),
68
69
  markdownDocs(),
70
+ // Before devReview: a request from a studio page is the dev studio's, every other the project's.
71
+ devStudio(),
69
72
  devReview(),
70
73
  gateFont(),
71
74
  ];
package/kit/ViewTools.tsx CHANGED
@@ -3,10 +3,12 @@ import { createContext, useContext, useEffect, useLayoutEffect, useState } from
3
3
  import { ensureDialRoot } from './dials';
4
4
  import {
5
5
  type Appearance,
6
+ pinAppearance,
6
7
  prefersReducedMotion,
7
8
  setAppearance,
8
9
  setForceReduce,
9
10
  setSpeed,
11
+ urlAppearance,
10
12
  useAppearance,
11
13
  } from './prefs';
12
14
 
@@ -79,6 +81,13 @@ function useTheme(
79
81
  useEffect(() => {
80
82
  if (on) setValue('theme', appearance);
81
83
  }, [on, appearance, setValue]);
84
+ // A panel without the theme is a prototype with one look: the page opens in the system theme, so hold it
85
+ // in light (or what ?theme= asks) while the prototype is open, as a Board holds its export look. Before paint, so a dark machine never flashes dark.
86
+ useLayoutEffect(() => {
87
+ if (on) return;
88
+ pinAppearance(urlAppearance() ?? 'light');
89
+ return () => pinAppearance(null);
90
+ }, [on]);
82
91
  }
83
92
 
84
93
  function useMotion(on: boolean, reduce: boolean | undefined, speed: string | undefined) {
package/kit/prefs.ts CHANGED
@@ -29,12 +29,12 @@ let forceReduce = false;
29
29
  let speed = 1;
30
30
  let lapse: LapseHandle | null = null;
31
31
 
32
+ // Every page opens in the system theme, so a reviewer sees what their OS asks for. ?theme= overrides it,
33
+ // and a choice in View or a product setting lasts until the page reloads.
32
34
  function readAppearance(): Appearance {
33
35
  const fromUrl = params.get('theme');
34
36
  if (fromUrl === 'light' || fromUrl === 'dark' || fromUrl === 'system') return fromUrl;
35
- const saved = readStored('proto:appearance');
36
- if (saved === 'light' || saved === 'dark' || saved === 'system') return saved;
37
- return 'light';
37
+ return 'system';
38
38
  }
39
39
 
40
40
  // Storage throws in a private window or with site data blocked: the page then runs on its defaults.
@@ -99,7 +99,6 @@ export const urlAppearance = (): 'light' | 'dark' | null => {
99
99
  export function setAppearance(next: Appearance) {
100
100
  if (next === appearance) return;
101
101
  appearance = next;
102
- store('proto:appearance', next);
103
102
  applyTheme();
104
103
  emit();
105
104
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milesroxas/design-hub",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "The Design Hub product: the hub that lists and serves prototypes, the review panel and its API, the sign-in gate, the kit, and the design-hub CLI. A project installs it and keeps only its prototypes, kit materials and docs.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -0,0 +1,142 @@
1
+ // The command menu: one box that finds any milestone, prototype, tool or
2
+ // action. Opened many times a day, so it opens and closes with no motion.
3
+ // Enter opens a prototype and shows a milestone's home.
4
+ import { Icon, type IconName } from '@kit/Icon';
5
+ import { type KeyboardEvent, useEffect, useRef, useState } from 'react';
6
+
7
+ export type CommandItem = {
8
+ id: string;
9
+ group: 'Recent' | 'Milestones' | 'Prototypes' | 'Tools' | 'Actions';
10
+ label: string;
11
+ /** Read by the search beside the label, never shown. */
12
+ text?: string;
13
+ hint?: string;
14
+ icon: IconName;
15
+ run: () => void;
16
+ };
17
+
18
+ const LIMIT = 8;
19
+ const GROUPS: CommandItem['group'][] = ['Milestones', 'Prototypes', 'Tools', 'Actions'];
20
+
21
+ /** What a query leaves: every group's matches, at most eight of each. With no query, what was opened last and the actions. */
22
+ function resultsOf(items: CommandItem[], q: string): CommandItem[] {
23
+ if (!q) return items.filter((i) => i.group === 'Recent' || i.group === 'Actions');
24
+ const hit = (i: CommandItem) => `${i.label} ${i.text ?? ''} ${i.hint ?? ''}`.toLowerCase().includes(q);
25
+ return GROUPS.flatMap((g) => items.filter((i) => i.group === g && hit(i)).slice(0, LIMIT));
26
+ }
27
+
28
+ /** Which way a key moves the active option: the arrows, or Control with N and P. */
29
+ const moveOf = (e: KeyboardEvent) => {
30
+ if (e.key === 'ArrowDown' || (e.ctrlKey && e.key === 'n')) return 1;
31
+ if (e.key === 'ArrowUp' || (e.ctrlKey && e.key === 'p')) return -1;
32
+ return 0;
33
+ };
34
+ const closes = (e: KeyboardEvent) =>
35
+ e.key === 'Escape' || ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k');
36
+
37
+ export function CommandMenu({ items, onClose }: { items: CommandItem[]; onClose: () => void }) {
38
+ const [query, setQuery] = useState('');
39
+ const [active, setActive] = useState(0);
40
+ const back = useRef<Element | null>(document.activeElement);
41
+ const list = useRef<HTMLDivElement>(null);
42
+ const field = useRef<HTMLInputElement>(null);
43
+ useEffect(() => field.current?.focus(), []);
44
+ const shown = resultsOf(items, query.trim().toLowerCase());
45
+ const at = Math.min(active, Math.max(0, shown.length - 1));
46
+
47
+ // Focus goes back to where it was once the page behind is no longer inert.
48
+ const close = () => {
49
+ const el = back.current as HTMLElement | null;
50
+ onClose();
51
+ requestAnimationFrame(() => el?.focus?.());
52
+ };
53
+ const run = (item?: CommandItem) => {
54
+ if (!item) return;
55
+ close();
56
+ item.run();
57
+ };
58
+
59
+ // biome-ignore lint/correctness/useExhaustiveDependencies: scroll the active option into view when it changes
60
+ useEffect(() => {
61
+ list.current?.querySelector('[aria-selected="true"]')?.scrollIntoView({ block: 'nearest' });
62
+ }, [at]);
63
+
64
+ const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
65
+ const move = moveOf(e);
66
+ if (!move && e.key !== 'Enter' && e.key !== 'Tab' && !closes(e)) return;
67
+ e.preventDefault();
68
+ e.stopPropagation();
69
+ if (move) setActive((at + move + shown.length) % Math.max(1, shown.length));
70
+ else if (e.key === 'Enter') run(shown[at]);
71
+ else if (closes(e)) close();
72
+ };
73
+
74
+ return (
75
+ <div
76
+ className="ix-cmd-overlay sys"
77
+ onPointerDown={(e) => {
78
+ if (e.target === e.currentTarget) close();
79
+ }}
80
+ >
81
+ <div className="ix-cmd" role="dialog" aria-modal="true" aria-label="Command menu" onKeyDown={onKeyDown}>
82
+ <div className="ix-cmd-field">
83
+ <Icon name="search-md" size={16} />
84
+ <input
85
+ ref={field}
86
+ role="combobox"
87
+ aria-expanded="true"
88
+ aria-controls="ix-cmd-list"
89
+ aria-activedescendant={shown[at] ? `ix-cmd-${shown[at].id}` : undefined}
90
+ aria-label="Search milestones, prototypes and actions"
91
+ placeholder="Search milestones, prototypes and actions"
92
+ autoComplete="off"
93
+ spellCheck={false}
94
+ value={query}
95
+ onChange={(e) => {
96
+ setQuery(e.target.value);
97
+ setActive(0);
98
+ }}
99
+ />
100
+ <span className="ix-kbd" aria-hidden="true">
101
+ esc
102
+ </span>
103
+ </div>
104
+ <div ref={list} id="ix-cmd-list" className="ix-cmd-list" role="listbox" aria-label="Results">
105
+ {shown.length === 0 ? <p className="ix-cmd-none">Nothing matches “{query}”.</p> : null}
106
+ {shown.map((item, i) => (
107
+ <div key={item.id} role="presentation">
108
+ {item.group !== shown[i - 1]?.group ? (
109
+ <div className="ix-cmd-group" role="presentation">
110
+ {item.group}
111
+ </div>
112
+ ) : null}
113
+ {/* biome-ignore lint/a11y/useKeyWithClickEvents: the input owns the keys; options take the pointer */}
114
+ <div
115
+ id={`ix-cmd-${item.id}`}
116
+ role="option"
117
+ aria-selected={i === at}
118
+ tabIndex={-1}
119
+ className="ix-cmd-item"
120
+ onPointerMove={() => i !== at && setActive(i)}
121
+ onClick={() => run(item)}
122
+ >
123
+ <Icon name={item.icon} size={16} />
124
+ <span className="ix-cmd-label">{item.label}</span>
125
+ {item.hint ? <span className="ix-cmd-hint">{item.hint}</span> : null}
126
+ </div>
127
+ </div>
128
+ ))}
129
+ </div>
130
+ <div className="ix-cmd-foot" aria-hidden="true">
131
+ <span>
132
+ <kbd>↑</kbd>
133
+ <kbd>↓</kbd> Move
134
+ </span>
135
+ <span>
136
+ <kbd>↵</kbd> Open
137
+ </span>
138
+ </div>
139
+ </div>
140
+ </div>
141
+ );
142
+ }