/
SidebarsPage.tsx
91 lines (87 loc) · 2.51 KB
/
SidebarsPage.tsx
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
/** @jsx jsx */
import { FC, useRef, ReactNode } from 'react';
import { jsx, Box } from 'theme-ui';
import * as qs from 'qs';
import { PageType } from '@component-controls/specification';
import { Tabs, Tab, TabList, TabPanel } from '@component-controls/components';
import { PageContainer } from '@component-controls/blocks';
import { SideContext } from '../SideContext';
import { Sidebar } from '../Sidebar';
export interface PageConfig {
/**
* route string
*/
key: string;
/**
* title will be used as tab caption
*/
title: string;
/**
* render function, returns a react component
*/
render: () => ReactNode;
}
export interface DocPageProps {
/**
* custom pages
*/
pagesFn?: (viewMode: string) => PageConfig[];
/**
* page type
*/
type: PageType;
}
/**
* document page - rendering with sidebars and tabs for multiple document views
*/
export const SidebarsPage: FC<DocPageProps> = ({ pagesFn, type }) => {
const pages = typeof pagesFn === 'function' ? pagesFn('') : [];
const pageRef = useRef<HTMLDivElement>(null);
const params =
typeof window !== 'undefined'
? qs.parse(window.location.search.substring(1))
: {};
const tabIndex = Math.max(
pages.findIndex(page => page.key === params['view']),
0,
);
return (
<Box variant="appsidebarpage.container">
<Sidebar type={type} />
<Box sx={{ flexGrow: 1 }} id="content">
<Tabs
fontSize={16}
selectedIndex={tabIndex}
onSelect={index => {
const key = pages[index]?.key;
if (key) {
const params = qs.parse(window.location.search.substring(1));
params['view'] = key;
const newHREF = `${window.location.origin}${
window.location.pathname
}?${qs.stringify(params)}${window.location.hash}`;
window.location.href = newHREF;
return false;
}
return true;
}}
>
{pages && pages.length > 1 && (
<TabList>
{pages.map(page => (
<Tab key={`tab_${page.key}`}>{page.title}</Tab>
))}
</TabList>
)}
<PageContainer variant={`pagecontainer.${type}`} ref={pageRef}>
{pages &&
pages.map(page => (
<TabPanel key={`panel_${page.key}`}>{page.render()}</TabPanel>
))}
</PageContainer>
</Tabs>
</Box>
<SideContext pageRef={pageRef} />
</Box>
);
};