aboutsummaryrefslogtreecommitdiffstats
path: root/dashboard/assets/components/SideBar.jsx
blob: 301e458d17abfed65b32782b3c520691a3326cba (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
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
// @flow

// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

import React, {Component} from 'react';

import withStyles from '@material-ui/core/styles/withStyles';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import Icon from '@material-ui/core/Icon';
import Transition from 'react-transition-group/Transition';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';

import {MENU, DURATION} from '../common';

// styles contains the constant styles of the component.
const styles = {
    menu: {
        default: {
            transition: `margin-left ${DURATION}ms`,
        },
        transition: {
            entered: {marginLeft: -200},
        },
    },
};

// themeStyles returns the styles generated from the theme for the component.
const themeStyles = theme => ({
    list: {
        background: theme.palette.grey[900],
    },
    listItem: {
        minWidth: theme.spacing.unit * 7,
    },
    icon: {
        fontSize: theme.spacing.unit * 3,
        overflow: 'unset',
    },
});

export type Props = {
    classes: Object, // injected by withStyles()
    opened: boolean,
    changeContent: string => void,
};

type State = {}

// SideBar renders the sidebar of the dashboard.
class SideBar extends Component<Props, State> {
    shouldComponentUpdate(nextProps: Readonly<Props>, nextState: Readonly<State>, nextContext: any) {
        return nextProps.opened !== this.props.opened;
    }

    // clickOn returns a click event handler function for the given menu item.
    clickOn = menu => (event) => {
        event.preventDefault();
        this.props.changeContent(menu);
    };

    // menuItems returns the menu items corresponding to the sidebar state.
    menuItems = (transitionState) => {
        const {classes} = this.props;
        const children = [];
        MENU.forEach((menu) => {
            children.push((
                <ListItem button key={menu.id} onClick={this.clickOn(menu.id)} className={classes.listItem}>
                    <ListItemIcon>
                        <Icon className={classes.icon}>
                            <FontAwesomeIcon icon={menu.icon} />
                        </Icon>
                    </ListItemIcon>
                    <ListItemText
                        primary={menu.title}
                        style={{
                            ...styles.menu.default,
                            ...styles.menu.transition[transitionState],
                            padding: 0,
                        }}
                    />
                </ListItem>
            ));
        });
        return children;
    };

    // menu renders the list of the menu items.
    menu = (transitionState: Object) => (
        <div className={this.props.classes.list}>
            <List>
                {this.menuItems(transitionState)}
            </List>
        </div>
    );

    render() {
        return (
            <Transition mountOnEnter in={this.props.opened} timeout={{enter: DURATION}}>
                {this.menu}
            </Transition>
        );
    }
}

export default withStyles(themeStyles)(SideBar);