ux: don't nest dialogs unnecessarily after upload

Massimo Melina committed Jun 1, 2024 at 11:25 UTC ee69c48be84de56952e074534bcbab9d29f0e0da
2 files changed +11 -5
frontend/src/upload.ts
+2
@@ -430,6 +430,8 @@ function UploadStatus({ snapshot, ...props }: { snapshot?: INTERNAL_Snapshot<typ
430 s, ' – ', h(Btn, { label: t`Show details`, asText: true, onClick: showDetails }) )
431
432 function showDetails() {
433 + if (!uploadDialogIsOpen)
434 + closeDialog() // don't nest dialogs unnecessarily (apply only to the dialog outside upload-dialog)
435 alertDialog(h('div', {},
436 ([
437 [msgDone, done],
shared/dialogs.ts
+9 -5
@@ -55,9 +55,11 @@ function tabCycle(target: EventTarget | null, invert=false) {
55 return true
56 }
57
58 -function isDescendant(child: Node | null, parent: Node) {
58 +export function isDescendant(child: Node | null | undefined, parentMatch: Node | null | undefined | ((child: Node) => boolean)) {
59 + if (!parentMatch) return false
60 + const fun = typeof parentMatch === 'function'
61 while (child) {
60 - if (child === parent)
62 + if (fun ? parentMatch(child) : child === parentMatch)
63 return true
64 child = child.parentNode
65 }
@@ -175,9 +177,11 @@ export function newDialog(options: DialogOptions) {
177 options.ts = ts
178 focusBak.push(document.activeElement) // saving this inside options object doesn't work (didn't dig enough to say why)
179 options = objSameKeys(options, x => isValidElement(x) ? ref(x) : x) as typeof options // encapsulate elements as react will try to write, but valtio makes them readonly
178 - dialogs.push(options)
179 - if (options.closable !== false)
180 - history.pushState({ $dialog: $id, ts, idx: history.state.idx + 1 }, '')
180 + setTimeout(() => { // in case dialogs were just closed, account for window.history delay. This should be harmless as ux is unaffected, and programmatically you already didn't expect this to happen immediately but at state change
181 + dialogs.push(options)
182 + if (options.closable !== false)
183 + history.pushState({ $dialog: $id, ts, idx: history.state.idx + 1 }, '')
184 + }, 1)
185 return { close }
186
187 function close(v?:any) {