feat: integrate nstart (#33)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user