mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-21 01:23:40 +00:00
feat(console): add in-app destructive-action confirmation
Deleting a route or a group used `window.confirm`, which blocks the page and ignores the console theme. A promise-based `useConfirm()` now drives one <ConfirmDialog /> built on the reka-ui AlertDialog that @elysola/ui ships; reka-ui is pre-bundled so dev keeps a single copy and the dialog stacks with the Rc dialogs. A request never outlives its asker: one made after the asking component unmounted resolves false, and one still open when it unmounts is cancelled.
This commit is contained in:
1 parent
d57e5c29c4
commit
24f5d0477c
8 files changed
+239
-7
No files matched your search
@@ -0,0 +1,148 @@
|
||||
<template>
|
||||
<AlertDialogRoot :open="!!request" @update:open="onOpenChange">
|
||||
<AlertDialogPortal>
|
||||
<AlertDialogOverlay class="confirm-overlay" />
|
||||
<AlertDialogContent class="confirm-dialog">
|
||||
<div class="confirm-body">
|
||||
<span class="confirm-icon" aria-hidden="true"><Trash2 :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">
|
||||
{{ shown.description }}
|
||||
</AlertDialogDescription>
|
||||
</div>
|
||||
</div>
|
||||
<div class="confirm-actions">
|
||||
<AlertDialogCancel as-child>
|
||||
<RcButton variant="outline" color="neutral">{{ t("confirm.cancel") }}</RcButton>
|
||||
</AlertDialogCancel>
|
||||
<RcButton color="error" @click="settle(true)">
|
||||
{{ shown?.confirmText || t("confirm.delete") }}
|
||||
</RcButton>
|
||||
</div>
|
||||
</AlertDialogContent>
|
||||
</AlertDialogPortal>
|
||||
</AlertDialogRoot>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { Trash2 } from "lucide-vue-next";
|
||||
import {
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogOverlay,
|
||||
AlertDialogPortal,
|
||||
AlertDialogRoot,
|
||||
AlertDialogTitle,
|
||||
} from "reka-ui";
|
||||
import type { ConfirmOptions } from "~/composables/useConfirm";
|
||||
|
||||
const { t } = useI18n();
|
||||
const { pending: request, settle, attachHost } = useConfirm();
|
||||
|
||||
// Keep the last request on screen while the close animation plays.
|
||||
const shown = ref<ConfirmOptions | null>(null);
|
||||
watch(
|
||||
request,
|
||||
(next) => {
|
||||
if (next) shown.value = next;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
// A request must never outlive the console that asked for it: resolving it
|
||||
// later would resume a destructive action of an unmounted component.
|
||||
let detach: (() => void) | undefined;
|
||||
onMounted(() => (detach = attachHost()));
|
||||
onBeforeUnmount(() => detach?.());
|
||||
|
||||
function onOpenChange(open: boolean): void {
|
||||
if (!open) settle(false);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.confirm-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 60;
|
||||
background: var(--wh-scrim);
|
||||
backdrop-filter: blur(4px);
|
||||
}
|
||||
.confirm-dialog {
|
||||
position: fixed;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
z-index: 60;
|
||||
display: grid;
|
||||
gap: 1.25rem;
|
||||
width: calc(100% - 2rem);
|
||||
max-width: 28rem;
|
||||
transform: translate(-50%, -50%);
|
||||
border-radius: 0.75rem;
|
||||
background: rgb(var(--wh-surface));
|
||||
box-shadow:
|
||||
inset 0 0 0 1px rgb(var(--wh-border)),
|
||||
0 20px 25px -5px rgb(0 0 0 / 0.1),
|
||||
0 8px 10px -6px rgb(0 0 0 / 0.1);
|
||||
padding: 1.5rem;
|
||||
color: rgb(var(--wh-text));
|
||||
}
|
||||
.confirm-dialog:focus {
|
||||
outline: none;
|
||||
}
|
||||
.confirm-body {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 0.875rem;
|
||||
}
|
||||
.confirm-icon {
|
||||
display: grid;
|
||||
flex: none;
|
||||
width: 2.25rem;
|
||||
height: 2.25rem;
|
||||
place-items: center;
|
||||
border-radius: 9999px;
|
||||
background: rgb(var(--wh-bad-dim));
|
||||
color: rgb(var(--wh-bad));
|
||||
}
|
||||
.confirm-title {
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
letter-spacing: -0.01em;
|
||||
line-height: 1.5rem;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.confirm-description {
|
||||
color: rgb(var(--wh-muted));
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.5rem;
|
||||
}
|
||||
.confirm-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 0.625rem;
|
||||
}
|
||||
.confirm-overlay[data-state="open"],
|
||||
.confirm-dialog[data-state="open"] {
|
||||
animation: confirm-in 150ms ease-out;
|
||||
}
|
||||
@keyframes confirm-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
||||
@media (max-width: 420px) {
|
||||
.confirm-actions {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
}
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.confirm-overlay[data-state="open"],
|
||||
.confirm-dialog[data-state="open"] {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -315,6 +315,8 @@
|
||||
@close="groupEditorOpen = false"
|
||||
@save="onSaveGroup"
|
||||
/>
|
||||
|
||||
<ConfirmDialog />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -343,6 +345,7 @@ import AdminHome from "~/components/AdminHome.vue";
|
||||
|
||||
const { t, toggle } = useI18n();
|
||||
const { push } = useToasts();
|
||||
const { confirm } = useConfirm();
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
|
||||
@@ -582,7 +585,8 @@ async function onToggle(route: Route): Promise<void> {
|
||||
async function onDelete(route: Route): Promise<void> {
|
||||
const group = selectedGroup.value;
|
||||
if (!group) return;
|
||||
if (!window.confirm(t("confirm.deleteRoute", { name: route.name || route.id }))) return;
|
||||
if (!(await confirm({ title: t("confirm.deleteRoute", { name: route.name || route.id }) })))
|
||||
return;
|
||||
try {
|
||||
await saveGroupRoutes(
|
||||
group.id,
|
||||
@@ -665,8 +669,11 @@ async function onDeleteGroup(group: Group): Promise<void> {
|
||||
} catch {
|
||||
// Count is best-effort; proceed without the warning.
|
||||
}
|
||||
const warn = used ? t("confirm.deleteGroupWarn", { n: used }) : "";
|
||||
if (!window.confirm(t("confirm.deleteGroup", { name: group.name || group.id }) + warn)) return;
|
||||
const confirmed = await confirm({
|
||||
title: t("confirm.deleteGroup", { name: group.name || group.id }),
|
||||
description: used ? t("confirm.deleteGroupWarn", { n: used }).trim() : undefined,
|
||||
});
|
||||
if (!confirmed) return;
|
||||
try {
|
||||
await saveGroups(groups.value.filter((g) => g.id !== group.id));
|
||||
push(t("toast.groupDeleted"));
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
export interface ConfirmOptions {
|
||||
/** The question, shown as the dialog title. */
|
||||
title: string;
|
||||
/** Optional consequence text under the title. */
|
||||
description?: string;
|
||||
/** Label of the destructive button (defaults to "Delete"). */
|
||||
confirmText?: string;
|
||||
}
|
||||
|
||||
interface ConfirmRequest extends ConfirmOptions {
|
||||
resolve: (confirmed: boolean) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Promise-based destructive-action confirmation shared by the whole console.
|
||||
* <ConfirmDialog /> (mounted once by ConsolePage) renders the pending request;
|
||||
* `confirm()` resolves true on confirm and false on cancel / Escape.
|
||||
*/
|
||||
export function useConfirm() {
|
||||
const pending = useState<ConfirmRequest | null>("wh-confirm", () => null);
|
||||
const hosts = useState<number>("wh-confirm-hosts", () => 0);
|
||||
// The component that asks; a request it makes after unmounting is refused,
|
||||
// and one still on screen when it unmounts is cancelled.
|
||||
const owner = getCurrentInstance();
|
||||
let asked: ConfirmRequest | null = null;
|
||||
if (owner) {
|
||||
onBeforeUnmount(() => {
|
||||
// Only our own request: another component's newer one stays open.
|
||||
if (asked && toRaw(pending.value) === asked) settle(false);
|
||||
asked = null;
|
||||
});
|
||||
}
|
||||
|
||||
function confirm(options: ConfirmOptions): Promise<boolean> {
|
||||
// Nobody can answer, or the asker is gone (the console was left while the
|
||||
// request was being prepared): never resume a stale destructive action.
|
||||
if (!hosts.value || owner?.isUnmounted) return Promise.resolve(false);
|
||||
// A newer request supersedes (and cancels) one still on screen.
|
||||
pending.value?.resolve(false);
|
||||
return new Promise((resolve) => {
|
||||
asked = { ...options, resolve };
|
||||
pending.value = asked;
|
||||
});
|
||||
}
|
||||
|
||||
function settle(confirmed: boolean): void {
|
||||
const request = pending.value;
|
||||
if (!request) return;
|
||||
pending.value = null;
|
||||
request.resolve(confirmed);
|
||||
}
|
||||
|
||||
/** Register the dialog that renders requests; the returned detach cancels any pending one. */
|
||||
function attachHost(): () => void {
|
||||
hosts.value++;
|
||||
return () => {
|
||||
hosts.value--;
|
||||
settle(false);
|
||||
};
|
||||
}
|
||||
|
||||
return { pending, confirm, settle, attachHost };
|
||||
}
|
||||
@@ -394,6 +394,8 @@ const en: Dict = {
|
||||
"confirm.deleteRoute": 'Delete route "{name}"?',
|
||||
"confirm.deleteGroup": 'Delete group "{name}"?',
|
||||
"confirm.deleteGroupWarn": " {n} route(s) reference it and will lose their group.",
|
||||
"confirm.cancel": "Cancel",
|
||||
"confirm.delete": "Delete",
|
||||
};
|
||||
|
||||
const zh: Dict = {
|
||||
@@ -782,6 +784,8 @@ const zh: Dict = {
|
||||
"confirm.deleteRoute": "确定删除路由“{name}”吗?",
|
||||
"confirm.deleteGroup": "确定删除分组“{name}”吗?",
|
||||
"confirm.deleteGroupWarn": " 有 {n} 条路由引用它,将失去所属分组。",
|
||||
"confirm.cancel": "取消",
|
||||
"confirm.delete": "删除",
|
||||
};
|
||||
|
||||
const messages: Record<Locale, Dict> = { en, zh };
|
||||
|
||||
Reference in new issue
Block a user