@milesroxas/design-hub 0.8.1 → 0.10.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 (78) hide show
  1. package/README.md +7 -2
  2. package/core/controls.test.ts +176 -0
  3. package/core/controls.ts +195 -0
  4. package/core/prototype.test.ts +83 -0
  5. package/core/prototype.ts +84 -0
  6. package/core/review/model.ts +32 -0
  7. package/core/review/policy.test.ts +4 -2
  8. package/core/review/policy.ts +4 -2
  9. package/core/review/service.test.ts +223 -4
  10. package/core/review/service.ts +111 -2
  11. package/core/review/store.ts +13 -0
  12. package/dist/core/controls.d.ts +55 -0
  13. package/dist/core/controls.js +174 -0
  14. package/dist/core/prototype.d.ts +35 -0
  15. package/dist/core/prototype.js +84 -0
  16. package/dist/core/review/model.d.ts +13 -0
  17. package/dist/core/review/model.js +27 -0
  18. package/dist/core/review/policy.d.ts +1 -1
  19. package/dist/core/review/policy.js +1 -0
  20. package/dist/core/review/service.d.ts +50 -1
  21. package/dist/core/review/service.js +97 -3
  22. package/dist/core/review/store.d.ts +13 -0
  23. package/dist/core/review/store.js +13 -0
  24. package/dist/scripts/guard.d.ts +11 -0
  25. package/dist/scripts/guard.js +44 -0
  26. package/dist/scripts/lib/cdp.js +6 -1
  27. package/dist/scripts/lib/export-smoke.d.ts +18 -0
  28. package/dist/scripts/lib/export-smoke.js +73 -0
  29. package/dist/scripts/lib/rules.d.ts +1 -0
  30. package/dist/scripts/lib/rules.js +1 -0
  31. package/dist/scripts/new.js +5 -2
  32. package/dist/scripts/release-check.d.ts +5 -3
  33. package/dist/scripts/release-check.js +17 -4
  34. package/dist/scripts/share.d.ts +2 -2
  35. package/dist/server/api.js +21 -0
  36. package/dist/server/session.d.ts +5 -1
  37. package/dist/server/session.js +39 -21
  38. package/dist/vercel/proxy.js +44 -8
  39. package/dist/vite/boundary.d.ts +93 -0
  40. package/dist/vite/boundary.js +218 -0
  41. package/dist/vite/css-boundary.d.ts +39 -0
  42. package/dist/vite/css-boundary.js +167 -0
  43. package/dist/vite/kit.js +15 -6
  44. package/dist/vite/leaks.d.ts +37 -0
  45. package/dist/vite/leaks.js +331 -0
  46. package/dist/vite/plugin.js +2 -1
  47. package/dist/vite/registry.d.ts +15 -4
  48. package/dist/vite/registry.js +531 -27
  49. package/dist/vite/settle.d.ts +27 -0
  50. package/dist/vite/settle.js +185 -0
  51. package/kit/Artboard.tsx +21 -8
  52. package/kit/Space.tsx +4 -3
  53. package/kit/Variants.tsx +193 -65
  54. package/kit/ViewTools.tsx +77 -11
  55. package/kit/audience.ts +85 -0
  56. package/kit/dials.css +59 -0
  57. package/kit/dials.tsx +272 -35
  58. package/kit/harness.css +34 -0
  59. package/kit/prefs.ts +23 -1
  60. package/package.json +5 -4
  61. package/shell/ClientViewNote.tsx +46 -0
  62. package/shell/Guide.tsx +17 -2
  63. package/shell/Index.tsx +257 -25
  64. package/shell/audience.test.ts +63 -0
  65. package/shell/audience.ts +47 -0
  66. package/shell/controls.ts +26 -0
  67. package/shell/hub.ts +36 -0
  68. package/shell/main.tsx +51 -5
  69. package/shell/registry.ts +28 -7
  70. package/shell/registry.virtual.d.ts +12 -4
  71. package/shell/review/client.ts +16 -4
  72. package/shell/review/review.css +2 -2
  73. package/shell/shell.css +54 -3
  74. package/shell/system.css +5 -0
  75. package/skills/miles-brand/SKILL.md +11 -1
  76. package/skills/miles-deploy/SKILL.md +19 -0
  77. package/skills/miles-new-feature/SKILL.md +1 -1
  78. package/skills/miles-prototype/SKILL.md +48 -2
@@ -1,5 +1,6 @@
1
1
  // Who may do what. Studio runs the review: it adds and revokes people,
2
- // deletes any comment and corrects triage. A Client comments, replies, edits
2
+ // deletes any comment, corrects triage and sets which prototypes are
3
+ // client-facing. A Client comments, replies, edits
3
4
  // and deletes what they wrote, and resolves or reopens any client-facing
4
5
  // thread, because clients work alongside others in their own organisation.
5
6
  // An internal thread does not exist for a Client: every action on one is
@@ -7,7 +8,7 @@
7
8
  // nothing here.
8
9
  import type { Audience, Comment, Person, PersonKind } from './model.ts';
9
10
 
10
- export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people';
11
+ export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people' | 'audience';
11
12
 
12
13
  /** Only what the rules read, so the browser's signed-in person (no epoch) is checked by the same rules. */
13
14
  type Actor = Pick<Person, 'id' | 'kind' | 'revokedAt'>;
@@ -36,6 +37,7 @@ export function can(actor: Actor | null, action: Action, root?: Comment, subject
36
37
  return own || studio;
37
38
  case 'triage':
38
39
  case 'people':
40
+ case 'audience':
39
41
  return studio;
40
42
  }
41
43
  }
@@ -47,6 +47,8 @@ beforeEach(async () => {
47
47
  const r = await review.addPerson(studio, { name: 'Rae', team: 'Acme' });
48
48
  client = r.person;
49
49
  clientToken = r.token;
50
+ // The prototype these tests comment on is client-facing, so a Client reaches its comments.
51
+ await review.setAudience(studio, anchor.prototype, 'client');
50
52
  });
51
53
 
52
54
  test('a link works once, and looking at it uses nothing up', async () => {
@@ -60,10 +62,8 @@ test('a link works once, and looking at it uses nothing up', async () => {
60
62
 
61
63
  test('two requests with one link at once sign in one device', async () => {
62
64
  const both = await Promise.all([review.useLink(clientToken), review.useLink(clientToken)]);
63
- assert.deepEqual(
64
- both.map((p) => p?.id ?? null),
65
- [client.id, null],
66
- );
65
+ // Either request may win the claim; exactly one does.
66
+ assert.deepEqual(both.map((p) => p?.id ?? null).sort(), [client.id, null].sort());
67
67
  });
68
68
 
69
69
  test('a new link is for another device: sessions and other links stay', async () => {
@@ -555,6 +555,7 @@ test('triage runs on add, and a failing triager never loses the comment', async
555
555
  return { kind: 'bug', state: states[0] ?? null, by: 'jev', confidence: 0.9 };
556
556
  },
557
557
  });
558
+ await triaged.setAudience(studio, anchor.prototype, 'client');
558
559
  const c = await triaged.add(client, {
559
560
  body: 'Broken',
560
561
  anchor,
@@ -563,6 +564,7 @@ test('triage runs on add, and a failing triager never loses the comment', async
563
564
  assert.deepEqual(seen, [['Empty', 'Real list']]);
564
565
  assert.equal(c.triage?.kind, 'bug');
565
566
  const failing = createReview(memoryKV(), { triage: async () => Promise.reject(new Error('down')) });
567
+ await failing.setAudience(studio, anchor.prototype, 'client');
566
568
  assert.equal((await failing.add(client, { body: 'Still saved', anchor })).triage, null);
567
569
  });
568
570
 
@@ -704,6 +706,7 @@ test('numbers are fixed: internal and client-facing threads count apart, and non
704
706
  });
705
707
 
706
708
  test('numbers count per prototype', async () => {
709
+ await review.setAudience(studio, 'other', 'client');
707
710
  const a = await review.add(client, { body: 'A1', anchor });
708
711
  const b = await review.add(client, { body: 'B1', anchor: { ...anchor, prototype: 'other' } });
709
712
  const a2 = await review.add(studio, { body: 'A2', anchor, audience: 'client' });
@@ -781,3 +784,219 @@ test('a stored comment without an audience is internal, and one without a number
781
784
  assert.equal(await r.locate(rae, 'old'), null);
782
785
  await rejects(r.edit(rae, { prototype: 'sample-list', id: 'old', body: 'x' }), 'not_found');
783
786
  });
787
+
788
+ test('a prototype is internal until Studio marks it client-facing, and internal again when unmarked', async () => {
789
+ assert.equal(await review.audienceOf('secret-plan'), 'internal');
790
+ assert.equal(await review.setAudience(studio, 'secret-plan', 'client'), 'client');
791
+ assert.equal(await review.audienceOf('secret-plan'), 'client');
792
+ assert.equal(await review.audienceOf('sample-page'), 'internal');
793
+ assert.equal(await review.setAudience(studio, 'secret-plan', 'internal'), 'internal');
794
+ assert.equal(await review.audienceOf('secret-plan'), 'internal');
795
+ // Marking internal twice, or one never marked, is no error.
796
+ assert.equal(await review.setAudience(studio, 'sample-page', 'internal'), 'internal');
797
+ });
798
+
799
+ test('only client-facing is stored: internal deletes the field, and absent or garbage reads as internal', async () => {
800
+ const kv = memoryKV();
801
+ const own = createReview(kv);
802
+ await own.setAudience(studio, 'secret-plan', 'client');
803
+ assert.deepEqual(await kv.hgetall(KEY.audiences), { 'secret-plan': 'client' });
804
+ await own.setAudience(studio, 'secret-plan', 'internal');
805
+ assert.deepEqual(await kv.hgetall(KEY.audiences), {});
806
+ for (const garbage of ['Client', 'internal', '', '"client"', 'true', ' client']) {
807
+ await kv.hset(KEY.audiences, 'secret-plan', garbage);
808
+ assert.equal(await own.audienceOf('secret-plan'), 'internal', garbage);
809
+ }
810
+ await kv.hset(KEY.audiences, 'sample-page', 'yes');
811
+ await kv.hset(KEY.audiences, 'guide', 'client');
812
+ await kv.hset(KEY.audiences, 'Not_A_Slug', 'client');
813
+ await kv.hset(KEY.audiences, 'sample-social', 'client');
814
+ assert.deepEqual(await own.visiblePrototypes(studio), { view: 'studio', clientFacing: ['sample-social'] });
815
+ // A name that can never be a prototype is internal without a store read.
816
+ assert.equal(await own.audienceOf('guide'), 'internal');
817
+ assert.equal(await own.audienceOf('all.js'), 'internal');
818
+ });
819
+
820
+ test('only Studio sets an audience: a Client is forbidden, a signed-out caller unauthorized, whatever the input', async () => {
821
+ await rejects(review.setAudience(client, 'secret-plan', 'client'), 'forbidden');
822
+ await rejects(review.setAudience(client, 'nope!', 'garbage'), 'forbidden');
823
+ await rejects(review.setAudience(null, 'secret-plan', 'client'), 'unauthorized');
824
+ const revoked = await review.revoke(studio, client.id);
825
+ await rejects(review.setAudience({ ...revoked, kind: 'studio' }, 'secret-plan', 'client'), 'forbidden');
826
+ assert.equal(await review.audienceOf('secret-plan'), 'internal');
827
+ });
828
+
829
+ test('a prototype’s audience is checked: the slug and the value, every problem named', async () => {
830
+ await assert.rejects(
831
+ review.setAudience(studio, 'Bad Slug', 'everyone'),
832
+ (e: unknown) =>
833
+ e instanceof ReviewError &&
834
+ e.code === 'invalid' &&
835
+ e.problems.join('|') === 'prototype: a prototype slug|audience: one of internal, client',
836
+ );
837
+ for (const reserved of ['guide', 'api', 'assets'])
838
+ await rejects(review.setAudience(studio, reserved, 'client'), 'invalid');
839
+ await rejects(review.setAudience(studio, 'sample-list', null), 'invalid');
840
+ await rejects(review.setAudience(studio, 'sample-list', undefined), 'invalid');
841
+ });
842
+
843
+ test('the index read: Studio’s view, Client view, and a Client’s, each with the client-facing slugs', async () => {
844
+ await review.setAudience(studio, 'sample-page', 'client');
845
+ await review.setAudience(studio, 'sample-list', 'client');
846
+ const facing = ['sample-list', 'sample-page'];
847
+ assert.deepEqual(await review.visiblePrototypes(studio), { view: 'studio', clientFacing: facing });
848
+ assert.deepEqual(await review.visiblePrototypes(studio, 'studio'), {
849
+ view: 'studio',
850
+ clientFacing: facing,
851
+ });
852
+ assert.deepEqual(await review.visiblePrototypes(studio, 'client'), {
853
+ view: 'client',
854
+ clientFacing: facing,
855
+ });
856
+ // A Client asking for Studio's view still gets a Client's.
857
+ assert.deepEqual(await review.visiblePrototypes(client), { view: 'client', clientFacing: facing });
858
+ assert.deepEqual(await review.visiblePrototypes(client, 'studio'), {
859
+ view: 'client',
860
+ clientFacing: facing,
861
+ });
862
+ await rejects(review.visiblePrototypes(studio, 'everyone'), 'invalid');
863
+ await rejects(review.visiblePrototypes(null), 'unauthorized');
864
+ await rejects(review.visiblePrototypes(null, 'client'), 'unauthorized');
865
+ });
866
+
867
+ /** A client-facing thread with a Client's reply on it, then the prototype switched back to internal. */
868
+ async function onInternalPrototype() {
869
+ const root = await review.add(studio, { body: 'For the client', anchor, audience: 'client' });
870
+ const reply = await review.reply(client, { prototype: anchor.prototype, thread: root.id, body: 'Seen' });
871
+ await review.setAudience(studio, anchor.prototype, 'internal');
872
+ return { root, reply };
873
+ }
874
+
875
+ test('a Client’s list of an internal prototype is empty, as if it had no comments, and so is Studio’s Client view', async () => {
876
+ await onInternalPrototype();
877
+ assert.deepEqual(await review.listComments(client, anchor.prototype), []);
878
+ assert.deepEqual(await review.listComments(client, anchor.prototype, 'studio'), []);
879
+ assert.deepEqual(await review.listComments(studio, anchor.prototype, 'client'), []);
880
+ // The same answer as a prototype nobody made.
881
+ assert.deepEqual(await review.listComments(client, 'no-such-thing'), []);
882
+ assert.equal((await review.listComments(studio, anchor.prototype)).length, 2);
883
+ assert.deepEqual(await review.allComments(client), []);
884
+ assert.equal((await review.allComments(studio)).length, 2);
885
+ // Client-facing again: the thread is back for them, with its reply.
886
+ await review.setAudience(studio, anchor.prototype, 'client');
887
+ assert.equal((await review.listComments(client, anchor.prototype)).length, 2);
888
+ });
889
+
890
+ test('a Client acting on a client-facing thread of an internal prototype is told it does not exist, by every op', async () => {
891
+ const { root, reply } = await onInternalPrototype();
892
+ const at = { prototype: anchor.prototype };
893
+ for (const id of [root.id, reply.id]) {
894
+ await rejects(review.reply(client, { ...at, thread: id, body: 'x' }), 'not_found');
895
+ await rejects(review.edit(client, { ...at, id, body: 'x' }), 'not_found');
896
+ await rejects(review.remove(client, { ...at, id }), 'not_found');
897
+ await rejects(review.resolve(client, { ...at, id, resolved: true }), 'not_found');
898
+ await rejects(review.resolve(client, { ...at, id, resolved: false }), 'not_found');
899
+ await rejects(review.retriage(client, { ...at, id, triage: { kind: 'bug' } }), 'forbidden');
900
+ assert.equal(await review.locate(client, id), null);
901
+ assert.ok(await review.locate(studio, id));
902
+ }
903
+ // The same answers as an id, and a prototype, that are not there at all.
904
+ await rejects(review.edit(client, { ...at, id: 'nope', body: 'x' }), 'not_found');
905
+ await rejects(review.edit(client, { prototype: 'no-such-thing', id: 'nope', body: 'x' }), 'not_found');
906
+ // A new thread there is refused the same way, and nothing is written or counted.
907
+ await rejects(review.add(client, { body: 'Hello?', anchor }), 'not_found');
908
+ await rejects(
909
+ review.add(client, { body: 'Hello?', anchor: { ...anchor, prototype: 'no-such-thing' } }),
910
+ 'not_found',
911
+ );
912
+ const after = await review.listComments(studio, anchor.prototype);
913
+ assert.deepEqual(
914
+ after.map((c) => [c.body, c.status]),
915
+ [
916
+ ['For the client', 'open'],
917
+ ['Seen', 'open'],
918
+ ],
919
+ );
920
+ // Studio, writing as Studio from Client view, still works there.
921
+ assert.ok(await review.add(studio, { body: 'Internal note', anchor }));
922
+ assert.ok(await review.reply(studio, { ...at, thread: root.id, body: 'Studio reply' }));
923
+ });
924
+
925
+ // Slice 6: what Studio marks on a prototype's dials and variants.
926
+ test('controls: Studio marks a dial either way and a variant on or off, one field each', async () => {
927
+ const kv = memoryKV();
928
+ const own = createReview(kv);
929
+ const mark = (kind: string, id: string, audience: string) =>
930
+ own.setControl(studio, { prototype: 'sample-list', kind, id, audience });
931
+ assert.deepEqual(await own.controlsOf(studio, 'sample-list'), { view: 'studio', dials: {}, variants: [] });
932
+ assert.deepEqual(await mark('dial', 'Review/density', 'client'), {
933
+ kind: 'dial',
934
+ id: 'Review/density',
935
+ audience: 'client',
936
+ });
937
+ // Internal is stored for a dial: it overrules a client() in the code.
938
+ await mark('dial', 'Review/avatars', 'internal');
939
+ await mark('variant', 'Lift', 'client');
940
+ await mark('variant', 'Sink', 'client');
941
+ await mark('variant', 'Sink', 'internal');
942
+ // Unmarking a variant never marked is no error.
943
+ await mark('variant', 'Baseline', 'internal');
944
+ assert.deepEqual(await kv.hgetall(KEY.controls('sample-list')), {
945
+ 'dial:Review/density': 'client',
946
+ 'dial:Review/avatars': 'internal',
947
+ 'variant:Lift': 'client',
948
+ });
949
+ assert.deepEqual(await own.controlsOf(studio, 'sample-list'), {
950
+ view: 'studio',
951
+ dials: { 'Review/density': 'client', 'Review/avatars': 'internal' },
952
+ variants: ['Lift'],
953
+ });
954
+ // Marks are per prototype.
955
+ assert.deepEqual(await own.controlsOf(studio, 'sample-page'), { view: 'studio', dials: {}, variants: [] });
956
+ });
957
+
958
+ test('controls: only Studio marks, and every field is checked', async () => {
959
+ const input = { prototype: 'sample-list', kind: 'dial', id: 'Review/density', audience: 'client' };
960
+ await rejects(review.setControl(client, input), 'forbidden');
961
+ await rejects(review.setControl(null, input), 'unauthorized');
962
+ await assert.rejects(
963
+ review.setControl(studio, { prototype: 'guide', kind: 'knob', id: ' x', audience: 'all' }),
964
+ (e: unknown) =>
965
+ e instanceof ReviewError &&
966
+ e.code === 'invalid' &&
967
+ e.problems.join('|') ===
968
+ [
969
+ 'prototype: a prototype slug',
970
+ 'kind: one of dial, variant',
971
+ 'id: the control’s name, one line of at most 200 characters',
972
+ 'audience: one of internal, client',
973
+ ].join('|'),
974
+ );
975
+ assert.deepEqual((await review.controlsOf(studio, 'sample-list')).dials, {});
976
+ });
977
+
978
+ test('controls: a Client reads the marks of a client-facing prototype, and none of an internal one', async () => {
979
+ await review.setControl(studio, {
980
+ prototype: 'sample-list',
981
+ kind: 'dial',
982
+ id: 'Review/density',
983
+ audience: 'client',
984
+ });
985
+ await review.setControl(studio, {
986
+ prototype: 'secret-plan',
987
+ kind: 'variant',
988
+ id: 'Lift',
989
+ audience: 'client',
990
+ });
991
+ assert.deepEqual(await review.controlsOf(client, 'sample-list'), {
992
+ view: 'client',
993
+ dials: { 'Review/density': 'client' },
994
+ variants: [],
995
+ });
996
+ // The same answer as for a prototype that does not exist.
997
+ await rejects(review.controlsOf(client, 'secret-plan'), 'not_found');
998
+ await rejects(review.controlsOf(client, 'never-made'), 'not_found');
999
+ await rejects(review.controlsOf(null, 'sample-list'), 'unauthorized');
1000
+ await rejects(review.controlsOf(studio, 'guide'), 'invalid');
1001
+ assert.deepEqual((await review.controlsOf(studio, 'secret-plan')).variants, ['Lift']);
1002
+ });
@@ -5,6 +5,8 @@
5
5
  // person, per comment), so two people commenting at once never collide;
6
6
  // two edits of the same comment at once keep the last.
7
7
  import { base64url, hex, sha256 } from '../bytes.ts';
8
+ import { type ControlKind, controlField, type Marks, readMarks } from '../controls.ts';
9
+ import { isPrototypeSlug } from '../prototype.ts';
8
10
  import {
9
11
  type Access,
10
12
  type Anchor,
@@ -17,7 +19,9 @@ import {
17
19
  checkAnchor,
18
20
  checkAudience,
19
21
  checkBody,
22
+ checkControl,
20
23
  checkPersonInput,
24
+ checkPrototypeAudience,
21
25
  checkStates,
22
26
  checkTriage,
23
27
  checkViewAs,
@@ -28,6 +32,7 @@ import {
28
32
  LINK_DAYS,
29
33
  type Link,
30
34
  type Person,
35
+ type PersonKind,
31
36
  readComment,
32
37
  type Triage,
33
38
  } from './model.ts';
@@ -219,6 +224,17 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
219
224
  const saveComment = (prototype: string, c: Comment) =>
220
225
  kv.hset(KEY.comments(prototype), c.id, JSON.stringify(c));
221
226
 
227
+ const isClientFacing = async (prototype: string) =>
228
+ isPrototypeSlug(prototype) && (await kv.hget(KEY.audiences, prototype)) === 'client';
229
+
230
+ /**
231
+ * Whether a view reaches a prototype's comments at all. A Client's view
232
+ * reaches only a client-facing prototype: on any other, every op answers
233
+ * as if it had no comments, so no answer confirms an internal slug.
234
+ */
235
+ const reachable = async (view: PersonKind, prototype: string) =>
236
+ view === 'studio' || (await isClientFacing(prototype));
237
+
222
238
  /**
223
239
  * A comment and its thread's root, if `actor` may see both; else null. A
224
240
  * reply whose root is gone is not there either.
@@ -228,6 +244,7 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
228
244
  prototype: string,
229
245
  id: string,
230
246
  ): Promise<{ comment: Comment; root: Comment } | null> {
247
+ if (!(await reachable(viewOf(actor), prototype))) return null;
231
248
  const comment = await commentAt(prototype, id);
232
249
  if (!comment) return null;
233
250
  const root = isRoot(comment) ? comment : await commentAt(prototype, comment.thread);
@@ -382,14 +399,17 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
382
399
  async listComments(actor: Person | null, prototype: string, viewAs?: unknown): Promise<Comment[]> {
383
400
  allow(actor, 'read');
384
401
  const view = viewOf(actor, valid(checkViewAs(viewAs)));
402
+ if (!(await reachable(view, prototype))) return [];
385
403
  return visibleTo(view, await commentsOf(prototype));
386
404
  },
387
405
 
388
406
  /** Every comment on every prototype the actor may see, for the CLI (which acts as Studio). */
389
407
  async allComments(actor: Person | null): Promise<Comment[]> {
390
408
  allow(actor, 'read');
391
- const prototypes = await kv.smembers(KEY.prototypes);
392
- return visibleTo(viewOf(actor), (await Promise.all(prototypes.map(commentsOf))).flat());
409
+ const view = viewOf(actor);
410
+ const prototypes = [];
411
+ for (const p of await kv.smembers(KEY.prototypes)) if (await reachable(view, p)) prototypes.push(p);
412
+ return visibleTo(view, (await Promise.all(prototypes.map(commentsOf))).flat());
393
413
  },
394
414
 
395
415
  /**
@@ -407,6 +427,9 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
407
427
  checkAnchor(input.anchor),
408
428
  checkAudience(input.audience),
409
429
  );
430
+ // A Client's thread on a prototype that is not client-facing: the answer an unknown id gets.
431
+ if (!(await reachable(viewOf(actor), anchor.prototype)))
432
+ throw new ReviewError('not_found', 'No such comment.');
410
433
  const id = newId();
411
434
  const triage = deps.triage
412
435
  ? await deps
@@ -508,6 +531,92 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
508
531
  return next;
509
532
  },
510
533
 
534
+ /**
535
+ * Marks a prototype client-facing or internal. Only client-facing is
536
+ * stored, so internal deletes the field and a prototype nobody marked is
537
+ * internal. Studio only.
538
+ */
539
+ async setAudience(actor: Person | null, prototype: unknown, audience: unknown): Promise<Audience> {
540
+ allow(actor, 'audience');
541
+ const [slug, value] = validAll<[string, Audience]>(
542
+ isPrototypeSlug(prototype)
543
+ ? { ok: true, value: prototype }
544
+ : { ok: false, problems: ['prototype: a prototype slug'] },
545
+ checkPrototypeAudience(audience),
546
+ );
547
+ if (value === 'client') await kv.hset(KEY.audiences, slug, 'client');
548
+ else await kv.hdel(KEY.audiences, slug);
549
+ return value;
550
+ },
551
+
552
+ /**
553
+ * What Studio marked on a prototype's dials and variants, with the view
554
+ * the actor gets. A Client reads the marks too: their page needs a dial
555
+ * Studio switched back to internal as much as one it shared, and the
556
+ * names are in the prototype's own code already. An internal prototype
557
+ * has none for a Client, as it has no comments.
558
+ */
559
+ async controlsOf(actor: Person | null, prototype: unknown): Promise<{ view: PersonKind } & Marks> {
560
+ allow(actor, 'read');
561
+ if (!isPrototypeSlug(prototype))
562
+ throw new ReviewError('invalid', 'Some fields are not valid.', ['prototype: a prototype slug']);
563
+ const view = viewOf(actor);
564
+ if (!(await reachable(view, prototype))) throw new ReviewError('not_found', 'No such prototype.');
565
+ return { view, ...readMarks(await kv.hgetall(KEY.controls(prototype))) };
566
+ },
567
+
568
+ /**
569
+ * Marks one dial or one variant client-facing or internal. A dial's mark
570
+ * is stored either way, because it overrules what the code says; a
571
+ * variant's internal is the field's absence. Studio only. The server
572
+ * keeps no list of a prototype's controls, so any well-formed id is taken.
573
+ */
574
+ async setControl(
575
+ actor: Person | null,
576
+ input: { prototype: unknown; kind: unknown; id: unknown; audience: unknown },
577
+ ): Promise<{ kind: ControlKind; id: string; audience: Audience }> {
578
+ allow(actor, 'audience');
579
+ const [slug, mark] = validAll<[string, { kind: ControlKind; id: string; audience: Audience }]>(
580
+ isPrototypeSlug(input.prototype)
581
+ ? { ok: true, value: input.prototype }
582
+ : { ok: false, problems: ['prototype: a prototype slug'] },
583
+ checkControl(input),
584
+ );
585
+ const field = controlField(mark.kind, mark.id);
586
+ if (mark.kind === 'variant' && mark.audience === 'internal') await kv.hdel(KEY.controls(slug), field);
587
+ else await kv.hset(KEY.controls(slug), field, mark.audience);
588
+ return mark;
589
+ },
590
+
591
+ /**
592
+ * Who a prototype is for, asked by the gate with no actor. Absent, or any
593
+ * value but `client`, is internal; a name that cannot be a prototype is
594
+ * internal without asking the store.
595
+ */
596
+ async audienceOf(prototype: string): Promise<Audience> {
597
+ return (await isClientFacing(prototype)) ? 'client' : 'internal';
598
+ },
599
+
600
+ /**
601
+ * What the index is built from: the view the actor gets (Client view for
602
+ * Studio with `viewAs: 'client'`) and every client-facing slug. The
603
+ * server keeps no list of internal ones: Studio's view reads the build's
604
+ * own list, which the gate refuses to a Client.
605
+ */
606
+ async visiblePrototypes(
607
+ actor: Person | null,
608
+ viewAs?: unknown,
609
+ ): Promise<{ view: PersonKind; clientFacing: string[] }> {
610
+ allow(actor, 'read');
611
+ const view = viewOf(actor, valid(checkViewAs(viewAs)));
612
+ const all = await kv.hgetall(KEY.audiences);
613
+ const clientFacing = Object.entries(all)
614
+ .filter(([slug, value]) => value === 'client' && isPrototypeSlug(slug))
615
+ .map(([slug]) => slug)
616
+ .sort();
617
+ return { view, clientFacing };
618
+ },
619
+
511
620
  /** Where a comment lives, for the CLI, which has an id and no prototype; null when the actor may not see it. */
512
621
  async locate(actor: Person | null, id: string): Promise<{ prototype: string; comment: Comment } | null> {
513
622
  allow(actor, 'read');
@@ -34,12 +34,25 @@ export const KEY = {
34
34
  /** Project settings, one field each: `access` is the default access length. */
35
35
  settings: `${PREFIX}settings`,
36
36
  prototypes: `${PREFIX}prototypes`,
37
+ /**
38
+ * Which prototypes are client-facing: field `<slug>`, value `client`. Only
39
+ * client-facing is stored; internal is the field's absence, and any other
40
+ * value reads as internal too.
41
+ */
42
+ audiences: `${PREFIX}audiences`,
37
43
  /**
38
44
  * The last thread number given on each prototype, only ever raised by HINCRBY: field
39
45
  * `<prototype>` counts client-facing threads, `<prototype>:internal` internal ones.
40
46
  */
41
47
  counters: `${PREFIX}counters`,
42
48
  comments: (prototype: string) => `${PREFIX}comments:${prototype}`,
49
+ /**
50
+ * What Studio marked on one prototype's controls (core/controls.ts): field
51
+ * `dial:<id>` is `client` or `internal`, overruling the code either way;
52
+ * field `variant:<name>` is `client`, and internal is its absence. One
53
+ * field per control, so two people marking at once never undo each other.
54
+ */
55
+ controls: (prototype: string) => `${PREFIX}controls:${prototype}`,
43
56
  } as const;
44
57
 
45
58
  export type Snapshot = { hashes: Record<string, Record<string, string>>; sets: Record<string, string[]> };
@@ -0,0 +1,55 @@
1
+ export type ControlAudience = 'internal' | 'client';
2
+ export declare const CONTROL_KINDS: readonly ['dial', 'variant'];
3
+ export type ControlKind = (typeof CONTROL_KINDS)[number];
4
+ /** What the hub stored for one prototype: a mark per dial Studio switched, and the variants a Client gets. */
5
+ export type Marks = {
6
+ dials: Record<string, ControlAudience>;
7
+ variants: string[];
8
+ };
9
+ export declare const NO_MARKS: Marks;
10
+ /** A dial's or a variant's name as the store keeps it: one line of text, trimmed, at most 200 characters. */
11
+ export declare const isControlId: (v: unknown) => v is string;
12
+ /** A dial is named by its panel and its key, a folder's dial by the keys down to it: `Review/density`, `Card/shadow.blur`. */
13
+ export declare const dialId: (panel: string, path: readonly string[]) => string;
14
+ /** The store's field for one control. */
15
+ export declare const controlField: (kind: ControlKind, id: string) => string;
16
+ /** The marks in a prototype's stored fields. A field or a value that is not one of ours is left out, so it reads as unmarked. */
17
+ export declare function readMarks(fields: Record<string, string>): Marks;
18
+ /** The marks after one control is switched. */
19
+ export declare function withMark(marks: Marks, kind: ControlKind, id: string, audience: ControlAudience): Marks;
20
+ /** Who a dial is for: the hub's mark when Studio set one, else what the code says, else internal. */
21
+ export declare const dialAudience: (marks: Marks, id: string, inCode: ControlAudience) => ControlAudience;
22
+ /**
23
+ * The variants a Client gets, in the order the prototype declares them. One
24
+ * is no picker. None marked is still one: the first, so a page is never empty.
25
+ */
26
+ export declare function clientVariants(names: readonly string[], marks: Marks): string[];
27
+ type Config = Record<string, unknown>;
28
+ /**
29
+ * Marks a dial client-facing where it is declared, or a folder and every dial
30
+ * in it. Typed as the dial itself, so the values a prototype reads keep their types.
31
+ */
32
+ export declare const client: <T>(dial: T) => T;
33
+ export type Dial = {
34
+ id: string;
35
+ path: string[];
36
+ inCode: ControlAudience;
37
+ };
38
+ /** A config as DialKit takes it (every `client()` unwrapped) and the dials in it. */
39
+ export declare function readConfig(panel: string, config: Config): {
40
+ config: Config;
41
+ dials: Dial[];
42
+ };
43
+ /** The part of an unwrapped config whose dials `shown` keeps. A folder with none left goes with them. */
44
+ export declare function keepDials(panel: string, config: Config, shown: (id: string) => boolean): Config;
45
+ /**
46
+ * What a dial reads when nobody can move it: the value DialKit would start it
47
+ * at. A test holds this equal to DialKit's own for every kind of dial.
48
+ */
49
+ export declare function defaultOf(dial: unknown): unknown;
50
+ /**
51
+ * The values a prototype reads: DialKit's for the dials it drew, the default
52
+ * for every dial it did not, in the shape of the whole config.
53
+ */
54
+ export declare function withDefaults(config: Config, values: Config): Config;
55
+ export {};