Open History to see the last commit
1/**
2 * Returns the siblings of a specific route (that is the previous and next routes).
3 */
4export function getRouteContext(_route, routes, ctx = {}) {
5 if (!_route) {
6 return ctx;
7 }
8β
9 const {
10 path
11 } = _route;
12 const {
13 parent
14 } = ctx;
15β
16 for (let i = 0; i < routes.length; i += 1) {
17 const route = routes[i];
18β
19 if (route.routes) {
20 ctx.parent = route;
21 ctx = getRouteContext(_route, route.routes, ctx); // If the active route and the next route was found in nested routes, return it
22β
23 if (ctx.nextRoute) return ctx;
24 }
25β
26 if (!route) continue;
27 if (!route.path) continue;
28β
29 if (ctx.route) {
30 ctx.nextRoute = parent && i === 0 ? { ...route,
31 title: `${parent.title}: ${route.title}`
32 } : route;
33 return ctx;
34 }
35β
36 if (route && route.path === path) {
37 ctx.route = { ..._route,
38 title: parent && !parent.heading ? `${parent.title}: ${_route.title}` : _route.title
39 }; // Continue the loop until we know the next route
40β
41 continue;
42 }
43β
44 ctx.prevRoute = parent && !parent.heading && !routes[i + 1]?.path ? { ...route,
45 title: `${parent.title}: ${route.title}`
46 } : route;
47 } // The loop ended and the previous route was found, or nothing
48β
49β
50 return ctx;
51}