@bsv/create-dpp-app 0.0.0-stage → 0.1.0-beta.1
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/README.md +73 -2
- package/dist/index.js +97 -0
- package/package.json +53 -4
- package/template/Dockerfile +35 -0
- package/template/README.md +134 -0
- package/template/_dockerignore +6 -0
- package/template/_env.example +53 -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 +49 -0
- package/template/apps/api/package.json +34 -0
- package/template/apps/api/src/auth.ts +55 -0
- package/template/apps/api/src/claims.ts +85 -0
- package/template/apps/api/src/cli.ts +42 -0
- package/template/apps/api/src/config.ts +66 -0
- package/template/apps/api/src/custody.ts +186 -0
- package/template/apps/api/src/dev-index.ts +71 -0
- package/template/apps/api/src/env.ts +14 -0
- package/template/apps/api/src/identifiers.ts +57 -0
- package/template/apps/api/src/index-client.ts +136 -0
- package/template/apps/api/src/main.ts +50 -0
- package/template/apps/api/src/parties.ts +68 -0
- package/template/apps/api/src/platform.ts +121 -0
- package/template/apps/api/src/profiles.ts +100 -0
- package/template/apps/api/src/reader.ts +96 -0
- package/template/apps/api/src/server.ts +420 -0
- package/template/apps/api/src/store.ts +249 -0
- package/template/apps/api/src/wallet.ts +346 -0
- package/template/apps/api/src/worker.ts +106 -0
- package/template/apps/api/src/writer.ts +351 -0
- package/template/apps/api/test/config.test.ts +15 -0
- package/template/apps/api/test/external-index.test.ts +31 -0
- package/template/apps/api/test/lifecycle.test.ts +136 -0
- package/template/apps/api/tsconfig.json +9 -0
- package/template/apps/api/vitest.config.ts +9 -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 +51 -0
- package/template/apps/web/src/components/bits.tsx +188 -0
- package/template/apps/web/src/components/layout.tsx +118 -0
- package/template/apps/web/src/components/passport-form.tsx +244 -0
- package/template/apps/web/src/components/read-passport.tsx +48 -0
- package/template/apps/web/src/components/reading.tsx +157 -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 +286 -0
- package/template/apps/web/src/lib/auth.ts +10 -0
- package/template/apps/web/src/lib/format.ts +64 -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 +108 -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/home.tsx +51 -0
- package/template/apps/web/src/pages/new-passport.tsx +114 -0
- package/template/apps/web/src/pages/operations.tsx +150 -0
- package/template/apps/web/src/pages/passport.tsx +568 -0
- package/template/apps/web/src/pages/public-passport.tsx +24 -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 +15 -0
- package/template/deploy/compose.yml +26 -0
- package/template/package.json +42 -0
- package/template/tsconfig.base.json +13 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://ui.shadcn.com/schema.json",
|
|
3
|
+
"style": "new-york",
|
|
4
|
+
"rsc": false,
|
|
5
|
+
"tsx": true,
|
|
6
|
+
"tailwind": {
|
|
7
|
+
"config": "",
|
|
8
|
+
"css": "src/index.css",
|
|
9
|
+
"baseColor": "neutral",
|
|
10
|
+
"cssVariables": true,
|
|
11
|
+
"prefix": ""
|
|
12
|
+
},
|
|
13
|
+
"iconLibrary": "lucide",
|
|
14
|
+
"aliases": {
|
|
15
|
+
"components": "@/components",
|
|
16
|
+
"utils": "@/lib/utils",
|
|
17
|
+
"ui": "@/components/ui",
|
|
18
|
+
"lib": "@/lib",
|
|
19
|
+
"hooks": "@/hooks"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>Digital Product Passports</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "web",
|
|
3
|
+
"private": true,
|
|
4
|
+
"description": "The web app: brand dashboard, passport pages, hand-on acceptance, public verification",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dev": "vite",
|
|
9
|
+
"build": "tsc -p tsconfig.json --noEmit && vite build",
|
|
10
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
11
|
+
"preview": "vite preview"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"better-auth": "1.7.7",
|
|
15
|
+
"class-variance-authority": "^0.7.1",
|
|
16
|
+
"cn": "^0.4.0",
|
|
17
|
+
"lucide-react": "^1.54.0",
|
|
18
|
+
"radix-ui": "^1.7.0",
|
|
19
|
+
"react": "19.3.0",
|
|
20
|
+
"react-dom": "19.3.0",
|
|
21
|
+
"react-router": "8.4.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@tailwindcss/vite": "^4.3.3",
|
|
25
|
+
"@types/node": "^22.20.3",
|
|
26
|
+
"@types/react": "^19.2.0",
|
|
27
|
+
"@types/react-dom": "^19.2.0",
|
|
28
|
+
"@vitejs/plugin-react": "^6.1.2",
|
|
29
|
+
"tailwindcss": "^4.3.3",
|
|
30
|
+
"typescript": "^7.0.2",
|
|
31
|
+
"vite": "^8.3.4"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// The routes. The public passport page sits at the identifier's own path,
|
|
2
|
+
// /01/<gtin>/21/<serial>, so a label's QR code opens it directly.
|
|
3
|
+
import { Link, Route, Routes } from 'react-router'
|
|
4
|
+
import { Layout } from '@/components/layout'
|
|
5
|
+
import { Accept } from '@/pages/accept'
|
|
6
|
+
import { Brand } from '@/pages/brand'
|
|
7
|
+
import { Brands } from '@/pages/brands'
|
|
8
|
+
import { Home } from '@/pages/home'
|
|
9
|
+
import { NewPassport } from '@/pages/new-passport'
|
|
10
|
+
import { Operations } from '@/pages/operations'
|
|
11
|
+
import { Passport } from '@/pages/passport'
|
|
12
|
+
import { PublicPassport } from '@/pages/public-passport'
|
|
13
|
+
import { SignIn } from '@/pages/sign-in'
|
|
14
|
+
import { Verify } from '@/pages/verify'
|
|
15
|
+
import { Wallet } from '@/pages/wallet'
|
|
16
|
+
|
|
17
|
+
function NotFound() {
|
|
18
|
+
return (
|
|
19
|
+
<div className="space-y-2">
|
|
20
|
+
<h1 className="text-2xl font-semibold">Not found</h1>
|
|
21
|
+
<p className="text-sm text-muted-foreground">
|
|
22
|
+
There is no page here.{' '}
|
|
23
|
+
<Link to="/" className="underline underline-offset-4">
|
|
24
|
+
Home
|
|
25
|
+
</Link>
|
|
26
|
+
</p>
|
|
27
|
+
</div>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function App() {
|
|
32
|
+
return (
|
|
33
|
+
<Routes>
|
|
34
|
+
<Route element={<Layout />}>
|
|
35
|
+
<Route index element={<Home />} />
|
|
36
|
+
<Route path="sign-in" element={<SignIn mode="sign-in" />} />
|
|
37
|
+
<Route path="sign-up" element={<SignIn mode="sign-up" />} />
|
|
38
|
+
<Route path="brands" element={<Brands />} />
|
|
39
|
+
<Route path="brands/:brandId" element={<Brand />} />
|
|
40
|
+
<Route path="brands/:brandId/new" element={<NewPassport />} />
|
|
41
|
+
<Route path="passports/:ref" element={<Passport />} />
|
|
42
|
+
<Route path="01/:gtin/21/:serial" element={<PublicPassport />} />
|
|
43
|
+
<Route path="verify" element={<Verify />} />
|
|
44
|
+
<Route path="accept/:requestId" element={<Accept />} />
|
|
45
|
+
<Route path="operations" element={<Operations />} />
|
|
46
|
+
<Route path="wallet" element={<Wallet />} />
|
|
47
|
+
<Route path="*" element={<NotFound />} />
|
|
48
|
+
</Route>
|
|
49
|
+
</Routes>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
@@ -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,118 @@
|
|
|
1
|
+
// The frame every page sits in: a header with the name, the navigation and
|
|
2
|
+
// the sign-in state, the page itself, and one footer line.
|
|
3
|
+
import type { ReactNode } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { Link, NavLink, Outlet, useNavigate } from 'react-router'
|
|
6
|
+
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
|
7
|
+
import { Button } from '@/components/ui/button'
|
|
8
|
+
import { Loading, ToneBadge } from '@/components/bits'
|
|
9
|
+
import { useAccount, useHealth } from '@/lib/session'
|
|
10
|
+
|
|
11
|
+
const navLink = ({ isActive }: { isActive: boolean }) => cn('text-sm transition-colors hover:text-foreground', isActive ? 'font-medium text-foreground' : 'text-muted-foreground')
|
|
12
|
+
|
|
13
|
+
/** A page for a signed-in member: with sign-in on and nobody signed in, say so instead of letting every call answer 401. */
|
|
14
|
+
export function RequireAccount({ children }: { children: ReactNode }) {
|
|
15
|
+
const account = useAccount()
|
|
16
|
+
if (account.pending) return <Loading />
|
|
17
|
+
if (!account.signedIn && account.canSignIn) {
|
|
18
|
+
return (
|
|
19
|
+
<Alert>
|
|
20
|
+
<AlertTitle>Sign in first</AlertTitle>
|
|
21
|
+
<AlertDescription>
|
|
22
|
+
<p>
|
|
23
|
+
This page is for a member of a brand.{' '}
|
|
24
|
+
<Link to="/sign-in" className="underline underline-offset-4">
|
|
25
|
+
Sign in
|
|
26
|
+
</Link>{' '}
|
|
27
|
+
or{' '}
|
|
28
|
+
<Link to="/sign-up" className="underline underline-offset-4">
|
|
29
|
+
sign up
|
|
30
|
+
</Link>
|
|
31
|
+
.
|
|
32
|
+
</p>
|
|
33
|
+
</AlertDescription>
|
|
34
|
+
</Alert>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
if (!account.signedIn) {
|
|
38
|
+
return (
|
|
39
|
+
<Alert variant="destructive">
|
|
40
|
+
<AlertTitle>No session</AlertTitle>
|
|
41
|
+
<AlertDescription>
|
|
42
|
+
<p>The API did not answer, so there is nobody to act as. Check that the API is running.</p>
|
|
43
|
+
</AlertDescription>
|
|
44
|
+
</Alert>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
return <>{children}</>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function Layout() {
|
|
51
|
+
const account = useAccount()
|
|
52
|
+
const { error } = useHealth()
|
|
53
|
+
const navigate = useNavigate()
|
|
54
|
+
|
|
55
|
+
async function signOut() {
|
|
56
|
+
await account.signOut?.()
|
|
57
|
+
navigate('/')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div className="flex min-h-screen flex-col">
|
|
62
|
+
<header className="border-b">
|
|
63
|
+
<div className="mx-auto flex w-full max-w-5xl flex-wrap items-center gap-x-6 gap-y-2 px-4 py-3">
|
|
64
|
+
<Link to="/" className="font-semibold tracking-tight">
|
|
65
|
+
Digital Product Passports
|
|
66
|
+
</Link>
|
|
67
|
+
<nav className="flex flex-wrap items-center gap-4" aria-label="Main">
|
|
68
|
+
<NavLink to="/verify" className={navLink}>
|
|
69
|
+
Verify
|
|
70
|
+
</NavLink>
|
|
71
|
+
{account.signedIn ? (
|
|
72
|
+
<>
|
|
73
|
+
<NavLink to="/brands" className={navLink}>
|
|
74
|
+
Brands
|
|
75
|
+
</NavLink>
|
|
76
|
+
<NavLink to="/operations" className={navLink}>
|
|
77
|
+
Operations
|
|
78
|
+
</NavLink>
|
|
79
|
+
<NavLink to="/wallet" className={navLink}>
|
|
80
|
+
Wallet
|
|
81
|
+
</NavLink>
|
|
82
|
+
</>
|
|
83
|
+
) : null}
|
|
84
|
+
</nav>
|
|
85
|
+
<div className="ml-auto flex items-center gap-3 text-sm">
|
|
86
|
+
{account.pending ? null : account.devSession ? (
|
|
87
|
+
<ToneBadge tone="warn">Developer</ToneBadge>
|
|
88
|
+
) : account.signedIn ? (
|
|
89
|
+
<>
|
|
90
|
+
<span className="text-muted-foreground">{account.name ?? account.email}</span>
|
|
91
|
+
<Button variant="outline" size="sm" onClick={signOut}>
|
|
92
|
+
Sign out
|
|
93
|
+
</Button>
|
|
94
|
+
</>
|
|
95
|
+
) : account.canSignIn ? (
|
|
96
|
+
<>
|
|
97
|
+
<Link to="/sign-in" className="text-muted-foreground hover:text-foreground">
|
|
98
|
+
Sign in
|
|
99
|
+
</Link>
|
|
100
|
+
<Button asChild size="sm">
|
|
101
|
+
<Link to="/sign-up">Sign up</Link>
|
|
102
|
+
</Button>
|
|
103
|
+
</>
|
|
104
|
+
) : (
|
|
105
|
+
<span className="text-muted-foreground">{error == null ? 'Sign-in is off' : 'API unreachable'}</span>
|
|
106
|
+
)}
|
|
107
|
+
</div>
|
|
108
|
+
</div>
|
|
109
|
+
</header>
|
|
110
|
+
<main className="mx-auto w-full max-w-5xl flex-1 px-4 py-8">
|
|
111
|
+
<Outlet />
|
|
112
|
+
</main>
|
|
113
|
+
<footer className="border-t">
|
|
114
|
+
<div className="mx-auto w-full max-w-5xl px-4 py-4 text-sm text-muted-foreground">Built on the open DPP standard on BSV</div>
|
|
115
|
+
</footer>
|
|
116
|
+
</div>
|
|
117
|
+
)
|
|
118
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// The product-data form a profile describes: one control per field, chosen
|
|
2
|
+
// from the field's valueType, grouped as the manifest groups them. Public
|
|
3
|
+
// fields go to the payload; every other tier goes to the owner fields the
|
|
4
|
+
// API encrypts off chain. The same form issues and updates.
|
|
5
|
+
import { useMemo, useState, type FormEvent, type ReactNode } from 'react'
|
|
6
|
+
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
|
7
|
+
import { Button } from '@/components/ui/button'
|
|
8
|
+
import { Input } from '@/components/ui/input'
|
|
9
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
|
10
|
+
import { Textarea } from '@/components/ui/textarea'
|
|
11
|
+
import { ErrorNotice, Field } from '@/components/bits'
|
|
12
|
+
import type { Profile, ProfileField } from '@/lib/api'
|
|
13
|
+
import { humanise } from '@/lib/format'
|
|
14
|
+
|
|
15
|
+
export interface PassportFormValues {
|
|
16
|
+
payload: Record<string, unknown>
|
|
17
|
+
ownerFields: Record<string, unknown>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface Props {
|
|
21
|
+
profile: Profile
|
|
22
|
+
/** Where the values start: the sample for a new passport, the current payload for an update. */
|
|
23
|
+
initial: { payload: Record<string, unknown>; ownerFields?: Record<string, unknown> }
|
|
24
|
+
submitLabel: string
|
|
25
|
+
busy?: boolean
|
|
26
|
+
error?: unknown
|
|
27
|
+
onSubmit: (values: PassportFormValues) => void
|
|
28
|
+
/** Rendered inside the form before the product fields, for the identifier inputs. */
|
|
29
|
+
before?: ReactNode
|
|
30
|
+
/** Rendered after the product fields, for a claim code or a note. */
|
|
31
|
+
after?: ReactNode
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type Control = 'text' | 'textarea' | 'number' | 'select' | 'date' | 'url' | 'json'
|
|
35
|
+
|
|
36
|
+
function controlFor(field: ProfileField): Control {
|
|
37
|
+
if (field.cardinality === 'many' || field.valueType === 'record' || field.valueType === 'multi' || field.valueType === 'any') return 'json'
|
|
38
|
+
switch (field.valueType) {
|
|
39
|
+
case 'decimal':
|
|
40
|
+
case 'integer':
|
|
41
|
+
case 'percent':
|
|
42
|
+
return 'number'
|
|
43
|
+
case 'enum':
|
|
44
|
+
return field.codeList != null && field.codeList.options.length > 0 ? 'select' : 'text'
|
|
45
|
+
case 'date':
|
|
46
|
+
return 'date'
|
|
47
|
+
case 'url':
|
|
48
|
+
case 'document':
|
|
49
|
+
case 'graphic':
|
|
50
|
+
return 'url'
|
|
51
|
+
default:
|
|
52
|
+
return (field.constraints?.maxLength ?? 0) > 200 ? 'textarea' : 'text'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function placeholderFor(field: ProfileField): string | undefined {
|
|
57
|
+
if (field.constraints?.patternHint != null) return field.constraints.patternHint
|
|
58
|
+
if (field.valueType === 'monthYear') return 'YYYY-MM'
|
|
59
|
+
if (field.valueType === 'country') return 'Two-letter country code, for example AU'
|
|
60
|
+
if (field.valueType === 'url' || field.valueType === 'document' || field.valueType === 'graphic') return 'https://'
|
|
61
|
+
return undefined
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const initialText = (field: ProfileField, value: unknown): string => {
|
|
65
|
+
if (value === undefined || value === null) return ''
|
|
66
|
+
if (controlFor(field) === 'json') return JSON.stringify(value, null, 2)
|
|
67
|
+
return typeof value === 'string' ? value : String(value)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function PassportForm({ profile, initial, submitLabel, busy, error, onSubmit, before, after }: Props) {
|
|
71
|
+
const fields = profile.fields
|
|
72
|
+
const fieldKeys = useMemo(() => new Set(fields.map((f) => f.key)), [fields])
|
|
73
|
+
const [showOptional, setShowOptional] = useState(false)
|
|
74
|
+
const [localError, setLocalError] = useState<string>()
|
|
75
|
+
const [draft, setDraft] = useState<Record<string, string>>(() => {
|
|
76
|
+
const out: Record<string, string> = {}
|
|
77
|
+
for (const field of fields) {
|
|
78
|
+
const source = field.accessTier === 'public' ? initial.payload : (initial.ownerFields ?? {})
|
|
79
|
+
if (field.key in source) out[field.key] = initialText(field, source[field.key])
|
|
80
|
+
}
|
|
81
|
+
return out
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const groups = useMemo(() => {
|
|
85
|
+
const map = new Map<string, ProfileField[]>()
|
|
86
|
+
for (const field of fields) {
|
|
87
|
+
const group = field.group ?? 'other'
|
|
88
|
+
if (!map.has(group)) map.set(group, [])
|
|
89
|
+
map.get(group)!.push(field)
|
|
90
|
+
}
|
|
91
|
+
return [...map.entries()]
|
|
92
|
+
}, [fields])
|
|
93
|
+
|
|
94
|
+
const optionalCount = fields.filter((f) => f.obligation === 'optional').length
|
|
95
|
+
const notice = typeof initial.payload.notice === 'string' ? initial.payload.notice : undefined
|
|
96
|
+
const set = (key: string, value: string) => setDraft((d) => ({ ...d, [key]: value }))
|
|
97
|
+
|
|
98
|
+
function submit(event: FormEvent) {
|
|
99
|
+
event.preventDefault()
|
|
100
|
+
setLocalError(undefined)
|
|
101
|
+
const payload: Record<string, unknown> = {}
|
|
102
|
+
const ownerFields: Record<string, unknown> = {}
|
|
103
|
+
// Stamps and the notice are not fields; they are carried as they came.
|
|
104
|
+
for (const [key, value] of Object.entries(initial.payload)) if (!fieldKeys.has(key)) payload[key] = value
|
|
105
|
+
for (const field of fields) {
|
|
106
|
+
const raw = draft[field.key] ?? ''
|
|
107
|
+
if (raw.trim() === '') continue
|
|
108
|
+
let value: unknown
|
|
109
|
+
const control = controlFor(field)
|
|
110
|
+
if (control === 'number') {
|
|
111
|
+
value = Number(raw)
|
|
112
|
+
if (Number.isNaN(value)) {
|
|
113
|
+
setLocalError(`${field.label} must be a number.`)
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
} else if (control === 'json') {
|
|
117
|
+
try {
|
|
118
|
+
value = JSON.parse(raw)
|
|
119
|
+
} catch (cause) {
|
|
120
|
+
setLocalError(`${field.label} must be valid JSON: ${cause instanceof Error ? cause.message : String(cause)}`)
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
value = raw
|
|
125
|
+
}
|
|
126
|
+
;(field.accessTier === 'public' ? payload : ownerFields)[field.key] = value
|
|
127
|
+
}
|
|
128
|
+
onSubmit({ payload, ownerFields })
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<form onSubmit={submit} className="space-y-8">
|
|
133
|
+
{notice == null ? null : (
|
|
134
|
+
<Alert>
|
|
135
|
+
<AlertTitle>Demonstration prefix</AlertTitle>
|
|
136
|
+
<AlertDescription>
|
|
137
|
+
<p>This platform issues under the GS1 demonstration prefix, so every passport carries this notice first: "{notice}"</p>
|
|
138
|
+
</AlertDescription>
|
|
139
|
+
</Alert>
|
|
140
|
+
)}
|
|
141
|
+
{before}
|
|
142
|
+
{groups.map(([group, groupFields]) => {
|
|
143
|
+
const shown = groupFields.filter((f) => f.obligation !== 'optional' || showOptional)
|
|
144
|
+
if (shown.length === 0) return null
|
|
145
|
+
return (
|
|
146
|
+
<fieldset key={group} className="space-y-4">
|
|
147
|
+
<legend className="mb-3 text-base font-semibold">{humanise(group)}</legend>
|
|
148
|
+
<div className="grid gap-4 sm:grid-cols-2">
|
|
149
|
+
{shown.map((field) => (
|
|
150
|
+
<FieldControl key={field.key} field={field} value={draft[field.key] ?? ''} onChange={(v) => set(field.key, v)} />
|
|
151
|
+
))}
|
|
152
|
+
</div>
|
|
153
|
+
</fieldset>
|
|
154
|
+
)
|
|
155
|
+
})}
|
|
156
|
+
{optionalCount > 0 ? (
|
|
157
|
+
<label className="flex items-center gap-2 text-sm">
|
|
158
|
+
<input type="checkbox" checked={showOptional} onChange={(e) => setShowOptional(e.target.checked)} />
|
|
159
|
+
Show {optionalCount} optional {optionalCount === 1 ? 'field' : 'fields'}
|
|
160
|
+
</label>
|
|
161
|
+
) : null}
|
|
162
|
+
{after}
|
|
163
|
+
{localError == null ? null : <ErrorNotice error={new Error(localError)} title="Check the form" />}
|
|
164
|
+
{error == null ? null : <ErrorNotice error={error} title="The API refused it" />}
|
|
165
|
+
<div className="flex items-center gap-3">
|
|
166
|
+
<Button type="submit" disabled={busy}>
|
|
167
|
+
{busy ? 'Working' : submitLabel}
|
|
168
|
+
</Button>
|
|
169
|
+
<span className="text-xs text-muted-foreground">
|
|
170
|
+
<span className="text-destructive">*</span> required by the profile
|
|
171
|
+
</span>
|
|
172
|
+
</div>
|
|
173
|
+
</form>
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function FieldControl({ field, value, onChange }: { field: ProfileField; value: string; onChange: (value: string) => void }) {
|
|
178
|
+
const control = controlFor(field)
|
|
179
|
+
const id = `field-${field.key}`
|
|
180
|
+
const required = field.obligation === 'required'
|
|
181
|
+
const tier = field.accessTier === 'public' ? undefined : `${humanise(field.accessTier)} tier, encrypted off chain`
|
|
182
|
+
const help = [field.note, tier, field.obligation === 'recommended' ? 'Recommended' : undefined].filter((h): h is string => h != null).join('. ')
|
|
183
|
+
const label = field.unit == null ? field.label : `${field.label} (${field.unit})`
|
|
184
|
+
const wide = control === 'json' || control === 'textarea'
|
|
185
|
+
|
|
186
|
+
let input: ReactNode
|
|
187
|
+
if (control === 'select' && field.codeList != null) {
|
|
188
|
+
const options = [...field.codeList.options]
|
|
189
|
+
if (value !== '' && !options.some((o) => o.value === value)) options.unshift({ value, label: value })
|
|
190
|
+
input = (
|
|
191
|
+
<Select value={value} onValueChange={onChange}>
|
|
192
|
+
<SelectTrigger id={id} className="w-full" aria-required={required}>
|
|
193
|
+
<SelectValue placeholder="Choose" />
|
|
194
|
+
</SelectTrigger>
|
|
195
|
+
<SelectContent>
|
|
196
|
+
{options.map((o) => (
|
|
197
|
+
<SelectItem key={o.value} value={o.value}>
|
|
198
|
+
{o.label}
|
|
199
|
+
</SelectItem>
|
|
200
|
+
))}
|
|
201
|
+
</SelectContent>
|
|
202
|
+
</Select>
|
|
203
|
+
)
|
|
204
|
+
} else if (control === 'json') {
|
|
205
|
+
input = <Textarea id={id} value={value} onChange={(e) => onChange(e.target.value)} required={required} className="min-h-28 font-mono text-xs" spellCheck={false} placeholder="JSON" />
|
|
206
|
+
} else if (control === 'textarea') {
|
|
207
|
+
input = <Textarea id={id} value={value} onChange={(e) => onChange(e.target.value)} required={required} maxLength={field.constraints?.maxLength} />
|
|
208
|
+
} else if (control === 'number') {
|
|
209
|
+
input = (
|
|
210
|
+
<Input
|
|
211
|
+
id={id}
|
|
212
|
+
type="number"
|
|
213
|
+
inputMode="decimal"
|
|
214
|
+
value={value}
|
|
215
|
+
onChange={(e) => onChange(e.target.value)}
|
|
216
|
+
required={required}
|
|
217
|
+
min={field.constraints?.minimum}
|
|
218
|
+
max={field.constraints?.maximum}
|
|
219
|
+
step={field.constraints?.step ?? (field.valueType === 'integer' ? 1 : 'any')}
|
|
220
|
+
/>
|
|
221
|
+
)
|
|
222
|
+
} else {
|
|
223
|
+
input = (
|
|
224
|
+
<Input
|
|
225
|
+
id={id}
|
|
226
|
+
type={control === 'date' ? 'date' : control === 'url' ? 'url' : 'text'}
|
|
227
|
+
value={value}
|
|
228
|
+
onChange={(e) => onChange(e.target.value)}
|
|
229
|
+
required={required}
|
|
230
|
+
maxLength={field.constraints?.maxLength}
|
|
231
|
+
pattern={field.constraints?.pattern}
|
|
232
|
+
placeholder={placeholderFor(field)}
|
|
233
|
+
/>
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return (
|
|
238
|
+
<div className={wide ? 'sm:col-span-2' : undefined}>
|
|
239
|
+
<Field label={label} htmlFor={id} required={required} help={help === '' ? undefined : help}>
|
|
240
|
+
{input}
|
|
241
|
+
</Field>
|
|
242
|
+
</div>
|
|
243
|
+
)
|
|
244
|
+
}
|