@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.
Files changed (178) hide show
  1. package/LICENSE +203 -0
  2. package/NOTICE +2 -0
  3. package/PATENTS +12 -0
  4. package/README.md +93 -2
  5. package/dist/index.js +125 -0
  6. package/package.json +57 -4
  7. package/template/Dockerfile +37 -0
  8. package/template/README.md +389 -0
  9. package/template/_dockerignore +6 -0
  10. package/template/_env.example +122 -0
  11. package/template/_github/workflows/ci.yml +23 -0
  12. package/template/_gitignore +8 -0
  13. package/template/_prettierrc +6 -0
  14. package/template/apps/api/API.md +119 -0
  15. package/template/apps/api/package.json +39 -0
  16. package/template/apps/api/src/auth.ts +55 -0
  17. package/template/apps/api/src/bound-profiles.ts +590 -0
  18. package/template/apps/api/src/claims.ts +204 -0
  19. package/template/apps/api/src/cli.ts +99 -0
  20. package/template/apps/api/src/config.ts +180 -0
  21. package/template/apps/api/src/custody.ts +358 -0
  22. package/template/apps/api/src/dev-index.ts +120 -0
  23. package/template/apps/api/src/env-file.ts +55 -0
  24. package/template/apps/api/src/env.ts +14 -0
  25. package/template/apps/api/src/epcis/exchange.ts +434 -0
  26. package/template/apps/api/src/epcis/mongo-storage.ts +381 -0
  27. package/template/apps/api/src/epcis/routes.ts +364 -0
  28. package/template/apps/api/src/epcis/sources.ts +142 -0
  29. package/template/apps/api/src/go-live.ts +226 -0
  30. package/template/apps/api/src/http-error.ts +15 -0
  31. package/template/apps/api/src/identifiers.ts +57 -0
  32. package/template/apps/api/src/index-client.ts +261 -0
  33. package/template/apps/api/src/index-projects.ts +53 -0
  34. package/template/apps/api/src/lifecycle-authority.ts +49 -0
  35. package/template/apps/api/src/lifecycle.ts +485 -0
  36. package/template/apps/api/src/main.ts +58 -0
  37. package/template/apps/api/src/parties.ts +68 -0
  38. package/template/apps/api/src/platform.ts +176 -0
  39. package/template/apps/api/src/portability/commands.ts +224 -0
  40. package/template/apps/api/src/portability/export.ts +708 -0
  41. package/template/apps/api/src/portability/package.ts +270 -0
  42. package/template/apps/api/src/portability/restore.ts +863 -0
  43. package/template/apps/api/src/profiles.ts +125 -0
  44. package/template/apps/api/src/reader.ts +265 -0
  45. package/template/apps/api/src/resolver.ts +159 -0
  46. package/template/apps/api/src/sector-examples.ts +390 -0
  47. package/template/apps/api/src/server.ts +613 -0
  48. package/template/apps/api/src/store.ts +426 -0
  49. package/template/apps/api/src/validation-engine.ts +172 -0
  50. package/template/apps/api/src/wallet.ts +359 -0
  51. package/template/apps/api/src/worker.ts +113 -0
  52. package/template/apps/api/src/writer.ts +603 -0
  53. package/template/apps/api/test/bound-http.test.ts +496 -0
  54. package/template/apps/api/test/bound-profiles.test.ts +547 -0
  55. package/template/apps/api/test/config.test.ts +160 -0
  56. package/template/apps/api/test/custody-journal.test.ts +415 -0
  57. package/template/apps/api/test/custody-recovery-profile.test.ts +224 -0
  58. package/template/apps/api/test/custom-profile.test.ts +167 -0
  59. package/template/apps/api/test/env-file.test.ts +53 -0
  60. package/template/apps/api/test/epcis-fixtures.ts +270 -0
  61. package/template/apps/api/test/epcis-http.test.ts +1179 -0
  62. package/template/apps/api/test/epcis-mongo-storage.test.ts +313 -0
  63. package/template/apps/api/test/epcis-separation.test.ts +253 -0
  64. package/template/apps/api/test/epcis-web.test.ts +230 -0
  65. package/template/apps/api/test/exit-exercise.test.ts +1038 -0
  66. package/template/apps/api/test/external-index.test.ts +32 -0
  67. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/background.ttl +33 -0
  68. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/binding.json +141 -0
  69. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/context.jsonld +36 -0
  70. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/product.schema.json +44 -0
  71. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/shapes.ttl +112 -0
  72. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/conflicting-gtin.json +44 -0
  73. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/conflicting-serial.json +44 -0
  74. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/granularity-mismatch.json +44 -0
  75. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/item-gtin-13.json +43 -0
  76. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/item.json +42 -0
  77. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/missing-required-property.json +43 -0
  78. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/model-as-subject.json +44 -0
  79. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/no-passport-identifier.json +43 -0
  80. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/other-product.json +44 -0
  81. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/part-not-dishwasher-safe.json +45 -0
  82. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/altered-artefact.json +19 -0
  83. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/altered-background.ttl +33 -0
  84. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/conflicting-binding.json +141 -0
  85. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/conflicting-second-registration.json +19 -0
  86. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/untrusted-binding.json +13 -0
  87. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/unlisted-context.json +47 -0
  88. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/wrong-binding-digest.json +44 -0
  89. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/binding.json +88 -0
  90. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/product.schema.json +25 -0
  91. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/item.json +26 -0
  92. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/missing-required-property.json +27 -0
  93. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/no-passport-identifier.json +27 -0
  94. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/other-product.json +28 -0
  95. package/template/apps/api/test/go-live.test.ts +179 -0
  96. package/template/apps/api/test/gs1-global-setup.ts +33 -0
  97. package/template/apps/api/test/gs1-test-env.ts +5 -0
  98. package/template/apps/api/test/identifier-host.test.ts +256 -0
  99. package/template/apps/api/test/index-projects.test.ts +81 -0
  100. package/template/apps/api/test/lifecycle-claims.test.ts +546 -0
  101. package/template/apps/api/test/lifecycle-fixtures.ts +167 -0
  102. package/template/apps/api/test/lifecycle.test.ts +147 -0
  103. package/template/apps/api/test/manifest-notice.test.ts +36 -0
  104. package/template/apps/api/test/network-guard.ts +113 -0
  105. package/template/apps/api/test/portability.test.ts +655 -0
  106. package/template/apps/api/test/profile-bundle-resolver.test.ts +198 -0
  107. package/template/apps/api/test/retired-profiles.test.ts +216 -0
  108. package/template/apps/api/test/sector-cutover.test.ts +95 -0
  109. package/template/apps/api/test/sector-forms.test.ts +420 -0
  110. package/template/apps/api/test/unjoined-index.test.ts +62 -0
  111. package/template/apps/api/test/validation-engine.test.ts +328 -0
  112. package/template/apps/api/test/wallet-monitor.test.ts +38 -0
  113. package/template/apps/api/test/writer-journal.test.ts +327 -0
  114. package/template/apps/api/tsconfig.json +9 -0
  115. package/template/apps/api/vitest.config.ts +12 -0
  116. package/template/apps/web/components.json +21 -0
  117. package/template/apps/web/index.html +12 -0
  118. package/template/apps/web/package.json +33 -0
  119. package/template/apps/web/src/app.tsx +55 -0
  120. package/template/apps/web/src/components/bits.tsx +188 -0
  121. package/template/apps/web/src/components/bound/bound-passport-form.tsx +368 -0
  122. package/template/apps/web/src/components/bound/field-view.tsx +237 -0
  123. package/template/apps/web/src/components/bound/pieces.tsx +169 -0
  124. package/template/apps/web/src/components/bound/readings.tsx +152 -0
  125. package/template/apps/web/src/components/bound/structured-value.tsx +114 -0
  126. package/template/apps/web/src/components/claim-dialog.tsx +344 -0
  127. package/template/apps/web/src/components/epcis-events.tsx +119 -0
  128. package/template/apps/web/src/components/layout.tsx +135 -0
  129. package/template/apps/web/src/components/lifecycle-claims.tsx +199 -0
  130. package/template/apps/web/src/components/passport-form.tsx +244 -0
  131. package/template/apps/web/src/components/read-passport.tsx +49 -0
  132. package/template/apps/web/src/components/reading.tsx +150 -0
  133. package/template/apps/web/src/components/ui/alert.tsx +65 -0
  134. package/template/apps/web/src/components/ui/badge.tsx +47 -0
  135. package/template/apps/web/src/components/ui/button.tsx +63 -0
  136. package/template/apps/web/src/components/ui/card.tsx +91 -0
  137. package/template/apps/web/src/components/ui/dialog.tsx +156 -0
  138. package/template/apps/web/src/components/ui/input.tsx +20 -0
  139. package/template/apps/web/src/components/ui/label.tsx +21 -0
  140. package/template/apps/web/src/components/ui/select.tsx +189 -0
  141. package/template/apps/web/src/components/ui/separator.tsx +25 -0
  142. package/template/apps/web/src/components/ui/table.tsx +115 -0
  143. package/template/apps/web/src/components/ui/tabs.tsx +90 -0
  144. package/template/apps/web/src/components/ui/textarea.tsx +17 -0
  145. package/template/apps/web/src/index.css +81 -0
  146. package/template/apps/web/src/lib/api.ts +396 -0
  147. package/template/apps/web/src/lib/auth.ts +10 -0
  148. package/template/apps/web/src/lib/bound/battery-form.ts +269 -0
  149. package/template/apps/web/src/lib/bound/display.ts +122 -0
  150. package/template/apps/web/src/lib/bound/fields.ts +128 -0
  151. package/template/apps/web/src/lib/bound/form-model.ts +521 -0
  152. package/template/apps/web/src/lib/bound/forms.ts +15 -0
  153. package/template/apps/web/src/lib/bound/identity.ts +48 -0
  154. package/template/apps/web/src/lib/bound/textile-form.ts +236 -0
  155. package/template/apps/web/src/lib/bound/units.ts +37 -0
  156. package/template/apps/web/src/lib/epcis.ts +345 -0
  157. package/template/apps/web/src/lib/format.ts +87 -0
  158. package/template/apps/web/src/lib/lifecycle.ts +350 -0
  159. package/template/apps/web/src/lib/session.tsx +92 -0
  160. package/template/apps/web/src/lib/use-load.ts +46 -0
  161. package/template/apps/web/src/main.tsx +18 -0
  162. package/template/apps/web/src/pages/accept.tsx +113 -0
  163. package/template/apps/web/src/pages/brand.tsx +94 -0
  164. package/template/apps/web/src/pages/brands.tsx +104 -0
  165. package/template/apps/web/src/pages/events.tsx +388 -0
  166. package/template/apps/web/src/pages/home.tsx +51 -0
  167. package/template/apps/web/src/pages/new-passport.tsx +137 -0
  168. package/template/apps/web/src/pages/operations.tsx +169 -0
  169. package/template/apps/web/src/pages/passport.tsx +632 -0
  170. package/template/apps/web/src/pages/public-passport.tsx +64 -0
  171. package/template/apps/web/src/pages/sign-in.tsx +93 -0
  172. package/template/apps/web/src/pages/verify.tsx +30 -0
  173. package/template/apps/web/src/pages/wallet.tsx +141 -0
  174. package/template/apps/web/tsconfig.json +29 -0
  175. package/template/apps/web/vite.config.ts +23 -0
  176. package/template/deploy/compose.yml +38 -0
  177. package/template/package.json +48 -0
  178. package/template/tsconfig.base.json +13 -0
@@ -0,0 +1,632 @@
1
+ // The brand's view of one passport: what is in the journal, and every action
2
+ // on it in a dialog. A passport that was handed on is held by a recipient,
3
+ // and every action then needs the claim code they accepted with.
4
+ import { useRef, useState, type FormEvent, type ReactNode } from 'react'
5
+ import { Link, useParams } from 'react-router'
6
+ import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
7
+ import { Button } from '@/components/ui/button'
8
+ import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'
9
+ import { Input } from '@/components/ui/input'
10
+ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
11
+ import { Textarea } from '@/components/ui/textarea'
12
+ import { CopyButton, DefinitionList, ErrorNotice, Field, Loading, Mono, PageTitle, Section, ToneBadge, YesNo, entriesOf, indexTone, networkTone, statusTone } from '@/components/bits'
13
+ import { BoundPassportForm, type BoundSubmission } from '@/components/bound/bound-passport-form'
14
+ import { EventsNaming, SuppliedDataNotice } from '@/components/epcis-events'
15
+ import { HolderReading, boundPayload } from '@/components/bound/readings'
16
+ import { RequireAccount } from '@/components/layout'
17
+ import { PassportForm, type PassportFormValues } from '@/components/passport-form'
18
+ import { passportSpellings } from '@/lib/epcis'
19
+ import { RecordNotice } from '@/components/reading'
20
+ import { ClaimDialog, DeclareErrorDialog } from '@/components/claim-dialog'
21
+ import { LifecycleClaims } from '@/components/lifecycle-claims'
22
+ import { api, type AuthorisedReading, type BoundProfile, type OfferView, type PassportView } from '@/lib/api'
23
+ import type { LifecycleClaimView } from '@/lib/lifecycle'
24
+ import { formatTime, holderLabel, humanise, isRecord, parseIdentifier, passportIdFromRef, publicPathOf, shortKey, shortTxid } from '@/lib/format'
25
+ import { useHealth } from '@/lib/session'
26
+ import { useLoad } from '@/lib/use-load'
27
+
28
+ type DialogKind = 'update' | 'offer' | 'retire' | 'claim'
29
+
30
+ export function Passport() {
31
+ return (
32
+ <RequireAccount>
33
+ <PassportPage />
34
+ </RequireAccount>
35
+ )
36
+ }
37
+
38
+ function PassportPage() {
39
+ const { ref = '' } = useParams()
40
+ const passportId = passportIdFromRef(ref)
41
+ const passport = useLoad(() => api.passport(passportId), [passportId])
42
+ const offers = useLoad(() => api.passportOffers(passportId), [passportId])
43
+ const claims = useLoad(() => api.passportClaims(passportId), [passportId])
44
+ const [dialog, setDialog] = useState<DialogKind>()
45
+ const [declaring, setDeclaring] = useState<LifecycleClaimView>()
46
+
47
+ if (passport.loading) return <Loading what="Loading the passport" />
48
+ if (passport.error != null || passport.data == null) return <ErrorNotice error={passport.error ?? new Error('no answer')} title="Could not load the passport" />
49
+
50
+ const view = passport.data
51
+ const needsClaimCode = view.holder.party.startsWith('recipient:')
52
+ const retired = view.status === 'retired'
53
+ // A passport under a retired profile is shown as it was written. The API refuses every write for it, so none is offered.
54
+ const profileRetired = view.profileRetired
55
+ const closed = view.status !== 'active' || profileRetired != null || view.states.some((s) => s.network !== 'accepted' && s.network !== 'dry-run')
56
+ const ident = parseIdentifier(view.passportId)
57
+ const publicPath = publicPathOf(view.passportId)
58
+ const close = () => setDialog(undefined)
59
+ // Under a profile held by a binding the payload is one product document, split into a public and a restricted part.
60
+ const bound = boundPayload(view.payload)
61
+
62
+ return (
63
+ <div className="space-y-10">
64
+ <PageTitle
65
+ title={ident == null ? view.passportId : `Serial ${ident.serial}`}
66
+ description={
67
+ <span className="flex flex-wrap items-center gap-2">
68
+ {ident == null ? null : <span>GTIN {ident.gtin}</span>}
69
+ <Mono>{view.passportId}</Mono>
70
+ <CopyButton text={view.passportId} label="Copy the identifier" />
71
+ </span>
72
+ }
73
+ action={
74
+ publicPath == null ? null : (
75
+ <Button asChild variant="outline">
76
+ <Link to={publicPath}>Public page</Link>
77
+ </Button>
78
+ )
79
+ }
80
+ />
81
+
82
+ <div className="flex flex-wrap items-center gap-x-4 gap-y-2 text-sm">
83
+ <ToneBadge tone={statusTone(view.status)}>{view.status}</ToneBadge>
84
+ {view.demonstration ? <ToneBadge tone="warn">Demonstration</ToneBadge> : null}
85
+ <span className="text-muted-foreground">Profile {view.profile}</span>
86
+ <span className="text-muted-foreground">
87
+ Held by {holderLabel(view.holder.party)} <Mono className="text-muted-foreground">{shortKey(view.holder.controllerKey)}</Mono>
88
+ </span>
89
+ <span className="text-muted-foreground">Issued {formatTime(view.createdAt)}</span>
90
+ <span className="text-muted-foreground">
91
+ Brand{' '}
92
+ <Link to={`/brands/${encodeURIComponent(view.brandId)}`} className="underline underline-offset-4">
93
+ {view.brandId}
94
+ </Link>
95
+ </span>
96
+ </div>
97
+
98
+ {profileRetired == null ? null : (
99
+ <Alert>
100
+ <AlertTitle>Profile {view.profile} is retired</AlertTitle>
101
+ <AlertDescription>
102
+ <p>
103
+ This passport stays as it was written, and its product data is not checked against the retired profile. No further state, hand-on or claim is made for it. {profileRetired.replacedBy} replaces the profile, and nothing converts this passport: issue a new one under the replacement.
104
+ </p>
105
+ </AlertDescription>
106
+ </Alert>
107
+ )}
108
+
109
+ {needsClaimCode ? (
110
+ <Alert>
111
+ <AlertTitle>Held by a recipient</AlertTitle>
112
+ <AlertDescription>
113
+ <p>This passport was handed on. Every action on it now needs the claim code the recipient accepted with.</p>
114
+ </AlertDescription>
115
+ </Alert>
116
+ ) : null}
117
+
118
+ <div className="flex flex-wrap gap-2">
119
+ <Button onClick={() => setDialog('update')} disabled={closed}>
120
+ Update
121
+ </Button>
122
+ <Button variant="outline" onClick={() => setDialog('offer')} disabled={closed}>
123
+ Hand on
124
+ </Button>
125
+ <Button variant="outline" onClick={() => setDialog('retire')} disabled={closed}>
126
+ Retire
127
+ </Button>
128
+ <Button variant="outline" onClick={() => setDialog('claim')} disabled={profileRetired != null}>
129
+ Sign a lifecycle claim
130
+ </Button>
131
+ {retired ? <span className="self-center text-sm text-muted-foreground">Retired: no further state can be written.</span> : null}
132
+ </div>
133
+
134
+ <RecordNotice payload={view.payload} />
135
+ {bound == null ? (
136
+ <Section title="Public payload">
137
+ <DefinitionList entries={entriesOf(view.payload, new Set(['notice']), humanise)} />
138
+ </Section>
139
+ ) : (
140
+ <HolderReading passportId={view.passportId} payload={bound} needsClaimCode={needsClaimCode} />
141
+ )}
142
+
143
+ <Section title="Journal">
144
+ <div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
145
+ <Stat label="States" value={view.journal.states} />
146
+ <Stat label="Proven" value={view.journal.proven} />
147
+ <Stat label="Pending" value={view.journal.pending} />
148
+ <Stat label="Status" value={view.journal.status} />
149
+ </div>
150
+ </Section>
151
+
152
+ <Section title="States">
153
+ <Table>
154
+ <TableHeader>
155
+ <TableRow>
156
+ <TableHead>Op</TableHead>
157
+ <TableHead>Time</TableHead>
158
+ <TableHead>Actor</TableHead>
159
+ <TableHead>Index</TableHead>
160
+ <TableHead>Network</TableHead>
161
+ <TableHead>Proven</TableHead>
162
+ <TableHead>Proof pushed</TableHead>
163
+ <TableHead>Block</TableHead>
164
+ <TableHead>Txid</TableHead>
165
+ </TableRow>
166
+ </TableHeader>
167
+ <TableBody>
168
+ {view.states.map((state) => (
169
+ <TableRow key={state.txid}>
170
+ <TableCell>
171
+ <ToneBadge tone="neutral">{state.op}</ToneBadge>
172
+ </TableCell>
173
+ <TableCell>{formatTime(state.timestamp)}</TableCell>
174
+ <TableCell>{state.actor}</TableCell>
175
+ <TableCell>
176
+ <ToneBadge tone={indexTone(state.index)}>{state.index}</ToneBadge>
177
+ {state.refusal == null ? null : <div className="mt-1 max-w-xs whitespace-normal text-xs text-muted-foreground">{state.refusal}</div>}
178
+ </TableCell>
179
+ <TableCell>
180
+ <ToneBadge tone={networkTone(state.network)}>{state.network}</ToneBadge>
181
+ </TableCell>
182
+ <TableCell>
183
+ <YesNo value={state.proven} />
184
+ </TableCell>
185
+ <TableCell>
186
+ <YesNo value={state.proofPushed} />
187
+ </TableCell>
188
+ <TableCell>{state.blockHeight ?? <span className="text-muted-foreground">none</span>}</TableCell>
189
+ <TableCell>
190
+ <span className="flex items-center gap-1">
191
+ <Mono>{shortTxid(state.txid)}</Mono>
192
+ <CopyButton text={state.txid} label="Copy the txid" />
193
+ </span>
194
+ </TableCell>
195
+ </TableRow>
196
+ ))}
197
+ </TableBody>
198
+ </Table>
199
+ </Section>
200
+
201
+ <Section title="Offers">
202
+ {offers.loading ? (
203
+ <Loading what="Loading the offers" />
204
+ ) : offers.error != null || offers.data == null ? (
205
+ <ErrorNotice error={offers.error ?? new Error('no answer')} title="Could not load the offers" />
206
+ ) : offers.data.length === 0 ? (
207
+ <p className="text-sm text-muted-foreground">No offers. Hand on makes one.</p>
208
+ ) : (
209
+ <Table>
210
+ <TableHeader>
211
+ <TableRow>
212
+ <TableHead>Request</TableHead>
213
+ <TableHead>Terms</TableHead>
214
+ <TableHead>Status</TableHead>
215
+ <TableHead>Made</TableHead>
216
+ <TableHead>Expires</TableHead>
217
+ <TableHead />
218
+ </TableRow>
219
+ </TableHeader>
220
+ <TableBody>
221
+ {offers.data.map((offer) => (
222
+ <TableRow key={offer.requestId}>
223
+ <TableCell>
224
+ <Mono>{offer.requestId}</Mono>
225
+ </TableCell>
226
+ <TableCell className="whitespace-normal">
227
+ <span className="font-medium">{String(offer.terms.word ?? '')}</span>
228
+ {typeof offer.terms.note === 'string' && offer.terms.note !== '' ? <span className="text-muted-foreground"> {offer.terms.note}</span> : null}
229
+ </TableCell>
230
+ <TableCell>
231
+ <ToneBadge tone={statusTone(offer.status)}>{offer.status}</ToneBadge>
232
+ {offer.transferTxid == null ? null : <div className="mt-1 text-xs text-muted-foreground">{shortTxid(offer.transferTxid)}</div>}
233
+ </TableCell>
234
+ <TableCell>{formatTime(offer.createdAt)}</TableCell>
235
+ <TableCell>{formatTime(offer.expiresAt)}</TableCell>
236
+ <TableCell className="text-right">{offer.status === 'open' ? <WithdrawButton offer={offer} needsClaimCode={needsClaimCode} onDone={offers.reload} /> : null}</TableCell>
237
+ </TableRow>
238
+ ))}
239
+ </TableBody>
240
+ </Table>
241
+ )}
242
+ </Section>
243
+
244
+ <Section title="Lifecycle claims">
245
+ {claims.loading ? (
246
+ <Loading what="Loading the claims" />
247
+ ) : claims.error != null || claims.data == null ? (
248
+ <ErrorNotice error={claims.error ?? new Error('no answer')} title="Could not load the claims" />
249
+ ) : (
250
+ <LifecycleClaims reading={claims.data} onDeclareError={closed ? undefined : setDeclaring} emptyText="No lifecycle claim concerns this passport. A claim is one EPCIS event a named party signed, on its own rail; it never spends the passport." />
251
+ )}
252
+ </Section>
253
+
254
+ {/* Kept apart from everything above. The sections above are this passport's own record: states its holder signed and claims their issuers signed. What follows is data other parties supplied, and none of it is signed. */}
255
+ <Section title="Events that name this product">
256
+ <div className="space-y-3 rounded-lg border border-dashed p-4">
257
+ <SuppliedDataNotice />
258
+ <p className="text-sm text-muted-foreground">
259
+ This list is separate from the passport's own record above. It changes nothing in it: an event here moved no custody and wrote no state. Events are found under the exact identifier they write. Searched:{' '}
260
+ {passportSpellings(view.passportId).map((spelling, i) => (
261
+ <span key={spelling}>
262
+ {i === 0 ? null : ' and '}
263
+ <Mono>{spelling}</Mono>
264
+ </span>
265
+ ))}
266
+ . An event that writes this product another way, as an EPC URN for one, is found on the{' '}
267
+ <Link to="/events" className="underline underline-offset-4">
268
+ Events page
269
+ </Link>
270
+ .
271
+ </p>
272
+ <EventsNaming identifiers={passportSpellings(view.passportId)} />
273
+ </div>
274
+ </Section>
275
+
276
+ {dialog === 'update' && bound == null ? <UpdateDialog passport={view} needsClaimCode={needsClaimCode} onClose={close} onDone={passport.setData} /> : null}
277
+ {dialog === 'update' && bound != null ? <BoundUpdateDialog passport={view} publicProduct={bound.product} needsClaimCode={needsClaimCode} onClose={close} onDone={passport.setData} /> : null}
278
+ {dialog === 'offer' ? <HandOnDialog passportId={view.passportId} needsClaimCode={needsClaimCode} onClose={close} refreshOffers={offers.reload} /> : null}
279
+ {dialog === 'retire' ? <RetireDialog passportId={view.passportId} needsClaimCode={needsClaimCode} onClose={close} onDone={passport.setData} /> : null}
280
+ {dialog === 'claim' ? <ClaimDialog passportId={view.passportId} onClose={close} onDone={claims.reload} /> : null}
281
+ {declaring == null ? null : <DeclareErrorDialog passportId={view.passportId} claim={declaring} onClose={() => setDeclaring(undefined)} onDone={claims.reload} />}
282
+ </div>
283
+ )
284
+ }
285
+
286
+ function Stat({ label, value }: { label: string; value: ReactNode }) {
287
+ return (
288
+ <div className="rounded-lg border p-3">
289
+ <div className="text-xs text-muted-foreground">{label}</div>
290
+ <div className="text-xl font-semibold">{value}</div>
291
+ </div>
292
+ )
293
+ }
294
+
295
+ function ClaimCodeField({ value, onChange, disabled = false }: { value: string; onChange: (value: string) => void; disabled?: boolean }) {
296
+ return (
297
+ <Field label="Claim code" htmlFor="claimCode" required help="The holder acts with the claim code they accepted with.">
298
+ <Input id="claimCode" disabled={disabled} value={value} onChange={(e) => onChange(e.target.value)} required autoComplete="off" spellCheck={false} />
299
+ </Field>
300
+ )
301
+ }
302
+
303
+ function WithdrawButton({ offer, needsClaimCode, holderClaimCode, onDone }: { offer: OfferView; needsClaimCode: boolean; holderClaimCode?: string; onDone: (withdrawn: OfferView) => void }) {
304
+ const [claimCode, setClaimCode] = useState(holderClaimCode ?? '')
305
+ const [busy, setBusy] = useState(false)
306
+ const [error, setError] = useState<unknown>()
307
+ async function withdraw() {
308
+ setBusy(true)
309
+ setError(undefined)
310
+ try {
311
+ onDone(await api.withdrawOffer(offer.requestId, needsClaimCode ? claimCode : undefined))
312
+ } catch (cause) {
313
+ setError(cause)
314
+ } finally {
315
+ setBusy(false)
316
+ }
317
+ }
318
+ return (
319
+ <div className="flex flex-col items-end gap-1">
320
+ <div className="flex items-center gap-2">
321
+ {needsClaimCode && holderClaimCode == null ? <Input value={claimCode} onChange={(e) => setClaimCode(e.target.value)} placeholder="Claim code" className="h-8 w-40" aria-label="Claim code" /> : null}
322
+ <Button variant="outline" size="sm" onClick={withdraw} disabled={busy || (needsClaimCode && claimCode === '')}>
323
+ {busy ? 'Working' : 'Withdraw'}
324
+ </Button>
325
+ </div>
326
+ {error == null ? null : <span className="max-w-xs whitespace-normal text-xs text-destructive">{error instanceof Error ? error.message : String(error)}</span>}
327
+ </div>
328
+ )
329
+ }
330
+
331
+ function UpdateDialog({ passport, needsClaimCode, onClose, onDone }: { passport: PassportView; needsClaimCode: boolean; onClose: () => void; onDone: (view: PassportView) => void }) {
332
+ const profile = useLoad(() => api.profile(passport.profile), [passport.profile])
333
+ const [claimCode, setClaimCode] = useState('')
334
+ const [busy, setBusy] = useState(false)
335
+ const [error, setError] = useState<unknown>()
336
+
337
+ async function submit(values: PassportFormValues) {
338
+ setBusy(true)
339
+ setError(undefined)
340
+ try {
341
+ const view = await api.updatePassport(passport.passportId, { ...values, ...(needsClaimCode ? { claimCode } : {}) })
342
+ onDone(view)
343
+ onClose()
344
+ } catch (cause) {
345
+ setError(cause)
346
+ } finally {
347
+ setBusy(false)
348
+ }
349
+ }
350
+
351
+ return (
352
+ <Dialog open onOpenChange={(open) => (open ? undefined : onClose())}>
353
+ <DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-3xl">
354
+ <DialogHeader>
355
+ <DialogTitle>Update the passport</DialogTitle>
356
+ <DialogDescription>A new state carrying the payload you submit. The current public payload is filled in.</DialogDescription>
357
+ </DialogHeader>
358
+ <Alert>
359
+ <AlertTitle>Owner-tier fields start empty</AlertTitle>
360
+ <AlertDescription>
361
+ <p>The API never returns the encrypted owner tier, so those fields are blank here. What you submit becomes the new owner tier; leave them blank and it is written blank.</p>
362
+ </AlertDescription>
363
+ </Alert>
364
+ {profile.loading ? (
365
+ <Loading what="Loading the profile" />
366
+ ) : profile.error != null || profile.data == null || profile.data.format !== 'manifest' ? (
367
+ <ErrorNotice error={profile.error ?? new Error('no answer')} title={`Could not load ${passport.profile}`} />
368
+ ) : (
369
+ <PassportForm
370
+ profile={profile.data}
371
+ initial={{ payload: isRecord(passport.payload) ? passport.payload : {} }}
372
+ submitLabel="Write the update"
373
+ busy={busy}
374
+ error={error}
375
+ onSubmit={submit}
376
+ after={needsClaimCode ? <ClaimCodeField value={claimCode} onChange={setClaimCode} /> : null}
377
+ />
378
+ )}
379
+ </DialogContent>
380
+ </Dialog>
381
+ )
382
+ }
383
+
384
+ /**
385
+ * An update under a profile held by a binding: a new complete product
386
+ * document. The form starts from the document as it stands, which needs the
387
+ * restricted part opened. Where it cannot be opened the form starts from
388
+ * the public part and says what that means.
389
+ */
390
+ function BoundUpdateDialog({ passport, publicProduct, needsClaimCode, onClose, onDone }: { passport: PassportView; publicProduct: Record<string, unknown>; needsClaimCode: boolean; onClose: () => void; onDone: (view: PassportView) => void }) {
391
+ const { health } = useHealth()
392
+ const profile = useLoad(() => api.profile(passport.profile), [passport.profile])
393
+ const [claimCode, setClaimCode] = useState('')
394
+ const [opened, setOpened] = useState<{ reading?: AuthorisedReading; error?: unknown }>()
395
+ const [busy, setBusy] = useState(false)
396
+ const [error, setError] = useState<unknown>()
397
+ // A brand member's restricted part is opened straight away; a recipient gives the claim code first.
398
+ const auto = useLoad(() => (needsClaimCode ? Promise.resolve(undefined) : api.authorisedView(passport.passportId)), [passport.passportId, needsClaimCode])
399
+ const reading = opened?.reading ?? auto.data
400
+ const ready = needsClaimCode ? opened != null : !auto.loading
401
+
402
+ async function open(event: FormEvent) {
403
+ event.preventDefault()
404
+ try {
405
+ setOpened({ reading: await api.authorisedView(passport.passportId, claimCode) })
406
+ } catch (cause) {
407
+ setOpened({ error: cause })
408
+ }
409
+ }
410
+
411
+ async function submit(submission: BoundSubmission) {
412
+ setBusy(true)
413
+ setError(undefined)
414
+ try {
415
+ const view = await api.updatePassport(passport.passportId, { product: submission.product, ...(needsClaimCode ? { claimCode } : {}) })
416
+ onDone(view)
417
+ onClose()
418
+ } catch (cause) {
419
+ setError(cause)
420
+ } finally {
421
+ setBusy(false)
422
+ }
423
+ }
424
+
425
+ const whole = reading?.product
426
+ return (
427
+ <Dialog open onOpenChange={(open) => (open ? undefined : onClose())}>
428
+ <DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-4xl">
429
+ <DialogHeader>
430
+ <DialogTitle>Update the passport</DialogTitle>
431
+ <DialogDescription>A new state carrying the complete product document you submit. Its public part is written on chain and its restricted part is sealed again.</DialogDescription>
432
+ </DialogHeader>
433
+ {needsClaimCode && opened?.reading == null ? (
434
+ <form onSubmit={open} className="space-y-3">
435
+ <ClaimCodeField value={claimCode} onChange={setClaimCode} />
436
+ {opened?.error == null ? null : <ErrorNotice error={opened.error} title="The restricted part was not opened" />}
437
+ <Button type="submit" variant="outline">
438
+ Open the document
439
+ </Button>
440
+ </form>
441
+ ) : null}
442
+ {!ready || profile.loading ? (
443
+ needsClaimCode && opened == null ? null : <Loading what="Loading the document" />
444
+ ) : profile.error != null || profile.data == null || profile.data.format !== 'binding' ? (
445
+ <ErrorNotice error={profile.error ?? new Error(`${passport.profile} is not registered in this application, so its documents cannot be written here`)} title={`Could not load ${passport.profile}`} />
446
+ ) : (
447
+ <>
448
+ {whole == null ? (
449
+ <Alert>
450
+ <AlertTitle>The restricted part is not filled in</AlertTitle>
451
+ <AlertDescription>
452
+ <p>
453
+ {reading?.authorisedView.summary ?? (auto.error instanceof Error ? auto.error.message : 'The restricted part could not be opened.')} The form starts from the public part alone. What you submit becomes the whole document: a restricted property you do not enter again is no longer
454
+ part of it.
455
+ </p>
456
+ </AlertDescription>
457
+ </Alert>
458
+ ) : null}
459
+ <BoundPassportForm
460
+ key={whole == null ? 'public' : 'whole'}
461
+ profile={profile.data as BoundProfile}
462
+ policy={{ host: health?.identifiers.host ?? window.location.host, prefix: health?.identifiers.prefix ?? '952' }}
463
+ passportId={passport.passportId}
464
+ initialProduct={whole ?? publicProduct}
465
+ submitLabel="Write the update"
466
+ busy={busy}
467
+ error={error}
468
+ onSubmit={submit}
469
+ />
470
+ </>
471
+ )}
472
+ </DialogContent>
473
+ </Dialog>
474
+ )
475
+ }
476
+
477
+ function HandOnDialog({ passportId, needsClaimCode, onClose, refreshOffers }: { passportId: string; needsClaimCode: boolean; onClose: () => void; refreshOffers: () => void }) {
478
+ const [word, setWord] = useState('Handed on')
479
+ const [note, setNote] = useState('')
480
+ const [claimCode, setClaimCode] = useState('')
481
+ const [busy, setBusy] = useState(false)
482
+ const [error, setError] = useState<unknown>()
483
+ const [result, setResult] = useState<{ offer: OfferView; claimCode?: string }>()
484
+
485
+ const attempt = useRef<{ termsKey: string; requestId: string; pending: boolean } | undefined>(undefined)
486
+
487
+ async function submit(event: FormEvent) {
488
+ event.preventDefault()
489
+ if (attempt.current?.pending) return
490
+ const terms = { word: word.trim(), ...(note.trim() === '' ? {} : { note: note.trim() }) }
491
+ const termsKey = JSON.stringify([passportId, terms])
492
+ if (attempt.current?.termsKey !== termsKey) attempt.current = { termsKey, requestId: crypto.randomUUID(), pending: false }
493
+ const submission = attempt.current
494
+ submission.pending = true
495
+ setBusy(true)
496
+ setError(undefined)
497
+ try {
498
+ const answer = await api.offerPassport(passportId, { requestId: submission.requestId, terms, ...(needsClaimCode ? { claimCode } : {}) })
499
+ setResult(answer)
500
+ } catch (cause) {
501
+ setError(cause)
502
+ } finally {
503
+ submission.pending = false
504
+ setBusy(false)
505
+ // The server may have saved the offer even if its response was lost.
506
+ refreshOffers()
507
+ }
508
+ }
509
+
510
+ const open = result?.offer.status === 'open' && Date.parse(result.offer.expiresAt) > Date.now()
511
+ const acceptUrl = result == null ? '' : `${window.location.origin}/accept/${encodeURIComponent(result.offer.requestId)}`
512
+
513
+ return (
514
+ <Dialog open onOpenChange={(open) => (open ? undefined : onClose())}>
515
+ <DialogContent className="max-h-[90vh] overflow-y-auto">
516
+ <DialogHeader>
517
+ <DialogTitle>Hand on this passport</DialogTitle>
518
+ <DialogDescription>The platform offers it under these terms. The recipient accepts with a claim code, and the transfer is written then.</DialogDescription>
519
+ </DialogHeader>
520
+ {result == null ? (
521
+ <form onSubmit={submit} className="space-y-4">
522
+ <Field label="Terms word" htmlFor="offer-word" required help="One word for what is happening, for example Sold or Gifted.">
523
+ <Input id="offer-word" disabled={busy} value={word} onChange={(e) => setWord(e.target.value)} required maxLength={40} />
524
+ </Field>
525
+ <Field label="Note" htmlFor="offer-note" help="Shown to the recipient before they decide.">
526
+ <Textarea id="offer-note" disabled={busy} value={note} onChange={(e) => setNote(e.target.value)} maxLength={400} />
527
+ </Field>
528
+ {needsClaimCode ? <ClaimCodeField value={claimCode} onChange={setClaimCode} disabled={busy} /> : null}
529
+ {error == null ? null : <ErrorNotice error={error} title="Could not confirm the offer" />}
530
+ <DialogFooter>
531
+ <Button type="submit" disabled={busy || word.trim() === ''}>
532
+ {busy ? 'Working' : 'Make the offer'}
533
+ </Button>
534
+ </DialogFooter>
535
+ </form>
536
+ ) : (
537
+ <div className="space-y-4">
538
+ <p className="text-sm">
539
+ Offer {result.offer.requestId} is {result.offer.status === 'open' && !open ? 'expired' : result.offer.status}. {open ? `Expires ${formatTime(result.offer.expiresAt)}.` : null}
540
+ </p>
541
+ {result.offer.status === 'open' && result.claimCode == null ? (
542
+ <div className="space-y-2">
543
+ <p>The claim code was returned when this offer was created. If you need a replacement code, withdraw this offer and make a new one.</p>
544
+ <WithdrawButton
545
+ offer={result.offer}
546
+ needsClaimCode={needsClaimCode}
547
+ holderClaimCode={claimCode}
548
+ onDone={(offer) => {
549
+ setResult({ offer })
550
+ refreshOffers()
551
+ }}
552
+ />
553
+ </div>
554
+ ) : null}
555
+ {open ? (
556
+ <>
557
+ {result.claimCode != null ? (
558
+ <div className="rounded-lg border-2 border-amber-400 bg-amber-50 p-4 dark:border-amber-700 dark:bg-amber-950">
559
+ <p className="text-xs font-medium uppercase tracking-wide text-muted-foreground">Claim code</p>
560
+ <div className="mt-1 flex items-center gap-2">
561
+ <code className="text-2xl font-semibold tracking-wider">{result.claimCode}</code>
562
+ <CopyButton text={result.claimCode} label="Copy the claim code" />
563
+ </div>
564
+ <p className="mt-3 text-sm">Give this code to the recipient out of band. It is shown once and never stored.</p>
565
+ </div>
566
+ ) : null}
567
+ <Field label="Accept link" help="Where the recipient enters the code.">
568
+ <div className="flex items-center gap-2">
569
+ <Mono>{acceptUrl}</Mono>
570
+ <CopyButton text={acceptUrl} label="Copy the accept link" />
571
+ </div>
572
+ </Field>
573
+ </>
574
+ ) : null}
575
+ <DialogFooter>
576
+ {result.offer.status === 'withdrawn' ? (
577
+ <Button variant="outline" onClick={() => { attempt.current = undefined; setResult(undefined) }}>Make a new offer</Button>
578
+ ) : null}
579
+ <Button onClick={onClose}>Done</Button>
580
+ </DialogFooter>
581
+ </div>
582
+ )}
583
+ </DialogContent>
584
+ </Dialog>
585
+ )
586
+ }
587
+
588
+ function RetireDialog({ passportId, needsClaimCode, onClose, onDone }: { passportId: string; needsClaimCode: boolean; onClose: () => void; onDone: (view: PassportView) => void }) {
589
+ const [reason, setReason] = useState('')
590
+ const [claimCode, setClaimCode] = useState('')
591
+ const [busy, setBusy] = useState(false)
592
+ const [error, setError] = useState<unknown>()
593
+
594
+ async function submit(event: FormEvent) {
595
+ event.preventDefault()
596
+ setBusy(true)
597
+ setError(undefined)
598
+ try {
599
+ const view = await api.retirePassport(passportId, { reason: reason.trim(), ...(needsClaimCode ? { claimCode } : {}) })
600
+ onDone(view)
601
+ onClose()
602
+ } catch (cause) {
603
+ setError(cause)
604
+ } finally {
605
+ setBusy(false)
606
+ }
607
+ }
608
+
609
+ return (
610
+ <Dialog open onOpenChange={(open) => (open ? undefined : onClose())}>
611
+ <DialogContent>
612
+ <DialogHeader>
613
+ <DialogTitle>Retire the passport</DialogTitle>
614
+ <DialogDescription>A final state. Nothing can be written after it.</DialogDescription>
615
+ </DialogHeader>
616
+ <form onSubmit={submit} className="space-y-4">
617
+ <Field label="Reason" htmlFor="retire-reason" required>
618
+ <Input id="retire-reason" value={reason} onChange={(e) => setReason(e.target.value)} required maxLength={200} placeholder="Recycled, destroyed, withdrawn" />
619
+ </Field>
620
+ {needsClaimCode ? <ClaimCodeField value={claimCode} onChange={setClaimCode} /> : null}
621
+ {error == null ? null : <ErrorNotice error={error} title="The retirement was refused" />}
622
+ <DialogFooter>
623
+ <Button type="submit" variant="destructive" disabled={busy || reason.trim() === ''}>
624
+ {busy ? 'Working' : 'Retire'}
625
+ </Button>
626
+ </DialogFooter>
627
+ </form>
628
+ </DialogContent>
629
+ </Dialog>
630
+ )
631
+ }
632
+