feat: integrate nstart (#33)

This commit is contained in:
Cody Tseng
2025-01-29 15:32:26 +08:00
committed by GitHub
parent 7daa566cec
commit a264b747e7
15 changed files with 154 additions and 47 deletions

View File

@@ -105,10 +105,19 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
}
const onPopState = (e: PopStateEvent) => {
const state = e.state ?? { index: -1, url: '/' }
let state = e.state as { index: number; url: string } | null
setSecondaryStack((pre) => {
const currentItem = pre[pre.length - 1] as TStackItem | undefined
const currentIndex = currentItem?.index
if (!state) {
if (window.location.pathname + window.location.search + window.location.hash !== '/') {
// Just change the URL
return pre
} else {
// Back to root
state = { index: -1, url: '/' }
}
}
// Go forward
if (currentIndex === undefined || state.index > currentIndex) {
@@ -124,7 +133,7 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
}
// Go back
const newStack = pre.filter((item) => item.index <= state.index)
const newStack = pre.filter((item) => item.index <= state!.index)
const topItem = newStack[newStack.length - 1] as TStackItem | undefined
if (!topItem) {
// Create a new stack item if it's not exist (e.g. when the user refreshes the page, the stack will be empty)