From 83e2e6e1698e677e19c24914329eb5d904a6a8ce Mon Sep 17 00:00:00 2001 From: thislight Date: Sat, 23 Nov 2024 23:58:20 +0800 Subject: [PATCH] StackedRouter: add replace: all --- src/masto/clients.ts | 2 +- src/platform/StackedRouter.tsx | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/masto/clients.ts b/src/masto/clients.ts index 43cd5ec..f853fa7 100644 --- a/src/masto/clients.ts +++ b/src/masto/clients.ts @@ -64,7 +64,7 @@ export function useSessions() { if (sessions().length > 0) return; push( "/accounts/sign-in?back=" + encodeURIComponent(location.pathname), - { replace: true }, + { replace: "all" }, ); }); diff --git a/src/platform/StackedRouter.tsx b/src/platform/StackedRouter.tsx index 8fde8ee..b20106f 100644 --- a/src/platform/StackedRouter.tsx +++ b/src/platform/StackedRouter.tsx @@ -36,9 +36,9 @@ export type NewFrameOptions = (T extends undefined } : { state: T }) & { /** - * The new frame should replace the current frame. + * The new frame should replace the current frame or all the stack. */ - replace?: boolean; + replace?: boolean | "all"; /** * The animatedOpen phase of the life cycle. * @@ -427,11 +427,19 @@ const StackedRouter: Component = (oprops) => { animateClose: opts?.animateClose, }; - mutStack(opts?.replace ? stack.length - 1 : stack.length, frame); - if (opts?.replace) { - window.history.replaceState(serializableStack(stack), "", path); + const replace = opts?.replace; + if (replace === "all") { + mutStack([frame]); } else { - window.history.pushState(serializableStack(stack), "", path); + mutStack(replace ? stack.length - 1 : stack.length, frame); + } + + const savedStack = serializableStack(stack); + + if (replace) { + window.history.replaceState(savedStack, "", path); + } else { + window.history.pushState(savedStack, "", path); } return frame; });