-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTabWrapper.tsx
127 lines (117 loc) · 3.84 KB
/
TabWrapper.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
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
126
127
import { ClassNameMap, createStyles, withStyles } from '@mui/styles';
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import Tabs from '@mui/material/Tabs';
import { TabClasses } from '@mui/material/Tab/tabClasses';
import Tab from '@mui/material/Tab';
import { Link, useLocation } from 'react-router-dom';
import HelpContainer from 'components/help/HelpContainer';
import history from 'modules/hist';
import { CustomTheme } from '@mui/material/styles';
import { IRoute } from 'routes/Private';
import { TdrState } from 'reducers';
import { RouterRootState } from 'connected-react-router';
import { SnapshotAccessRequest } from 'generated/tdr';
interface ITabConfig {
label: string;
path: string;
hidden?: boolean;
}
const styles = (theme: CustomTheme) =>
createStyles({
tabsIndicator: {
borderBottom: '8px solid #74ae43',
},
tabsRoot: {
borderBottom: `2px solid ${theme.palette.terra.green}`,
boxShadow: '0 2px 5px 0 rgba(0,0,0,0.26), 0 2px 10px 0 rgba(0,0,0,0.16)',
color: '#333F52',
fontFamily: theme.typography.fontFamily,
height: 18,
fontSize: 14,
fontWeight: 600,
lineHeight: 18,
textAlign: 'center',
width: '100%',
transition: '0.3s background-color ease-in-out',
},
tabSelected: {
transition: '0.3s background-color ease-in-out',
backgroundColor: '#ddebd0',
color: theme.palette.secondary.dark,
fontWeight: '700 !important',
},
tabWrapper: {
display: 'flex',
position: 'relative',
zIndex: 2,
},
helpIconDiv: {
borderBottom: `2px solid ${theme.palette.terra.green}`,
boxShadow: '0 2px 5px 0 rgba(0,0,0,0.26), 0 2px 10px 0 rgba(0,0,0,0.16)',
float: 'right',
width: '20px',
},
});
type TabWrapperProps = {
routes: Array<IRoute>;
classes: ClassNameMap;
snapshotAccessRequests: Array<SnapshotAccessRequest>;
};
function TabWrapper(props: TabWrapperProps) {
const { classes, routes, snapshotAccessRequests } = props;
const [selectedTab, setSelectedTab] = React.useState<string>('datasets');
const location = useLocation();
useEffect(() => {
const locationSplit = history.location.pathname.split('/');
setSelectedTab(`/${locationSplit[1] || 'datasets'}`);
}, [location]);
const tabConfigs: Array<ITabConfig> = [
{ label: 'Datasets', path: '/datasets' },
{ label: 'Snapshots', path: '/snapshots' },
{ label: 'Activity', path: '/activity', hidden: true },
{ label: 'Ingest Data', path: '/ingestdata' },
{
label: 'Requests',
path: '/requests',
hidden: snapshotAccessRequests && snapshotAccessRequests.length < 1,
},
];
const visibleTabs = tabConfigs.filter(
(config: ITabConfig) =>
routes.findIndex((route: IRoute) => route.path === config.path) !== -1 && !config.hidden,
);
return (
<div className={classes.tabWrapper}>
<Tabs
value={visibleTabs.map((tab) => tab.path).includes(selectedTab) ? selectedTab : false}
classes={
{
root: classes.tabsRoot,
indicator: classes.tabsIndicator,
} as Partial<TabClasses>
}
>
{visibleTabs.map((config: ITabConfig, i: number) => (
<Tab
key={`navbar-link-${i}`}
label={config.label}
component={Link}
value={config.path}
to={config.path}
classes={{ selected: classes.tabSelected } as Partial<TabClasses>}
disableFocusRipple
disableRipple
/>
))}
</Tabs>
<HelpContainer className={classes.helpIconDiv} />
</div>
);
}
function mapStateToProps(state: TdrState & RouterRootState) {
return {
snapshotAccessRequests: state.snapshotAccessRequests?.snapshotAccessRequests,
};
}
export default connect(mapStateToProps)(withStyles(styles)(TabWrapper));