@yaks/inspect 0.0.0-stage → 0.2.36

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 (133) hide show
  1. package/Comp.js +469 -0
  2. package/Comp.js.map +1 -0
  3. package/Comp.ts +471 -0
  4. package/Effect.js +136 -0
  5. package/Effect.js.map +1 -0
  6. package/Effect.ts +142 -0
  7. package/Entity.js +112 -0
  8. package/Entity.js.map +1 -0
  9. package/Entity.ts +137 -0
  10. package/Facts.js +143 -0
  11. package/Facts.js.map +1 -0
  12. package/Facts.ts +150 -0
  13. package/Head.js +196 -0
  14. package/Head.js.map +1 -0
  15. package/Head.ts +195 -0
  16. package/Held.js +144 -0
  17. package/Held.js.map +1 -0
  18. package/Held.ts +172 -0
  19. package/History.js +157 -0
  20. package/History.js.map +1 -0
  21. package/History.ts +215 -0
  22. package/Home.js +482 -0
  23. package/Home.js.map +1 -0
  24. package/Home.ts +550 -0
  25. package/LICENSE +202 -0
  26. package/Links.js +197 -0
  27. package/Links.js.map +1 -0
  28. package/Links.ts +212 -0
  29. package/Package.js +195 -0
  30. package/Package.js.map +1 -0
  31. package/Package.ts +191 -0
  32. package/Past.js +163 -0
  33. package/Past.js.map +1 -0
  34. package/Past.ts +181 -0
  35. package/Prop.js +225 -0
  36. package/Prop.js.map +1 -0
  37. package/Prop.ts +208 -0
  38. package/Query.js +207 -0
  39. package/Query.js.map +1 -0
  40. package/Query.ts +229 -0
  41. package/README.md +141 -2
  42. package/Words.js +43 -0
  43. package/Words.js.map +1 -0
  44. package/Words.ts +70 -0
  45. package/_dist/Entity.d.ts +7 -0
  46. package/_dist/Entity.d.ts.map +1 -0
  47. package/_dist/Facts.d.ts +7 -0
  48. package/_dist/Facts.d.ts.map +1 -0
  49. package/_dist/Head.d.ts +22 -0
  50. package/_dist/Head.d.ts.map +1 -0
  51. package/_dist/Held.d.ts +37 -0
  52. package/_dist/Held.d.ts.map +1 -0
  53. package/_dist/Links.d.ts +9 -0
  54. package/_dist/Links.d.ts.map +1 -0
  55. package/_dist/Past.d.ts +65 -0
  56. package/_dist/Past.d.ts.map +1 -0
  57. package/_dist/Words.d.ts +27 -0
  58. package/_dist/Words.d.ts.map +1 -0
  59. package/_dist/census.d.ts +10 -0
  60. package/_dist/census.d.ts.map +1 -0
  61. package/_dist/door.d.ts +28 -0
  62. package/_dist/door.d.ts.map +1 -0
  63. package/_dist/front.d.ts +4 -0
  64. package/_dist/front.d.ts.map +1 -0
  65. package/_dist/host.d.ts +94 -0
  66. package/_dist/host.d.ts.map +1 -0
  67. package/_dist/mod.d.ts +24 -0
  68. package/_dist/mod.d.ts.map +1 -0
  69. package/_dist/notes.d.ts +38 -0
  70. package/_dist/notes.d.ts.map +1 -0
  71. package/_dist/read.d.ts +59 -0
  72. package/_dist/read.d.ts.map +1 -0
  73. package/_dist/reading.d.ts +21 -0
  74. package/_dist/reading.d.ts.map +1 -0
  75. package/_dist/registry.d.ts +12 -0
  76. package/_dist/registry.d.ts.map +1 -0
  77. package/_dist/schema.d.ts +4 -0
  78. package/_dist/schema.d.ts.map +1 -0
  79. package/_dist/state.d.ts +22 -0
  80. package/_dist/state.d.ts.map +1 -0
  81. package/_dist/value.d.ts +20 -0
  82. package/_dist/value.d.ts.map +1 -0
  83. package/_dist/views.d.ts +3 -0
  84. package/_dist/views.d.ts.map +1 -0
  85. package/census.js +50 -0
  86. package/census.js.map +1 -0
  87. package/census.ts +76 -0
  88. package/deno.json +1 -0
  89. package/door.js +54 -0
  90. package/door.js.map +1 -0
  91. package/door.ts +59 -0
  92. package/front.js +12 -0
  93. package/front.js.map +1 -0
  94. package/front.json +76 -0
  95. package/front.ts +13 -0
  96. package/grid.js +249 -0
  97. package/grid.js.map +1 -0
  98. package/grid.ts +395 -0
  99. package/host.js +20 -0
  100. package/host.js.map +1 -0
  101. package/host.ts +134 -0
  102. package/mod.js +20 -0
  103. package/mod.js.map +1 -0
  104. package/mod.ts +45 -0
  105. package/notes.js +184 -0
  106. package/notes.js.map +1 -0
  107. package/notes.ts +288 -0
  108. package/package.json +35 -4
  109. package/read.js +300 -0
  110. package/read.js.map +1 -0
  111. package/read.ts +386 -0
  112. package/reading.js +64 -0
  113. package/reading.js.map +1 -0
  114. package/reading.ts +73 -0
  115. package/registry.js +89 -0
  116. package/registry.js.map +1 -0
  117. package/registry.ts +89 -0
  118. package/rows.js +12 -0
  119. package/rows.js.map +1 -0
  120. package/rows.ts +26 -0
  121. package/schema.js +47 -0
  122. package/schema.js.map +1 -0
  123. package/schema.ts +70 -0
  124. package/state.js +85 -0
  125. package/state.js.map +1 -0
  126. package/state.ts +128 -0
  127. package/terminal-links.ts +16 -0
  128. package/value.js +67 -0
  129. package/value.js.map +1 -0
  130. package/value.ts +79 -0
  131. package/views.js +4 -0
  132. package/views.js.map +1 -0
  133. package/views.ts +6 -0
package/Past.ts ADDED
@@ -0,0 +1,181 @@
1
+ /**
2
+ * An entity as it was: what it held just after one of its transactions,
3
+ * rebuilt from its history (@yaks/journal's `_change`), and a deleted
4
+ * entity's page, which reads what it held last. Both read the way a page is
5
+ * read and are never edited: a head saying when, and by whom; what it said
6
+ * (./Entity.ts `Body`); what else it stated (./Facts.ts); and under them its
7
+ * history (./History.ts), each moment of which opens its own version.
8
+ *
9
+ * A change's value is what it wrote of one component, so a version is every
10
+ * change up to its transaction merged, oldest first: an upsert into its
11
+ * component, a removal taking the component away, and a deletion (a change of
12
+ * `entity` with no value) all of it, for the rest of that transaction.
13
+ *
14
+ * The history is asked once, its newest `VERSIONS` changes, and a version is
15
+ * rebuilt from those; an entity with more says the older ones are left out.
16
+ *
17
+ * @module
18
+ */
19
+
20
+ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h, type JSX } from 'react'
21
+ import * as ui from '@yaks/ui'
22
+ import type { Bundle, Io } from './host.ts'
23
+ import { Body } from './Entity.js'
24
+ import { Facts } from './Facts.js'
25
+ import { History } from './History.js'
26
+ import { useNamed } from './notes.js'
27
+ import { type Comp, comp, str, unique } from './read.js'
28
+ import { rows, waiting } from './rows.js'
29
+ import { mention } from './value.js'
30
+
31
+ /** How many of an entity's newest changes its versions are rebuilt from. */
32
+ export let VERSIONS = 500
33
+
34
+ /** One change a version is rebuilt from: its transaction, the name of the
35
+ * component it wrote, and what it wrote there, or null where it removed it. */
36
+ export type Change = { tx: string; comp: string; value: Comp | null }
37
+
38
+ /** What an entity held just after a transaction, and whether that
39
+ * transaction deleted it. */
40
+ export type Version = {
41
+ tx: string
42
+ held: Record<string, Comp>
43
+ deleted: boolean
44
+ }
45
+
46
+ /**
47
+ * Each transaction's version of an entity, oldest first, from its changes in
48
+ * the order they were made.
49
+ *
50
+ * ```ts
51
+ * import { assertEquals } from '@std/assert'
52
+ * let doc = (value: Record<string, unknown> | null) => ({ comp: 'doc', value })
53
+ * assertEquals(versions([
54
+ * { tx: 't1', ...doc({ title: 'Plan', body: 'draft' }) },
55
+ * { tx: 't2', ...doc({ title: 'Plan B' }) },
56
+ * { tx: 't3', comp: 'entity', value: null },
57
+ * { tx: 't3', ...doc({ title: 'gone' }) },
58
+ * ]), [
59
+ * { tx: 't1', held: { doc: { title: 'Plan', body: 'draft' } }, deleted: false },
60
+ * { tx: 't2', held: { doc: { title: 'Plan B', body: 'draft' } }, deleted: false },
61
+ * { tx: 't3', held: {}, deleted: true },
62
+ * ])
63
+ * ```
64
+ */
65
+ export let versions = (changes: Change[]): Version[] => {
66
+ let out: Version[] = []
67
+ let held: Record<string, Comp> = {}
68
+ for (let c of changes) {
69
+ let last = out.at(-1)
70
+ if (last?.tx != c.tx) out.push(last = { tx: c.tx, held, deleted: false })
71
+ if (last.deleted || !c.comp) continue
72
+ if (c.comp == 'entity') {
73
+ if (c.value == null) [held, last.deleted] = [{}, true]
74
+ } else if (c.value == null) {
75
+ let { [c.comp]: _gone, ...rest } = held
76
+ held = rest
77
+ } else held = { ...held, [c.comp]: { ...held[c.comp], ...c.value } }
78
+ last.held = held
79
+ }
80
+ return out
81
+ }
82
+
83
+ /** The version a page reads: the one just after `tx`, or without one the
84
+ * last before a deletion, what a deleted entity held last. */
85
+ export let version = (all: Version[], tx?: string): Version | undefined =>
86
+ tx ? all.find((v) => v.tx == tx) : all.findLast((v) => !v.deleted)
87
+
88
+ // An entity's versions, from its newest changes and the components they name.
89
+ let useVersions = (io: Io, eid: string) => {
90
+ let got = io.ask({
91
+ changes: {
92
+ query: `._change&._change.target=${eid}&.limit=${VERSIONS}`,
93
+ once: true,
94
+ },
95
+ })
96
+ let all = rows(got.changes)
97
+ let comps = unique(all.map((b) => str(b, '_change', 'comp')))
98
+ io.ask(
99
+ comps.length
100
+ ? { comps: `._comp&.entity.eid=${comps.join(',')}&.fields=_comp.name` }
101
+ : {},
102
+ )
103
+ let key = (b: Bundle) => [str(b, '_change', 'tx'), b.entity.eid].join(' ')
104
+ let changes = all.toSorted((a, b) => key(a).localeCompare(key(b)))
105
+ .map((b): Change => ({
106
+ tx: str(b, '_change', 'tx'),
107
+ comp: str(io.get(str(b, '_change', 'comp')), '_comp', 'name'),
108
+ value: (comp(b, '_change').value ?? null) as Comp | null,
109
+ }))
110
+ return {
111
+ answer: got.changes,
112
+ list: versions(changes),
113
+ cut: all.length >= VERSIONS,
114
+ }
115
+ }
116
+
117
+ /** What a past page is drawn with. */
118
+ export type PastProps = {
119
+ /** the entity: its eid, or the id that names it, which its history is
120
+ * asked by */
121
+ e: Bundle
122
+ io: Io
123
+ /** what the page calls it: the id in its address */
124
+ id: string
125
+ /** the transaction whose version it reads; without one, the last version
126
+ * of a deleted entity */
127
+ at?: string
128
+ /** what shows when its history holds no such version */
129
+ lost: JSX.Element
130
+ }
131
+
132
+ /** An entity as it was after a transaction, or as it was last, deleted. */
133
+ export let Past = ({ e, io, id, at, lost }: PastProps): JSX.Element => {
134
+ let eid = e.entity.eid
135
+ let { answer, list, cut } = useVersions(io, eid)
136
+ let shown = version(list, at)
137
+ let gone = at ? undefined : list.findLast((v) => v.deleted)
138
+ let tx = at ?? gone?.tx
139
+ io.ask(tx ? { tx: `._tx&.entity.eid=${tx}` } : {})
140
+ let w = comp(io.get(tx ?? ''), '_tx')
141
+ let by = typeof w.by == 'string' ? w.by : ''
142
+ let via = typeof w.via == 'string' && w.via != by ? w.via : ''
143
+ useNamed(io, [by, via])
144
+ let wait = waiting(answer)
145
+ if (wait) return wait
146
+ if (!shown || !at && !gone) return lost
147
+ let b: Bundle = { ...shown.held, entity: { ...e.entity } }
148
+ let still: Io = { ...io, edits: false }
149
+ return h(
150
+ 'div',
151
+ { 'data-past': eid },
152
+ h(
153
+ ui.Head,
154
+ {},
155
+ h(
156
+ ui.Head.Title,
157
+ {},
158
+ str(b, 'doc', 'title') || id,
159
+ h(ui.Head.Id, {}, id),
160
+ h(ui.Head.Kind, {}, io.kind(b)),
161
+ ),
162
+ h(
163
+ ui.Head.Facts,
164
+ {},
165
+ h(
166
+ 'span',
167
+ {},
168
+ at ? 'as it was ' : 'deleted ',
169
+ w.at ? io.when(String(w.at)) : '',
170
+ by ? [' by ', mention(io, by)] : '',
171
+ via ? [' via ', mention(io, via)] : '',
172
+ ),
173
+ at ? h('a', { href: io.link(eid) }, 'as it is') : null,
174
+ cut ? `rebuilt from its newest ${VERSIONS} changes` : null,
175
+ ),
176
+ ),
177
+ h(Body, { e: b, io: still, ctx: {}, got: {} }),
178
+ h(Facts, { e: b, io: still, ctx: { notes: new Map() }, got: {} }),
179
+ h(History, { e, io, notes: new Map() }),
180
+ )
181
+ }
package/Prop.js ADDED
@@ -0,0 +1,225 @@
1
+ /**
2
+ * A property's page (`_prop`, @yaks/vocab): what it is and holds; the values
3
+ * it holds, most common first, with how many entities hold each; and the
4
+ * recent writes of its component that touched it.
5
+ *
6
+ * Ranking values is a tally over every entity carrying the component, asked
7
+ * once. Where the vocabulary bounds the values (an enum), or few entities
8
+ * carry the component, it is asked as the page opens; otherwise a press asks
9
+ * it, since a tally over a free-text property of many entities is as large
10
+ * as the entities are many (README, Limits).
11
+ *
12
+ * @module
13
+ */ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h } from "react";
14
+ import { parse } from "@yaks/query";
15
+ import { Button, Rows, Value } from "@yaks/ui";
16
+ import { useCensus } from "./census.js";
17
+ import { usePageNotes } from "./Entity.js";
18
+ import { Grid, SIZE } from "./grid.js";
19
+ import { History } from "./History.js";
20
+ import { chip, compEid, Described, described } from "./schema.js";
21
+ import { Part, useNamed } from "./notes.js";
22
+ import { comp, count, face, flags, line, named, str, typed } from "./read.js";
23
+ import { waiting } from "./rows.js";
24
+ import { grid, key, turned } from "./state.js";
25
+ /** How many entities may carry a component before ranking its values waits
26
+ * for a press. */ export let RANKED = 5000;
27
+ let HEADINGS = [
28
+ 'Values',
29
+ 'Recent writes'
30
+ ];
31
+ // A property's component and its own name.
32
+ let split = (e)=>{
33
+ let [name, ...rest] = str(e, 'doc', 'title').split('.');
34
+ return [
35
+ name,
36
+ rest.join('.') || str(e, '_prop', 'name')
37
+ ];
38
+ };
39
+ // Whether the graph ranks this property's values: text, enum, id and number.
40
+ let ranks = (p)=>!!p && (p.category != 'scalar' || [
41
+ 'text',
42
+ 'url',
43
+ 'query',
44
+ 'number',
45
+ 'priority'
46
+ ].includes(p.scalar));
47
+ /**
48
+ * A value as a query term: bare where it is one word, quoted otherwise.
49
+ *
50
+ * ```ts
51
+ * import { quoted } from './Prop.ts'
52
+ * quoted('open') // 'open'
53
+ * quoted('two words') // '"two words"'
54
+ * quoted('say "hi"') // "'say \"hi\"'"
55
+ * ```
56
+ */ export let quoted = (v)=>/^[\w.:@#-]+$/.test(v) ? v : v.includes('"') ? `'${v}'` : `"${v}"`;
57
+ // What the head says under the property's name: what it is, then whose it
58
+ // is, its type and what is special about it.
59
+ let about = (io, e)=>{
60
+ let [name] = split(e);
61
+ let p = comp(e, '_prop');
62
+ let pkg = str(e, '_prop', 'namespace');
63
+ let ref = str(e, '_prop', 'ref');
64
+ let listed = (k)=>p[k] != null ? [
65
+ ` · ${k} ${face(p[k])}`
66
+ ] : [];
67
+ return [
68
+ 'of ',
69
+ chip(io, name),
70
+ pkg ? [
71
+ ' from ',
72
+ /*#__PURE__*/ h('a', {
73
+ href: io.link(pkg)
74
+ }, io.name(pkg))
75
+ ] : '',
76
+ ' · ',
77
+ typed({
78
+ ...p,
79
+ ref: null
80
+ }) || 'untyped',
81
+ ref ? [
82
+ ' → ',
83
+ ref == 'entity' ? 'any entity' : chip(io, ref)
84
+ ] : '',
85
+ ...flags(p).map((f)=>` · ${f}`),
86
+ ...[
87
+ 'enum',
88
+ 'default',
89
+ 'death'
90
+ ].flatMap(listed)
91
+ ];
92
+ };
93
+ // The values it holds, most common first.
94
+ let Values = ({ e, io, notes })=>{
95
+ let eid = e.entity.eid;
96
+ let [name, prop] = split(e);
97
+ let p = io.vocab.prop(name, prop);
98
+ let id = key(eid, 'Values');
99
+ let census = useCensus(io);
100
+ let n = census.ready ? census.carried[name] ?? 0 : undefined;
101
+ let ranked = ranks(p) && (p?.category == 'enum' || !!grid(io, id).rank || n != null && n <= RANKED);
102
+ let tallied = io.ask(ranked ? {
103
+ tally: {
104
+ query: `.${name}&.tally=${name}.${prop}`,
105
+ once: true
106
+ }
107
+ } : {});
108
+ let tally = tallied.tally?.tally ?? {};
109
+ let values = Object.keys(tally).toSorted((a, b)=>tally[b] - tally[a]);
110
+ let ref = p?.category == 'ref';
111
+ let numbered = [
112
+ 'number',
113
+ 'priority'
114
+ ].includes(p?.scalar ?? '');
115
+ let page = grid(io, id).after?.length ?? 0;
116
+ useNamed(io, ref ? values.slice(page * SIZE, (page + 1) * SIZE) : []);
117
+ let body = !ranks(p) ? /*#__PURE__*/ h(Rows.More, {}, 'the graph ranks only text, enum, id and number values') : !ranked ? waiting({
118
+ rows: [],
119
+ ready: census.ready,
120
+ error: census.error
121
+ }) ?? /*#__PURE__*/ h(Rows.More, {}, `${count(n ?? 0)} entities carry ${name}; ranking what they hold here ` + 'reads every one. ', /*#__PURE__*/ h(Button, {
122
+ type: 'button',
123
+ mod: 'quiet',
124
+ onClick: ()=>io.set(turned(id, {
125
+ rank: true
126
+ }))
127
+ }, 'rank them')) : waiting(tallied.tally) ?? /*#__PURE__*/ h(Grid, {
128
+ io,
129
+ id,
130
+ local: true,
131
+ rows: values.map((v)=>({
132
+ entity: {
133
+ eid: v
134
+ }
135
+ })),
136
+ pick: ref ? (b)=>named(b.entity.eid) ? b.entity.eid : undefined : false,
137
+ columns: [
138
+ {
139
+ name: 'value',
140
+ cell: (b)=>{
141
+ let v = b.entity.eid;
142
+ return ref && named(v) ? /*#__PURE__*/ h('a', {
143
+ href: io.link(v)
144
+ }, io.name(v)) : /*#__PURE__*/ h('a', {
145
+ href: io.find(`.${name}.${prop}=${quoted(v)}`)
146
+ }, /*#__PURE__*/ h(Value, {
147
+ mod: numbered ? 'num' : 'text'
148
+ }, line(v, 120)));
149
+ }
150
+ },
151
+ {
152
+ name: 'entities',
153
+ mod: 'num',
154
+ cell: (b)=>count(tally[b.entity.eid] ?? 0)
155
+ }
156
+ ]
157
+ });
158
+ return /*#__PURE__*/ h(Part, {
159
+ io,
160
+ eid,
161
+ subject: str(e, 'doc', 'title'),
162
+ heading: 'Values',
163
+ count: tallied.tally?.tally ? values.length : undefined,
164
+ notes
165
+ }, body);
166
+ };
167
+ // What a property's page says, under whichever head tops it.
168
+ let useProp = ({ e, io })=>{
169
+ let notes = usePageNotes(io, e, HEADINGS);
170
+ let [name, prop] = split(e);
171
+ useNamed(io, [
172
+ str(e, '_prop', 'namespace')
173
+ ]);
174
+ return {
175
+ notes,
176
+ text: str(e, 'doc', 'body'),
177
+ facts: about(io, e),
178
+ parts: [
179
+ /*#__PURE__*/ h(Values, {
180
+ e,
181
+ io,
182
+ notes
183
+ }),
184
+ /*#__PURE__*/ h(History, {
185
+ e,
186
+ io,
187
+ notes,
188
+ heading: 'Recent writes',
189
+ where: `comp=${compEid(io.vocab, name)}`,
190
+ prop,
191
+ once: true
192
+ })
193
+ ]
194
+ };
195
+ };
196
+ /** A property's own page. */ export let PropPage = (p)=>{
197
+ let { notes, text, facts, parts } = useProp(p);
198
+ return /*#__PURE__*/ h('div', {
199
+ 'data-inspect': p.e.entity.eid
200
+ }, p.io.show(p.e, 'Inspect.Head', {
201
+ sub: described(text, facts),
202
+ notes: notes.get(''),
203
+ subject: str(p.e, 'doc', 'title')
204
+ }), ...parts);
205
+ };
206
+ /** A property's page under another head (the body of its Full page). */ export let PropBody = (p)=>{
207
+ let { text, facts, parts } = useProp(p);
208
+ return /*#__PURE__*/ h('div', {}, /*#__PURE__*/ h(Described, {
209
+ text,
210
+ facts
211
+ }), ...parts);
212
+ };
213
+ /** A property's page, and its body under another head. */ export let propViews = [
214
+ {
215
+ view: 'Inspect.Page',
216
+ match: parse('._prop'),
217
+ Render: PropPage
218
+ },
219
+ {
220
+ view: 'Full.Body',
221
+ match: parse('.doc ._prop'),
222
+ Render: PropBody
223
+ }
224
+ ];
225
+ //# sourceMappingURL=Prop.js.map
package/Prop.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Prop.js","sources":["./Prop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;CAYC,GAED,uCAAuC,GAAG,SAAS,iBAAiB,CAAC,gBAAoD;AACzH,SAAS,KAAK,2BAAgC;AAC9C,SAAS,MAAM,EAAE,IAAI,EAAE,KAAK,wBAA6B;AAGzD,SAAS,SAAS,sBAAqB;AACvC,SAAS,YAAY,sBAAqB;AAC1C,SAAS,IAAI,EAAE,IAAI,oBAAmB;AACtC,SAAS,OAAO,uBAAsB;AACtC,SAAS,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,sBAAqB;AACjE,SAAS,IAAI,EAAE,QAAQ,qBAAoB;AAC3C,SAAS,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,oBAAmB;AAC7E,SAAS,OAAO,oBAAmB;AACnC,SAAS,IAAI,EAAE,GAAG,EAAE,MAAM,qBAAoB;AAE9C;gBACgB,GAChB,OAAO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AQ;EAAS;CACpE,CAAA"}
package/Prop.ts ADDED
@@ -0,0 +1,208 @@
1
+ /**
2
+ * A property's page (`_prop`, @yaks/vocab): what it is and holds; the values
3
+ * it holds, most common first, with how many entities hold each; and the
4
+ * recent writes of its component that touched it.
5
+ *
6
+ * Ranking values is a tally over every entity carrying the component, asked
7
+ * once. Where the vocabulary bounds the values (an enum), or few entities
8
+ * carry the component, it is asked as the page opens; otherwise a press asks
9
+ * it, since a tally over a free-text property of many entities is as large
10
+ * as the entities are many (README, Limits).
11
+ *
12
+ * @module
13
+ */
14
+
15
+ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h, type JSX, type ReactNode } from 'react'
16
+ import { parse } from '@yaks/query'
17
+ import { Button, Rows, Value } from '@yaks/ui'
18
+ import type { Prop } from 'jsr:@yaks/vocab@0.2.36'
19
+ import type { Bundle, Io, Props, View } from './host.ts'
20
+ import { useCensus } from './census.js'
21
+ import { usePageNotes } from './Entity.js'
22
+ import { Grid, SIZE } from './grid.js'
23
+ import { History } from './History.js'
24
+ import { chip, compEid, Described, described } from './schema.js'
25
+ import { Part, useNamed } from './notes.js'
26
+ import { comp, count, face, flags, line, named, str, typed } from './read.js'
27
+ import { waiting } from './rows.js'
28
+ import { grid, key, turned } from './state.js'
29
+
30
+ /** How many entities may carry a component before ranking its values waits
31
+ * for a press. */
32
+ export let RANKED = 5000
33
+
34
+ let HEADINGS = ['Values', 'Recent writes']
35
+
36
+ // A property's component and its own name.
37
+ let split = (e: Bundle): [string, string] => {
38
+ let [name, ...rest] = str(e, 'doc', 'title').split('.')
39
+ return [name, rest.join('.') || str(e, '_prop', 'name')]
40
+ }
41
+
42
+ // Whether the graph ranks this property's values: text, enum, id and number.
43
+ let ranks = (p?: Prop) =>
44
+ !!p &&
45
+ (p.category != 'scalar' ||
46
+ ['text', 'url', 'query', 'number', 'priority'].includes(p.scalar!))
47
+
48
+ /**
49
+ * A value as a query term: bare where it is one word, quoted otherwise.
50
+ *
51
+ * ```ts
52
+ * import { quoted } from './Prop.ts'
53
+ * quoted('open') // 'open'
54
+ * quoted('two words') // '"two words"'
55
+ * quoted('say "hi"') // "'say \"hi\"'"
56
+ * ```
57
+ */
58
+ export let quoted = (v: string): string =>
59
+ /^[\w.:@#-]+$/.test(v) ? v : v.includes('"') ? `'${v}'` : `"${v}"`
60
+
61
+ type Part_ = { e: Bundle; io: Io; notes: Map<string, Bundle[]> }
62
+
63
+ // What the head says under the property's name: what it is, then whose it
64
+ // is, its type and what is special about it.
65
+ let about = (io: Io, e: Bundle): ReactNode[] => {
66
+ let [name] = split(e)
67
+ let p = comp(e, '_prop')
68
+ let pkg = str(e, '_prop', 'namespace')
69
+ let ref = str(e, '_prop', 'ref')
70
+ let listed = (k: string) => p[k] != null ? [` · ${k} ${face(p[k])}`] : []
71
+ return [
72
+ 'of ',
73
+ chip(io, name),
74
+ pkg ? [' from ', h('a', { href: io.link(pkg) }, io.name(pkg))] : '',
75
+ ' · ',
76
+ typed({ ...p, ref: null }) || 'untyped',
77
+ ref ? [' → ', ref == 'entity' ? 'any entity' : chip(io, ref)] : '',
78
+ ...flags(p).map((f) => ` · ${f}`),
79
+ ...['enum', 'default', 'death'].flatMap(listed),
80
+ ]
81
+ }
82
+
83
+ // The values it holds, most common first.
84
+ let Values = ({ e, io, notes }: Part_) => {
85
+ let eid = e.entity.eid
86
+ let [name, prop] = split(e)
87
+ let p = io.vocab.prop(name, prop)
88
+ let id = key(eid, 'Values')
89
+ let census = useCensus(io)
90
+ let n = census.ready ? census.carried[name] ?? 0 : undefined
91
+ let ranked = ranks(p) &&
92
+ (p?.category == 'enum' || !!grid(io, id).rank || (n != null && n <= RANKED))
93
+ let tallied = io.ask(
94
+ ranked
95
+ ? { tally: { query: `.${name}&.tally=${name}.${prop}`, once: true } }
96
+ : {},
97
+ )
98
+ let tally = tallied.tally?.tally ?? {}
99
+ let values = Object.keys(tally).toSorted((a, b) => tally[b] - tally[a])
100
+ let ref = p?.category == 'ref'
101
+ let numbered = ['number', 'priority'].includes(p?.scalar ?? '')
102
+ let page = grid(io, id).after?.length ?? 0
103
+ useNamed(io, ref ? values.slice(page * SIZE, (page + 1) * SIZE) : [])
104
+ let body = !ranks(p)
105
+ ? h(Rows.More, {}, 'the graph ranks only text, enum, id and number values')
106
+ : !ranked
107
+ ? waiting({ rows: [], ready: census.ready, error: census.error }) ?? h(
108
+ Rows.More,
109
+ {},
110
+ `${count(n ?? 0)} entities carry ${name}; ranking what they hold here ` +
111
+ 'reads every one. ',
112
+ h(Button, {
113
+ type: 'button',
114
+ mod: 'quiet',
115
+ onClick: () => io.set(turned(id, { rank: true })),
116
+ }, 'rank them'),
117
+ )
118
+ : waiting(tallied.tally) ?? h(Grid, {
119
+ io,
120
+ id,
121
+ local: true,
122
+ rows: values.map((v): Bundle => ({ entity: { eid: v } })),
123
+ pick: ref ? (b) => named(b.entity.eid) ? b.entity.eid : undefined : false,
124
+ columns: [
125
+ {
126
+ name: 'value',
127
+ cell: (b) => {
128
+ let v = b.entity.eid
129
+ return ref && named(v)
130
+ ? h('a', { href: io.link(v) }, io.name(v))
131
+ : h(
132
+ 'a',
133
+ { href: io.find(`.${name}.${prop}=${quoted(v)}`) },
134
+ h(Value, { mod: numbered ? 'num' : 'text' }, line(v, 120)),
135
+ )
136
+ },
137
+ },
138
+ {
139
+ name: 'entities',
140
+ mod: 'num',
141
+ cell: (b) => count(tally[b.entity.eid] ?? 0),
142
+ },
143
+ ],
144
+ })
145
+ return h(
146
+ Part,
147
+ {
148
+ io,
149
+ eid,
150
+ subject: str(e, 'doc', 'title'),
151
+ heading: 'Values',
152
+ count: tallied.tally?.tally ? values.length : undefined,
153
+ notes,
154
+ },
155
+ body,
156
+ )
157
+ }
158
+
159
+ // What a property's page says, under whichever head tops it.
160
+ let useProp = ({ e, io }: Props) => {
161
+ let notes = usePageNotes(io, e, HEADINGS)
162
+ let [name, prop] = split(e)
163
+ useNamed(io, [str(e, '_prop', 'namespace')])
164
+ return {
165
+ notes,
166
+ text: str(e, 'doc', 'body'),
167
+ facts: about(io, e),
168
+ parts: [
169
+ h(Values, { e, io, notes }),
170
+ h(History, {
171
+ e,
172
+ io,
173
+ notes,
174
+ heading: 'Recent writes',
175
+ where: `comp=${compEid(io.vocab, name)}`,
176
+ prop,
177
+ once: true,
178
+ }),
179
+ ],
180
+ }
181
+ }
182
+
183
+ /** A property's own page. */
184
+ export let PropPage = (p: Props): JSX.Element => {
185
+ let { notes, text, facts, parts } = useProp(p)
186
+ return h(
187
+ 'div',
188
+ { 'data-inspect': p.e.entity.eid },
189
+ p.io.show(p.e, 'Inspect.Head', {
190
+ sub: described(text, facts),
191
+ notes: notes.get(''),
192
+ subject: str(p.e, 'doc', 'title'),
193
+ }),
194
+ ...parts,
195
+ )
196
+ }
197
+
198
+ /** A property's page under another head (the body of its Full page). */
199
+ export let PropBody = (p: Props): JSX.Element => {
200
+ let { text, facts, parts } = useProp(p)
201
+ return h('div', {}, h(Described, { text, facts }), ...parts)
202
+ }
203
+
204
+ /** A property's page, and its body under another head. */
205
+ export let propViews: View[] = [
206
+ { view: 'Inspect.Page', match: parse('._prop'), Render: PropPage },
207
+ { view: 'Full.Body', match: parse('.doc ._prop'), Render: PropBody },
208
+ ]