blob: 0f196c5d58ba50aa63ed925dad255ca973203639 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import React from 'react';
import { Redirect, Router } from '@reach/router';
import { Error } from 'app/components';
const mainTitle = 'Vespa App';
function AppRoute({ element, title, default: isDefault, ...props }) {
const clone = React.cloneElement(element, props, props.children);
if (title != null) {
const titleStr = typeof title === 'function' ? title(props) : title;
document.title = titleStr.endsWith(mainTitle)
? titleStr
: `${titleStr} - ${mainTitle}`;
} else if (isDefault) {
// Reset the title if title is not set and this is a default router
document.title = mainTitle;
}
return clone;
}
export function AppRouter({ children, props: inParentProps }) {
const newProps = Object.assign(
{ primary: false, component: React.Fragment },
inParentProps
);
// If there is only one route then this comes as an object.
if (!Array.isArray(children)) children = [children];
const hasDefault = children.some((child) => child.props.default);
return (
<Router {...newProps}>
{children
.filter(({ props }) => props.enabled ?? true)
.map((e, i) => {
if (e.type === Redirect) return e;
return <AppRoute key={i} element={e} {...e.props} />;
})}
{!hasDefault && <Error code={404} default />}
</Router>
);
}
|