@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,87 @@
|
|
|
1
|
+
// Small presentation helpers shared by the pages.
|
|
2
|
+
|
|
3
|
+
export const shortTxid = (txid: string): string => (txid.length <= 20 ? txid : `${txid.slice(0, 10)}...${txid.slice(-6)}`)
|
|
4
|
+
|
|
5
|
+
export const shortKey = (key: string): string => (key.length <= 16 ? key : `${key.slice(0, 8)}...${key.slice(-6)}`)
|
|
6
|
+
|
|
7
|
+
export function formatTime(iso: string | undefined): string {
|
|
8
|
+
if (iso == null || iso === '') return ''
|
|
9
|
+
const date = new Date(iso)
|
|
10
|
+
return Number.isNaN(date.getTime()) ? iso : date.toLocaleString()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** `endOfLife` becomes `End of life`; `profile_version` becomes `Profile version`. */
|
|
14
|
+
export const humanise = (key: string): string =>
|
|
15
|
+
key
|
|
16
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
17
|
+
.replace(/[_-]+/g, ' ')
|
|
18
|
+
.toLowerCase()
|
|
19
|
+
.replace(/^./, (c) => c.toUpperCase())
|
|
20
|
+
|
|
21
|
+
/** The identifier's own path, `/01/<gtin>/21/<serial>`: a short form of the identifier to show. The API answers this path as the identifier's resolver. */
|
|
22
|
+
export function identifierPathOf(passportId: string): string | undefined {
|
|
23
|
+
try {
|
|
24
|
+
return new URL(passportId).pathname
|
|
25
|
+
} catch {
|
|
26
|
+
return undefined
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Where the public pages live: each at its identifier's own path after this prefix, where the resolver redirects a browser that asked for the identifier (apps/api/src/resolver.ts). */
|
|
31
|
+
export const PAGE_PREFIX = '/passport'
|
|
32
|
+
|
|
33
|
+
/** The passport's public page in this app, `/passport/01/<gtin>/21/<serial>`. A link inside the app goes straight to it; what is printed or copied stays the identifier. */
|
|
34
|
+
export function publicPathOf(passportId: string): string | undefined {
|
|
35
|
+
const path = identifierPathOf(passportId)
|
|
36
|
+
return path == null ? undefined : `${PAGE_PREFIX}${path}`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The GTIN and serial an identifier names, when it is a GS1 Digital Link of the form this app serves. */
|
|
40
|
+
export function parseIdentifier(passportId: string): { gtin: string; serial: string } | undefined {
|
|
41
|
+
const match = identifierPathOf(passportId)?.match(/^\/01\/(\d+)\/21\/([^/]+)$/)
|
|
42
|
+
if (match == null) return undefined
|
|
43
|
+
let serial = match[2]
|
|
44
|
+
try {
|
|
45
|
+
serial = decodeURIComponent(serial)
|
|
46
|
+
} catch {
|
|
47
|
+
// Left as written.
|
|
48
|
+
}
|
|
49
|
+
return { gtin: match[1], serial }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The route parameter back to an identifier: React Router decodes it once; a still-encoded value is decoded here. */
|
|
53
|
+
export function passportIdFromRef(ref: string): string {
|
|
54
|
+
if (ref.includes('://')) return ref
|
|
55
|
+
try {
|
|
56
|
+
return decodeURIComponent(ref)
|
|
57
|
+
} catch {
|
|
58
|
+
return ref
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Decode the original accept-link segment once so an ID can contain either a slash or the literal characters %2F. */
|
|
63
|
+
export function offerIdFromPath(pathname: string): string | undefined {
|
|
64
|
+
const segment = /\/accept\/([^/]+)\/*$/i.exec(pathname)?.[1]
|
|
65
|
+
if (segment == null) return undefined
|
|
66
|
+
try {
|
|
67
|
+
return decodeURIComponent(segment)
|
|
68
|
+
} catch {
|
|
69
|
+
return undefined
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
74
|
+
|
|
75
|
+
/** Payload keys that are stamps or the demonstration marker, not product data. */
|
|
76
|
+
export const STAMP_KEYS = new Set(['notice', 'profile', 'profile_version'])
|
|
77
|
+
|
|
78
|
+
export const holderLabel = (party: string): string => {
|
|
79
|
+
const separator = party.indexOf(':')
|
|
80
|
+
if (separator < 0) return party
|
|
81
|
+
const scope = party.slice(0, separator)
|
|
82
|
+
const id = party.slice(separator + 1)
|
|
83
|
+
if (scope === 'brand') return `Brand ${id}`
|
|
84
|
+
if (scope === 'recipient') return `Recipient of offer ${id}`
|
|
85
|
+
if (scope === 'member') return `Member ${id}`
|
|
86
|
+
return party
|
|
87
|
+
}
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
// Lifecycle claims on a page: the shapes the API sends, the words each
|
|
2
|
+
// finding is shown with, and the event a member enters, built from a form.
|
|
3
|
+
//
|
|
4
|
+
// An EPCIS lifecycle claim (`dpp-epcis-lifecycle@1`) is reported one finding
|
|
5
|
+
// at a time and never as one verdict. What was not checked says unknown.
|
|
6
|
+
// An anchor is shown as what it is: an anchoring service's commitment to the
|
|
7
|
+
// claim's bytes. It is never worded as proof that the event happened, that
|
|
8
|
+
// anyone owns the product, or that anything complies with a rule.
|
|
9
|
+
//
|
|
10
|
+
// A historical native claim (`dpp-lifecycle-v1`) is shown as historical. Its
|
|
11
|
+
// event type is one of four categories this application issued before it
|
|
12
|
+
// moved to EPCIS lifecycle claims, and is never shown as an EPCIS event.
|
|
13
|
+
//
|
|
14
|
+
// This module touches neither the page nor the network, so the API's tests
|
|
15
|
+
// hold its words to the answers the API really sends.
|
|
16
|
+
|
|
17
|
+
export type LifecycleTone = 'pass' | 'fail' | 'warn' | 'unknown' | 'muted' | 'neutral'
|
|
18
|
+
|
|
19
|
+
export interface LifecycleFinding {
|
|
20
|
+
status: string
|
|
21
|
+
reasonCode?: string
|
|
22
|
+
detail?: unknown
|
|
23
|
+
from?: string[]
|
|
24
|
+
to?: string[]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface LifecycleFindings {
|
|
28
|
+
eventStructure: LifecycleFinding
|
|
29
|
+
eventSemantics: LifecycleFinding
|
|
30
|
+
productBinding: LifecycleFinding
|
|
31
|
+
signature: LifecycleFinding
|
|
32
|
+
authority: LifecycleFinding
|
|
33
|
+
possession: LifecycleFinding & { from: string[]; to: string[] }
|
|
34
|
+
ownership: LifecycleFinding & { from: string[]; to: string[] }
|
|
35
|
+
anchorCommitment: LifecycleFinding
|
|
36
|
+
inclusion: LifecycleFinding
|
|
37
|
+
correction: LifecycleFinding
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface LifecycleBinding {
|
|
41
|
+
passportId: string
|
|
42
|
+
field: string
|
|
43
|
+
identifier: string
|
|
44
|
+
level: 'instance' | 'class'
|
|
45
|
+
status: string
|
|
46
|
+
reasonCode?: string
|
|
47
|
+
detail?: unknown
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface RegistryAnswer {
|
|
51
|
+
url: string
|
|
52
|
+
status: number
|
|
53
|
+
accepted: boolean
|
|
54
|
+
attestationId?: string
|
|
55
|
+
detail?: string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface LifecycleClaimView {
|
|
59
|
+
claimFormat: 'dpp-epcis-lifecycle@1'
|
|
60
|
+
attestationId: string
|
|
61
|
+
foundBy: Array<'anchor' | 'this-application'>
|
|
62
|
+
held: boolean
|
|
63
|
+
unavailable?: string
|
|
64
|
+
participation: 'item' | 'class' | 'not-established'
|
|
65
|
+
digest?: string
|
|
66
|
+
eventId?: string
|
|
67
|
+
eventType?: string
|
|
68
|
+
event?: Record<string, unknown>
|
|
69
|
+
issuer?: string
|
|
70
|
+
assertedRole?: string
|
|
71
|
+
authorityScope?: string[]
|
|
72
|
+
issuedAt?: string
|
|
73
|
+
source?: Record<string, unknown>
|
|
74
|
+
bindingsHere: LifecycleBinding[]
|
|
75
|
+
boundPassports: string[]
|
|
76
|
+
itemParticipants: string[]
|
|
77
|
+
declaresError: boolean
|
|
78
|
+
findings?: LifecycleFindings
|
|
79
|
+
relations: Array<{ digest: string; relation: string }>
|
|
80
|
+
anchors: number
|
|
81
|
+
record?: { id: string; issuedBy: string; createdAt: string; importedFrom?: { importId: string; pointer: string }; registry?: RegistryAnswer }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface HistoricalClaimView {
|
|
85
|
+
claimFormat: 'dpp-lifecycle-v1'
|
|
86
|
+
historical: true
|
|
87
|
+
id: string
|
|
88
|
+
passportId: string
|
|
89
|
+
issuer: string
|
|
90
|
+
issuedBy: string
|
|
91
|
+
createdAt: string
|
|
92
|
+
claim: { eventType: string; timestamp: string; recordId?: string; [key: string]: unknown }
|
|
93
|
+
signature: string
|
|
94
|
+
registry?: RegistryAnswer
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface PassportClaimsReading {
|
|
98
|
+
passportId: string
|
|
99
|
+
historical: HistoricalClaimView[]
|
|
100
|
+
lifecycle: LifecycleClaimView[]
|
|
101
|
+
discovery: { index: 'asked' | 'partial' | 'unreachable'; indexDetail?: string; relatedClaims?: 'complete' | 'incomplete'; registry: 'configured' | 'not-configured' }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/* ------------------------------------------------------------------ words */
|
|
105
|
+
|
|
106
|
+
/** What every list of lifecycle claims says above it. */
|
|
107
|
+
export const LIFECYCLE_NOTICE =
|
|
108
|
+
'Each claim is one EPCIS event a named party signed, bound to the passports whose products it names. Every finding below is reported on its own. A valid signature says which key signed, not that its holder saw the event. An anchor says an anchoring service committed to these exact bytes. Neither proves the event happened, who owns the product, or that anything complies with a rule.'
|
|
109
|
+
|
|
110
|
+
/** How a historical native claim is labelled wherever it is shown. */
|
|
111
|
+
export const HISTORICAL_LABEL = 'Historical native claim'
|
|
112
|
+
export const HISTORICAL_NOTICE =
|
|
113
|
+
'A dpp-lifecycle-v1 claim, issued before this application moved to EPCIS lifecycle claims. It is kept exactly as it was signed and verified under its own rules. Its event type is one of four historical categories (Origin, Transfer, Transformation, Disposition) with their original meaning; it is not an EPCIS event and is not read as one.'
|
|
114
|
+
|
|
115
|
+
const ROLE_WORDS: Record<string, string> = {
|
|
116
|
+
observer: 'as the observer of the event',
|
|
117
|
+
owner: 'as the owner',
|
|
118
|
+
custodian: 'as the custodian',
|
|
119
|
+
'authorised-reporter': 'as a reporter of an event someone else observed',
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function roleWords(role: string | undefined): string {
|
|
123
|
+
return role == null ? 'in no stated role' : (ROLE_WORDS[role] ?? `as ${role}`)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** What verifying the claim established for the passport on the page. An anchor's subject never decides it. */
|
|
127
|
+
export function participationLabel(view: Pick<LifecycleClaimView, 'participation' | 'held' | 'foundBy'>): { tone: LifecycleTone; label: string; sentence: string } {
|
|
128
|
+
if (view.participation === 'item') return { tone: 'neutral', label: 'This item', sentence: "The claim binds this passport's own item, and that binding verified under the identity rules." }
|
|
129
|
+
if (view.participation === 'class') return { tone: 'muted', label: 'Its class only', sentence: "The claim names a class of products this passport's product belongs to. That does not say this item took part." }
|
|
130
|
+
if (!view.held) return { tone: 'unknown', label: 'Not established', sentence: 'An anchor names this passport, and the claim it commits to could not be read here, so nothing about this passport is established.' }
|
|
131
|
+
return {
|
|
132
|
+
tone: 'warn',
|
|
133
|
+
label: 'Not established',
|
|
134
|
+
sentence: view.foundBy.includes('anchor') ? 'An anchor names this passport as its subject, but verifying the claim does not bind it to this passport. An anchor\'s subject alone never makes a passport part of an event.' : 'Verifying the claim does not bind it to this passport.',
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const reasonOf = (finding: LifecycleFinding): string => {
|
|
139
|
+
if (finding.reasonCode == null) return ''
|
|
140
|
+
return ` (${finding.reasonCode})`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const parties = (list: string[]): string => (list.length === 0 ? 'none' : list.join(', '))
|
|
144
|
+
|
|
145
|
+
/** One row per finding, in a fixed order, each with its own state and sentence. Nothing is folded into a verdict. */
|
|
146
|
+
export function findingRows(findings: LifecycleFindings): Array<{ key: keyof LifecycleFindings; label: string; status: string; tone: LifecycleTone; sentence: string }> {
|
|
147
|
+
const tone = (status: string): LifecycleTone =>
|
|
148
|
+
status === 'pass' || status === 'supported' ? 'pass' : status === 'fail' || status === 'refused' ? 'fail' : status === 'declared-in-error' ? 'warn' : status === 'asserted' || status === 'none' ? 'neutral' : status === 'not-asserted' ? 'muted' : 'unknown'
|
|
149
|
+
const unknown = (what: string, finding: LifecycleFinding): string => `Unknown${reasonOf(finding)}: ${what}`
|
|
150
|
+
const f = findings
|
|
151
|
+
return [
|
|
152
|
+
{
|
|
153
|
+
key: 'eventStructure',
|
|
154
|
+
label: 'Event structure',
|
|
155
|
+
status: f.eventStructure.status,
|
|
156
|
+
tone: tone(f.eventStructure.status),
|
|
157
|
+
sentence: f.eventStructure.status === 'pass' ? 'The event has the members its EPCIS type requires and the pinned EPCIS 2.0.1 schema accepts it.' : f.eventStructure.status === 'fail' ? `The event is refused${reasonOf(f.eventStructure)}.` : unknown('the pinned schema was not applied here.', f.eventStructure),
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
key: 'eventSemantics',
|
|
161
|
+
label: 'Event semantics',
|
|
162
|
+
status: f.eventSemantics.status,
|
|
163
|
+
tone: tone(f.eventSemantics.status),
|
|
164
|
+
sentence: f.eventSemantics.status === 'pass' ? 'The event names an observed object, and every extension rule it requires held.' : f.eventSemantics.status === 'fail' ? `The event does not hold together${reasonOf(f.eventSemantics)}.` : f.eventSemantics.status === 'unavailable' ? `Unavailable${reasonOf(f.eventSemantics)}: the event requires an extension rule this application does not hold.` : unknown('the event semantics were not checked.', f.eventSemantics),
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
key: 'productBinding',
|
|
168
|
+
label: 'Product binding',
|
|
169
|
+
status: f.productBinding.status,
|
|
170
|
+
tone: tone(f.productBinding.status),
|
|
171
|
+
sentence: f.productBinding.status === 'pass' ? 'Every identifier the claim binds is where it says in the event, at the level it says, and denotes its passport\'s product under the identity rules.' : f.productBinding.status === 'fail' ? `A binding does not hold${reasonOf(f.productBinding)}.` : unknown('no product comparison was made.', f.productBinding),
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
key: 'signature',
|
|
175
|
+
label: 'Signature',
|
|
176
|
+
status: f.signature.status,
|
|
177
|
+
tone: tone(f.signature.status),
|
|
178
|
+
sentence: f.signature.status === 'pass' ? 'The named key signed these exact bytes. That says who signed, not that the signer saw the event.' : f.signature.status === 'fail' ? `The signature does not verify${reasonOf(f.signature)}.` : unknown('the signature could not be read.', f.signature),
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
key: 'authority',
|
|
182
|
+
label: 'Authority',
|
|
183
|
+
status: f.authority.status,
|
|
184
|
+
tone: tone(f.authority.status),
|
|
185
|
+
sentence: f.authority.status === 'supported' ? 'Evidence supports the role the issuer asserts, for every passport in its scope.' : f.authority.status === 'refused' ? `The role the issuer asserts is refused${reasonOf(f.authority)}.` : `Unavailable${reasonOf(f.authority)}: nothing here shows that the issuer holds the role it asserts. A valid signature and an account on this application are not that evidence.`,
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
key: 'possession',
|
|
189
|
+
label: 'Possession',
|
|
190
|
+
status: f.possession.status,
|
|
191
|
+
tone: tone(f.possession.status),
|
|
192
|
+
sentence: f.possession.status === 'asserted' ? `The signed event states possessing parties, from ${parties(f.possession.from)} to ${parties(f.possession.to)}. That is what the issuer signed, not a fact this application established, and it says nothing about ownership.` : f.possession.status === 'not-asserted' ? 'The event names no possessing party. Nothing about possession is inferred from its business step, its disposition or any passport operation.' : unknown('the event was not read.', f.possession),
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
key: 'ownership',
|
|
196
|
+
label: 'Ownership',
|
|
197
|
+
status: f.ownership.status,
|
|
198
|
+
tone: tone(f.ownership.status),
|
|
199
|
+
sentence: f.ownership.status === 'asserted' ? `The signed event states owning parties, from ${parties(f.ownership.from)} to ${parties(f.ownership.to)}. That is what the issuer signed, not legal ownership this application established.` : f.ownership.status === 'not-asserted' ? 'The event names no owning party. Nothing about ownership is inferred from possession, a sale, a business step or any passport operation.' : unknown('the event was not read.', f.ownership),
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
key: 'anchorCommitment',
|
|
203
|
+
label: 'Anchor',
|
|
204
|
+
status: f.anchorCommitment.status,
|
|
205
|
+
tone: tone(f.anchorCommitment.status),
|
|
206
|
+
sentence: f.anchorCommitment.status === 'pass' ? 'An anchoring service committed to these exact bytes, with matching metadata. That proves the commitment only: not that the event happened, not who owns the product, and not compliance with anything.' : f.anchorCommitment.status === 'fail' ? `An anchor found for the claim does not match it${reasonOf(f.anchorCommitment)}.` : unknown('no anchor of the claim was found.', f.anchorCommitment),
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
key: 'inclusion',
|
|
210
|
+
label: 'Inclusion',
|
|
211
|
+
status: f.inclusion.status,
|
|
212
|
+
tone: tone(f.inclusion.status),
|
|
213
|
+
sentence: f.inclusion.status === 'pass' ? 'The anchor transaction is in a block the header source holds. That dates the commitment, and proves nothing more about the event.' : f.inclusion.status === 'fail' ? `The inclusion proof does not hold${reasonOf(f.inclusion)}.` : unknown('no merkle path and header source were available.', f.inclusion),
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
key: 'correction',
|
|
217
|
+
label: 'Correction',
|
|
218
|
+
status: f.correction.status,
|
|
219
|
+
tone: tone(f.correction.status),
|
|
220
|
+
sentence: f.correction.status === 'declared-in-error' ? 'The event is declared in error by a verified claim of its issuer. The original claim is kept exactly as it was signed.' : f.correction.status === 'none' ? 'No claim this application holds declares the event in error. Another party may hold one.' : unknown('whether the event was declared in error is not known.', f.correction),
|
|
221
|
+
},
|
|
222
|
+
]
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** What the claim's correction state says, in one line for the list. */
|
|
226
|
+
export function correctionLabel(view: Pick<LifecycleClaimView, 'declaresError' | 'findings'>): { tone: LifecycleTone; label: string } | undefined {
|
|
227
|
+
if (view.declaresError) return { tone: 'warn', label: 'Declares an earlier event in error' }
|
|
228
|
+
if (view.findings?.correction.status === 'declared-in-error') return { tone: 'warn', label: 'Declared in error' }
|
|
229
|
+
return undefined
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/* ------------------------------------------------------------------ the form */
|
|
233
|
+
|
|
234
|
+
export const LIFECYCLE_EVENT_TYPES = ['ObjectEvent', 'AggregationEvent', 'TransactionEvent', 'TransformationEvent', 'AssociationEvent'] as const
|
|
235
|
+
export type LifecycleEventType = (typeof LIFECYCLE_EVENT_TYPES)[number]
|
|
236
|
+
export const LIFECYCLE_ACTIONS = ['ADD', 'OBSERVE', 'DELETE'] as const
|
|
237
|
+
export const LIFECYCLE_ROLES = ['observer', 'owner', 'custodian', 'authorised-reporter'] as const
|
|
238
|
+
|
|
239
|
+
/** A few CBV 2.0 business steps and dispositions, offered as suggestions. Any CBV term or an extension URI may be typed. */
|
|
240
|
+
export const SUGGESTED_BUSINESS_STEPS = ['commissioning', 'shipping', 'receiving', 'packing', 'unpacking', 'assembling', 'installing', 'removing', 'repairing', 'inspecting', 'sensor_reporting', 'retail_selling', 'collecting', 'disassembling', 'decommissioning', 'accepting', 'departing', 'arriving']
|
|
241
|
+
export const SUGGESTED_DISPOSITIONS = ['active', 'in_progress', 'in_transit', 'conformant', 'non_conformant', 'needs_replacement', 'retail_sold', 'inactive', 'destroyed']
|
|
242
|
+
|
|
243
|
+
/** Which identifier and quantity lists each event type has (EPCIS 2.0.1 section 7). A list the type does not have is not offered and never sent. */
|
|
244
|
+
export const EVENT_FIELDS: Record<LifecycleEventType, { action: boolean; lists: Array<'epcList' | 'childEPCs' | 'inputEPCList' | 'outputEPCList'>; parent: boolean; quantities: Array<'quantityList' | 'childQuantityList' | 'inputQuantityList' | 'outputQuantityList'>; transactions: boolean; transformationID: boolean }> = {
|
|
245
|
+
ObjectEvent: { action: true, lists: ['epcList'], parent: false, quantities: ['quantityList'], transactions: true, transformationID: false },
|
|
246
|
+
AggregationEvent: { action: true, lists: ['childEPCs'], parent: true, quantities: ['childQuantityList'], transactions: true, transformationID: false },
|
|
247
|
+
TransactionEvent: { action: true, lists: ['epcList'], parent: true, quantities: ['quantityList'], transactions: true, transformationID: false },
|
|
248
|
+
TransformationEvent: { action: false, lists: ['inputEPCList', 'outputEPCList'], parent: false, quantities: ['inputQuantityList', 'outputQuantityList'], transactions: true, transformationID: true },
|
|
249
|
+
AssociationEvent: { action: true, lists: ['childEPCs'], parent: true, quantities: ['childQuantityList'], transactions: true, transformationID: false },
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export interface LifecycleEventForm {
|
|
253
|
+
type: LifecycleEventType
|
|
254
|
+
eventID: string
|
|
255
|
+
eventTime: string
|
|
256
|
+
eventTimeZoneOffset: string
|
|
257
|
+
action: string
|
|
258
|
+
bizStep: string
|
|
259
|
+
disposition: string
|
|
260
|
+
/** One identifier per line, for each list the type has. */
|
|
261
|
+
epcList: string
|
|
262
|
+
childEPCs: string
|
|
263
|
+
inputEPCList: string
|
|
264
|
+
outputEPCList: string
|
|
265
|
+
parentID: string
|
|
266
|
+
/** One quantity element per line: the class, then optionally a quantity and a unit code. */
|
|
267
|
+
quantityList: string
|
|
268
|
+
childQuantityList: string
|
|
269
|
+
inputQuantityList: string
|
|
270
|
+
outputQuantityList: string
|
|
271
|
+
/** One business transaction per line: its type, then its identifier, or the identifier alone. */
|
|
272
|
+
bizTransactionList: string
|
|
273
|
+
transformationID: string
|
|
274
|
+
readPoint: string
|
|
275
|
+
bizLocation: string
|
|
276
|
+
/** One source or destination per line: its type (owning_party, possessing_party or location), then the party or place. */
|
|
277
|
+
sourceList: string
|
|
278
|
+
destinationList: string
|
|
279
|
+
/** The sensor elements as EPCIS JSON, exactly as they are to be signed. */
|
|
280
|
+
sensorElementList: string
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function emptyEventForm(identifier = '', now = new Date(), eventID = `urn:uuid:${globalThis.crypto.randomUUID()}`): LifecycleEventForm {
|
|
284
|
+
return {
|
|
285
|
+
type: 'ObjectEvent', eventID, eventTime: now.toISOString(), eventTimeZoneOffset: '+00:00', action: 'OBSERVE', bizStep: '', disposition: '',
|
|
286
|
+
epcList: identifier, childEPCs: '', inputEPCList: '', outputEPCList: '', parentID: '',
|
|
287
|
+
quantityList: '', childQuantityList: '', inputQuantityList: '', outputQuantityList: '',
|
|
288
|
+
bizTransactionList: '', transformationID: '', readPoint: '', bizLocation: '', sourceList: '', destinationList: '', sensorElementList: '',
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const lines = (text: string): string[] => text.split('\n').map((line) => line.trim()).filter((line) => line !== '')
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* The EPCIS event the form describes, or what is wrong with it. Only the
|
|
296
|
+
* members the event type has are sent: a TransformationEvent never gets an
|
|
297
|
+
* action. Empty fields are left out, never sent empty. Nothing is added that
|
|
298
|
+
* the member did not enter, apart from the identifiers the form starts with.
|
|
299
|
+
*/
|
|
300
|
+
export function eventFromForm(form: LifecycleEventForm): { event: Record<string, unknown> } | { problems: string[] } {
|
|
301
|
+
const problems: string[] = []
|
|
302
|
+
const shape = EVENT_FIELDS[form.type]
|
|
303
|
+
const event: Record<string, unknown> = { type: form.type, eventTime: form.eventTime.trim(), eventTimeZoneOffset: form.eventTimeZoneOffset.trim(), eventID: form.eventID.trim() }
|
|
304
|
+
if (shape.action) event.action = form.action
|
|
305
|
+
if (shape.parent && form.parentID.trim() !== '') event.parentID = form.parentID.trim()
|
|
306
|
+
for (const list of shape.lists) if (lines(form[list]).length > 0) event[list] = lines(form[list])
|
|
307
|
+
for (const list of shape.quantities) {
|
|
308
|
+
const elements = lines(form[list]).map((line, i) => {
|
|
309
|
+
const [epcClass, quantity, uom] = line.split(/\s+/)
|
|
310
|
+
const element: Record<string, unknown> = { epcClass }
|
|
311
|
+
if (quantity != null) {
|
|
312
|
+
const value = Number(quantity)
|
|
313
|
+
if (!Number.isFinite(value)) problems.push(`${list} line ${i + 1}: ${quantity} is not a quantity`)
|
|
314
|
+
element.quantity = value
|
|
315
|
+
}
|
|
316
|
+
if (uom != null) element.uom = uom
|
|
317
|
+
return element
|
|
318
|
+
})
|
|
319
|
+
if (elements.length > 0) event[list] = elements
|
|
320
|
+
}
|
|
321
|
+
if (form.bizStep.trim() !== '') event.bizStep = form.bizStep.trim()
|
|
322
|
+
if (form.disposition.trim() !== '') event.disposition = form.disposition.trim()
|
|
323
|
+
if (form.readPoint.trim() !== '') event.readPoint = { id: form.readPoint.trim() }
|
|
324
|
+
if (form.bizLocation.trim() !== '') event.bizLocation = { id: form.bizLocation.trim() }
|
|
325
|
+
if (shape.transactions && lines(form.bizTransactionList).length > 0) {
|
|
326
|
+
event.bizTransactionList = lines(form.bizTransactionList).map((line) => {
|
|
327
|
+
const [first, second] = line.split(/\s+/)
|
|
328
|
+
return second == null ? { bizTransaction: first } : { type: first, bizTransaction: second }
|
|
329
|
+
})
|
|
330
|
+
}
|
|
331
|
+
if (shape.transformationID && form.transformationID.trim() !== '') event.transformationID = form.transformationID.trim()
|
|
332
|
+
for (const [list, member] of [['sourceList', 'source'], ['destinationList', 'destination']] as const) {
|
|
333
|
+
const entries = lines(form[list]).map((line, i) => {
|
|
334
|
+
const [type, value] = line.split(/\s+/)
|
|
335
|
+
if (value == null) problems.push(`${list} line ${i + 1}: give a type (owning_party, possessing_party or location) and then the party or place`)
|
|
336
|
+
return { type, [member]: value }
|
|
337
|
+
})
|
|
338
|
+
if (entries.length > 0) event[list] = entries
|
|
339
|
+
}
|
|
340
|
+
if (form.sensorElementList.trim() !== '') {
|
|
341
|
+
try {
|
|
342
|
+
const parsed: unknown = JSON.parse(form.sensorElementList)
|
|
343
|
+
if (!Array.isArray(parsed)) problems.push('the sensor elements are a JSON list of sensor elements')
|
|
344
|
+
else event.sensorElementList = parsed
|
|
345
|
+
} catch {
|
|
346
|
+
problems.push('the sensor elements are not JSON')
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return problems.length > 0 ? { problems } : { event }
|
|
350
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Who is looking: the API's health answer (sign-in on or off, the development
|
|
2
|
+
// session) and, when sign-in is on, the Better Auth session. Pages read both
|
|
3
|
+
// through two hooks and never call the auth client when sign-in is off.
|
|
4
|
+
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react'
|
|
5
|
+
import { api, type Health } from './api'
|
|
6
|
+
import { authClient } from './auth'
|
|
7
|
+
import { toError } from './use-load'
|
|
8
|
+
|
|
9
|
+
interface HealthState {
|
|
10
|
+
health: Health | undefined
|
|
11
|
+
error: Error | undefined
|
|
12
|
+
loading: boolean
|
|
13
|
+
reload: () => void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const HealthContext = createContext<HealthState>({ health: undefined, error: undefined, loading: true, reload: () => {} })
|
|
17
|
+
|
|
18
|
+
export function HealthProvider({ children }: { children: ReactNode }) {
|
|
19
|
+
const [health, setHealth] = useState<Health>()
|
|
20
|
+
const [error, setError] = useState<Error>()
|
|
21
|
+
const [loading, setLoading] = useState(true)
|
|
22
|
+
const [tick, setTick] = useState(0)
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
let cancelled = false
|
|
26
|
+
setLoading(true)
|
|
27
|
+
api.health().then(
|
|
28
|
+
(answer) => {
|
|
29
|
+
if (cancelled) return
|
|
30
|
+
setHealth(answer)
|
|
31
|
+
setError(undefined)
|
|
32
|
+
setLoading(false)
|
|
33
|
+
},
|
|
34
|
+
(cause: unknown) => {
|
|
35
|
+
if (cancelled) return
|
|
36
|
+
setError(toError(cause))
|
|
37
|
+
setLoading(false)
|
|
38
|
+
}
|
|
39
|
+
)
|
|
40
|
+
return () => {
|
|
41
|
+
cancelled = true
|
|
42
|
+
}
|
|
43
|
+
}, [tick])
|
|
44
|
+
|
|
45
|
+
return <HealthContext.Provider value={{ health, error, loading, reload: () => setTick((t) => t + 1) }}>{children}</HealthContext.Provider>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const useHealth = (): HealthState => useContext(HealthContext)
|
|
49
|
+
|
|
50
|
+
export interface Account {
|
|
51
|
+
/** Someone may act for a brand: a signed-in user, or the development session. */
|
|
52
|
+
signedIn: boolean
|
|
53
|
+
pending: boolean
|
|
54
|
+
name?: string
|
|
55
|
+
email?: string
|
|
56
|
+
/** Sign-in is on: MongoDB is configured and Better Auth is mounted. */
|
|
57
|
+
canSignIn: boolean
|
|
58
|
+
devSession: boolean
|
|
59
|
+
signOut?: () => Promise<void>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const nobody: Account = { signedIn: false, pending: true, canSignIn: false, devSession: false }
|
|
63
|
+
const AccountContext = createContext<Account>(nobody)
|
|
64
|
+
|
|
65
|
+
export function AccountProvider({ children }: { children: ReactNode }) {
|
|
66
|
+
const { health, loading } = useHealth()
|
|
67
|
+
if (loading) return <AccountContext.Provider value={nobody}>{children}</AccountContext.Provider>
|
|
68
|
+
if (health?.signIn) return <AuthAccountProvider>{children}</AuthAccountProvider>
|
|
69
|
+
const dev = health?.devSession
|
|
70
|
+
const value: Account = { signedIn: dev != null, pending: false, name: dev?.name, canSignIn: false, devSession: dev != null }
|
|
71
|
+
return <AccountContext.Provider value={value}>{children}</AccountContext.Provider>
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function AuthAccountProvider({ children }: { children: ReactNode }) {
|
|
75
|
+
const session = authClient.useSession()
|
|
76
|
+
const user = session.data?.user
|
|
77
|
+
const value: Account = {
|
|
78
|
+
signedIn: user != null,
|
|
79
|
+
pending: session.isPending,
|
|
80
|
+
name: user?.name,
|
|
81
|
+
email: user?.email,
|
|
82
|
+
canSignIn: true,
|
|
83
|
+
devSession: false,
|
|
84
|
+
signOut: async () => {
|
|
85
|
+
await authClient.signOut()
|
|
86
|
+
await session.refetch()
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
return <AccountContext.Provider value={value}>{children}</AccountContext.Provider>
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const useAccount = (): Account => useContext(AccountContext)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// One hook for "load this when these change": data, error, loading and a
|
|
2
|
+
// reload, with a stale answer from an earlier run never overwriting a newer one.
|
|
3
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
4
|
+
|
|
5
|
+
export interface Loaded<T> {
|
|
6
|
+
data: T | undefined
|
|
7
|
+
error: Error | undefined
|
|
8
|
+
loading: boolean
|
|
9
|
+
reload: () => void
|
|
10
|
+
setData: (data: T) => void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const toError = (cause: unknown): Error => (cause instanceof Error ? cause : new Error(String(cause)))
|
|
14
|
+
|
|
15
|
+
export function useLoad<T>(load: () => Promise<T>, deps: readonly unknown[]): Loaded<T> {
|
|
16
|
+
const [data, setData] = useState<T>()
|
|
17
|
+
const [error, setError] = useState<Error>()
|
|
18
|
+
const [loading, setLoading] = useState(true)
|
|
19
|
+
const [tick, setTick] = useState(0)
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
let cancelled = false
|
|
23
|
+
setLoading(true)
|
|
24
|
+
setError(undefined)
|
|
25
|
+
load().then(
|
|
26
|
+
(result) => {
|
|
27
|
+
if (cancelled) return
|
|
28
|
+
setData(result)
|
|
29
|
+
setLoading(false)
|
|
30
|
+
},
|
|
31
|
+
(cause: unknown) => {
|
|
32
|
+
if (cancelled) return
|
|
33
|
+
setError(toError(cause))
|
|
34
|
+
setLoading(false)
|
|
35
|
+
}
|
|
36
|
+
)
|
|
37
|
+
return () => {
|
|
38
|
+
cancelled = true
|
|
39
|
+
}
|
|
40
|
+
// The caller names the inputs; the loader itself is recreated on every render.
|
|
41
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
42
|
+
}, [...deps, tick])
|
|
43
|
+
|
|
44
|
+
const reload = useCallback(() => setTick((t) => t + 1), [])
|
|
45
|
+
return { data, error, loading, reload, setData }
|
|
46
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { StrictMode } from 'react'
|
|
2
|
+
import { createRoot } from 'react-dom/client'
|
|
3
|
+
import { BrowserRouter } from 'react-router'
|
|
4
|
+
import { App } from '@/app'
|
|
5
|
+
import { AccountProvider, HealthProvider } from '@/lib/session'
|
|
6
|
+
import './index.css'
|
|
7
|
+
|
|
8
|
+
createRoot(document.getElementById('root')!).render(
|
|
9
|
+
<StrictMode>
|
|
10
|
+
<BrowserRouter>
|
|
11
|
+
<HealthProvider>
|
|
12
|
+
<AccountProvider>
|
|
13
|
+
<App />
|
|
14
|
+
</AccountProvider>
|
|
15
|
+
</HealthProvider>
|
|
16
|
+
</BrowserRouter>
|
|
17
|
+
</StrictMode>
|
|
18
|
+
)
|