fix: 🐛

This commit is contained in:
codytseng
2025-01-22 16:50:13 +08:00
parent e9ebf1bc00
commit c92545a22d
2 changed files with 47 additions and 25 deletions

View File

@@ -70,36 +70,55 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
useEffect(() => { useEffect(() => {
if (window.location.pathname !== '/') { if (window.location.pathname !== '/') {
pushSecondaryPage(window.location.pathname + window.location.search + window.location.hash) const url = window.location.pathname + window.location.search + window.location.hash
setSecondaryStack((prevStack) => {
if (isCurrentPage(prevStack, url)) return prevStack
const { newStack } = pushNewPageToStack(
prevStack,
url,
maxStackSize,
window.history.state?.index
)
return newStack
})
} }
const onPopState = (e: PopStateEvent) => { const onPopState = (e: PopStateEvent) => {
const state = e.state ?? { index: -1, url: '/' } const state = e.state ?? { index: -1, url: '/' }
setSecondaryStack((pre) => { setSecondaryStack((pre) => {
const currentItem = pre[pre.length - 1] const currentItem = pre[pre.length - 1] as TStackItem | undefined
const currentIndex = currentItem ? currentItem.index : 0 const currentIndex = currentItem?.index
// Go forward
if (currentIndex === undefined || state.index > currentIndex) {
const { newStack } = pushNewPageToStack(pre, state.url, maxStackSize)
return newStack
}
if (state.index === currentIndex) { if (state.index === currentIndex) {
if (currentIndex !== 0) return pre if (currentIndex !== 0) return pre
window.history.replaceState(null, '', '/') window.history.replaceState(null, '', '/')
return [] return []
} }
// Go back
if (state.index < currentIndex) {
const newStack = pre.filter((item) => item.index <= state.index)
const topItem = newStack[newStack.length - 1]
// Load the component if it's not cached
if (topItem && !topItem.component) {
topItem.component = findAndCreateComponent(topItem.url, state.index)
}
if (newStack.length === 0) {
window.history.replaceState(null, '', '/')
}
return newStack
}
// Go forward // Go back
const { newStack } = pushNewPageToStack(pre, state.url, maxStackSize) 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)
const newComponent = findAndCreateComponent(state.url, state.index)
if (newComponent) {
newStack.push({ index: state.index, url: state.url, component: newComponent })
}
} else if (!topItem.component) {
// Load the component if it's not cached
topItem.component = findAndCreateComponent(topItem.url, state.index)
}
if (newStack.length === 0) {
window.history.replaceState(null, '', '/')
}
return newStack return newStack
}) })
} }
@@ -122,11 +141,11 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
} }
} }
const pushSecondaryPage = (url: string) => { const pushSecondaryPage = (url: string, index?: number) => {
setSecondaryStack((prevStack) => { setSecondaryStack((prevStack) => {
if (isCurrentPage(prevStack, url)) return prevStack if (isCurrentPage(prevStack, url)) return prevStack
const { newStack, newItem } = pushNewPageToStack(prevStack, url, maxStackSize) const { newStack, newItem } = pushNewPageToStack(prevStack, url, maxStackSize, index)
if (newItem) { if (newItem) {
window.history.pushState({ index: newItem.index, url }, '', url) window.history.pushState({ index: newItem.index, url }, '', url)
} }
@@ -286,14 +305,19 @@ function findAndCreateComponent(url: string, index: number) {
return null return null
} }
function pushNewPageToStack(stack: TStackItem[], url: string, maxStackSize = 5) { function pushNewPageToStack(
stack: TStackItem[],
url: string,
maxStackSize = 5,
specificIndex?: number
) {
const currentItem = stack[stack.length - 1] const currentItem = stack[stack.length - 1]
const currentIndex = currentItem ? currentItem.index + 1 : 0 const currentIndex = specificIndex ?? (currentItem ? currentItem.index + 1 : 0)
const component = findAndCreateComponent(url, currentIndex) const component = findAndCreateComponent(url, currentIndex)
if (!component) return { newStack: stack, newItem: null } if (!component) return { newStack: stack, newItem: null }
const newItem = { component, url, index: currentItem ? currentItem.index + 1 : 0 } const newItem = { component, url, index: currentIndex }
const newStack = [...stack, newItem] const newStack = [...stack, newItem]
const lastCachedIndex = newStack.findIndex((stack) => stack.component) const lastCachedIndex = newStack.findIndex((stack) => stack.component)
// Clear the oldest cached component if there are too many cached components // Clear the oldest cached component if there are too many cached components

View File

@@ -1,7 +1,6 @@
import { match } from 'path-to-regexp' import { match } from 'path-to-regexp'
import { isValidElement } from 'react' import { isValidElement } from 'react'
import FollowingListPage from './pages/secondary/FollowingListPage' import FollowingListPage from './pages/secondary/FollowingListPage'
import HomePage from './pages/secondary/HomePage'
import MuteListPage from './pages/secondary/MuteListPage' import MuteListPage from './pages/secondary/MuteListPage'
import NoteListPage from './pages/secondary/NoteListPage' import NoteListPage from './pages/secondary/NoteListPage'
import NotePage from './pages/secondary/NotePage' import NotePage from './pages/secondary/NotePage'
@@ -14,7 +13,6 @@ import RelaySettingsPage from './pages/secondary/RelaySettingsPage'
import SettingsPage from './pages/secondary/SettingsPage' import SettingsPage from './pages/secondary/SettingsPage'
const ROUTES = [ const ROUTES = [
{ path: '/', element: <HomePage /> },
{ path: '/notes', element: <NoteListPage /> }, { path: '/notes', element: <NoteListPage /> },
{ path: '/notes/:id', element: <NotePage /> }, { path: '/notes/:id', element: <NotePage /> },
{ path: '/users', element: <ProfileListPage /> }, { path: '/users', element: <ProfileListPage /> },