-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathapp.js
More file actions
125 lines (118 loc) · 3.81 KB
/
app.js
File metadata and controls
125 lines (118 loc) · 3.81 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React from 'react';
import { createRoot, hydrateRoot } from 'react-dom/client';
import { Router, useLocation } from '@reach/router';
import 'client-styles'; // Webpack replaces this import: patternfly-docs.css.js
import { SideNavLayout } from '@patternfly/documentation-framework/layouts';
import { MDXTemplate } from '@patternfly/documentation-framework/templates/mdx';
import { routes, groupedRoutes, fullscreenRoutes, getAsyncComponent } from './routes';
import { trackEvent } from './helpers';
import './components/autoLinkHeader/autoLinkHeader.css';
import './components/cssVariables/cssVariables.css';
import './components/tableOfContents/tableOfContents.css';
import './components/example/example.css';
import './components/footer/footer.css';
import './components/feedbackButton/feedbackButton.css';
import './layouts/sideNavLayout/sideNavLayout.css';
const AppRoute = ({ child, title, path }) => {
const pathname = useLocation().pathname;
if (typeof window !== 'undefined' && window.gtag) {
gtag('config', process.env.googleAnalyticsID, {
'page_path': pathname,
'page_title': (title || pathname)
});
}
// Redirect all v4 url paths to the archived v4 site
if(pathname.startsWith("/v4")){
window.location.href = `https://v4-archive.patternfly.org${pathname}`;
return;
}
// Send 404 event if redirected to 404 page
if (path === '/404' && pathname.split('/').pop() !== '404') {
trackEvent('404_redirect', 'redirect', pathname);
}
return (
<React.Fragment>
{child}
</React.Fragment>
);
}
const SideNavRouter = () => {
const componentsData = process.env.componentsData;
return (
<SideNavLayout groupedRoutes={groupedRoutes} navOpen={true} >
<Router id="ws-page-content-router" component="main">
{Object.entries(routes)
.map(([path, { Component, title, sources, id }]) => Component
? <AppRoute
key={path}
path={path}
default={path === '/404'}
child={<Component />}
title={title}
/>
: <AppRoute
key={path}
path={path + '/*'}
child={
<MDXTemplate
path={path}
title={title}
sources={sources}
id={id}
componentsData={componentsData}
/>
}
title={title}
/>
)
}
</Router>
</SideNavLayout>
);
}
const FullscreenComponent = ({ Component, title }) => {
const [isLoaded, setIsLoaded] = React.useState(false);
React.useEffect(() => {
Component.preload().then(() => setIsLoaded(true));
}, []);
const { examples = {} } = Component.getPageData();
const Example = examples[title];
return isLoaded ? <Example isFullscreen={false} isFullscreenPreview /> : <Component />;
};
// Export for SSR
export const App = () => (
<Router id="ws-router">
<SideNavRouter path="/*" />
{Object.entries(fullscreenRoutes)
.map(([path, { title, Component }]) =>
<FullscreenComponent
key={path}
path={path}
Component={Component}
title={title}
/>
)
}
</Router>
);
const isProd = process.env.NODE_ENV === 'production';
const isPrerender = process.env.PRERENDER;
// Don't use ReactDOM in SSR
if (!isPrerender) {
function render() {
const container = document.getElementById('root');
if (isProd) {
hydrateRoot(container, <App />);
} else {
createRoot(container).render(<App />);
}
}
// On first load, await promise for the current page to avoid flashing a "Loading..." state
const Component = getAsyncComponent(null);
if (Component) {
Component.preload().then(render);
}
else {
render();
}
}