@astra-spec/ui 0.0.1 → 0.0.3
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.
- package/README.md +70 -4
- package/TOKENS.md +6 -2
- package/components.css +1 -0
- package/dist/components/index.d.ts +3 -1
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +2 -1
- package/dist/components/index.js.map +1 -1
- package/dist/components/record-preview.d.ts +61 -0
- package/dist/components/record-preview.d.ts.map +1 -0
- package/dist/components/record-preview.js +177 -0
- package/dist/components/record-preview.js.map +1 -0
- package/dist/lib/labels.d.ts +12 -0
- package/dist/lib/labels.d.ts.map +1 -1
- package/dist/lib/labels.js +12 -0
- package/dist/lib/labels.js.map +1 -1
- package/dist/primitives/index.d.ts +3 -1
- package/dist/primitives/index.d.ts.map +1 -1
- package/dist/primitives/index.js +1 -0
- package/dist/primitives/index.js.map +1 -1
- package/dist/primitives/preview-popover.d.ts +30 -0
- package/dist/primitives/preview-popover.d.ts.map +1 -0
- package/dist/primitives/preview-popover.js +132 -0
- package/dist/primitives/preview-popover.js.map +1 -0
- package/dist/primitives/prose.d.ts +7 -1
- package/dist/primitives/prose.d.ts.map +1 -1
- package/dist/primitives/prose.js +13 -5
- package/dist/primitives/prose.js.map +1 -1
- package/package.json +2 -1
- package/primitives.css +1 -0
- package/src/components/index.ts +11 -1
- package/src/components/record-preview.tsx +761 -0
- package/src/lib/labels.tsx +27 -0
- package/src/primitives/index.ts +14 -1
- package/src/primitives/preview-popover.tsx +278 -0
- package/src/primitives/prose.tsx +28 -5
- package/styles/components/record-preview.css +316 -0
- package/styles/primitives/preview-popover.css +76 -0
- package/styles/tokens.css +15 -8
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnalysisIndex,
|
|
3
|
+
ResolvedAnalysisDocument,
|
|
4
|
+
ResolvedAnalysisNode,
|
|
5
|
+
ResolvedDecision,
|
|
6
|
+
ResolvedEvidence,
|
|
7
|
+
ResolvedInput,
|
|
8
|
+
ResolvedInsight,
|
|
9
|
+
ResolvedOutput,
|
|
10
|
+
ResolvedRecord,
|
|
11
|
+
} from '@astra-spec/sdk';
|
|
12
|
+
import {
|
|
13
|
+
forwardRef,
|
|
14
|
+
type HTMLAttributes,
|
|
15
|
+
type ReactElement,
|
|
16
|
+
type ReactNode,
|
|
17
|
+
} from 'react';
|
|
18
|
+
import { cn } from '../lib/cn.js';
|
|
19
|
+
import { useLabels } from '../lib/labels.js';
|
|
20
|
+
import { doiHref } from '../model/doi.js';
|
|
21
|
+
import { analysisTitle, recordTitle } from '../model/records.js';
|
|
22
|
+
import { decisionInsights } from '../model/relations.js';
|
|
23
|
+
import { Prose, type TextRenderer } from '../primitives/prose.js';
|
|
24
|
+
import { surfaceGlyph, type SurfaceKind } from '../primitives/kind.js';
|
|
25
|
+
import { SurfaceHeader } from '../primitives/surface-header.js';
|
|
26
|
+
import type { ArtifactRenderer } from './artifact-preview.js';
|
|
27
|
+
import { primaryLiteratureEvidence } from './insight-detail.js';
|
|
28
|
+
import type { OpenRecordHandler } from './relation-items.js';
|
|
29
|
+
|
|
30
|
+
export interface RecordPreviewTarget {
|
|
31
|
+
record: ResolvedRecord;
|
|
32
|
+
analysis: ResolvedAnalysisNode;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface RecordPreviewReference {
|
|
36
|
+
target: RecordPreviewTarget;
|
|
37
|
+
/** Default record trigger. A host may wrap it in another PreviewPopover. */
|
|
38
|
+
trigger: ReactElement;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type RecordPreviewReferenceRenderer = (
|
|
42
|
+
reference: RecordPreviewReference,
|
|
43
|
+
) => ReactNode;
|
|
44
|
+
|
|
45
|
+
export interface RecordPreviewCitationContext {
|
|
46
|
+
evidence: ResolvedEvidence;
|
|
47
|
+
record: ResolvedInsight;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type RecordPreviewCitationRenderer = (
|
|
51
|
+
doi: string,
|
|
52
|
+
context: RecordPreviewCitationContext,
|
|
53
|
+
) => ReactNode;
|
|
54
|
+
|
|
55
|
+
export type RecordPreviewEntry =
|
|
56
|
+
| {
|
|
57
|
+
kind: 'record';
|
|
58
|
+
record: ResolvedRecord;
|
|
59
|
+
analysis: ResolvedAnalysisNode;
|
|
60
|
+
}
|
|
61
|
+
| {
|
|
62
|
+
kind: 'analysis';
|
|
63
|
+
analysis: ResolvedAnalysisNode;
|
|
64
|
+
href?: string | undefined;
|
|
65
|
+
}
|
|
66
|
+
| {
|
|
67
|
+
kind: 'value';
|
|
68
|
+
record: ResolvedOutput | ResolvedDecision;
|
|
69
|
+
analysis: ResolvedAnalysisNode;
|
|
70
|
+
value: ReactNode;
|
|
71
|
+
unit?: string | undefined;
|
|
72
|
+
column?: string | undefined;
|
|
73
|
+
filter?: string | undefined;
|
|
74
|
+
product?: string | undefined;
|
|
75
|
+
selection?: string | undefined;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export interface RecordPreviewProps
|
|
79
|
+
extends Omit<HTMLAttributes<HTMLElement>, 'children'> {
|
|
80
|
+
entry: RecordPreviewEntry;
|
|
81
|
+
document: ResolvedAnalysisDocument;
|
|
82
|
+
index: AnalysisIndex;
|
|
83
|
+
renderArtifact?: ArtifactRenderer | undefined;
|
|
84
|
+
renderText?: TextRenderer | undefined;
|
|
85
|
+
renderCitation?: RecordPreviewCitationRenderer | undefined;
|
|
86
|
+
/** Opens the full record surface when a related-record trigger is activated. */
|
|
87
|
+
onOpenRecord?: OpenRecordHandler | undefined;
|
|
88
|
+
/** Optional navigation for an analysis preview. The callback receives only a safe HTTP(S) or relative href. */
|
|
89
|
+
onOpenAnalysis?: ((analysis: ResolvedAnalysisNode, href?: string) => void) | undefined;
|
|
90
|
+
/** Wraps related-record triggers, for example in a nested PreviewPopover. */
|
|
91
|
+
renderRecordReference?: RecordPreviewReferenceRenderer | undefined;
|
|
92
|
+
/** Compact decision previews show this many supporting insights before a remaining count. */
|
|
93
|
+
maxSupportingInsights?: number | undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function claimExcerpt(claim: string): string {
|
|
97
|
+
const text = claim.trim().replace(/\s+/g, ' ');
|
|
98
|
+
const sentence = /^.{10,90}?[.!?](?=\s|$)/.exec(text)?.[0];
|
|
99
|
+
if (sentence) return sentence;
|
|
100
|
+
if (text.length <= 90) return text;
|
|
101
|
+
return `${text.slice(0, 80).replace(/\s+\S*$/, '')}…`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function relationTitle(record: ResolvedRecord): string {
|
|
105
|
+
if (
|
|
106
|
+
(record.kind === 'finding' || record.kind === 'prior_insight') &&
|
|
107
|
+
!record.label
|
|
108
|
+
) {
|
|
109
|
+
return claimExcerpt(record.claim) || record.id;
|
|
110
|
+
}
|
|
111
|
+
return recordTitle(record);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function KindEyebrow({ kind, label }: { kind: SurfaceKind; label: ReactNode }) {
|
|
115
|
+
return (
|
|
116
|
+
<span className="astra-record-preview__kind">
|
|
117
|
+
<span aria-hidden="true">{surfaceGlyph(kind)}</span>
|
|
118
|
+
{label}
|
|
119
|
+
</span>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function PreviewHeader({
|
|
124
|
+
kind,
|
|
125
|
+
kindLabel,
|
|
126
|
+
title,
|
|
127
|
+
}: {
|
|
128
|
+
kind: SurfaceKind;
|
|
129
|
+
kindLabel: ReactNode;
|
|
130
|
+
title?: ReactNode | undefined;
|
|
131
|
+
}) {
|
|
132
|
+
return (
|
|
133
|
+
<SurfaceHeader
|
|
134
|
+
density="compact"
|
|
135
|
+
kind={kind}
|
|
136
|
+
titleAs="h3"
|
|
137
|
+
eyebrow={<KindEyebrow kind={kind} label={kindLabel} />}
|
|
138
|
+
title={title}
|
|
139
|
+
className="astra-record-preview__header"
|
|
140
|
+
/>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function Citation({
|
|
145
|
+
evidence,
|
|
146
|
+
record,
|
|
147
|
+
renderCitation,
|
|
148
|
+
}: {
|
|
149
|
+
evidence: ResolvedEvidence;
|
|
150
|
+
record: ResolvedInsight;
|
|
151
|
+
renderCitation?: RecordPreviewCitationRenderer | undefined;
|
|
152
|
+
}) {
|
|
153
|
+
const doi = evidence.doi;
|
|
154
|
+
if (!doi) return null;
|
|
155
|
+
return (
|
|
156
|
+
<div className="astra-record-preview__citation">
|
|
157
|
+
{renderCitation ? (
|
|
158
|
+
renderCitation(doi, { evidence, record })
|
|
159
|
+
) : (
|
|
160
|
+
<a href={doiHref(doi)} target="_blank" rel="noreferrer">
|
|
161
|
+
{doi} <span aria-hidden="true">↗</span>
|
|
162
|
+
</a>
|
|
163
|
+
)}
|
|
164
|
+
</div>
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function RelatedRecord({
|
|
169
|
+
record,
|
|
170
|
+
index,
|
|
171
|
+
onOpenRecord,
|
|
172
|
+
renderRecordReference,
|
|
173
|
+
detail,
|
|
174
|
+
}: {
|
|
175
|
+
record: ResolvedRecord;
|
|
176
|
+
index: AnalysisIndex;
|
|
177
|
+
onOpenRecord?: OpenRecordHandler | undefined;
|
|
178
|
+
renderRecordReference?: RecordPreviewReferenceRenderer | undefined;
|
|
179
|
+
detail?: ReactNode | undefined;
|
|
180
|
+
}) {
|
|
181
|
+
const labels = useLabels();
|
|
182
|
+
const analysis = index.analysisByRecordPath.get(record.canonicalPath);
|
|
183
|
+
const label = relationTitle(record);
|
|
184
|
+
const copy = (
|
|
185
|
+
<>
|
|
186
|
+
<span className="astra-record-preview__relation-glyph" aria-hidden="true">
|
|
187
|
+
{surfaceGlyph(record.kind)}
|
|
188
|
+
</span>
|
|
189
|
+
<span className="astra-record-preview__relation-label">{label}</span>
|
|
190
|
+
{detail != null ? (
|
|
191
|
+
<small className="astra-record-preview__relation-detail">{detail}</small>
|
|
192
|
+
) : null}
|
|
193
|
+
</>
|
|
194
|
+
);
|
|
195
|
+
const trigger = analysis && onOpenRecord ? (
|
|
196
|
+
<button
|
|
197
|
+
type="button"
|
|
198
|
+
className="astra-record-preview__relation-trigger"
|
|
199
|
+
data-kind={record.kind}
|
|
200
|
+
aria-label={labels.preview.openRecord(labels.kinds[record.kind], label)}
|
|
201
|
+
onClick={() => {
|
|
202
|
+
onOpenRecord(record, analysis);
|
|
203
|
+
}}
|
|
204
|
+
>
|
|
205
|
+
{copy}
|
|
206
|
+
</button>
|
|
207
|
+
) : (
|
|
208
|
+
<span className="astra-record-preview__relation-trigger" data-kind={record.kind}>
|
|
209
|
+
{copy}
|
|
210
|
+
</span>
|
|
211
|
+
);
|
|
212
|
+
if (!analysis || !renderRecordReference) return trigger;
|
|
213
|
+
return renderRecordReference({ target: { record, analysis }, trigger });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
interface SharedPreviewProps {
|
|
217
|
+
document: ResolvedAnalysisDocument;
|
|
218
|
+
index: AnalysisIndex;
|
|
219
|
+
renderArtifact?: ArtifactRenderer | undefined;
|
|
220
|
+
renderText?: TextRenderer | undefined;
|
|
221
|
+
renderCitation?: RecordPreviewCitationRenderer | undefined;
|
|
222
|
+
onOpenRecord?: OpenRecordHandler | undefined;
|
|
223
|
+
renderRecordReference?: RecordPreviewReferenceRenderer | undefined;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function DecisionPreview({
|
|
227
|
+
record,
|
|
228
|
+
index,
|
|
229
|
+
renderText,
|
|
230
|
+
onOpenRecord,
|
|
231
|
+
renderRecordReference,
|
|
232
|
+
maxSupportingInsights,
|
|
233
|
+
}: SharedPreviewProps & {
|
|
234
|
+
record: ResolvedDecision;
|
|
235
|
+
maxSupportingInsights: number;
|
|
236
|
+
}) {
|
|
237
|
+
const labels = useLabels();
|
|
238
|
+
const supporting = decisionInsights(index, record);
|
|
239
|
+
const shown = supporting.slice(0, maxSupportingInsights);
|
|
240
|
+
const remaining = supporting.length - shown.length;
|
|
241
|
+
return (
|
|
242
|
+
<>
|
|
243
|
+
<PreviewHeader
|
|
244
|
+
kind="decision"
|
|
245
|
+
kindLabel={labels.kinds.decision}
|
|
246
|
+
title={recordTitle(record)}
|
|
247
|
+
/>
|
|
248
|
+
<div className="astra-record-preview__body">
|
|
249
|
+
{record.rationale ? (
|
|
250
|
+
<div className="astra-record-preview__description">
|
|
251
|
+
<Prose
|
|
252
|
+
text={record.rationale}
|
|
253
|
+
field="rationale"
|
|
254
|
+
renderText={renderText}
|
|
255
|
+
/>
|
|
256
|
+
</div>
|
|
257
|
+
) : null}
|
|
258
|
+
{record.options.length ? (
|
|
259
|
+
<section className="astra-record-preview__section">
|
|
260
|
+
<h4>{labels.preview.optionDetail}</h4>
|
|
261
|
+
<ul className="astra-record-preview__options">
|
|
262
|
+
{record.options.map((option) => {
|
|
263
|
+
const selected = option.id === record.selectedOptionId;
|
|
264
|
+
return (
|
|
265
|
+
<li
|
|
266
|
+
key={option.id}
|
|
267
|
+
data-selected={selected ? '' : undefined}
|
|
268
|
+
data-unselected={selected ? undefined : ''}
|
|
269
|
+
data-excluded={option.excluded ? '' : undefined}
|
|
270
|
+
>
|
|
271
|
+
<span aria-hidden="true">{selected ? '●' : '○'}</span>
|
|
272
|
+
<span className="astra-record-preview__option-status">
|
|
273
|
+
{labels.preview.optionStatus(selected, Boolean(option.excluded))}
|
|
274
|
+
</span>
|
|
275
|
+
<strong>{option.label}</strong>
|
|
276
|
+
</li>
|
|
277
|
+
);
|
|
278
|
+
})}
|
|
279
|
+
</ul>
|
|
280
|
+
</section>
|
|
281
|
+
) : null}
|
|
282
|
+
{supporting.length ? (
|
|
283
|
+
<section className="astra-record-preview__section">
|
|
284
|
+
<h4>{labels.preview.supportedBy}</h4>
|
|
285
|
+
<ul className="astra-record-preview__relations">
|
|
286
|
+
{shown.map((insight) => (
|
|
287
|
+
<li key={insight.canonicalPath} data-kind={insight.kind}>
|
|
288
|
+
<RelatedRecord
|
|
289
|
+
record={insight}
|
|
290
|
+
index={index}
|
|
291
|
+
onOpenRecord={onOpenRecord}
|
|
292
|
+
renderRecordReference={renderRecordReference}
|
|
293
|
+
/>
|
|
294
|
+
</li>
|
|
295
|
+
))}
|
|
296
|
+
{remaining > 0 ? (
|
|
297
|
+
<li className="astra-record-preview__remaining">
|
|
298
|
+
{labels.preview.remainingDecisionDetails(remaining)}
|
|
299
|
+
</li>
|
|
300
|
+
) : null}
|
|
301
|
+
</ul>
|
|
302
|
+
</section>
|
|
303
|
+
) : null}
|
|
304
|
+
</div>
|
|
305
|
+
</>
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function FindingPreview({
|
|
310
|
+
record,
|
|
311
|
+
index,
|
|
312
|
+
renderArtifact,
|
|
313
|
+
renderText,
|
|
314
|
+
renderCitation,
|
|
315
|
+
onOpenRecord,
|
|
316
|
+
renderRecordReference,
|
|
317
|
+
}: SharedPreviewProps & { record: ResolvedInsight }) {
|
|
318
|
+
const labels = useLabels();
|
|
319
|
+
return (
|
|
320
|
+
<>
|
|
321
|
+
<PreviewHeader
|
|
322
|
+
kind="finding"
|
|
323
|
+
kindLabel={labels.kinds.finding}
|
|
324
|
+
title={recordTitle(record)}
|
|
325
|
+
/>
|
|
326
|
+
<div className="astra-record-preview__body">
|
|
327
|
+
<div className="astra-record-preview__claim">
|
|
328
|
+
<Prose text={record.claim} field="claim" renderText={renderText} />
|
|
329
|
+
</div>
|
|
330
|
+
{record.scope ? (
|
|
331
|
+
<span className="astra-record-preview__scope">
|
|
332
|
+
<Prose text={record.scope} renderText={renderText} />
|
|
333
|
+
</span>
|
|
334
|
+
) : null}
|
|
335
|
+
{record.notes ? (
|
|
336
|
+
<div className="astra-record-preview__description">
|
|
337
|
+
<Prose text={record.notes} field="notes" renderText={renderText} />
|
|
338
|
+
</div>
|
|
339
|
+
) : null}
|
|
340
|
+
{record.evidence.length ? (
|
|
341
|
+
<section className="astra-record-preview__section">
|
|
342
|
+
<h4>{labels.preview.evidence}</h4>
|
|
343
|
+
<ul className="astra-record-preview__evidence">
|
|
344
|
+
{record.evidence.map((evidence, indexInRecord) => {
|
|
345
|
+
const candidate = evidence.resolvedOutputPath
|
|
346
|
+
? index.recordByPath.get(evidence.resolvedOutputPath)
|
|
347
|
+
: undefined;
|
|
348
|
+
const output = candidate?.kind === 'output' ? candidate : undefined;
|
|
349
|
+
const artifact = output && renderArtifact
|
|
350
|
+
? renderArtifact(output, { compact: true })
|
|
351
|
+
: null;
|
|
352
|
+
return (
|
|
353
|
+
<li key={`${evidence.id}-${indexInRecord}`}>
|
|
354
|
+
{artifact != null ? (
|
|
355
|
+
<div className="astra-record-preview__artifact">
|
|
356
|
+
{artifact}
|
|
357
|
+
</div>
|
|
358
|
+
) : null}
|
|
359
|
+
{output ? (
|
|
360
|
+
<RelatedRecord
|
|
361
|
+
record={output}
|
|
362
|
+
index={index}
|
|
363
|
+
detail={output.type}
|
|
364
|
+
onOpenRecord={onOpenRecord}
|
|
365
|
+
renderRecordReference={renderRecordReference}
|
|
366
|
+
/>
|
|
367
|
+
) : evidence.artifact ? (
|
|
368
|
+
<span
|
|
369
|
+
className="astra-record-preview__relation-trigger"
|
|
370
|
+
data-kind="output"
|
|
371
|
+
>
|
|
372
|
+
<span
|
|
373
|
+
className="astra-record-preview__relation-glyph"
|
|
374
|
+
aria-hidden="true"
|
|
375
|
+
>
|
|
376
|
+
{surfaceGlyph('output')}
|
|
377
|
+
</span>
|
|
378
|
+
<span className="astra-record-preview__relation-label">
|
|
379
|
+
{evidence.artifact}
|
|
380
|
+
</span>
|
|
381
|
+
</span>
|
|
382
|
+
) : null}
|
|
383
|
+
{evidence.quote ? (
|
|
384
|
+
<blockquote className="astra-record-preview__quote">
|
|
385
|
+
<Prose
|
|
386
|
+
text={evidence.quote.exact}
|
|
387
|
+
field="quote"
|
|
388
|
+
renderText={renderText}
|
|
389
|
+
/>
|
|
390
|
+
</blockquote>
|
|
391
|
+
) : null}
|
|
392
|
+
<Citation
|
|
393
|
+
evidence={evidence}
|
|
394
|
+
record={record}
|
|
395
|
+
renderCitation={renderCitation}
|
|
396
|
+
/>
|
|
397
|
+
</li>
|
|
398
|
+
);
|
|
399
|
+
})}
|
|
400
|
+
</ul>
|
|
401
|
+
</section>
|
|
402
|
+
) : null}
|
|
403
|
+
</div>
|
|
404
|
+
</>
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function InsightPreview({
|
|
409
|
+
record,
|
|
410
|
+
renderText,
|
|
411
|
+
renderCitation,
|
|
412
|
+
}: SharedPreviewProps & { record: ResolvedInsight }) {
|
|
413
|
+
const labels = useLabels();
|
|
414
|
+
const source = primaryLiteratureEvidence(record);
|
|
415
|
+
return (
|
|
416
|
+
<>
|
|
417
|
+
<PreviewHeader
|
|
418
|
+
kind="prior_insight"
|
|
419
|
+
kindLabel={labels.kinds.prior_insight}
|
|
420
|
+
/>
|
|
421
|
+
<div className="astra-record-preview__body">
|
|
422
|
+
<div className="astra-record-preview__description">
|
|
423
|
+
<Prose text={record.claim} field="claim" renderText={renderText} />
|
|
424
|
+
</div>
|
|
425
|
+
{source?.quote ? (
|
|
426
|
+
<blockquote className="astra-record-preview__quote">
|
|
427
|
+
<Prose
|
|
428
|
+
text={source.quote.exact}
|
|
429
|
+
field="quote"
|
|
430
|
+
renderText={renderText}
|
|
431
|
+
/>
|
|
432
|
+
</blockquote>
|
|
433
|
+
) : null}
|
|
434
|
+
{source ? (
|
|
435
|
+
<Citation
|
|
436
|
+
evidence={source}
|
|
437
|
+
record={record}
|
|
438
|
+
renderCitation={renderCitation}
|
|
439
|
+
/>
|
|
440
|
+
) : null}
|
|
441
|
+
</div>
|
|
442
|
+
</>
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function OutputRecordPreview({
|
|
447
|
+
record,
|
|
448
|
+
index,
|
|
449
|
+
renderArtifact,
|
|
450
|
+
renderText,
|
|
451
|
+
}: SharedPreviewProps & { record: ResolvedOutput }) {
|
|
452
|
+
const labels = useLabels();
|
|
453
|
+
const inputNames = record.provenance.inputPaths.map((path) => {
|
|
454
|
+
const input = index.recordByPath.get(path);
|
|
455
|
+
return input ? recordTitle(input) : path;
|
|
456
|
+
});
|
|
457
|
+
const recipe = record.recipe?.command ?? record.recipe?.container;
|
|
458
|
+
const flow = [
|
|
459
|
+
inputNames.length ? inputNames.join(', ') : undefined,
|
|
460
|
+
recipe,
|
|
461
|
+
record.canonicalPath,
|
|
462
|
+
].filter((value): value is string => Boolean(value));
|
|
463
|
+
const artifact = renderArtifact
|
|
464
|
+
? renderArtifact(record, { compact: true })
|
|
465
|
+
: null;
|
|
466
|
+
return (
|
|
467
|
+
<>
|
|
468
|
+
<PreviewHeader
|
|
469
|
+
kind="output"
|
|
470
|
+
kindLabel={labels.kinds.output}
|
|
471
|
+
title={recordTitle(record)}
|
|
472
|
+
/>
|
|
473
|
+
<div className="astra-record-preview__body">
|
|
474
|
+
{record.description ? (
|
|
475
|
+
<div className="astra-record-preview__description">
|
|
476
|
+
<Prose
|
|
477
|
+
text={record.description}
|
|
478
|
+
field="description"
|
|
479
|
+
renderText={renderText}
|
|
480
|
+
/>
|
|
481
|
+
</div>
|
|
482
|
+
) : null}
|
|
483
|
+
{artifact != null ? (
|
|
484
|
+
<div className="astra-record-preview__artifact">
|
|
485
|
+
{artifact}
|
|
486
|
+
</div>
|
|
487
|
+
) : null}
|
|
488
|
+
{flow.length > 1 ? (
|
|
489
|
+
<section className="astra-record-preview__section">
|
|
490
|
+
<h4>{labels.preview.provenance}</h4>
|
|
491
|
+
<ol className="astra-record-preview__flow">
|
|
492
|
+
{flow.map((node, indexInFlow) => (
|
|
493
|
+
<li key={`${node}-${indexInFlow}`}>
|
|
494
|
+
<code>{node}</code>
|
|
495
|
+
</li>
|
|
496
|
+
))}
|
|
497
|
+
</ol>
|
|
498
|
+
</section>
|
|
499
|
+
) : null}
|
|
500
|
+
</div>
|
|
501
|
+
</>
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function InputPreview({
|
|
506
|
+
record,
|
|
507
|
+
renderText,
|
|
508
|
+
}: SharedPreviewProps & { record: ResolvedInput }) {
|
|
509
|
+
const labels = useLabels();
|
|
510
|
+
const source =
|
|
511
|
+
record.source ?? record.ref ?? record.resolvedFrom ?? record.from;
|
|
512
|
+
return (
|
|
513
|
+
<>
|
|
514
|
+
<PreviewHeader
|
|
515
|
+
kind="input"
|
|
516
|
+
kindLabel={labels.kinds.input}
|
|
517
|
+
title={recordTitle(record)}
|
|
518
|
+
/>
|
|
519
|
+
<div className="astra-record-preview__body">
|
|
520
|
+
{record.description ? (
|
|
521
|
+
<div className="astra-record-preview__description">
|
|
522
|
+
<Prose
|
|
523
|
+
text={record.description}
|
|
524
|
+
field="description"
|
|
525
|
+
renderText={renderText}
|
|
526
|
+
/>
|
|
527
|
+
</div>
|
|
528
|
+
) : null}
|
|
529
|
+
{source ? (
|
|
530
|
+
<section className="astra-record-preview__section">
|
|
531
|
+
<h4>{labels.preview.source}</h4>
|
|
532
|
+
<code className="astra-record-preview__source">{source}</code>
|
|
533
|
+
</section>
|
|
534
|
+
) : null}
|
|
535
|
+
</div>
|
|
536
|
+
</>
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function RecordEntryPreview({
|
|
541
|
+
record,
|
|
542
|
+
maxSupportingInsights,
|
|
543
|
+
...shared
|
|
544
|
+
}: SharedPreviewProps & {
|
|
545
|
+
record: ResolvedRecord;
|
|
546
|
+
maxSupportingInsights: number;
|
|
547
|
+
}) {
|
|
548
|
+
switch (record.kind) {
|
|
549
|
+
case 'decision':
|
|
550
|
+
return (
|
|
551
|
+
<DecisionPreview
|
|
552
|
+
{...shared}
|
|
553
|
+
record={record}
|
|
554
|
+
maxSupportingInsights={maxSupportingInsights}
|
|
555
|
+
/>
|
|
556
|
+
);
|
|
557
|
+
case 'finding':
|
|
558
|
+
return <FindingPreview {...shared} record={record} />;
|
|
559
|
+
case 'prior_insight':
|
|
560
|
+
return <InsightPreview {...shared} record={record} />;
|
|
561
|
+
case 'output':
|
|
562
|
+
return <OutputRecordPreview {...shared} record={record} />;
|
|
563
|
+
case 'input':
|
|
564
|
+
return <InputPreview {...shared} record={record} />;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function AnalysisPreview({
|
|
569
|
+
analysis,
|
|
570
|
+
href,
|
|
571
|
+
renderText,
|
|
572
|
+
onOpenAnalysis,
|
|
573
|
+
}: {
|
|
574
|
+
analysis: ResolvedAnalysisNode;
|
|
575
|
+
href?: string | undefined;
|
|
576
|
+
renderText?: TextRenderer | undefined;
|
|
577
|
+
onOpenAnalysis?: ((analysis: ResolvedAnalysisNode, href?: string) => void) | undefined;
|
|
578
|
+
}) {
|
|
579
|
+
const labels = useLabels();
|
|
580
|
+
const title = analysisTitle(analysis);
|
|
581
|
+
const safeHref = (() => {
|
|
582
|
+
if (!href) return undefined;
|
|
583
|
+
try {
|
|
584
|
+
const parsed = new URL(href, 'https://astra-ui.invalid/');
|
|
585
|
+
return parsed.protocol === 'http:' || parsed.protocol === 'https:'
|
|
586
|
+
? href
|
|
587
|
+
: undefined;
|
|
588
|
+
} catch {
|
|
589
|
+
return undefined;
|
|
590
|
+
}
|
|
591
|
+
})();
|
|
592
|
+
let titleNode: ReactNode = title;
|
|
593
|
+
if (onOpenAnalysis) {
|
|
594
|
+
titleNode = (
|
|
595
|
+
<button
|
|
596
|
+
type="button"
|
|
597
|
+
className="astra-record-preview__analysis-link"
|
|
598
|
+
onClick={() => {
|
|
599
|
+
onOpenAnalysis(analysis, safeHref);
|
|
600
|
+
}}
|
|
601
|
+
>
|
|
602
|
+
{title}
|
|
603
|
+
</button>
|
|
604
|
+
);
|
|
605
|
+
} else if (safeHref) {
|
|
606
|
+
titleNode = (
|
|
607
|
+
<a className="astra-record-preview__analysis-link" href={safeHref}>
|
|
608
|
+
{title}
|
|
609
|
+
</a>
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
return (
|
|
613
|
+
<>
|
|
614
|
+
<PreviewHeader
|
|
615
|
+
kind="analysis"
|
|
616
|
+
kindLabel={labels.kinds.analysis}
|
|
617
|
+
title={titleNode}
|
|
618
|
+
/>
|
|
619
|
+
<div className="astra-record-preview__body">
|
|
620
|
+
{analysis.description ? (
|
|
621
|
+
<div className="astra-record-preview__description">
|
|
622
|
+
<Prose
|
|
623
|
+
text={analysis.description}
|
|
624
|
+
field="description"
|
|
625
|
+
renderText={renderText}
|
|
626
|
+
/>
|
|
627
|
+
</div>
|
|
628
|
+
) : null}
|
|
629
|
+
<p className="astra-record-preview__counts">
|
|
630
|
+
{labels.sectionCount('decisions', analysis.decisions.length)}
|
|
631
|
+
{' · '}
|
|
632
|
+
{labels.sectionCount('outputs', analysis.outputs.length)}
|
|
633
|
+
</p>
|
|
634
|
+
</div>
|
|
635
|
+
</>
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function ValuePreview({
|
|
640
|
+
entry,
|
|
641
|
+
renderText,
|
|
642
|
+
}: {
|
|
643
|
+
entry: Extract<RecordPreviewEntry, { kind: 'value' }>;
|
|
644
|
+
renderText?: TextRenderer | undefined;
|
|
645
|
+
}) {
|
|
646
|
+
const labels = useLabels();
|
|
647
|
+
const { record } = entry;
|
|
648
|
+
const kindLabel =
|
|
649
|
+
record.kind === 'output' ? labels.kinds.output : labels.kinds.decision;
|
|
650
|
+
const description =
|
|
651
|
+
record.kind === 'output' ? record.description : record.rationale;
|
|
652
|
+
return (
|
|
653
|
+
<>
|
|
654
|
+
<PreviewHeader
|
|
655
|
+
kind={record.kind}
|
|
656
|
+
kindLabel={kindLabel}
|
|
657
|
+
title={
|
|
658
|
+
<span className="astra-record-preview__value">
|
|
659
|
+
{entry.value}
|
|
660
|
+
{entry.unit ? (
|
|
661
|
+
<small className="astra-record-preview__unit"> {entry.unit}</small>
|
|
662
|
+
) : null}
|
|
663
|
+
</span>
|
|
664
|
+
}
|
|
665
|
+
/>
|
|
666
|
+
<div className="astra-record-preview__body">
|
|
667
|
+
{entry.column ||
|
|
668
|
+
entry.filter ||
|
|
669
|
+
entry.product ||
|
|
670
|
+
entry.selection ? (
|
|
671
|
+
<div className="astra-record-preview__selection">
|
|
672
|
+
{entry.column ? <code>{entry.column}</code> : null}
|
|
673
|
+
{entry.filter ? <span>{entry.filter}</span> : null}
|
|
674
|
+
{entry.selection ? <code>{entry.selection}</code> : null}
|
|
675
|
+
{entry.product ? (
|
|
676
|
+
<span>{labels.preview.valueSource(entry.product)}</span>
|
|
677
|
+
) : null}
|
|
678
|
+
</div>
|
|
679
|
+
) : null}
|
|
680
|
+
{description ? (
|
|
681
|
+
<div className="astra-record-preview__description">
|
|
682
|
+
<Prose
|
|
683
|
+
text={description}
|
|
684
|
+
field={record.kind === 'output' ? 'description' : 'rationale'}
|
|
685
|
+
renderText={renderText}
|
|
686
|
+
/>
|
|
687
|
+
</div>
|
|
688
|
+
) : null}
|
|
689
|
+
</div>
|
|
690
|
+
</>
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function previewKind(entry: RecordPreviewEntry): SurfaceKind {
|
|
695
|
+
if (entry.kind === 'analysis') return 'analysis';
|
|
696
|
+
return entry.record.kind;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Compact, positioning-agnostic preview content for resolved records,
|
|
701
|
+
* analyses, and inline values. Pair it with PreviewPopover or render it inline.
|
|
702
|
+
*/
|
|
703
|
+
export const RecordPreview = forwardRef<HTMLElement, RecordPreviewProps>(
|
|
704
|
+
function RecordPreview(
|
|
705
|
+
{
|
|
706
|
+
entry,
|
|
707
|
+
document,
|
|
708
|
+
index,
|
|
709
|
+
renderArtifact,
|
|
710
|
+
renderText,
|
|
711
|
+
renderCitation,
|
|
712
|
+
onOpenRecord,
|
|
713
|
+
onOpenAnalysis,
|
|
714
|
+
renderRecordReference,
|
|
715
|
+
maxSupportingInsights = 3,
|
|
716
|
+
className,
|
|
717
|
+
...props
|
|
718
|
+
},
|
|
719
|
+
ref,
|
|
720
|
+
) {
|
|
721
|
+
const maximum = Number.isFinite(maxSupportingInsights)
|
|
722
|
+
? Math.max(0, Math.floor(maxSupportingInsights))
|
|
723
|
+
: Number.MAX_SAFE_INTEGER;
|
|
724
|
+
const shared: SharedPreviewProps = {
|
|
725
|
+
document,
|
|
726
|
+
index,
|
|
727
|
+
renderArtifact,
|
|
728
|
+
renderText,
|
|
729
|
+
renderCitation,
|
|
730
|
+
onOpenRecord,
|
|
731
|
+
renderRecordReference,
|
|
732
|
+
};
|
|
733
|
+
return (
|
|
734
|
+
<article
|
|
735
|
+
data-slot="record-preview"
|
|
736
|
+
data-entry-kind={entry.kind}
|
|
737
|
+
data-kind={previewKind(entry)}
|
|
738
|
+
{...props}
|
|
739
|
+
ref={ref}
|
|
740
|
+
className={cn('astra-record-preview', className)}
|
|
741
|
+
>
|
|
742
|
+
{entry.kind === 'record' ? (
|
|
743
|
+
<RecordEntryPreview
|
|
744
|
+
{...shared}
|
|
745
|
+
record={entry.record}
|
|
746
|
+
maxSupportingInsights={maximum}
|
|
747
|
+
/>
|
|
748
|
+
) : entry.kind === 'analysis' ? (
|
|
749
|
+
<AnalysisPreview
|
|
750
|
+
analysis={entry.analysis}
|
|
751
|
+
href={entry.href}
|
|
752
|
+
renderText={renderText}
|
|
753
|
+
onOpenAnalysis={onOpenAnalysis}
|
|
754
|
+
/>
|
|
755
|
+
) : (
|
|
756
|
+
<ValuePreview entry={entry} renderText={renderText} />
|
|
757
|
+
)}
|
|
758
|
+
</article>
|
|
759
|
+
);
|
|
760
|
+
},
|
|
761
|
+
);
|