-
Notifications
You must be signed in to change notification settings - Fork 0
/
Views.js
234 lines (212 loc) · 6.12 KB
/
Views.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import { view } from "@risingstack/react-easy-state";
import { Button, Classes, ControlGroup, Tab } from "@blueprintjs/core";
import { PointedTabs } from "./PointedTabs.js";
import { ToolPopover } from "./ToolPopover.js";
import "./Views.css";
const ShowHide = props => {
return (
<span
className={props.className}
data-tool-id={props.id}
title={props.title}
style={props.visible ? {} : { display: "none" }}
>
{props.children}
</span>
);
};
export const Tool = ({ tool, id, visible, props }) => {
if (tool.icon) {
return (
<ToolPopover
className={Classes.FIXED}
disabled={!visible}
position={tool.position}
>
<ShowHide visible={visible} className="view-tool-trigger">
<Button
data-tool-id={id}
icon={
tool.icon.prefix ? (
<b title="Support for FontAwesome removed">!!!</b>
) : (
tool.icon
)
}
minimal={true}
title={tool.title}
/>
</ShowHide>
{tool.createContentElement(props)}
</ToolPopover>
);
} else {
return (
<ShowHide
visible={visible}
className={Classes.FIXED + " view-tool-trigger"}
id={id}
title={tool.title}
>
{tool.createContentElement(props)}
</ShowHide>
);
}
};
// Don't render if the contents of the tab is not visible.
const SwitchContent = React.memo(
({ visible, createElement }) => {
return <>{createElement(visible)}</>;
},
(prev, next) => {
return !next.visible;
}
);
const Switch = ({ visible, createElement, overflow }) => {
const [initialized, setInitialized] = useState(false);
useEffect(() => {
if (visible) {
setInitialized(true);
}
}, [visible]);
if (!initialized) {
return null;
}
return (
<div
className={classNames({
ViewVisible: visible,
ViewHidden: !visible,
ViewOverflowHidden: overflow === "hidden",
ViewOverflowVisible: overflow === "visible",
ViewOverflowAuto: overflow === "auto"
})}
>
<SwitchContent visible={visible} createElement={createElement} />
</div>
);
};
Switch.propTypes = {
visible: PropTypes.bool.isRequired,
createElement: PropTypes.func.isRequired
};
export const flattenViews = views =>
views.reduce((map, group) => {
Object.assign(map, group.views);
return map;
}, {});
export const Views = ({
views,
className,
activeView,
onViewChange,
children,
ErrorBoundary = ViewErrorBoundary
}) => {
// Flatten views from all groups into a single object.
const allViews = flattenViews(views);
const viewKeys = Object.keys(allViews);
return (
<div className={classNames("Views", className)}>
<ControlGroup className="ViewsTabs" fill={true} vertical={false}>
<PointedTabs selectedTabId={activeView} onChange={onViewChange}>
{views.map(group => {
const components = [];
if (group.label) {
components.push(
<div className="TabGroupLabel" key={group.label}>
{group.label}
</div>
);
}
return components.concat(
Object.keys(group.views).map(v => {
return <Tab key={v} id={v} title={allViews[v].label} />;
})
);
})}
</PointedTabs>
{viewKeys
.filter(v => allViews[v].tools && allViews[v].tools.length > 0)
.reduce(function (tools, v) {
allViews[v].tools.forEach(t => {
const key = v + "." + t.id;
tools.push(
<Tool key={key} id={key} tool={t} visible={activeView === v} />
);
});
return tools;
}, [])}
</ControlGroup>
<div className="ViewsContent">
{children}
{viewKeys.map(v => {
return (
<Switch
key={v}
overflow={allViews[v].overflow || "auto"}
visible={v === activeView}
createElement={visible =>
React.createElement(
ErrorBoundary,
{ view: v },
allViews[v].createContentElement(visible)
)
}
/>
);
})}
</div>
</div>
);
};
Views.propTypes = {
activeView: PropTypes.string.isRequired,
views: PropTypes.array.isRequired,
onViewChange: PropTypes.func.isRequired
};
export const renderIfNextVisible = comp =>
React.memo(comp, (prev, next) => !next["visible"]);
/**
* Wraps a component in an react-easy-state view in such a way that the
* component renders only if the "visible" property passed to it is true.
* If "visible" is false, the component doesn't render even if the
* "inner" react-easy-state store changes and would normally force a
* re-render.
*
* This is useful for wrapping components rendering the content in the
* tabbed view. If the view has many tabs, we don't want to eagerly render
* all tabs if the underlying data changes. Instead, we only want to re-render
* the visible tab and render the remaining tabs only when they become visible,
* if at all.
*/
export const lazyView = component => {
const Lazy = renderIfNextVisible(props => component(props));
return view(props => <Lazy {...props} />);
};
class ViewErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true, error: error };
}
componentDidCatch(error, errorInfo) {}
render() {
if (this.state.hasError) {
const e = this.state.error;
return (
<div className="ViewError">
<h1>An error has occurred in the {this.props.view} view.</h1>
<pre>{e.stack}</pre>
</div>
);
}
return this.props.children;
}
}