@vdatacloud/cx-commons 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -42,7 +42,7 @@ This repository publishes `@vdatacloud/cx-commons` with modular export entry poi
42
42
  ├── (default) --> exports index.ts (Astro integration plugin `cxCommons()`)
43
43
  ├── /styles/global.css --> exports canonical LNF design system tokens & Tailwind rules
44
44
  ├── /components/* --> exports shared Astro UI components (Nav, Footer, StatusBadge, EyebrowLabel, ApiErrorNotice, CantonId, LanguageSwitcher)
45
- ├── /sdk/* --> exports browser SDK helpers (i18n, cx-messages, dialogs, api-error, api-error-notice, canton-id, canton-id-view, agreement-sig, status-colors)
45
+ ├── /sdk/* --> exports browser SDK helpers (i18n, cx-messages, dialogs, api-error, api-error-notice, canton-id, canton-id-view, agreement-sig, pending-approvals, status-colors)
46
46
  └── /schema/api-error.json --> the canonical API error JSON Schema
47
47
  ```
48
48
 
@@ -92,6 +92,12 @@ How a party signs one version of an agreement, mirroring daml-escrow-commons `ag
92
92
 
93
93
  ---
94
94
 
95
+ ### Pending approvals (`sdk/pending-approvals`)
96
+
97
+ One worklist for acts waiting on several people's approval, whatever the act (daml-escrow PLAN.md Phase 91.4, the shared approval process). `mountPendingApprovals(host, items, { me, onDecide })` shows each request's description, proposer, progress (n of N, with a progress bar) and who approved, with the decisions the viewer may take: the proposer withdraws; someone who approved does nothing more; anyone else approves or rejects (`noApprove` hides approve, e.g. for a stale act or the viewer's own appointment). Each decision confirms and asks for a reason before `onDecide(item, decision, reason)`; the app sends it and reloads. Text is never parsed as markup; pass a `Node` for a rich description. Strings are `cx.approvals.*` (en, fr).
98
+
99
+ ---
100
+
95
101
  ### Internationalization (`sdk/i18n`, `sdk/cx-messages`)
96
102
 
97
103
  Zero-dependency i18n for every app on the platform (English and French today; built so Japanese, Chinese and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vdatacloud/cx-commons",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Product CX Commons - Shared design system, UI components, and Astro integration for Data Cloud web applications",
5
5
  "repository": {
6
6
  "type": "git",
@@ -67,6 +67,18 @@ export const cxMessages = {
67
67
  ok: 'OK',
68
68
  close: 'Close',
69
69
  },
70
+ approvals: {
71
+ empty: 'Nothing is waiting for approval.',
72
+ proposedBy: 'Proposed by',
73
+ progress: '{count} of {required} approvals',
74
+ approvedBy: '{count, plural, one {Approved by # person} other {Approved by # people}}',
75
+ approve: 'Approve',
76
+ reject: 'Reject',
77
+ withdraw: 'Withdraw',
78
+ confirm: '{decision} this request?',
79
+ reasonPrompt: 'Why? This goes on the record.',
80
+ reason: 'Reason',
81
+ },
70
82
  language: {
71
83
  label: 'Language',
72
84
  choose: 'Choose language',
@@ -135,6 +147,18 @@ export const cxMessages = {
135
147
  ok: 'OK',
136
148
  close: 'Fermer',
137
149
  },
150
+ approvals: {
151
+ empty: 'Rien n’attend d’approbation.',
152
+ proposedBy: 'Proposé par',
153
+ progress: 'Approbations\u202f: {count} sur {required}',
154
+ approvedBy: '{count, plural, one {Approuvé par # personne} other {Approuvé par # personnes}}',
155
+ approve: 'Approuver',
156
+ reject: 'Rejeter',
157
+ withdraw: 'Retirer',
158
+ confirm: '{decision} cette demande\u202f?',
159
+ reasonPrompt: 'Pourquoi\u202f? Cela figure au registre.',
160
+ reason: 'Motif',
161
+ },
138
162
  language: {
139
163
  label: 'Langue',
140
164
  choose: 'Choisir la langue',
@@ -0,0 +1,160 @@
1
+ // One worklist for acts waiting on several people's approval (daml-escrow
2
+ // PLAN.md Phase 91.4, Technical Debt TD-1): whatever the act -- a mandate
3
+ // change, a signer change -- each pending request shows what it does, who
4
+ // proposed it, how far it has come (n of N) and who approved, with approve /
5
+ // reject / withdraw, each asking for a reason. Framework-free DOM; every
6
+ // value is set with textContent, so nothing an app passes as text can inject
7
+ // markup. An app that describes an act richly passes a Node it built itself.
8
+ //
9
+ // Who may decide follows the shared approval process: the proposer only
10
+ // withdraws; someone who has approved does nothing more; anyone else may
11
+ // approve or reject. Without `me` all three decisions show (operators,
12
+ // tests) and the server's refusal is the answer.
13
+
14
+ import { confirmDialog, promptDialog } from './dialogs';
15
+ import { renderCantonId } from './canton-id-view';
16
+ import { getTranslator, type Translator } from './i18n';
17
+ import './cx-messages';
18
+
19
+ export type ApprovalDecision = 'approve' | 'reject' | 'withdraw';
20
+
21
+ export interface PendingApproval {
22
+ /** The request's id (what the decision is sent for). */
23
+ id: string;
24
+ /** Who proposed it: also its first approval. */
25
+ proposedBy: string;
26
+ /** Who approved so far, in order, the proposer first. */
27
+ approvals: string[];
28
+ /** How many approvals the act needs, the proposer's included. */
29
+ required: number;
30
+ /** What the act does: text, or a Node the app built. */
31
+ what: string | Node;
32
+ /** What sort of act it is, e.g. "signer-change" (shown as a tag when kindLabel is given). */
33
+ kind?: string;
34
+ kindLabel?: string;
35
+ /** A warning under the act, e.g. that it can no longer take effect. */
36
+ note?: string;
37
+ /** Approve isn't offered: the act can't stand, or the viewer is its subject. */
38
+ noApprove?: boolean;
39
+ }
40
+
41
+ export interface PendingApprovalsOptions {
42
+ /** The viewer's party; decides which decisions they're offered. */
43
+ me?: string;
44
+ /** Sends a decision, with the reason given; the list doesn't change itself (the app reloads). */
45
+ onDecide: (item: PendingApproval, decision: ApprovalDecision, reason: string) => void | Promise<void>;
46
+ /** Shows a party (default: the shared Canton id display). */
47
+ party?: (party: string) => Node;
48
+ /** Text when nothing is pending (default cx.approvals.empty). */
49
+ empty?: string;
50
+ /** Locale (default: the page's <html lang>). */
51
+ locale?: string;
52
+ }
53
+
54
+ const ITEM = 'py-3 flex flex-wrap items-start justify-between gap-3';
55
+ const WHAT = 'text-xs font-bold text-slate-700 dark:text-slate-300';
56
+ const META = 'text-xs text-slate-400 dark:text-slate-500 mt-1 flex flex-wrap items-center gap-1';
57
+ const NOTE = 'text-xs text-status-disputed mt-1';
58
+ const TAG = 'cx-caps inline-flex items-center rounded-full border border-slate-200 px-2 py-0.5 text-[10px] font-bold uppercase tracking-widest text-slate-500 dark:border-slate-700 dark:text-slate-400';
59
+ const BAR = 'mt-2 h-1 w-40 overflow-hidden rounded-full bg-slate-100 dark:bg-slate-800';
60
+ const FILL = 'h-full rounded-full bg-brand-600 dark:bg-brand-400';
61
+ const DETAILS = 'mt-2 text-xs text-slate-500 dark:text-slate-400';
62
+ const BUTTON = 'cx-caps rounded-md border px-2 py-1 text-[10px] font-bold uppercase tracking-widest';
63
+ const BUTTON_TONE = {
64
+ approve: 'border-brand-600/30 text-brand-600 hover:bg-brand-600/10 dark:border-brand-400/30 dark:text-brand-400 dark:hover:bg-brand-400/10',
65
+ reject: 'border-status-disputed/30 text-status-disputed hover:bg-status-disputed/10',
66
+ withdraw: 'border-status-disputed/30 text-status-disputed hover:bg-status-disputed/10',
67
+ };
68
+
69
+ function el<K extends keyof HTMLElementTagNameMap>(tag: K, className = '', text?: string): HTMLElementTagNameMap[K] {
70
+ const n = document.createElement(tag);
71
+ if (className) n.className = className;
72
+ if (text !== undefined) n.textContent = text;
73
+ return n;
74
+ }
75
+
76
+ /** The decisions viewer `me` is offered on item (all three without `me`). */
77
+ export function decisionsFor(item: PendingApproval, me?: string): ApprovalDecision[] {
78
+ if (!me) return item.noApprove ? ['reject', 'withdraw'] : ['approve', 'reject', 'withdraw'];
79
+ if (me === item.proposedBy) return ['withdraw'];
80
+ if (item.approvals.includes(me)) return [];
81
+ return item.noApprove ? ['reject'] : ['approve', 'reject'];
82
+ }
83
+
84
+ // decide confirms, asks for the reason, then hands the decision to the app.
85
+ async function decide(t: Translator, item: PendingApproval, d: ApprovalDecision, opts: PendingApprovalsOptions): Promise<void> {
86
+ const label = t.t(`cx.approvals.${d}`);
87
+ if (!(await confirmDialog(t.t('cx.approvals.confirm', { decision: label }), { confirmLabel: label, danger: d !== 'approve' }))) return;
88
+ const reason = await promptDialog(t.t('cx.approvals.reasonPrompt'), { label: t.t('cx.approvals.reason'), required: true });
89
+ if (reason) await opts.onDecide(item, d, reason);
90
+ }
91
+
92
+ function renderItem(t: Translator, item: PendingApproval, opts: PendingApprovalsOptions): HTMLElement {
93
+ const party = opts.party ?? ((p: string) => renderCantonId(p, { locale: t.locale }));
94
+ const li = el('li', ITEM);
95
+ li.dataset.approval = item.id;
96
+ if (item.kind) li.dataset.approvalKind = item.kind;
97
+
98
+ const body = el('div', 'min-w-0');
99
+ const what = el('div', WHAT);
100
+ what.append(typeof item.what === 'string' ? document.createTextNode(item.what) : item.what);
101
+ body.append(what);
102
+ if (item.note) body.append(el('div', NOTE, item.note));
103
+
104
+ const count = Math.min(item.approvals.length, item.required);
105
+ const meta = el('div', META);
106
+ if (item.kindLabel) meta.append(el('span', TAG, item.kindLabel));
107
+ meta.append(t.t('cx.approvals.proposedBy') + ' ', party(item.proposedBy), ` · ${t.t('cx.approvals.progress', { count, required: item.required })}`);
108
+ body.append(meta);
109
+
110
+ const bar = el('div', BAR);
111
+ bar.setAttribute('role', 'progressbar');
112
+ bar.setAttribute('aria-valuemin', '0');
113
+ bar.setAttribute('aria-valuemax', String(item.required));
114
+ bar.setAttribute('aria-valuenow', String(count));
115
+ const fill = el('div', FILL);
116
+ fill.style.width = `${item.required > 0 ? Math.round((100 * count) / item.required) : 0}%`;
117
+ bar.append(fill);
118
+ body.append(bar);
119
+
120
+ const details = el('details', DETAILS);
121
+ details.append(el('summary', 'cursor-pointer', t.t('cx.approvals.approvedBy', { count })));
122
+ const who = el('ol', 'list-decimal pl-5 mt-1 space-y-1');
123
+ for (const p of item.approvals) {
124
+ const row = el('li');
125
+ row.append(party(p));
126
+ who.append(row);
127
+ }
128
+ details.append(who);
129
+ body.append(details);
130
+
131
+ const actions = el('div', 'flex gap-2');
132
+ for (const d of decisionsFor(item, opts.me)) {
133
+ const b = el('button', `${BUTTON} ${BUTTON_TONE[d]}`, t.t(`cx.approvals.${d}`));
134
+ b.type = 'button';
135
+ b.dataset.approvalDecision = d;
136
+ b.addEventListener('click', () => void decide(t, item, d, opts));
137
+ actions.append(b);
138
+ }
139
+ li.append(body, actions);
140
+ return li;
141
+ }
142
+
143
+ /** Renders the pending requests, or the empty text. */
144
+ export function renderPendingApprovals(items: PendingApproval[], opts: PendingApprovalsOptions): HTMLElement {
145
+ const t = getTranslator(opts.locale);
146
+ if (!items.length) {
147
+ const p = el('p', 'text-xs italic text-slate-400 dark:text-slate-600', opts.empty ?? t.t('cx.approvals.empty'));
148
+ p.dataset.approvalsEmpty = '';
149
+ return p;
150
+ }
151
+ const ul = el('ul', 'divide-y divide-slate-100 dark:divide-slate-800');
152
+ ul.dataset.approvals = '';
153
+ for (const item of items) ul.append(renderItem(t, item, opts));
154
+ return ul;
155
+ }
156
+
157
+ /** Replaces host's content with the list. */
158
+ export function mountPendingApprovals(host: HTMLElement, items: PendingApproval[], opts: PendingApprovalsOptions): void {
159
+ host.replaceChildren(renderPendingApprovals(items, opts));
160
+ }