-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathhome-page.jsx
294 lines (263 loc) · 8.52 KB
/
home-page.jsx
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import React, { useState, useEffect } from "react";
import ArrowUpward from "@material-ui/icons/ArrowUpward";
import Check from "@material-ui/icons/Check";
import CircularProgress from "@material-ui/core/CircularProgress";
import PropTypes from "prop-types";
import styled from "@emotion/styled";
import Header from "../components/header";
import PageBody from "../components/page-body";
import PageHeader from "../components/page-header";
import TopContainer from "../components/page-containers/top-container";
import BelowFoldContainer from "../components/page-containers/below-fold-container";
import MarketingCopySplash from "../components/splash/marketing-copy-splash";
import LoggedInSplash from "../components/splash/logged-in-splash";
import TrendingNotebooksList from "../components/trending-notebooks-list";
import AttentionBlock from "../components/attention-block";
import NewNotebookButton from "../components/new-notebook-button";
import FeaturedNotebooks from "../../shared/components/featured-notebooks";
import DropTarget from "../../shared/components/drop-target";
import THEME from "../../shared/theme";
import { sharedProperties } from "../../server/style/base";
import { createNotebookRequest } from "../../shared/server-api/notebook";
import { saveFileToServer } from "../../shared/utils/file-operations";
import { fadeIn, bounce } from "../../shared/keyframes";
const Overlay = styled.div`
align-items: center;
animation: ${fadeIn} 0.25s;
background: ${THEME.header.backgroundLeft};
box-sizing: border-box;
color: #fff;
display: grid;
grid-gap: 100px;
justify-items: center;
overflow: auto;
padding: 2%;
z-index: 10;
/* Expand to fill the viewport */
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
&.invisible {
display: none;
}
`;
// This wrapper works around a peculiarity with centering in CSS Grid.
//
// Setting "align-content: center" on a grid container gives it "true" vertical
// centering, which is to say that content will be clipped it it cannot fit on
// screen, even when "overflow: auto" is used.
//
// In other words, we could ditch this wrapper and set "align-content: center"
// on the overlay when an upload is in progress, but if there were more files
// being uploaded than could be shown on screen, some of them would be
// invisible.
//
// The real solution is to ditch this wrapper and use "align-content: safe
// center" which addresses this exact problem, but it's not supported in most
// browsers at the time of this writing.
//
// https://stackoverflow.com/a/33455342/4297741
const OverlayUploadingBody = styled.div`
display: grid;
grid-gap: 100px;
`;
const OverlayTitle = styled.h2`
font-size: 3rem;
font-weight: bold;
margin: 0;
`;
const OverlayArrow = styled(ArrowUpward)`
/* Increase specificity to override Material defaults */
#home-page & {
animation: ${bounce} 1s ease-in-out infinite alternate;
font-size: 10rem;
}
`;
const OverlayFileList = styled.ul`
display: grid;
font-size: 2rem;
grid-gap: 100px;
list-style: none;
margin: 0;
padding: 0;
li {
align-items: center;
display: grid;
grid-auto-flow: column;
grid-gap: 50px;
justify-content: space-between;
.file-status-icon {
font-size: 50px;
}
}
`;
const TrendingNotebooksPage = ({ notebookList }) => (
<React.Fragment>
<PageHeader>The Firehose of Notebooks</PageHeader>
<TrendingNotebooksList notebookList={notebookList} />
</React.Fragment>
);
TrendingNotebooksPage.propTypes = {
notebookList: PropTypes.arrayOf(PropTypes.object)
};
const LetsGetStarted = () => (
<AttentionBlock>
<div>Welcome to Iodide. Shall we get started?</div>
<NewNotebookButton />
</AttentionBlock>
);
let previousBodyOverflow;
export default function HomePage({ notebookList, userInfo, headerMessage }) {
const [overlayVisible, setOverlayVisible] = useState(false);
const [numHoveredFiles, setNumHoveredFiles] = useState(0);
const [files, setFiles] = useState([]);
const isLoggedIn = "name" in userInfo;
const maxDroppable = 8;
useEffect(
function toggleOverlay() {
if (overlayVisible) {
previousBodyOverflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
} else {
document.body.style.overflow = previousBodyOverflow;
}
return function cleanup() {
document.body.style.overflow = previousBodyOverflow;
};
},
[overlayVisible]
);
useEffect(
function handleDroppedFiles() {
if (files.length === 0) return;
// useEffect will not accept an async function, so we need to write an
// inner async function and immediately call it
async function handleDroppedFilesInner() {
const redirectDelay = 1500;
const fetches = files.map(
({ file }, i) => `text: file${i}=${file.name}`
);
const body = `%% fetch\n${fetches.join("\n")}`;
const { id: notebookID } = await createNotebookRequest(
"New Notebook",
body
);
await Promise.all(
files.map(({ file }, i) => {
return saveFileToServer(
notebookID,
file,
file.name,
undefined,
true
).then(() => {
const filesCopy = [...files];
filesCopy[i].status = "saved";
setFiles(filesCopy);
});
})
);
setTimeout(() => {
window.location.href = `${window.location}notebooks/${notebookID}`;
}, redirectDelay);
}
handleDroppedFilesInner();
},
[files.length !== 0]
);
function onHoverStart(e) {
setNumHoveredFiles(e.dataTransfer.items.length);
setOverlayVisible(true);
}
function onHoverEnd() {
setOverlayVisible(false);
}
function onDrop(e) {
// Don't let the user drop more files if some are already uploading
if (files.length) return;
if (Array.from(e.dataTransfer.files).length > maxDroppable) {
setOverlayVisible(false);
} else {
setFiles(
Array.from(e.dataTransfer.files).map((file, i) => ({
id: i,
file,
status: "uploading"
}))
);
}
}
return (
<DropTarget
id="home-page"
active={isLoggedIn}
onHoverStart={onHoverStart}
onHoverEnd={onHoverEnd}
onDrop={onDrop}
>
<Overlay
className={`overlay ${overlayVisible ? "visible" : "invisible"}`}
>
{files.length > 0 && (
<OverlayUploadingBody>
<OverlayTitle>Creating new notebook...</OverlayTitle>
<OverlayFileList id="overlay-file-list">
{files.map(fm => (
<li key={fm.id}>
{fm.file.name}
{fm.status === "uploading" && (
<CircularProgress size="50px" color="inherit" />
)}
{fm.status === "saved" && (
<Check className="file-status-icon" />
)}
</li>
))}
</OverlayFileList>
</OverlayUploadingBody>
)}
{files.length === 0 && numHoveredFiles > maxDroppable && (
<OverlayTitle>
Cannot upload more than {maxDroppable} files
</OverlayTitle>
)}
{files.length === 0 && numHoveredFiles <= maxDroppable && (
<React.Fragment>
<OverlayTitle>
Drop to create a new notebook with {numHoveredFiles} attached{" "}
{numHoveredFiles === 1 ? "file" : "files"}
</OverlayTitle>
<OverlayArrow />
</React.Fragment>
)}
</Overlay>
<Header userInfo={userInfo} headerMessage={headerMessage} />
<PageBody>
<TopContainer>
{!isLoggedIn && process.env.IODIDE_PUBLIC && <MarketingCopySplash />}
{!isLoggedIn && !process.env.IODIDE_PUBLIC && <LetsGetStarted />}
{isLoggedIn && <LoggedInSplash userInfo={userInfo} />}
<PageHeader>Try These Examples</PageHeader>
<FeaturedNotebooks width={`${sharedProperties.pageWidth}px`} />
</TopContainer>
<BelowFoldContainer>
{notebookList.length ? (
<TrendingNotebooksPage notebookList={notebookList} />
) : (
<LetsGetStarted />
)}
</BelowFoldContainer>
</PageBody>
</DropTarget>
);
}
HomePage.propTypes = {
userInfo: PropTypes.shape({
name: PropTypes.string,
avatar: PropTypes.string
}),
headerMessage: PropTypes.string,
notebookList: PropTypes.arrayOf(PropTypes.object)
};