forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbar.react.js
More file actions
150 lines (140 loc) · 4.08 KB
/
Toolbar.react.js
File metadata and controls
150 lines (140 loc) · 4.08 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import PropTypes from 'lib/PropTypes';
import React, { useEffect } from 'react';
import Icon from 'components/Icon/Icon.react';
import styles from 'components/Toolbar/Toolbar.scss';
import Popover from 'components/Popover/Popover.react';
import Position from 'lib/Position';
import { useNavigate, useNavigationType, NavigationType } from 'react-router-dom';
const POPOVER_CONTENT_ID = 'toolbarStatsPopover';
const Stats = ({ data }) => {
const [selected, setSelected] = React.useState(null);
const [open, setOpen] = React.useState(false);
const buttonRef = React.useRef();
const statsOptions = [
{
type: 'sum',
label: 'Sum',
getValue: data => data.reduce((sum, value) => sum + value, 0),
},
{
type: 'mean',
label: 'Mean',
getValue: data => data.reduce((sum, value) => sum + value, 0) / data.length,
},
{
type: 'count',
label: 'Count',
getValue: data => data.length,
},
{
type: 'p99',
label: 'P99',
getValue: data => {
const sorted = data.sort((a, b) => a - b);
return sorted[Math.floor(sorted.length * 0.99)];
},
},
];
const toggle = () => {
setOpen(!open);
};
const renderPopover = () => {
const node = buttonRef.current;
const position = Position.inDocument(node);
return (
<Popover
fixed={true}
position={position}
onExternalClick={toggle}
contentId={POPOVER_CONTENT_ID}
>
<div id={POPOVER_CONTENT_ID}>
<div
onClick={toggle}
style={{
cursor: 'pointer',
width: node.clientWidth,
height: node.clientHeight,
}}
></div>
<div className={styles.stats_popover_container}>
{statsOptions.map(item => {
const itemStyle = [styles.stats_popover_item];
if (item.type === selected?.type) {
itemStyle.push(styles.active);
}
return (
<div
key={item.type}
className={itemStyle.join(' ')}
onClick={() => {
setSelected(item);
toggle();
}}
>
<span>{item.label}</span>
</div>
);
})}
</div>
</div>
</Popover>
);
};
useEffect(() => {
setSelected(statsOptions[0]);
}, []);
return (
<>
{selected ? (
<button ref={buttonRef} className={styles.stats} onClick={toggle}>
{`${selected.label}: ${selected.getValue(data)}`}
</button>
) : null}
{open ? renderPopover() : null}
</>
);
};
const Toolbar = props => {
const action = useNavigationType();
const navigate = useNavigate();
let backButton;
if (props.relation || (props.filters && props.filters.size && action !== NavigationType.Pop)) {
backButton = (
<a className={styles.iconButton} onClick={() => navigate(-1)}>
<Icon width={32} height={32} fill="#ffffff" name="left-outline" />
</a>
);
}
return (
<div className={styles.toolbar}>
<div className={styles.title}>
<div className={styles.nav}>{backButton}</div>
<div className={styles.titleText}>
<div className={styles.section}>{props.section}</div>
<div>
<span className={styles.subsection}>{props.subsection}</span>
<span className={styles.details}>{props.details}</span>
</div>
</div>
</div>
{props?.selectedData?.length ? <Stats data={props.selectedData} /> : null}
<div className={styles.actions}>{props.children}</div>
</div>
);
};
Toolbar.propTypes = {
section: PropTypes.string,
subsection: PropTypes.string,
details: PropTypes.string,
relation: PropTypes.object,
selectedData: PropTypes.array,
};
export default Toolbar;