refactor(unsaved): extract createUnsavedWarning hook and UnsavedWarningBanner

Move the identical isDirty / beforeNavigate / discard pattern out of the
three admin detail pages (groups, tags, users) into a reusable
createUnsavedWarning() hook and a UnsavedWarningBanner presentational
component.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-15 13:31:17 +02:00
parent 34e7436fdc
commit accfa5373e
6 changed files with 184 additions and 111 deletions

View File

@@ -0,0 +1,46 @@
import { beforeNavigate, goto } from '$app/navigation';
export function createUnsavedWarning() {
let isDirty = $state(false);
let showUnsavedWarning = $state(false);
let discardTarget: string | null = $state(null);
beforeNavigate(({ cancel, to }) => {
if (isDirty) {
cancel();
showUnsavedWarning = true;
discardTarget = to?.url.href ?? null;
}
});
function markDirty() {
isDirty = true;
showUnsavedWarning = false;
}
function discard() {
isDirty = false;
showUnsavedWarning = false;
if (discardTarget) goto(discardTarget);
}
function clearOnSuccess() {
isDirty = false;
showUnsavedWarning = false;
}
return {
get isDirty() {
return isDirty;
},
get showUnsavedWarning() {
return showUnsavedWarning;
},
get discardTarget() {
return discardTarget;
},
markDirty,
discard,
clearOnSuccess
};
}