Skip to content

Commit 9aa6e40

Browse files
committed
make router history a bit safer
1 parent cb5b46d commit 9aa6e40

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/stores/history/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export const useHistoryStore = create(
1919
registerRoute(route) {
2020
set((s) => {
2121
s.routes.push(route);
22+
if (s.routes.length > 50) {
23+
s.routes.shift();
24+
}
2225
});
2326
},
2427
})),
@@ -40,16 +43,18 @@ export function useLastNonPlayerLink() {
4043
const routes = useHistoryStore((s) => s.routes);
4144
const location = useLocation();
4245
const lastNonPlayerLink = useMemo(() => {
43-
const reversedRoutes = [...routes];
44-
reversedRoutes.reverse();
45-
const route = reversedRoutes.find(
46-
(v) =>
46+
for (let i = routes.length - 1; i >= 0; i -= 1) {
47+
const v = routes[i];
48+
if (
4749
!v.path.startsWith("/media") && // cannot be a player link
4850
location.pathname !== v.path && // cannot be current link
4951
!v.path.startsWith("/s/") && // cannot be a quick search link
50-
!v.path.startsWith("/onboarding"), // cannot be an onboarding link
51-
);
52-
return route?.path ?? "/";
52+
!v.path.startsWith("/onboarding") // cannot be an onboarding link
53+
) {
54+
return v.path;
55+
}
56+
}
57+
return "/";
5358
}, [routes, location]);
5459
return lastNonPlayerLink;
5560
}

0 commit comments

Comments
 (0)