@panneau/actions 5.0.0-alpha.1 → 5.0.0-alpha.11

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.
@@ -4,8 +4,10 @@ import { buildActionRequest, getLinkTarget, isFinished } from "./request.js";
4
4
  import { describeResult } from "./describe.js";
5
5
  import { ResultsDialog } from "./ResultsDialog.js";
6
6
  import { ActionRunnerContext } from "./context.js";
7
+ import { c } from "react/compiler-runtime";
7
8
  import { useEffect, useEffectEvent, useRef, useState } from "react";
8
9
  import { usePlugins } from "@panneau/core";
10
+ import { getActionLabel } from "@panneau/intl";
9
11
  import { usePanneauIntl, useToast } from "@panneau/ui";
10
12
  import { jsx, jsxs } from "react/jsx-runtime";
11
13
  //#region src/ActionRunnerProvider.tsx
@@ -30,214 +32,355 @@ function openDownload(download) {
30
32
  * redirects and opens downloads. Put it inside `PanneauRoot`, and above the router of an app, so
31
33
  * that runs are followed from page to page. `useActionRunner(resource)` runs an action.
32
34
  */
33
- function ActionRunnerProvider({ adapter, children, onResult, onNavigate = navigateTo, onDownload = openDownload, clientActions, pollInterval = 2e3, locales }) {
35
+ function ActionRunnerProvider(t0) {
36
+ const $ = c(61);
37
+ const { adapter, children, onResult, onNavigate: t1, onDownload: t2, clientActions, pollInterval: t3, locales } = t0;
38
+ const onNavigate = t1 === void 0 ? navigateTo : t1;
39
+ const onDownload = t2 === void 0 ? openDownload : t2;
40
+ const pollInterval = t3 === void 0 ? 2e3 : t3;
34
41
  const intl = usePanneauIntl();
35
42
  const toasts = useToast();
36
43
  const plugins = usePlugins();
37
44
  const [parameters, setParameters] = useState(null);
38
45
  const [client, setClient] = useState(null);
39
46
  const [details, setDetails] = useState(null);
40
- const [runs, setRuns] = useState([]);
41
- const notified = useRef(/* @__PURE__ */ new Set());
42
- const finish = async (resource, target, result) => {
43
- await onResult?.(result, {
44
- resource,
45
- target
46
- });
47
- const content = describeResult(result, target.action.label, intl);
48
- const hasDetails = (result.results ?? []).length > 0;
49
- const download = result.download ?? null;
50
- if (download !== null) onDownload(download);
51
- toasts.show({
52
- ...content,
53
- ...hasDetails ? { action: {
54
- label: intl.formatMessage(messages.details),
55
- onAction: () => setDetails({
56
- label: target.action.label,
57
- result
58
- })
59
- } } : download !== null ? { action: {
60
- label: intl.formatMessage(messages.download),
61
- onAction: () => onDownload(download)
62
- } } : {}
63
- });
64
- const redirect = result.redirect ?? null;
65
- if (redirect !== null) onNavigate(redirect);
66
- };
67
- const follow = (resource_0, target_0, run) => {
68
- setRuns((current) => [...current.filter((entry) => entry.run.id !== run.id), {
69
- run,
70
- resource: resource_0,
71
- target: target_0
72
- }]);
73
- toasts.show({
74
- title: intl.formatMessage(messages.queued, { action: target_0.action.label }),
75
- description: intl.formatMessage(messages.queuedDescription),
76
- tone: "info"
77
- });
78
- };
79
- const ended = async (entry_0) => {
80
- const { run: run_0, resource: resource_1, target: target_1 } = entry_0;
81
- if (notified.current.has(run_0.id)) return;
82
- notified.current.add(run_0.id);
83
- if (run_0.result !== void 0) await finish(resource_1, target_1, run_0.result);
84
- else if (run_0.status === "cancelled") toasts.show({ title: intl.formatMessage(messages.cancelled, { action: target_1.action.label }) });
85
- else if (run_0.status === "failed") toasts.show({
86
- title: intl.formatMessage(messages.failed, { action: target_1.action.label }),
87
- tone: "danger"
88
- });
89
- else toasts.show({
90
- title: intl.formatMessage(messages.done, { action: target_1.action.label }),
91
- tone: "success"
92
- });
93
- };
94
- const execute = async (resource_2, target_2, values = {}) => {
95
- const response = await adapter.runAction(resource_2, target_2.action.id, buildActionRequest(target_2, values));
96
- if (response.run !== void 0) {
97
- follow(resource_2, target_2, response.run);
47
+ let t4;
48
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
49
+ t4 = [];
50
+ $[0] = t4;
51
+ } else t4 = $[0];
52
+ const [runs, setRuns] = useState(t4);
53
+ let t5;
54
+ if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
55
+ t5 = /* @__PURE__ */ new Set();
56
+ $[1] = t5;
57
+ } else t5 = $[1];
58
+ const notified = useRef(t5);
59
+ let t6;
60
+ if ($[2] !== intl || $[3] !== onDownload || $[4] !== onNavigate || $[5] !== onResult || $[6] !== toasts) {
61
+ t6 = async (resource, target, result) => {
62
+ await onResult?.(result, {
63
+ resource,
64
+ target
65
+ });
66
+ const content = describeResult(result, getActionLabel(target.action, intl), intl);
67
+ const hasDetails = (result.results ?? []).length > 0;
68
+ const download = result.download ?? null;
69
+ if (download !== null) onDownload(download);
70
+ toasts.show({
71
+ ...content,
72
+ ...hasDetails ? { action: {
73
+ label: intl.formatMessage(messages.details),
74
+ onAction: () => setDetails({
75
+ label: getActionLabel(target.action, intl),
76
+ result
77
+ })
78
+ } } : download !== null ? { action: {
79
+ label: intl.formatMessage(messages.download),
80
+ onAction: () => onDownload(download)
81
+ } } : {}
82
+ });
83
+ const redirect = result.redirect ?? null;
84
+ if (redirect !== null) onNavigate(redirect);
85
+ };
86
+ $[2] = intl;
87
+ $[3] = onDownload;
88
+ $[4] = onNavigate;
89
+ $[5] = onResult;
90
+ $[6] = toasts;
91
+ $[7] = t6;
92
+ } else t6 = $[7];
93
+ const finish = t6;
94
+ let t7;
95
+ if ($[8] !== intl || $[9] !== toasts) {
96
+ t7 = (resource_0, target_0, run) => {
97
+ setRuns((current) => [...current.filter((entry) => entry.run.id !== run.id), {
98
+ run,
99
+ resource: resource_0,
100
+ target: target_0
101
+ }]);
102
+ toasts.show({
103
+ title: intl.formatMessage(messages.queued, { action: getActionLabel(target_0.action, intl) }),
104
+ description: intl.formatMessage(messages.queuedDescription),
105
+ tone: "info"
106
+ });
107
+ };
108
+ $[8] = intl;
109
+ $[9] = toasts;
110
+ $[10] = t7;
111
+ } else t7 = $[10];
112
+ const follow = t7;
113
+ let t8;
114
+ if ($[11] !== finish || $[12] !== intl || $[13] !== toasts) {
115
+ t8 = async (entry_0) => {
116
+ const { run: run_0, resource: resource_1, target: target_1 } = entry_0;
117
+ if (notified.current.has(run_0.id)) return;
118
+ notified.current.add(run_0.id);
119
+ if (run_0.result !== void 0) await finish(resource_1, target_1, run_0.result);
120
+ else if (run_0.status === "cancelled") toasts.show({ title: intl.formatMessage(messages.cancelled, { action: getActionLabel(target_1.action, intl) }) });
121
+ else if (run_0.status === "failed") toasts.show({
122
+ title: intl.formatMessage(messages.failed, { action: getActionLabel(target_1.action, intl) }),
123
+ tone: "danger"
124
+ });
125
+ else toasts.show({
126
+ title: intl.formatMessage(messages.done, { action: getActionLabel(target_1.action, intl) }),
127
+ tone: "success"
128
+ });
129
+ };
130
+ $[11] = finish;
131
+ $[12] = intl;
132
+ $[13] = toasts;
133
+ $[14] = t8;
134
+ } else t8 = $[14];
135
+ const ended = t8;
136
+ let t9;
137
+ if ($[15] !== adapter || $[16] !== finish || $[17] !== follow) {
138
+ t9 = async (resource_2, target_2, t10) => {
139
+ const values = t10 === void 0 ? {} : t10;
140
+ const response = await adapter.runAction(resource_2, target_2.action.id, buildActionRequest(target_2, values));
141
+ if (response.run !== void 0) {
142
+ follow(resource_2, target_2, response.run);
143
+ return {
144
+ status: "queued",
145
+ run: response.run
146
+ };
147
+ }
148
+ await finish(resource_2, target_2, response.result);
98
149
  return {
99
- status: "queued",
100
- run: response.run
150
+ status: "done",
151
+ result: response.result
101
152
  };
102
- }
103
- await finish(resource_2, target_2, response.result);
104
- return {
105
- status: "done",
106
- result: response.result
107
153
  };
108
- };
109
- const run_1 = (resource_3, target_3) => {
110
- const { action } = target_3;
111
- if (action.kind === "link") {
112
- const to = getLinkTarget(target_3);
113
- if (to === null) return Promise.reject(/* @__PURE__ */ new Error(`The action "${action.id}" has no URL or route.`));
114
- onNavigate(to);
115
- return Promise.resolve({
116
- status: "navigated",
117
- to
118
- });
119
- }
120
- if (action.kind === "client") {
121
- const name = action.component ?? "";
122
- const Handler = (clientActions !== void 0 && Object.hasOwn(clientActions, name) ? clientActions[name] : void 0) ?? plugins.actions.get(name);
123
- if (Handler === void 0) return Promise.reject(/* @__PURE__ */ new Error(`No handler for the client action "${action.id}" (${name}).`));
124
- return new Promise((resolve) => setClient({
154
+ $[15] = adapter;
155
+ $[16] = finish;
156
+ $[17] = follow;
157
+ $[18] = t9;
158
+ } else t9 = $[18];
159
+ const execute = t9;
160
+ let t10;
161
+ if ($[19] !== clientActions || $[20] !== execute || $[21] !== onNavigate || $[22] !== plugins) {
162
+ t10 = (resource_3, target_3) => {
163
+ const { action } = target_3;
164
+ if (action.kind === "link") {
165
+ const to = getLinkTarget(target_3);
166
+ if (to === null) return Promise.reject(/* @__PURE__ */ new Error(`The action "${action.id}" has no URL or route.`));
167
+ onNavigate(to);
168
+ return Promise.resolve({
169
+ status: "navigated",
170
+ to
171
+ });
172
+ }
173
+ if (action.kind === "client") {
174
+ const name = action.component ?? "";
175
+ const Handler = (clientActions !== void 0 && Object.hasOwn(clientActions, name) ? clientActions[name] : void 0) ?? plugins.actions.get(name);
176
+ if (Handler === void 0) return Promise.reject(/* @__PURE__ */ new Error(`No handler for the client action "${action.id}" (${name}).`));
177
+ return new Promise((resolve) => setClient({
178
+ resource: resource_3,
179
+ target: target_3,
180
+ resolve,
181
+ Handler
182
+ }));
183
+ }
184
+ if ((action.fields ?? []).length > 0) return new Promise((resolve_0) => setParameters({
125
185
  resource: resource_3,
126
186
  target: target_3,
127
- resolve,
128
- Handler
129
- }));
130
- }
131
- if ((action.fields ?? []).length > 0) return new Promise((resolve_0) => setParameters({
132
- resource: resource_3,
133
- target: target_3,
134
- resolve: resolve_0
135
- }));
136
- return execute(resource_3, target_3);
137
- };
138
- const cancelRun = async (id) => {
139
- if (adapter.cancelActionRun === void 0) throw new Error("The adapter cannot cancel runs.");
140
- await adapter.cancelActionRun(id);
141
- const updated = await adapter.getActionRun(id);
142
- setRuns((current_0) => current_0.map((entry_1) => entry_1.run.id === id ? {
143
- ...entry_1,
144
- run: updated
145
- } : entry_1));
146
- const entry_2 = runs.find((candidate) => candidate.run.id === id);
147
- if (entry_2 !== void 0 && isFinished(updated.status)) await ended({
148
- ...entry_2,
149
- run: updated
150
- });
151
- };
152
- const dismissRun = (id_0) => setRuns((current_1) => current_1.filter((entry_3) => entry_3.run.id !== id_0));
153
- const onRunEnded = useEffectEvent((entry_4) => ended(entry_4));
154
- useEffect(() => {
155
- const active = runs.filter((entry_5) => !isFinished(entry_5.run.status));
156
- if (active.length === 0) return;
157
- let stopped = false;
158
- const timer = setTimeout(() => {
159
- Promise.all(active.map(async (entry_6) => {
160
- try {
161
- const next = await adapter.getActionRun(entry_6.run.id);
162
- if (stopped) return;
163
- setRuns((current_3) => current_3.map((candidate_0) => candidate_0.run.id === next.id ? {
164
- ...candidate_0,
165
- run: next
166
- } : candidate_0));
167
- if (isFinished(next.status)) await onRunEnded({
168
- ...entry_6,
169
- run: next
170
- });
171
- } catch {
172
- if (!stopped) setRuns((current_2) => [...current_2]);
173
- }
187
+ resolve: resolve_0
174
188
  }));
175
- }, pollInterval);
176
- return () => {
177
- stopped = true;
178
- clearTimeout(timer);
189
+ return execute(resource_3, target_3);
179
190
  };
180
- }, [
181
- runs,
182
- adapter,
183
- pollInterval
184
- ]);
185
- const value = {
186
- adapter,
187
- run: run_1,
188
- runs,
189
- cancelRun,
190
- dismissRun
191
- };
192
- const ClientHandler = client?.Handler;
193
- return /* @__PURE__ */ jsxs(ActionRunnerContext, {
194
- value,
195
- children: [
196
- children,
197
- parameters !== null ? /* @__PURE__ */ jsx(ParametersDialog, {
198
- action: parameters.target.action,
199
- count: parameters.target.count,
200
- locales,
201
- onSubmit: (values_0) => execute(parameters.resource, parameters.target, values_0),
202
- onDone: (outcome) => {
203
- setParameters(null);
204
- parameters.resolve(outcome);
205
- },
206
- onCancel: () => {
207
- setParameters(null);
208
- parameters.resolve(false);
209
- }
210
- }) : null,
211
- client !== null && ClientHandler !== void 0 ? /* @__PURE__ */ jsx(ClientHandler, {
212
- action: client.target.action,
213
- resource: client.resource,
214
- target: client.target,
215
- onDone: (result_0) => {
216
- setClient(null);
217
- if (result_0 === void 0) {
218
- client.resolve({
219
- status: "done",
220
- result: {}
191
+ $[19] = clientActions;
192
+ $[20] = execute;
193
+ $[21] = onNavigate;
194
+ $[22] = plugins;
195
+ $[23] = t10;
196
+ } else t10 = $[23];
197
+ const run_1 = t10;
198
+ let t11;
199
+ if ($[24] !== adapter || $[25] !== ended || $[26] !== runs) {
200
+ t11 = async (id) => {
201
+ if (adapter.cancelActionRun === void 0) throw new Error("The adapter cannot cancel runs.");
202
+ await adapter.cancelActionRun(id);
203
+ const updated = await adapter.getActionRun(id);
204
+ setRuns((current_0) => current_0.map((entry_1) => entry_1.run.id === id ? {
205
+ ...entry_1,
206
+ run: updated
207
+ } : entry_1));
208
+ const entry_2 = runs.find((candidate) => candidate.run.id === id);
209
+ if (entry_2 !== void 0 && isFinished(updated.status)) await ended({
210
+ ...entry_2,
211
+ run: updated
212
+ });
213
+ };
214
+ $[24] = adapter;
215
+ $[25] = ended;
216
+ $[26] = runs;
217
+ $[27] = t11;
218
+ } else t11 = $[27];
219
+ const cancelRun = t11;
220
+ let t12;
221
+ if ($[28] === Symbol.for("react.memo_cache_sentinel")) {
222
+ t12 = (id_0) => setRuns((current_1) => current_1.filter((entry_3) => entry_3.run.id !== id_0));
223
+ $[28] = t12;
224
+ } else t12 = $[28];
225
+ const dismissRun = t12;
226
+ let t13;
227
+ if ($[29] !== ended) {
228
+ t13 = (entry_4) => ended(entry_4);
229
+ $[29] = ended;
230
+ $[30] = t13;
231
+ } else t13 = $[30];
232
+ const onRunEnded = useEffectEvent(t13);
233
+ let t14;
234
+ if ($[31] !== adapter || $[32] !== onRunEnded || $[33] !== pollInterval || $[34] !== runs) {
235
+ t14 = () => {
236
+ const active = runs.filter(_temp);
237
+ if (active.length === 0) return;
238
+ let stopped = false;
239
+ const timer = setTimeout(() => {
240
+ Promise.all(active.map(async (entry_6) => {
241
+ try {
242
+ const next = await adapter.getActionRun(entry_6.run.id);
243
+ if (stopped) return;
244
+ setRuns((current_3) => current_3.map((candidate_0) => candidate_0.run.id === next.id ? {
245
+ ...candidate_0,
246
+ run: next
247
+ } : candidate_0));
248
+ if (isFinished(next.status)) await onRunEnded({
249
+ ...entry_6,
250
+ run: next
221
251
  });
222
- return;
252
+ } catch {
253
+ if (!stopped) setRuns(_temp2);
223
254
  }
224
- finish(client.resource, client.target, result_0).then(() => client.resolve({
255
+ }));
256
+ }, pollInterval);
257
+ return () => {
258
+ stopped = true;
259
+ clearTimeout(timer);
260
+ };
261
+ };
262
+ $[31] = adapter;
263
+ $[32] = onRunEnded;
264
+ $[33] = pollInterval;
265
+ $[34] = runs;
266
+ $[35] = t14;
267
+ } else t14 = $[35];
268
+ let t15;
269
+ if ($[36] !== adapter || $[37] !== pollInterval || $[38] !== runs) {
270
+ t15 = [
271
+ runs,
272
+ adapter,
273
+ pollInterval
274
+ ];
275
+ $[36] = adapter;
276
+ $[37] = pollInterval;
277
+ $[38] = runs;
278
+ $[39] = t15;
279
+ } else t15 = $[39];
280
+ useEffect(t14, t15);
281
+ let t16;
282
+ if ($[40] !== adapter || $[41] !== cancelRun || $[42] !== run_1 || $[43] !== runs) {
283
+ t16 = {
284
+ adapter,
285
+ run: run_1,
286
+ runs,
287
+ cancelRun,
288
+ dismissRun
289
+ };
290
+ $[40] = adapter;
291
+ $[41] = cancelRun;
292
+ $[42] = run_1;
293
+ $[43] = runs;
294
+ $[44] = t16;
295
+ } else t16 = $[44];
296
+ const value = t16;
297
+ const ClientHandler = client?.Handler;
298
+ let t17;
299
+ if ($[45] !== execute || $[46] !== locales || $[47] !== parameters) {
300
+ t17 = parameters !== null ? /* @__PURE__ */ jsx(ParametersDialog, {
301
+ action: parameters.target.action,
302
+ count: parameters.target.count,
303
+ locales,
304
+ onSubmit: (values_0) => execute(parameters.resource, parameters.target, values_0),
305
+ onDone: (outcome) => {
306
+ setParameters(null);
307
+ parameters.resolve(outcome);
308
+ },
309
+ onCancel: () => {
310
+ setParameters(null);
311
+ parameters.resolve(false);
312
+ }
313
+ }) : null;
314
+ $[45] = execute;
315
+ $[46] = locales;
316
+ $[47] = parameters;
317
+ $[48] = t17;
318
+ } else t17 = $[48];
319
+ let t18;
320
+ if ($[49] !== ClientHandler || $[50] !== client || $[51] !== finish) {
321
+ t18 = client !== null && ClientHandler !== void 0 ? /* @__PURE__ */ jsx(ClientHandler, {
322
+ action: client.target.action,
323
+ resource: client.resource,
324
+ target: client.target,
325
+ onDone: (result_0) => {
326
+ setClient(null);
327
+ if (result_0 === void 0) {
328
+ client.resolve({
225
329
  status: "done",
226
- result: result_0
227
- }));
228
- },
229
- onCancel: () => {
230
- setClient(null);
231
- client.resolve(false);
330
+ result: {}
331
+ });
332
+ return;
232
333
  }
233
- }) : null,
234
- details !== null ? /* @__PURE__ */ jsx(ResultsDialog, {
235
- label: details.label,
236
- result: details.result,
237
- onClose: () => setDetails(null)
238
- }) : null
239
- ]
240
- });
334
+ finish(client.resource, client.target, result_0).then(() => client.resolve({
335
+ status: "done",
336
+ result: result_0
337
+ }));
338
+ },
339
+ onCancel: () => {
340
+ setClient(null);
341
+ client.resolve(false);
342
+ }
343
+ }) : null;
344
+ $[49] = ClientHandler;
345
+ $[50] = client;
346
+ $[51] = finish;
347
+ $[52] = t18;
348
+ } else t18 = $[52];
349
+ let t19;
350
+ if ($[53] !== details) {
351
+ t19 = details !== null ? /* @__PURE__ */ jsx(ResultsDialog, {
352
+ label: details.label,
353
+ result: details.result,
354
+ onClose: () => setDetails(null)
355
+ }) : null;
356
+ $[53] = details;
357
+ $[54] = t19;
358
+ } else t19 = $[54];
359
+ let t20;
360
+ if ($[55] !== children || $[56] !== t17 || $[57] !== t18 || $[58] !== t19 || $[59] !== value) {
361
+ t20 = /* @__PURE__ */ jsxs(ActionRunnerContext, {
362
+ value,
363
+ children: [
364
+ children,
365
+ t17,
366
+ t18,
367
+ t19
368
+ ]
369
+ });
370
+ $[55] = children;
371
+ $[56] = t17;
372
+ $[57] = t18;
373
+ $[58] = t19;
374
+ $[59] = value;
375
+ $[60] = t20;
376
+ } else t20 = $[60];
377
+ return t20;
378
+ }
379
+ function _temp2(current_2) {
380
+ return [...current_2];
381
+ }
382
+ function _temp(entry_5) {
383
+ return !isFinished(entry_5.run.status);
241
384
  }
242
385
  //#endregion
243
386
  export { ActionRunnerProvider };