mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
feat(console): confirm leaving unsaved page work
Guard route changes when an editor or changed order draft is open. Cancel superseded questions and restore the shown history entry when repeated Back or Forward navigation is refused; only completed navigation resets page work.
This commit is contained in:
1 parent
535bfa98ce
commit
1c4639b07d
7 files changed
+104
-6
No files matched your search
@@ -4,7 +4,9 @@
|
||||
<AlertDialogOverlay class="confirm-overlay" />
|
||||
<AlertDialogContent class="confirm-dialog">
|
||||
<div class="confirm-body">
|
||||
<span class="confirm-icon" aria-hidden="true"><Trash2 :size="18" /></span>
|
||||
<span class="confirm-icon" aria-hidden="true"
|
||||
><component :is="KIND_ICONS[shown?.kind ?? 'delete']" :size="18"
|
||||
/></span>
|
||||
<div class="min-w-0 space-y-1.5">
|
||||
<AlertDialogTitle class="confirm-title">{{ shown?.title }}</AlertDialogTitle>
|
||||
<AlertDialogDescription v-if="shown?.description" class="confirm-description">
|
||||
@@ -26,7 +28,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { Trash2 } from "lucide-vue-next";
|
||||
import { Trash2, TriangleAlert } from "lucide-vue-next";
|
||||
import {
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
@@ -41,6 +43,11 @@ import type { ConfirmOptions } from "~/composables/useConfirm";
|
||||
const { t } = useI18n();
|
||||
const { pending: request, settle, attachHost } = useConfirm();
|
||||
|
||||
const KIND_ICONS = {
|
||||
delete: Trash2,
|
||||
leave: TriangleAlert,
|
||||
} satisfies Record<NonNullable<ConfirmOptions["kind"]>, typeof Trash2>;
|
||||
|
||||
// Keep the last request on screen while the close animation plays.
|
||||
const shown = ref<ConfirmOptions | null>(null);
|
||||
watch(
|
||||
|
||||
@@ -413,7 +413,7 @@ import AdminHome from "~/components/AdminHome.vue";
|
||||
|
||||
const { t, toggle } = useI18n();
|
||||
const { push } = useToasts();
|
||||
const { confirm } = useConfirm();
|
||||
const { confirm, settle: dismissConfirm } = useConfirm();
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
|
||||
@@ -594,6 +594,89 @@ function onMobileNavClick(event: MouseEvent): void {
|
||||
if (tab?.getAttribute("aria-selected") === "true" && routeGroupId.value) switchView("groups");
|
||||
}
|
||||
|
||||
/** Work on the current page that leaving it would throw away. */
|
||||
function hasUnsavedWork(): boolean {
|
||||
return editorOpen.value || groupEditorOpen.value || (reordering.value && orderChanged.value);
|
||||
}
|
||||
|
||||
/** Counts navigations reaching the guards; a newer one supersedes the question. */
|
||||
let navigationAttempt = 0;
|
||||
|
||||
/** Ask before a navigation throws work away; the page resets once it happened. */
|
||||
async function confirmLeave(): Promise<boolean> {
|
||||
if (!hasUnsavedWork()) return true;
|
||||
const attempt = navigationAttempt;
|
||||
const leave = await confirm({
|
||||
title: t("confirm.leaveUnsaved"),
|
||||
description: t("confirm.leaveUnsavedDesc"),
|
||||
confirmText: t("confirm.leave"),
|
||||
kind: "leave",
|
||||
});
|
||||
// Superseded while asking: let the router cancel this navigation itself.
|
||||
// Refusing it would read as "stay" and step the browser history back again,
|
||||
// on top of the newer navigation that already moved it.
|
||||
if (attempt !== navigationAttempt) return true;
|
||||
// Staying after Back/Forward: the browser has already moved. The router
|
||||
// steps back its own move; earlier, superseded moves are undone after it.
|
||||
if (!leave && historyPosition() !== shownPosition) {
|
||||
window.addEventListener("popstate", restoreShownEntry, { once: true });
|
||||
}
|
||||
return leave;
|
||||
}
|
||||
|
||||
/** vue-router's position (in `history.state`) of the current history entry. */
|
||||
function historyPosition(): number | null {
|
||||
const position = (window.history.state as { position?: unknown } | null)?.position;
|
||||
return typeof position === "number" ? position : null;
|
||||
}
|
||||
|
||||
/** History position of the page on screen, as of the last completed navigation. */
|
||||
let shownPosition = typeof window === "undefined" ? null : historyPosition();
|
||||
const stopTrackingPosition = router.afterEach((_to, _from, failure) => {
|
||||
if (!failure) shownPosition = historyPosition();
|
||||
});
|
||||
onBeforeUnmount(stopTrackingPosition);
|
||||
|
||||
/** Move the browser back to the entry on screen without starting a navigation. */
|
||||
function restoreShownEntry(): void {
|
||||
const position = historyPosition();
|
||||
if (position !== null && shownPosition !== null && position !== shownPosition) {
|
||||
router.options.history.go(shownPosition - position, false);
|
||||
}
|
||||
}
|
||||
|
||||
// A newer navigation supersedes one still waiting for an answer: that
|
||||
// question (like any other confirmation of this page) is cancelled first.
|
||||
function supersedePending(): void {
|
||||
navigationAttempt++;
|
||||
dismissConfirm(false);
|
||||
}
|
||||
|
||||
onBeforeRouteUpdate((to, from) => {
|
||||
supersedePending();
|
||||
return to.path === from.path || confirmLeave();
|
||||
});
|
||||
onBeforeRouteLeave(() => {
|
||||
supersedePending();
|
||||
return confirmLeave();
|
||||
});
|
||||
|
||||
/** Bumped by every page change: work begun on one page must not act on the next. */
|
||||
let pageVisit = 0;
|
||||
|
||||
// Only a navigation that actually happened resets the page it left: its
|
||||
// editors, a reorder draft and any confirmation still open.
|
||||
watch(
|
||||
() => route.path,
|
||||
() => {
|
||||
pageVisit++;
|
||||
dismissConfirm(false);
|
||||
editorOpen.value = false;
|
||||
groupEditorOpen.value = false;
|
||||
cancelReorder();
|
||||
},
|
||||
);
|
||||
|
||||
async function refreshOverview(): Promise<void> {
|
||||
refreshKey.value++;
|
||||
await Promise.all([loadGroups(), loadLogs(50, logFilterGroup.value || undefined), loadMetrics()]);
|
||||
|
||||
Reference in new issue
Block a user