Skip to content

Commit 51c7417

Browse files
committed
ux: Confirm before deleting workflows
1 parent 8867e15 commit 51c7417

File tree

4 files changed

+62
-16
lines changed

4 files changed

+62
-16
lines changed

rollup.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export default function (opts) {
9191
'RegExp',
9292
'String',
9393
'Symbol',
94+
'Swal',
9495
'TypeError',
9596
'undefined',
9697
'WeakMap',

src/lib/registries/workflow-registry.mts

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
instantiateCompressedToJsonArray
88
} from '../decorators/to-json-formatters/format-to-json-array-compressed.mjs';
99
import {WorkflowExecution} from '../execution/workflow-execution.mjs';
10+
import {alertError} from '../util/alert';
1011
import {debugLog, errorLog} from '../util/log.mjs';
1112

1213
const enum Strings {
@@ -115,16 +116,10 @@ function store(workflows: Workflow[] | readonly Workflow[]): void | never {
115116
try {
116117
ctx.accountStorage.setItem(Strings.CFG_KEY, compressArray(workflows));
117118
} catch (e) {
118-
Swal
119-
.fire({
120-
cancelButtonColor: '#d33',
121-
cancelButtonText: 'Kurwa!',
122-
showCancelButton: true,
123-
showConfirmButton: false,
124-
text: 'Melvor mods have a 8kB storage limit and we\'ve reached it. Gonna have to delete some workflows.',
125-
title: 'Failed to save',
126-
})
127-
.catch(errorLog);
119+
alertError(
120+
'Melvor mods have a 8kB storage limit and we\'ve reached it. Gonna have to delete some workflows.',
121+
'Failed to save'
122+
);
128123

129124
throw e;
130125
}

src/lib/util/alert.tsx

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {Observable} from 'rxjs';
2+
import type {SweetAlertOptions} from 'sweetalert2';
3+
4+
const baseOpts: Partial<SweetAlertOptions> = {
5+
cancelButtonColor: '#d33',
6+
cancelButtonText: 'Cancel',
7+
showCancelButton: true,
8+
showConfirmButton: true,
9+
};
10+
11+
export function alertError(text: string, title = 'Confirmation'): void {
12+
Swal
13+
.fire({
14+
...baseOpts,
15+
cancelButtonText: 'Oh no',
16+
confirmButtonText: 'OK',
17+
showConfirmButton: false,
18+
text,
19+
title,
20+
})
21+
.catch(console.error);
22+
}
23+
24+
export function alertConfirm(text: string, title = 'Confirmation'): Observable<void> {
25+
return new Observable<void>(subscriber => {
26+
Swal
27+
.fire({
28+
...baseOpts,
29+
text,
30+
title,
31+
})
32+
.then(
33+
v => {
34+
if (v.isConfirmed) {
35+
subscriber.next();
36+
}
37+
subscriber.complete();
38+
},
39+
e => {
40+
console.error(e);
41+
subscriber.complete();
42+
}
43+
);
44+
});
45+
}

src/ui/pages/workflows-dashboard.tsx

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {WorkflowEventType} from '../../lib/execution/workflow-event.mjs';
1414
import type {WorkflowExecution} from '../../lib/execution/workflow-execution.mjs';
1515
import WorkflowRegistry from '../../lib/registries/workflow-registry.mjs';
1616
import {EMPTY_ARR} from '../../lib/util.mjs';
17+
import {alertConfirm} from '../../lib/util/alert';
1718
import swapElements from '../../lib/util/swap-elements.mjs';
1819
import {BorderedBlock} from '../components/block';
1920
import Btn from '../components/btn';
@@ -296,12 +297,16 @@ const BtnsNotRunning = memo<BtnsNotRunningProps>(
296297
});
297298
}, [editedWorkflow, activeWorkflow]);
298299
const del = useCallback(() => {
299-
const activeId = activeWorkflow.peek()!.listId;
300-
batch(() => {
301-
activeWorkflow.value = undefined;
302-
reg.rmByIdx(reg.workflows.findIndex(wf => wf.listId === activeId));
303-
doRefresh();
304-
});
300+
const activeWf = activeWorkflow.peek()!;
301+
302+
alertConfirm(`Are you sure you want to delete "${activeWf.name}"?`)
303+
.subscribe(() => {
304+
batch(() => {
305+
activeWorkflow.value = undefined;
306+
reg.rmByIdx(reg.workflows.findIndex(wf => wf.listId === activeWf.listId));
307+
doRefresh();
308+
});
309+
});
305310
}, [activeWorkflow, doRefresh]);
306311
const clone = useCallback(() => {
307312
const cloned = Workflow.fromJSON(JSON.parse(JSON.stringify(activeWorkflow.peek()!)))!;

0 commit comments

Comments
 (0)