1
- import React , { useState } from 'react' ;
1
+ import React , { useState , useEffect , useRef } from 'react' ;
2
2
import { Routes , Route , Navigate , useParams } from 'react-router-dom' ;
3
3
import PerfectScrollbar from 'perfect-scrollbar' ;
4
4
import 'perfect-scrollbar/css/perfect-scrollbar.css' ;
@@ -12,78 +12,87 @@ import logo from '../assets/img/git-proxy.png';
12
12
import { UserContext } from '../../context' ;
13
13
import { getUser } from '../services/user' ;
14
14
15
- let ps ;
16
- let refresh = false ;
15
+ interface RouteType {
16
+ layout : string ;
17
+ path : string ;
18
+ component : React . ComponentType < any > ;
19
+ }
17
20
18
- const switchRoutes = (
19
- < Routes >
20
- { routes . map ( ( prop , key ) => {
21
- if ( prop . layout === '/dashboard' ) {
22
- return < Route exact path = { prop . path } element = { < prop . component /> } key = { key } /> ;
23
- }
24
- return null ;
25
- } ) }
26
- < Route exact path = '/dashboard' element = { < Navigate to = '/dashboard/repo' /> } />
27
- </ Routes >
28
- ) ;
21
+ interface DashboardProps {
22
+ [ key : string ] : any ;
23
+ }
29
24
30
- const useStyles = makeStyles ( styles ) ;
25
+ interface UserType {
26
+ id ?: string ;
27
+ name ?: string ;
28
+ email ?: string ;
29
+ }
30
+
31
+ let ps : PerfectScrollbar | undefined ;
32
+ let refresh = false ;
31
33
32
- export default function Dashboard ( { ...rest } ) {
33
- // styles
34
+ const useStyles = makeStyles ( styles as any ) ;
35
+
36
+ const Dashboard : React . FC < DashboardProps > = ( { ...rest } ) => {
34
37
const classes = useStyles ( ) ;
35
- // ref to help us initialize PerfectScrollbar on windows devices
36
- const mainPanel = React . createRef ( ) ;
37
- // states and functions
38
- const [ color ] = React . useState ( 'blue' ) ;
39
- const [ mobileOpen , setMobileOpen ] = React . useState ( false ) ;
40
- const [ user , setUser ] = useState ( { } ) ;
41
-
42
- const handleDrawerToggle = ( ) => {
43
- setMobileOpen ( ! mobileOpen ) ;
44
- } ;
45
- const getRoute = ( ) => {
46
- return window . location . pathname !== '/dashboard/maps' ;
47
- } ;
38
+ const mainPanel = useRef < HTMLDivElement > ( null ) ;
39
+ const [ color ] = useState < 'purple' | 'blue' | 'green' | 'orange' | 'red' > ( 'blue' ) ;
40
+ const [ mobileOpen , setMobileOpen ] = useState < boolean > ( false ) ;
41
+ const [ user , setUser ] = useState < UserType > ( { } ) ;
42
+ const { id } = useParams < { id ?: string } > ( ) ;
43
+
44
+ const handleDrawerToggle = ( ) => setMobileOpen ( ( prev ) => ! prev ) ;
45
+ const isMapRoute = ( ) => window . location . pathname === '/dashboard/maps' ;
46
+
48
47
const resizeFunction = ( ) => {
49
48
if ( window . innerWidth >= 960 ) {
50
49
setMobileOpen ( false ) ;
51
50
}
52
51
} ;
53
- // initialize and destroy the PerfectScrollbar plugin
54
52
55
- const { id } = useParams ( ) ;
56
- React . useEffect ( ( ) => {
53
+ const switchRoutes = (
54
+ < Routes >
55
+ { routes . map ( ( prop : RouteType , key : number ) => {
56
+ if ( prop . layout === '/dashboard' ) {
57
+ const Component = prop . component ;
58
+ return < Route path = { prop . path } element = { < Component /> } key = { key } /> ;
59
+ }
60
+ return null ;
61
+ } ) }
62
+ < Route path = '/dashboard' element = { < Navigate to = '/dashboard/repo' /> } />
63
+ </ Routes >
64
+ ) ;
65
+
66
+ useEffect ( ( ) => {
57
67
async function loadUser ( ) {
58
- if ( navigator . platform . indexOf ( 'Win' ) > - 1 ) {
68
+ if ( navigator . platform . indexOf ( 'Win' ) > - 1 && mainPanel . current ) {
59
69
ps = new PerfectScrollbar ( mainPanel . current , {
60
70
suppressScrollX : true ,
61
71
suppressScrollY : false ,
62
72
} ) ;
63
73
document . body . style . overflow = 'hidden' ;
64
- if ( ! refresh ) {
65
- refresh = true ;
66
- await getUser ( null , setUser , null , null , null ) ;
67
- }
68
74
}
69
- window . addEventListener ( 'resize' , resizeFunction ) ;
75
+
70
76
if ( ! refresh ) {
71
77
refresh = true ;
72
- await getUser ( null , setUser , null , null , null ) ;
78
+ await getUser ( undefined , setUser ) ;
73
79
}
80
+
81
+ window . addEventListener ( 'resize' , resizeFunction ) ;
74
82
}
83
+
75
84
loadUser ( ) ;
76
85
77
- // Specify how to clean up after this effect:
78
- return function cleanup ( ) {
86
+ return ( ) => {
79
87
if ( navigator . platform . indexOf ( 'Win' ) > - 1 && ps ) {
80
88
ps . destroy ( ) ;
81
89
}
82
90
window . removeEventListener ( 'resize' , resizeFunction ) ;
83
91
} ;
84
92
} , [ id ] ) ;
93
+
85
94
return (
86
- < UserContext . Provider value = { { user, setUser } } >
95
+ < UserContext . Provider value = { { user, setUser } as any } >
87
96
< div className = { classes . wrapper } >
88
97
< Sidebar
89
98
background = '#1a1a1a'
@@ -96,17 +105,20 @@ export default function Dashboard({ ...rest }) {
96
105
/>
97
106
< div className = { classes . mainPanel } ref = { mainPanel } >
98
107
< Navbar routes = { routes } handleDrawerToggle = { handleDrawerToggle } { ...rest } />
99
- { /* On the /maps route we want the map to be on full screen - this is not possible if the content and conatiner classes are present because they have some paddings which would make the map smaller */ }
100
- { getRoute ( ) ? (
101
- < div className = { classes . content } >
102
- < div className = { classes . container } > { switchRoutes } </ div >
103
- </ div >
104
- ) : (
108
+ { isMapRoute ( ) ? (
105
109
< div className = { classes . map } > { switchRoutes } </ div >
110
+ ) : (
111
+ < >
112
+ < div className = { classes . content } >
113
+ < div className = { classes . container } > { switchRoutes } </ div >
114
+ </ div >
115
+ < Footer />
116
+ </ >
106
117
) }
107
- { getRoute ( ) ? < Footer /> : null }
108
118
</ div >
109
119
</ div >
110
120
</ UserContext . Provider >
111
121
) ;
112
- }
122
+ } ;
123
+
124
+ export default Dashboard ;
0 commit comments