@vdatacloud/cx-commons 0.4.0 → 0.6.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 +4 -2
- package/package.json +1 -1
- package/src/sdk/pending-approvals.ts +68 -11
package/README.md
CHANGED
|
@@ -88,13 +88,15 @@ How a party signs one version of an agreement, mirroring daml-escrow-commons `ag
|
|
|
88
88
|
- `verifySignature(alg, publicKey, message, signature)`: `ed25519` (wallets) or `ecdsa-p256-sha256` (KMS keys such as the custodian's; strict DER, low s), through WebCrypto.
|
|
89
89
|
- `draftHash` / `draftMessage`: the frozen `tripart.draft-version/1` (Phase 77 drafts).
|
|
90
90
|
|
|
91
|
-
`test/fixtures/api-error`, `test/fixtures/canton-id
|
|
91
|
+
`test/fixtures/api-error`, `test/fixtures/canton-id`, `test/fixtures/agreement-sig` and `test/fixtures/approval` hold copies of the Go goldens, from the commit in `SOURCE`. After changing the Go package, run `npm run sync:api-error-fixtures`; CI fails if the copies drift.
|
|
92
92
|
|
|
93
93
|
---
|
|
94
94
|
|
|
95
95
|
### Pending approvals (`sdk/pending-approvals`)
|
|
96
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).
|
|
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). `decisionsFor` is tested against daml-escrow-commons `approval/testdata/decisions.json`, the server's rule.
|
|
98
|
+
|
|
99
|
+
An act that isn't a proposal (daml-escrow PLAN.md Phase 92.5: a staged release its approvers approve, a compliance review's outcome) leaves `proposedBy` out and passes its own `actions` (`{ id, label, tone?, reason?: 'required' | 'optional' | 'none', confirm?, approved? }`) in place of approve / reject / withdraw: each confirms, asks for a reason as it says, then calls `onAction(item, actionId, reason)`. Someone who has approved is offered only the actions marked `approved: true` (e.g. revoking their approval), which nobody else sees (`actionsFor`).
|
|
98
100
|
|
|
99
101
|
---
|
|
100
102
|
|
package/package.json
CHANGED
|
@@ -10,6 +10,12 @@
|
|
|
10
10
|
// withdraws; someone who has approved does nothing more; anyone else may
|
|
11
11
|
// approve or reject. Without `me` all three decisions show (operators,
|
|
12
12
|
// tests) and the server's refusal is the answer.
|
|
13
|
+
//
|
|
14
|
+
// An act that isn't a proposal (daml-escrow PLAN.md Phase 92.5: a staged
|
|
15
|
+
// release its approvers approve, a compliance review's outcome) names no
|
|
16
|
+
// proposer and brings its own actions instead: each with its label, tone and
|
|
17
|
+
// whether it asks for a reason. Someone who has approved is offered only the
|
|
18
|
+
// actions marked for them (revoking their approval, say).
|
|
13
19
|
|
|
14
20
|
import { confirmDialog, promptDialog } from './dialogs';
|
|
15
21
|
import { renderCantonId } from './canton-id-view';
|
|
@@ -21,9 +27,9 @@ export type ApprovalDecision = 'approve' | 'reject' | 'withdraw';
|
|
|
21
27
|
export interface PendingApproval {
|
|
22
28
|
/** The request's id (what the decision is sent for). */
|
|
23
29
|
id: string;
|
|
24
|
-
/** Who proposed it: also its first approval. */
|
|
25
|
-
proposedBy
|
|
26
|
-
/** Who approved so far, in order, the proposer first. */
|
|
30
|
+
/** Who proposed it: also its first approval. Empty or absent: nobody did (not shown). */
|
|
31
|
+
proposedBy?: string;
|
|
32
|
+
/** Who approved so far, in order, the proposer (if any) first. */
|
|
27
33
|
approvals: string[];
|
|
28
34
|
/** How many approvals the act needs, the proposer's included. */
|
|
29
35
|
required: number;
|
|
@@ -36,6 +42,24 @@ export interface PendingApproval {
|
|
|
36
42
|
note?: string;
|
|
37
43
|
/** Approve isn't offered: the act can't stand, or the viewer is its subject. */
|
|
38
44
|
noApprove?: boolean;
|
|
45
|
+
/** Its own actions in place of approve / reject / withdraw (sent through onAction). */
|
|
46
|
+
actions?: ApprovalAction[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** An act's own action, e.g. "approve" a release or "cleared" a review. */
|
|
50
|
+
export interface ApprovalAction {
|
|
51
|
+
/** What is sent (onAction's actionId). */
|
|
52
|
+
id: string;
|
|
53
|
+
/** The button's text. */
|
|
54
|
+
label: string;
|
|
55
|
+
/** "danger" asks with the danger confirmation (default "approve"). */
|
|
56
|
+
tone?: 'approve' | 'danger';
|
|
57
|
+
/** Whether a reason is asked: required (default), optional, or none (confirm only). */
|
|
58
|
+
reason?: 'required' | 'optional' | 'none';
|
|
59
|
+
/** The confirmation's question (default cx.approvals.confirm with the label). */
|
|
60
|
+
confirm?: string;
|
|
61
|
+
/** Offered only to someone who has approved (e.g. revoking their approval), and to nobody else. */
|
|
62
|
+
approved?: boolean;
|
|
39
63
|
}
|
|
40
64
|
|
|
41
65
|
export interface PendingApprovalsOptions {
|
|
@@ -43,6 +67,8 @@ export interface PendingApprovalsOptions {
|
|
|
43
67
|
me?: string;
|
|
44
68
|
/** Sends a decision, with the reason given; the list doesn't change itself (the app reloads). */
|
|
45
69
|
onDecide: (item: PendingApproval, decision: ApprovalDecision, reason: string) => void | Promise<void>;
|
|
70
|
+
/** Sends one of an item's own actions, with the reason given ('' when none was asked or given). */
|
|
71
|
+
onAction?: (item: PendingApproval, actionId: string, reason: string) => void | Promise<void>;
|
|
46
72
|
/** Shows a party (default: the shared Canton id display). */
|
|
47
73
|
party?: (party: string) => Node;
|
|
48
74
|
/** Text when nothing is pending (default cx.approvals.empty). */
|
|
@@ -76,11 +102,30 @@ function el<K extends keyof HTMLElementTagNameMap>(tag: K, className = '', text?
|
|
|
76
102
|
/** The decisions viewer `me` is offered on item (all three without `me`). */
|
|
77
103
|
export function decisionsFor(item: PendingApproval, me?: string): ApprovalDecision[] {
|
|
78
104
|
if (!me) return item.noApprove ? ['reject', 'withdraw'] : ['approve', 'reject', 'withdraw'];
|
|
79
|
-
if (me === item.proposedBy) return ['withdraw'];
|
|
105
|
+
if (item.proposedBy && me === item.proposedBy) return ['withdraw'];
|
|
80
106
|
if (item.approvals.includes(me)) return [];
|
|
81
107
|
return item.noApprove ? ['reject'] : ['approve', 'reject'];
|
|
82
108
|
}
|
|
83
109
|
|
|
110
|
+
/** The item's own actions viewer `me` is offered: once they have approved, only those marked `approved`. */
|
|
111
|
+
export function actionsFor(item: PendingApproval, me?: string): ApprovalAction[] {
|
|
112
|
+
const done = !!me && item.approvals.includes(me);
|
|
113
|
+
return (item.actions ?? []).filter((a) => !!a.approved === done);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// act confirms, asks for the reason the action wants, then hands it to the app.
|
|
117
|
+
async function act(t: Translator, item: PendingApproval, a: ApprovalAction, opts: PendingApprovalsOptions): Promise<void> {
|
|
118
|
+
if (!(await confirmDialog(a.confirm ?? t.t('cx.approvals.confirm', { decision: a.label }), { confirmLabel: a.label, danger: a.tone === 'danger' }))) return;
|
|
119
|
+
const mode = a.reason ?? 'required';
|
|
120
|
+
let reason = '';
|
|
121
|
+
if (mode !== 'none') {
|
|
122
|
+
const given = await promptDialog(t.t('cx.approvals.reasonPrompt'), { label: t.t('cx.approvals.reason'), required: mode === 'required' });
|
|
123
|
+
if (given === null || (mode === 'required' && !given)) return;
|
|
124
|
+
reason = given;
|
|
125
|
+
}
|
|
126
|
+
await opts.onAction?.(item, a.id, reason);
|
|
127
|
+
}
|
|
128
|
+
|
|
84
129
|
// decide confirms, asks for the reason, then hands the decision to the app.
|
|
85
130
|
async function decide(t: Translator, item: PendingApproval, d: ApprovalDecision, opts: PendingApprovalsOptions): Promise<void> {
|
|
86
131
|
const label = t.t(`cx.approvals.${d}`);
|
|
@@ -104,7 +149,9 @@ function renderItem(t: Translator, item: PendingApproval, opts: PendingApprovals
|
|
|
104
149
|
const count = Math.min(item.approvals.length, item.required);
|
|
105
150
|
const meta = el('div', META);
|
|
106
151
|
if (item.kindLabel) meta.append(el('span', TAG, item.kindLabel));
|
|
107
|
-
|
|
152
|
+
const progress = t.t('cx.approvals.progress', { count, required: item.required });
|
|
153
|
+
if (item.proposedBy) meta.append(t.t('cx.approvals.proposedBy') + ' ', party(item.proposedBy), ` · ${progress}`);
|
|
154
|
+
else meta.append(progress);
|
|
108
155
|
body.append(meta);
|
|
109
156
|
|
|
110
157
|
const bar = el('div', BAR);
|
|
@@ -129,12 +176,22 @@ function renderItem(t: Translator, item: PendingApproval, opts: PendingApprovals
|
|
|
129
176
|
body.append(details);
|
|
130
177
|
|
|
131
178
|
const actions = el('div', 'flex gap-2');
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
179
|
+
if (item.actions) {
|
|
180
|
+
for (const a of actionsFor(item, opts.me)) {
|
|
181
|
+
const b = el('button', `${BUTTON} ${BUTTON_TONE[a.tone === 'danger' ? 'reject' : 'approve']}`, a.label);
|
|
182
|
+
b.type = 'button';
|
|
183
|
+
b.dataset.approvalAction = a.id;
|
|
184
|
+
b.addEventListener('click', () => void act(t, item, a, opts));
|
|
185
|
+
actions.append(b);
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
for (const d of decisionsFor(item, opts.me)) {
|
|
189
|
+
const b = el('button', `${BUTTON} ${BUTTON_TONE[d]}`, t.t(`cx.approvals.${d}`));
|
|
190
|
+
b.type = 'button';
|
|
191
|
+
b.dataset.approvalDecision = d;
|
|
192
|
+
b.addEventListener('click', () => void decide(t, item, d, opts));
|
|
193
|
+
actions.append(b);
|
|
194
|
+
}
|
|
138
195
|
}
|
|
139
196
|
li.append(body, actions);
|
|
140
197
|
return li;
|