@bsv/create-dpp-app 0.0.0-stage → 0.1.0-beta.2
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/LICENSE +203 -0
- package/NOTICE +2 -0
- package/PATENTS +12 -0
- package/README.md +93 -2
- package/dist/index.js +125 -0
- package/package.json +57 -4
- package/template/Dockerfile +37 -0
- package/template/README.md +389 -0
- package/template/_dockerignore +6 -0
- package/template/_env.example +122 -0
- package/template/_github/workflows/ci.yml +23 -0
- package/template/_gitignore +8 -0
- package/template/_prettierrc +6 -0
- package/template/apps/api/API.md +119 -0
- package/template/apps/api/package.json +39 -0
- package/template/apps/api/src/auth.ts +55 -0
- package/template/apps/api/src/bound-profiles.ts +590 -0
- package/template/apps/api/src/claims.ts +204 -0
- package/template/apps/api/src/cli.ts +99 -0
- package/template/apps/api/src/config.ts +180 -0
- package/template/apps/api/src/custody.ts +358 -0
- package/template/apps/api/src/dev-index.ts +120 -0
- package/template/apps/api/src/env-file.ts +55 -0
- package/template/apps/api/src/env.ts +14 -0
- package/template/apps/api/src/epcis/exchange.ts +434 -0
- package/template/apps/api/src/epcis/mongo-storage.ts +381 -0
- package/template/apps/api/src/epcis/routes.ts +364 -0
- package/template/apps/api/src/epcis/sources.ts +142 -0
- package/template/apps/api/src/go-live.ts +226 -0
- package/template/apps/api/src/http-error.ts +15 -0
- package/template/apps/api/src/identifiers.ts +57 -0
- package/template/apps/api/src/index-client.ts +261 -0
- package/template/apps/api/src/index-projects.ts +53 -0
- package/template/apps/api/src/lifecycle-authority.ts +49 -0
- package/template/apps/api/src/lifecycle.ts +485 -0
- package/template/apps/api/src/main.ts +58 -0
- package/template/apps/api/src/parties.ts +68 -0
- package/template/apps/api/src/platform.ts +176 -0
- package/template/apps/api/src/portability/commands.ts +224 -0
- package/template/apps/api/src/portability/export.ts +708 -0
- package/template/apps/api/src/portability/package.ts +270 -0
- package/template/apps/api/src/portability/restore.ts +863 -0
- package/template/apps/api/src/profiles.ts +125 -0
- package/template/apps/api/src/reader.ts +265 -0
- package/template/apps/api/src/resolver.ts +159 -0
- package/template/apps/api/src/sector-examples.ts +390 -0
- package/template/apps/api/src/server.ts +613 -0
- package/template/apps/api/src/store.ts +426 -0
- package/template/apps/api/src/validation-engine.ts +172 -0
- package/template/apps/api/src/wallet.ts +359 -0
- package/template/apps/api/src/worker.ts +113 -0
- package/template/apps/api/src/writer.ts +603 -0
- package/template/apps/api/test/bound-http.test.ts +496 -0
- package/template/apps/api/test/bound-profiles.test.ts +547 -0
- package/template/apps/api/test/config.test.ts +160 -0
- package/template/apps/api/test/custody-journal.test.ts +415 -0
- package/template/apps/api/test/custody-recovery-profile.test.ts +224 -0
- package/template/apps/api/test/custom-profile.test.ts +167 -0
- package/template/apps/api/test/env-file.test.ts +53 -0
- package/template/apps/api/test/epcis-fixtures.ts +270 -0
- package/template/apps/api/test/epcis-http.test.ts +1179 -0
- package/template/apps/api/test/epcis-mongo-storage.test.ts +313 -0
- package/template/apps/api/test/epcis-separation.test.ts +253 -0
- package/template/apps/api/test/epcis-web.test.ts +230 -0
- package/template/apps/api/test/exit-exercise.test.ts +1038 -0
- package/template/apps/api/test/external-index.test.ts +32 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/background.ttl +33 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/binding.json +141 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/context.jsonld +36 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/product.schema.json +44 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/shapes.ttl +112 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/conflicting-gtin.json +44 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/conflicting-serial.json +44 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/granularity-mismatch.json +44 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/item-gtin-13.json +43 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/item.json +42 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/missing-required-property.json +43 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/model-as-subject.json +44 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/no-passport-identifier.json +43 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/other-product.json +44 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/part-not-dishwasher-safe.json +45 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/altered-artefact.json +19 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/altered-background.ttl +33 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/conflicting-binding.json +141 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/conflicting-second-registration.json +19 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/untrusted-binding.json +13 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/unlisted-context.json +47 -0
- package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/wrong-binding-digest.json +44 -0
- package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/binding.json +88 -0
- package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/product.schema.json +25 -0
- package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/item.json +26 -0
- package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/missing-required-property.json +27 -0
- package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/no-passport-identifier.json +27 -0
- package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/other-product.json +28 -0
- package/template/apps/api/test/go-live.test.ts +179 -0
- package/template/apps/api/test/gs1-global-setup.ts +33 -0
- package/template/apps/api/test/gs1-test-env.ts +5 -0
- package/template/apps/api/test/identifier-host.test.ts +256 -0
- package/template/apps/api/test/index-projects.test.ts +81 -0
- package/template/apps/api/test/lifecycle-claims.test.ts +546 -0
- package/template/apps/api/test/lifecycle-fixtures.ts +167 -0
- package/template/apps/api/test/lifecycle.test.ts +147 -0
- package/template/apps/api/test/manifest-notice.test.ts +36 -0
- package/template/apps/api/test/network-guard.ts +113 -0
- package/template/apps/api/test/portability.test.ts +655 -0
- package/template/apps/api/test/profile-bundle-resolver.test.ts +198 -0
- package/template/apps/api/test/retired-profiles.test.ts +216 -0
- package/template/apps/api/test/sector-cutover.test.ts +95 -0
- package/template/apps/api/test/sector-forms.test.ts +420 -0
- package/template/apps/api/test/unjoined-index.test.ts +62 -0
- package/template/apps/api/test/validation-engine.test.ts +328 -0
- package/template/apps/api/test/wallet-monitor.test.ts +38 -0
- package/template/apps/api/test/writer-journal.test.ts +327 -0
- package/template/apps/api/tsconfig.json +9 -0
- package/template/apps/api/vitest.config.ts +12 -0
- package/template/apps/web/components.json +21 -0
- package/template/apps/web/index.html +12 -0
- package/template/apps/web/package.json +33 -0
- package/template/apps/web/src/app.tsx +55 -0
- package/template/apps/web/src/components/bits.tsx +188 -0
- package/template/apps/web/src/components/bound/bound-passport-form.tsx +368 -0
- package/template/apps/web/src/components/bound/field-view.tsx +237 -0
- package/template/apps/web/src/components/bound/pieces.tsx +169 -0
- package/template/apps/web/src/components/bound/readings.tsx +152 -0
- package/template/apps/web/src/components/bound/structured-value.tsx +114 -0
- package/template/apps/web/src/components/claim-dialog.tsx +344 -0
- package/template/apps/web/src/components/epcis-events.tsx +119 -0
- package/template/apps/web/src/components/layout.tsx +135 -0
- package/template/apps/web/src/components/lifecycle-claims.tsx +199 -0
- package/template/apps/web/src/components/passport-form.tsx +244 -0
- package/template/apps/web/src/components/read-passport.tsx +49 -0
- package/template/apps/web/src/components/reading.tsx +150 -0
- package/template/apps/web/src/components/ui/alert.tsx +65 -0
- package/template/apps/web/src/components/ui/badge.tsx +47 -0
- package/template/apps/web/src/components/ui/button.tsx +63 -0
- package/template/apps/web/src/components/ui/card.tsx +91 -0
- package/template/apps/web/src/components/ui/dialog.tsx +156 -0
- package/template/apps/web/src/components/ui/input.tsx +20 -0
- package/template/apps/web/src/components/ui/label.tsx +21 -0
- package/template/apps/web/src/components/ui/select.tsx +189 -0
- package/template/apps/web/src/components/ui/separator.tsx +25 -0
- package/template/apps/web/src/components/ui/table.tsx +115 -0
- package/template/apps/web/src/components/ui/tabs.tsx +90 -0
- package/template/apps/web/src/components/ui/textarea.tsx +17 -0
- package/template/apps/web/src/index.css +81 -0
- package/template/apps/web/src/lib/api.ts +396 -0
- package/template/apps/web/src/lib/auth.ts +10 -0
- package/template/apps/web/src/lib/bound/battery-form.ts +269 -0
- package/template/apps/web/src/lib/bound/display.ts +122 -0
- package/template/apps/web/src/lib/bound/fields.ts +128 -0
- package/template/apps/web/src/lib/bound/form-model.ts +521 -0
- package/template/apps/web/src/lib/bound/forms.ts +15 -0
- package/template/apps/web/src/lib/bound/identity.ts +48 -0
- package/template/apps/web/src/lib/bound/textile-form.ts +236 -0
- package/template/apps/web/src/lib/bound/units.ts +37 -0
- package/template/apps/web/src/lib/epcis.ts +345 -0
- package/template/apps/web/src/lib/format.ts +87 -0
- package/template/apps/web/src/lib/lifecycle.ts +350 -0
- package/template/apps/web/src/lib/session.tsx +92 -0
- package/template/apps/web/src/lib/use-load.ts +46 -0
- package/template/apps/web/src/main.tsx +18 -0
- package/template/apps/web/src/pages/accept.tsx +113 -0
- package/template/apps/web/src/pages/brand.tsx +94 -0
- package/template/apps/web/src/pages/brands.tsx +104 -0
- package/template/apps/web/src/pages/events.tsx +388 -0
- package/template/apps/web/src/pages/home.tsx +51 -0
- package/template/apps/web/src/pages/new-passport.tsx +137 -0
- package/template/apps/web/src/pages/operations.tsx +169 -0
- package/template/apps/web/src/pages/passport.tsx +632 -0
- package/template/apps/web/src/pages/public-passport.tsx +64 -0
- package/template/apps/web/src/pages/sign-in.tsx +93 -0
- package/template/apps/web/src/pages/verify.tsx +30 -0
- package/template/apps/web/src/pages/wallet.tsx +141 -0
- package/template/apps/web/tsconfig.json +29 -0
- package/template/apps/web/vite.config.ts +23 -0
- package/template/deploy/compose.yml +38 -0
- package/template/package.json +48 -0
- package/template/tsconfig.base.json +13 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Small pieces every page uses: loading and error states, a copy button,
|
|
2
|
+
// coloured badges for check and journal answers, section headings and a
|
|
3
|
+
// definition list.
|
|
4
|
+
import { useState, type ReactNode } from 'react'
|
|
5
|
+
import { cn } from 'cn'
|
|
6
|
+
import { Check, Copy, LoaderCircle } from 'lucide-react'
|
|
7
|
+
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
|
8
|
+
import { Badge } from '@/components/ui/badge'
|
|
9
|
+
import { Button } from '@/components/ui/button'
|
|
10
|
+
import { Label } from '@/components/ui/label'
|
|
11
|
+
import { ApiError, type CheckStatus } from '@/lib/api'
|
|
12
|
+
import { isRecord } from '@/lib/format'
|
|
13
|
+
|
|
14
|
+
export function Loading({ what = 'Loading' }: { what?: string }) {
|
|
15
|
+
return (
|
|
16
|
+
<p className="flex items-center gap-2 py-4 text-sm text-muted-foreground" role="status">
|
|
17
|
+
<LoaderCircle className="size-4 animate-spin" aria-hidden />
|
|
18
|
+
{what}
|
|
19
|
+
</p>
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function ErrorNotice({ error, title = 'Something went wrong' }: { error: unknown; title?: string }) {
|
|
24
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
25
|
+
const code = error instanceof ApiError ? error.code : undefined
|
|
26
|
+
return (
|
|
27
|
+
<Alert variant="destructive">
|
|
28
|
+
<AlertTitle>{title}</AlertTitle>
|
|
29
|
+
<AlertDescription>
|
|
30
|
+
<p>
|
|
31
|
+
{message}
|
|
32
|
+
{code == null ? null : <span className="ml-2 font-mono text-xs opacity-80">{code}</span>}
|
|
33
|
+
</p>
|
|
34
|
+
</AlertDescription>
|
|
35
|
+
</Alert>
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function CopyButton({ text, label = 'Copy' }: { text: string; label?: string }) {
|
|
40
|
+
const [copied, setCopied] = useState(false)
|
|
41
|
+
const [failed, setFailed] = useState(false)
|
|
42
|
+
async function copy() {
|
|
43
|
+
try {
|
|
44
|
+
await navigator.clipboard.writeText(text)
|
|
45
|
+
setCopied(true)
|
|
46
|
+
setFailed(false)
|
|
47
|
+
setTimeout(() => setCopied(false), 1500)
|
|
48
|
+
} catch {
|
|
49
|
+
setFailed(true)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return (
|
|
53
|
+
<Button type="button" variant="ghost" size="icon-xs" onClick={copy} aria-label={copied ? 'Copied' : label} title={failed ? 'Copying is not available here' : label}>
|
|
54
|
+
{copied ? <Check /> : <Copy />}
|
|
55
|
+
</Button>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type Tone = 'pass' | 'fail' | 'unknown' | 'muted' | 'neutral' | 'warn'
|
|
60
|
+
|
|
61
|
+
const tones: Record<Tone, string> = {
|
|
62
|
+
pass: 'border-transparent bg-emerald-100 text-emerald-900 dark:bg-emerald-950 dark:text-emerald-200',
|
|
63
|
+
fail: 'border-transparent bg-red-100 text-red-900 dark:bg-red-950 dark:text-red-200',
|
|
64
|
+
unknown: 'border-transparent bg-neutral-200 text-neutral-800 dark:bg-neutral-800 dark:text-neutral-200',
|
|
65
|
+
muted: 'border-border bg-transparent text-muted-foreground',
|
|
66
|
+
neutral: 'border-transparent bg-secondary text-secondary-foreground',
|
|
67
|
+
warn: 'border-transparent bg-amber-100 text-amber-900 dark:bg-amber-950 dark:text-amber-200',
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function ToneBadge({ tone, children, className }: { tone: Tone; children: ReactNode; className?: string }) {
|
|
71
|
+
return (
|
|
72
|
+
<Badge variant="outline" className={cn(tones[tone], className)}>
|
|
73
|
+
{children}
|
|
74
|
+
</Badge>
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const checkTones: Record<CheckStatus, { tone: Tone; label: string }> = {
|
|
79
|
+
pass: { tone: 'pass', label: 'Pass' },
|
|
80
|
+
fail: { tone: 'fail', label: 'Fail' },
|
|
81
|
+
unknown: { tone: 'unknown', label: 'Unknown' },
|
|
82
|
+
'not-applicable': { tone: 'muted', label: 'Not applicable' },
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function CheckBadge({ status }: { status: CheckStatus | string }) {
|
|
86
|
+
const entry = checkTones[status as CheckStatus] ?? { tone: 'neutral' as Tone, label: status }
|
|
87
|
+
return <ToneBadge tone={entry.tone}>{entry.label}</ToneBadge>
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function indexTone(answer: string): Tone {
|
|
91
|
+
if (answer === 'admitted') return 'pass'
|
|
92
|
+
if (answer === 'refused' || answer === 'unauthorised') return 'fail'
|
|
93
|
+
if (answer === 'pending' || answer === 'unreachable' || answer === 'not-asked') return 'warn'
|
|
94
|
+
return 'neutral'
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function networkTone(answer: string): Tone {
|
|
98
|
+
if (answer === 'accepted') return 'pass'
|
|
99
|
+
if (answer === 'refused') return 'fail'
|
|
100
|
+
if (answer === 'pending' || answer === 'unanswered') return 'warn'
|
|
101
|
+
if (answer === 'not-sent') return 'muted'
|
|
102
|
+
return 'neutral'
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function statusTone(status: string): Tone {
|
|
106
|
+
if (status === 'active' || status === 'open' || status === 'transferred') return 'pass'
|
|
107
|
+
if (status === 'retired' || status === 'declined' || status === 'withdrawn' || status === 'expired') return 'muted'
|
|
108
|
+
return 'neutral'
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function YesNo({ value }: { value: boolean }) {
|
|
112
|
+
return <ToneBadge tone={value ? 'pass' : 'muted'}>{value ? 'Yes' : 'No'}</ToneBadge>
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function PageTitle({ title, description, action }: { title: ReactNode; description?: ReactNode; action?: ReactNode }) {
|
|
116
|
+
return (
|
|
117
|
+
<div className="mb-8 flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between">
|
|
118
|
+
<div className="min-w-0 space-y-1">
|
|
119
|
+
<h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
|
|
120
|
+
{description == null ? null : <div className="text-sm text-muted-foreground">{description}</div>}
|
|
121
|
+
</div>
|
|
122
|
+
{action == null ? null : <div className="shrink-0">{action}</div>}
|
|
123
|
+
</div>
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function Section({ title, children, action, className }: { title: ReactNode; children: ReactNode; action?: ReactNode; className?: string }) {
|
|
128
|
+
return (
|
|
129
|
+
<section className={cn('space-y-3', className)}>
|
|
130
|
+
<div className="flex items-center justify-between gap-3">
|
|
131
|
+
<h2 className="text-lg font-semibold">{title}</h2>
|
|
132
|
+
{action}
|
|
133
|
+
</div>
|
|
134
|
+
{children}
|
|
135
|
+
</section>
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** A value as the page shows it: text and numbers inline, anything else as JSON. */
|
|
140
|
+
export function ValueView({ value }: { value: unknown }) {
|
|
141
|
+
if (value == null || value === '') return <span className="text-muted-foreground">none</span>
|
|
142
|
+
if (typeof value === 'string') {
|
|
143
|
+
if (/^https?:\/\//.test(value)) return <span className="break-all">{value}</span>
|
|
144
|
+
return <span className="whitespace-pre-wrap break-words">{value}</span>
|
|
145
|
+
}
|
|
146
|
+
if (typeof value === 'number' || typeof value === 'boolean') return <span>{String(value)}</span>
|
|
147
|
+
if (Array.isArray(value) && value.every((v) => typeof v === 'string' || typeof v === 'number')) return <span>{value.join(', ')}</span>
|
|
148
|
+
return <pre className="overflow-x-auto rounded-md bg-muted p-2 text-xs">{JSON.stringify(value, null, 2)}</pre>
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function DefinitionList({ entries, className }: { entries: Array<[ReactNode, ReactNode]>; className?: string }) {
|
|
152
|
+
if (entries.length === 0) return <p className="text-sm text-muted-foreground">Nothing here.</p>
|
|
153
|
+
return (
|
|
154
|
+
<dl className={cn('grid grid-cols-1 gap-x-6 gap-y-2 text-sm sm:grid-cols-[minmax(10rem,1fr)_3fr]', className)}>
|
|
155
|
+
{entries.map(([term, detail], i) => (
|
|
156
|
+
<div key={i} className="contents">
|
|
157
|
+
<dt className="font-medium text-muted-foreground sm:text-foreground">{term}</dt>
|
|
158
|
+
<dd className="min-w-0 pb-2 sm:pb-0">{detail}</dd>
|
|
159
|
+
</div>
|
|
160
|
+
))}
|
|
161
|
+
</dl>
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Entries for a definition list from a plain object, skipping the keys given. */
|
|
166
|
+
export function entriesOf(value: unknown, skip: Set<string> = new Set(), label: (key: string) => string = (k) => k): Array<[ReactNode, ReactNode]> {
|
|
167
|
+
if (!isRecord(value)) return []
|
|
168
|
+
return Object.entries(value)
|
|
169
|
+
.filter(([key]) => !skip.has(key))
|
|
170
|
+
.map(([key, v]) => [label(key), <ValueView key={key} value={v} />])
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function Field({ label, htmlFor, help, required, children }: { label: ReactNode; htmlFor?: string; help?: ReactNode; required?: boolean; children: ReactNode }) {
|
|
174
|
+
return (
|
|
175
|
+
<div className="space-y-1.5">
|
|
176
|
+
<Label htmlFor={htmlFor}>
|
|
177
|
+
{label}
|
|
178
|
+
{required ? <span className="text-destructive" aria-hidden>*</span> : null}
|
|
179
|
+
</Label>
|
|
180
|
+
{children}
|
|
181
|
+
{help == null ? null : <p className="text-xs text-muted-foreground">{help}</p>}
|
|
182
|
+
</div>
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function Mono({ children, className }: { children: ReactNode; className?: string }) {
|
|
187
|
+
return <code className={cn('break-all font-mono text-xs', className)}>{children}</code>
|
|
188
|
+
}
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
// The form for a passport under a profile held by a binding. The user
|
|
2
|
+
// enters one product document; the API validates the whole of it, splits it
|
|
3
|
+
// by the binding's list into a public part and a restricted part, signs
|
|
4
|
+
// only the public part and seals the rest.
|
|
5
|
+
//
|
|
6
|
+
// What the page is careful about:
|
|
7
|
+
//
|
|
8
|
+
// - the GTIN and serial are entered once, as the passport identifier's
|
|
9
|
+
// own parts, and the document's identity members are written from them;
|
|
10
|
+
// - every top-level property shows whether it will be public or
|
|
11
|
+
// restricted, before anything is sent, and the user confirms the public
|
|
12
|
+
// list under a warning that public means permanent;
|
|
13
|
+
// - a field left empty is absent from the document;
|
|
14
|
+
// - what the validator finds is shown at the field it concerns.
|
|
15
|
+
//
|
|
16
|
+
// A profile with no sector form here, such as a profile of your own, is
|
|
17
|
+
// written as a JSON document, with the same labelling and the same warning.
|
|
18
|
+
import { useMemo, useState, type ChangeEvent, type FormEvent, type ReactNode } from 'react'
|
|
19
|
+
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
|
20
|
+
import { Button } from '@/components/ui/button'
|
|
21
|
+
import { Input } from '@/components/ui/input'
|
|
22
|
+
import { Textarea } from '@/components/ui/textarea'
|
|
23
|
+
import { ErrorNotice, Field as Labelled, Mono } from '@/components/bits'
|
|
24
|
+
import { FieldView, type FieldContext } from '@/components/bound/field-view'
|
|
25
|
+
import { FindingLines, PublicDataWarning, TierBadge, ViewPanel } from '@/components/bound/pieces'
|
|
26
|
+
import { ApiError, api, type BoundProfile, type Finding, type ViewCheck } from '@/lib/api'
|
|
27
|
+
import { humaniseName } from '@/lib/bound/display'
|
|
28
|
+
import { buildDocument, codeClasses, emptyState, escapePointer, fieldPointerFor, isObject, labelsOf, parseDocument, splitPreview, stripComments, tierOf, type FormState, type Json, type Value } from '@/lib/bound/form-model'
|
|
29
|
+
import { sectorFormFor } from '@/lib/bound/forms'
|
|
30
|
+
import { identityFromDocument, mintIdentity, type IdentifierPolicy } from '@/lib/bound/identity'
|
|
31
|
+
import { useLoad } from '@/lib/use-load'
|
|
32
|
+
|
|
33
|
+
export interface BoundSubmission {
|
|
34
|
+
itemReference: string
|
|
35
|
+
serial: string
|
|
36
|
+
product: Json
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface Props {
|
|
40
|
+
profile: BoundProfile
|
|
41
|
+
policy: IdentifierPolicy
|
|
42
|
+
/** For an update: the passport's identifier, which cannot change. */
|
|
43
|
+
passportId?: string
|
|
44
|
+
/** For an update: the document as it stands, to start from. */
|
|
45
|
+
initialProduct?: Json
|
|
46
|
+
submitLabel: string
|
|
47
|
+
busy?: boolean
|
|
48
|
+
error?: unknown
|
|
49
|
+
onSubmit: (submission: BoundSubmission) => void
|
|
50
|
+
after?: ReactNode
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface Attempt {
|
|
54
|
+
state: FormState
|
|
55
|
+
rows: Map<string, number[]>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const IDENTITY = '/~identity'
|
|
59
|
+
const OTHERS = '/~others'
|
|
60
|
+
|
|
61
|
+
export function BoundPassportForm({ profile, policy, passportId, initialProduct, submitLabel, busy, error, onSubmit, after }: Props) {
|
|
62
|
+
const form = sectorFormFor(profile.id)
|
|
63
|
+
const fixed = passportId == null ? undefined : identityFromDocument(policy, passportId)
|
|
64
|
+
const [itemReference, setItemReference] = useState(fixed != null && 'itemReference' in fixed ? fixed.itemReference : '')
|
|
65
|
+
const [serial, setSerial] = useState(fixed != null && 'serial' in fixed ? fixed.serial : '')
|
|
66
|
+
const [state, setState] = useState<FormState>(() => (form != null && initialProduct != null ? parseDocument(form, initialProduct).state : emptyState()))
|
|
67
|
+
// Other properties, and for a profile with no sector form the whole document, are edited as JSON text.
|
|
68
|
+
const [othersText, setOthersText] = useState(() => (form != null && initialProduct != null ? jsonText(parseDocument(form, initialProduct).state.others) : ''))
|
|
69
|
+
const [documentText, setDocumentText] = useState(() => (form == null && initialProduct != null ? JSON.stringify(initialProduct, null, 2) : ''))
|
|
70
|
+
const [confirmed, setConfirmed] = useState(false)
|
|
71
|
+
const [loadNote, setLoadNote] = useState<string>()
|
|
72
|
+
const [attempt, setAttempt] = useState<Attempt>()
|
|
73
|
+
const codes = useLoad(() => (form == null ? Promise.resolve({}) : api.profileCodes(profile.id, codeClasses(form))), [profile.id])
|
|
74
|
+
|
|
75
|
+
const identity = mintIdentity(policy, itemReference.trim(), serial.trim())
|
|
76
|
+
const contexts = useMemo(() => (form == null ? [] : form.contexts.map((path) => profile.contexts.find((context) => context.path === path)?.uri).filter((uri): uri is string => uri != null)), [form, profile.contexts])
|
|
77
|
+
const others = useMemo(() => parseObject(othersText), [othersText])
|
|
78
|
+
const written = useMemo(() => parseObject(documentText), [documentText])
|
|
79
|
+
|
|
80
|
+
// The document as it would be sent now, and what stops it.
|
|
81
|
+
const built = useMemo(() => {
|
|
82
|
+
if (!identity.ok) return undefined
|
|
83
|
+
if (form == null) return written.ok && written.value != null ? { product: written.value, problems: [], rows: new Map<string, number[]>() } : undefined
|
|
84
|
+
return buildDocument(form, { ...state, others: others.ok ? (others.value ?? {}) : {} }, identity, contexts)
|
|
85
|
+
// The identity is rebuilt on every render; its parts are what matter.
|
|
86
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
87
|
+
}, [form, state, others, written, contexts, identity.ok, identity.ok ? identity.passportId : ''])
|
|
88
|
+
const preview = built == null ? undefined : splitPreview(built.product, profile.access)
|
|
89
|
+
const labels = useMemo(() => (form == null ? new Map<string, string>() : labelsOf(form)), [form])
|
|
90
|
+
|
|
91
|
+
// The validation of the last refused attempt, with each finding at the field it concerns.
|
|
92
|
+
const refusal = error instanceof ApiError ? error.details : undefined
|
|
93
|
+
const validation = (refusal?.publicView ?? refusal?.validation) as ViewCheck | undefined
|
|
94
|
+
const { placed, unplaced } = useMemo(() => {
|
|
95
|
+
const placed = new Map<string, Finding[]>()
|
|
96
|
+
const unplaced: Finding[] = []
|
|
97
|
+
for (const finding of validation?.findings ?? []) {
|
|
98
|
+
const at = form == null || attempt == null || finding.pointer == null ? undefined : fieldPointerFor(form, attempt.state, attempt.rows, finding.pointer)
|
|
99
|
+
if (at == null) unplaced.push(finding)
|
|
100
|
+
else placed.set(at, [...(placed.get(at) ?? []), finding])
|
|
101
|
+
}
|
|
102
|
+
return { placed, unplaced }
|
|
103
|
+
}, [validation, form, attempt])
|
|
104
|
+
const problems = useMemo(() => {
|
|
105
|
+
const map = new Map<string, string[]>()
|
|
106
|
+
for (const problem of built?.problems ?? []) map.set(problem.pointer, [...(map.get(problem.pointer) ?? []), problem.message])
|
|
107
|
+
return map
|
|
108
|
+
}, [built])
|
|
109
|
+
|
|
110
|
+
const engineDown = !profile.engine.available
|
|
111
|
+
const jsonProblem = form == null ? (written.ok ? (written.value == null ? 'Enter the product document.' : undefined) : written.problem) : others.ok ? undefined : others.problem
|
|
112
|
+
const blocked = !identity.ok || built == null || built.problems.length > 0 || jsonProblem != null || engineDown
|
|
113
|
+
const context: FieldContext = { codes: codes.data ?? {}, findings: placed, problems, disabled: busy }
|
|
114
|
+
|
|
115
|
+
function load(product: Json, source: string) {
|
|
116
|
+
const clean = stripComments(product)
|
|
117
|
+
const removed = JSON.stringify(product).includes('"_comment') ? ' Its note members, which the profile does not define, were removed.' : ''
|
|
118
|
+
setConfirmed(false)
|
|
119
|
+
setAttempt(undefined)
|
|
120
|
+
if (passportId == null) {
|
|
121
|
+
const named = identityFromDocument(policy, [clean.id, clean['@id']].find((value): value is string => typeof value === 'string'))
|
|
122
|
+
if ('problem' in named) {
|
|
123
|
+
setLoadNote(`${source} was not loaded. ${named.problem}`)
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
setItemReference(named.itemReference)
|
|
127
|
+
setSerial(named.serial)
|
|
128
|
+
}
|
|
129
|
+
if (form == null) {
|
|
130
|
+
setDocumentText(JSON.stringify(clean, null, 2))
|
|
131
|
+
setLoadNote(`${source} was loaded.${removed}`)
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
const parsed = parseDocument(form, clean)
|
|
135
|
+
setState(parsed.state)
|
|
136
|
+
setOthersText(jsonText(parsed.state.others))
|
|
137
|
+
setLoadNote(
|
|
138
|
+
`${source} was loaded.${removed} ${parsed.kept.length === 0 ? 'Every property is in a field of the form.' : `${parsed.kept.length} ${parsed.kept.length === 1 ? 'property is' : 'properties are'} kept as JSON under Other properties, because the form has no field that holds ${parsed.kept.length === 1 ? 'it' : 'them'} exactly: ${parsed.kept.join(', ')}.`}`
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function loadFile(event: ChangeEvent<HTMLInputElement>) {
|
|
143
|
+
const file = event.target.files?.[0]
|
|
144
|
+
event.target.value = ''
|
|
145
|
+
if (file == null) return
|
|
146
|
+
try {
|
|
147
|
+
const parsed: unknown = JSON.parse(await file.text())
|
|
148
|
+
if (!isObject(parsed)) throw new Error('it is not one JSON object')
|
|
149
|
+
load(parsed, file.name)
|
|
150
|
+
} catch (cause) {
|
|
151
|
+
setLoadNote(`${file.name} was not loaded: ${cause instanceof Error ? cause.message : String(cause)}.`)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function submit(event: FormEvent) {
|
|
156
|
+
event.preventDefault()
|
|
157
|
+
if (blocked || built == null || !identity.ok || !confirmed) return
|
|
158
|
+
setAttempt({ state: { ...state, others: others.ok ? (others.value ?? {}) : {} }, rows: built.rows })
|
|
159
|
+
onSubmit({ itemReference: itemReference.trim(), serial: serial.trim(), product: built.product })
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const setValue = (key: string, value: Value): void => {
|
|
163
|
+
setState((current) => ({ ...current, values: { ...current.values, [key]: value } }))
|
|
164
|
+
setConfirmed(false)
|
|
165
|
+
}
|
|
166
|
+
const digits = Math.max(0, 12 - policy.prefix.length)
|
|
167
|
+
|
|
168
|
+
return (
|
|
169
|
+
<form onSubmit={submit} className="space-y-8">
|
|
170
|
+
{engineDown ? (
|
|
171
|
+
<Alert variant="destructive">
|
|
172
|
+
<AlertTitle>This profile cannot be written yet</AlertTitle>
|
|
173
|
+
<AlertDescription>
|
|
174
|
+
<p>The validation engine is not available, and a document that was not validated is never written. {profile.engine.problem}</p>
|
|
175
|
+
</AlertDescription>
|
|
176
|
+
</Alert>
|
|
177
|
+
) : null}
|
|
178
|
+
{profile.demonstration ? (
|
|
179
|
+
<Alert>
|
|
180
|
+
<AlertTitle>Demonstration prefix</AlertTitle>
|
|
181
|
+
<AlertDescription>
|
|
182
|
+
<p>This platform issues under the GS1 demonstration prefix, so the API writes a notice first in every payload, saying the record describes no real product.</p>
|
|
183
|
+
</AlertDescription>
|
|
184
|
+
</Alert>
|
|
185
|
+
) : null}
|
|
186
|
+
|
|
187
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
188
|
+
{profile.examples.map((example) => (
|
|
189
|
+
<Button key={example.title} type="button" variant="outline" size="sm" onClick={() => load(example.product, `The example "${example.title}"`)} disabled={busy}>
|
|
190
|
+
Load example: {example.title}
|
|
191
|
+
</Button>
|
|
192
|
+
))}
|
|
193
|
+
<label className="inline-flex cursor-pointer items-center gap-2 rounded-md border px-3 py-1.5 text-sm">
|
|
194
|
+
Load a document from a file
|
|
195
|
+
<input type="file" accept=".json,.jsonld,application/json,application/ld+json" className="sr-only" onChange={loadFile} disabled={busy} />
|
|
196
|
+
</label>
|
|
197
|
+
</div>
|
|
198
|
+
{loadNote == null ? null : <p className="text-sm text-muted-foreground">{loadNote}</p>}
|
|
199
|
+
{codes.error == null ? null : <p className="text-sm text-muted-foreground">The profile's code lists could not be loaded ({codes.error.message}). A coded value can still be typed, exactly as the profile's vocabulary spells it.</p>}
|
|
200
|
+
|
|
201
|
+
<fieldset className="space-y-4">
|
|
202
|
+
<legend className="mb-3 text-base font-semibold">Identifier</legend>
|
|
203
|
+
<p className="text-sm text-muted-foreground">
|
|
204
|
+
The GTIN and the serial are entered once, here. They make the passport identifier, and the document's own identifier, GTIN and serial are written from them, so the two cannot disagree. All of them are public.
|
|
205
|
+
</p>
|
|
206
|
+
<div className="grid gap-4 sm:grid-cols-2">
|
|
207
|
+
<Labelled label="Item reference" htmlFor="itemReference" required help={`The digits after the GS1 prefix ${policy.prefix}: ${digits} digits.`}>
|
|
208
|
+
<Input id="itemReference" value={itemReference} onChange={(e) => setItemReference(e.target.value)} required inputMode="numeric" pattern="[0-9]+" placeholder={'0'.repeat(digits)} disabled={busy || passportId != null} />
|
|
209
|
+
</Labelled>
|
|
210
|
+
<Labelled label="Serial" htmlFor="serial" required help="The item's serial, 1 to 20 characters.">
|
|
211
|
+
<Input id="serial" value={serial} onChange={(e) => setSerial(e.target.value)} required maxLength={20} disabled={busy || passportId != null} spellCheck={false} />
|
|
212
|
+
</Labelled>
|
|
213
|
+
</div>
|
|
214
|
+
{identity.ok ? (
|
|
215
|
+
<dl className="grid grid-cols-1 gap-x-4 gap-y-1 text-sm sm:grid-cols-[10rem_1fr]">
|
|
216
|
+
<dt className="text-muted-foreground">Passport identifier</dt>
|
|
217
|
+
<dd>
|
|
218
|
+
<Mono>{identity.passportId}</Mono>
|
|
219
|
+
</dd>
|
|
220
|
+
<dt className="text-muted-foreground">GTIN</dt>
|
|
221
|
+
<dd>
|
|
222
|
+
<Mono>{identity.gtin}</Mono>
|
|
223
|
+
</dd>
|
|
224
|
+
{form == null ? null : (
|
|
225
|
+
<>
|
|
226
|
+
<dt className="text-muted-foreground">Granularity</dt>
|
|
227
|
+
<dd>{state.granularity ? 'Item: one passport for one serialised item, as the identifier says.' : 'Not stated in the loaded document.'}</dd>
|
|
228
|
+
</>
|
|
229
|
+
)}
|
|
230
|
+
</dl>
|
|
231
|
+
) : itemReference === '' && serial === '' ? null : (
|
|
232
|
+
<p className="text-sm text-destructive">{identity.problem}</p>
|
|
233
|
+
)}
|
|
234
|
+
<FindingLines findings={placed.get(IDENTITY)} />
|
|
235
|
+
</fieldset>
|
|
236
|
+
|
|
237
|
+
{form == null ? (
|
|
238
|
+
<JsonDocument profile={profile} identity={identity.ok ? identity.passportId : undefined} text={documentText} onChange={(text) => (setDocumentText(text), setConfirmed(false))} problem={documentText === '' ? undefined : jsonProblem} disabled={busy} />
|
|
239
|
+
) : (
|
|
240
|
+
<>
|
|
241
|
+
{form.sections.map((section) => (
|
|
242
|
+
<fieldset key={section.title} className="space-y-4">
|
|
243
|
+
<legend className="mb-1 text-base font-semibold">{section.title}</legend>
|
|
244
|
+
{section.description == null ? null : <p className="text-sm text-muted-foreground">{section.description}</p>}
|
|
245
|
+
<div className="grid gap-4 sm:grid-cols-2">
|
|
246
|
+
{section.fields.map((field) => (
|
|
247
|
+
<FieldView key={field.key} field={field} value={state.values[field.key]} onChange={(value) => setValue(field.key, value)} pointer={`/${escapePointer(field.key)}`} context={context} tier={tierOf(profile.access, field.key)} />
|
|
248
|
+
))}
|
|
249
|
+
</div>
|
|
250
|
+
</fieldset>
|
|
251
|
+
))}
|
|
252
|
+
<fieldset className="space-y-2">
|
|
253
|
+
<legend className="mb-1 text-base font-semibold">Other properties</legend>
|
|
254
|
+
<p className="text-sm text-muted-foreground">Properties of the profile this form has no field for, as one JSON object. They are written exactly as they are here. A property the binding does not list as public is restricted.</p>
|
|
255
|
+
<Textarea value={othersText} onChange={(e) => (setOthersText(e.target.value), setConfirmed(false))} className="min-h-24 font-mono text-xs" spellCheck={false} placeholder="{ }" aria-label="Other properties, as JSON" disabled={busy} />
|
|
256
|
+
<FindingLines findings={placed.get(OTHERS)} problems={[...(others.ok ? [] : [others.problem]), ...(problems.get(OTHERS) ?? [])]} />
|
|
257
|
+
</fieldset>
|
|
258
|
+
</>
|
|
259
|
+
)}
|
|
260
|
+
|
|
261
|
+
<section className="space-y-4">
|
|
262
|
+
<h2 className="text-base font-semibold">Before you write it</h2>
|
|
263
|
+
<PublicDataWarning split={profile.access != null} />
|
|
264
|
+
{preview == null ? (
|
|
265
|
+
<p className="text-sm text-muted-foreground">Enter the identifier{form == null ? ' and the document' : ''} to see what will be public.</p>
|
|
266
|
+
) : (
|
|
267
|
+
<div className="grid gap-4 sm:grid-cols-2">
|
|
268
|
+
<TierList tier="public" title="Public, on chain for good" names={preview.public} labels={labels} empty="Nothing." />
|
|
269
|
+
<TierList tier="restricted" title="Restricted, sealed off chain for the holder" names={preview.restricted} labels={labels} empty={profile.access == null ? 'This profile has no restricted part.' : 'Nothing.'} />
|
|
270
|
+
</div>
|
|
271
|
+
)}
|
|
272
|
+
<label className="flex items-start gap-2 text-sm">
|
|
273
|
+
<input type="checkbox" className="mt-1" checked={confirmed} onChange={(e) => setConfirmed(e.target.checked)} disabled={blocked || busy} />
|
|
274
|
+
<span>I have read the public list. Nothing in it, and nothing its values repeat or link to, has to stay private.</span>
|
|
275
|
+
</label>
|
|
276
|
+
</section>
|
|
277
|
+
|
|
278
|
+
{after}
|
|
279
|
+
{built != null && built.problems.length > 0 ? (
|
|
280
|
+
<ErrorNotice error={new Error(`${built.problems.length} ${built.problems.length === 1 ? 'field needs' : 'fields need'} attention before the document can be built. Each is marked above.`)} title="Check the form" />
|
|
281
|
+
) : null}
|
|
282
|
+
{error == null ? null : <ErrorNotice error={error} title="The API refused it" />}
|
|
283
|
+
{validation == null ? null : (
|
|
284
|
+
<ViewPanel title={validation.view === 'public' ? 'The public part on its own' : 'The whole document'} check={validation}>
|
|
285
|
+
{unplaced.length === 0 || form == null ? null : <p className="text-xs text-muted-foreground">Findings that name a field are shown at that field. The ones listed here could not be placed.</p>}
|
|
286
|
+
</ViewPanel>
|
|
287
|
+
)}
|
|
288
|
+
<div className="flex items-center gap-3">
|
|
289
|
+
<Button type="submit" disabled={busy || blocked || !confirmed}>
|
|
290
|
+
{busy ? 'Working' : submitLabel}
|
|
291
|
+
</Button>
|
|
292
|
+
<span className="text-xs text-muted-foreground">
|
|
293
|
+
<span className="text-destructive">*</span> the profile's rules require it
|
|
294
|
+
</span>
|
|
295
|
+
</div>
|
|
296
|
+
</form>
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const jsonText = (value: Json): string => (Object.keys(value).length === 0 ? '' : JSON.stringify(value, null, 2))
|
|
301
|
+
|
|
302
|
+
/** JSON text as one object: nothing for empty text, a problem for anything that is not an object. */
|
|
303
|
+
function parseObject(text: string): { ok: true; value?: Json } | { ok: false; problem: string } {
|
|
304
|
+
if (text.trim() === '') return { ok: true }
|
|
305
|
+
try {
|
|
306
|
+
const parsed: unknown = JSON.parse(text)
|
|
307
|
+
return isObject(parsed) ? { ok: true, value: parsed } : { ok: false, problem: 'This must be one JSON object.' }
|
|
308
|
+
} catch (cause) {
|
|
309
|
+
return { ok: false, problem: `This is not valid JSON: ${cause instanceof Error ? cause.message : String(cause)}` }
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function TierList({ tier, title, names, labels, empty }: { tier: 'public' | 'restricted'; title: string; names: string[]; labels: Map<string, string>; empty: string }) {
|
|
314
|
+
return (
|
|
315
|
+
<div className="space-y-2 rounded-lg border p-3">
|
|
316
|
+
<div className="flex items-center gap-2 text-sm font-medium">
|
|
317
|
+
<TierBadge tier={tier} />
|
|
318
|
+
{title}
|
|
319
|
+
</div>
|
|
320
|
+
{names.length === 0 ? (
|
|
321
|
+
<p className="text-sm text-muted-foreground">{empty}</p>
|
|
322
|
+
) : (
|
|
323
|
+
<ul className="space-y-1 text-sm">
|
|
324
|
+
{names.map((name) => (
|
|
325
|
+
<li key={name} className="flex flex-wrap items-baseline gap-x-2">
|
|
326
|
+
<span>{name === '@context' ? 'Contexts' : name === 'id' || name === '@id' ? 'Identifier' : name === 'type' || name === '@type' ? 'Type' : (labels.get(name) ?? humaniseName(name))}</span>
|
|
327
|
+
<Mono className="text-muted-foreground">{name}</Mono>
|
|
328
|
+
</li>
|
|
329
|
+
))}
|
|
330
|
+
</ul>
|
|
331
|
+
)}
|
|
332
|
+
</div>
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** The whole product document as JSON, for a profile this application has no form for. */
|
|
337
|
+
function JsonDocument({ profile, identity, text, onChange, problem, disabled }: { profile: BoundProfile; identity?: string; text: string; onChange: (text: string) => void; problem?: string; disabled?: boolean }) {
|
|
338
|
+
function insertIdentity() {
|
|
339
|
+
if (identity == null) return
|
|
340
|
+
const parsed = parseObject(text)
|
|
341
|
+
const document: Json = parsed.ok ? (parsed.value ?? {}) : {}
|
|
342
|
+
// Only the identifier just entered goes in. Which contexts the document names, and everything else, is the author's.
|
|
343
|
+
onChange(JSON.stringify({ ...document, id: identity }, null, 2))
|
|
344
|
+
}
|
|
345
|
+
return (
|
|
346
|
+
<fieldset className="space-y-2">
|
|
347
|
+
<legend className="mb-1 text-base font-semibold">Product document</legend>
|
|
348
|
+
<p className="text-sm text-muted-foreground">
|
|
349
|
+
{profile.title} has no form in this application, so its product document is written as JSON, in the profile's own data model. Its identifier must name the same GTIN and serial as the passport identifier above.
|
|
350
|
+
</p>
|
|
351
|
+
<Textarea value={text} onChange={(e) => onChange(e.target.value)} className="min-h-64 font-mono text-xs" spellCheck={false} placeholder="{ }" aria-label="Product document, as JSON" disabled={disabled} />
|
|
352
|
+
{problem == null ? null : <p className="text-xs text-destructive">{problem}</p>}
|
|
353
|
+
{profile.contexts.length === 0 ? null : (
|
|
354
|
+
<p className="text-xs text-muted-foreground">
|
|
355
|
+
A document may name these contexts and no others:{' '}
|
|
356
|
+
{profile.contexts.map((context) => (
|
|
357
|
+
<Mono key={context.uri} className="mr-2">
|
|
358
|
+
{context.uri}
|
|
359
|
+
</Mono>
|
|
360
|
+
))}
|
|
361
|
+
</p>
|
|
362
|
+
)}
|
|
363
|
+
<Button type="button" variant="outline" size="sm" onClick={insertIdentity} disabled={disabled || identity == null}>
|
|
364
|
+
Put the passport identifier into the document
|
|
365
|
+
</Button>
|
|
366
|
+
</fieldset>
|
|
367
|
+
)
|
|
368
|
+
}
|