-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathMakeSelectProject.tsx
137 lines (130 loc) · 4.06 KB
/
MakeSelectProject.tsx
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
import { formatDistanceToNow } from 'date-fns';
import React from 'react';
import '../shared/polyfill';
import {
GetProjectsRequest,
GetProjectsResponse,
MakeProjectRequest,
MakeProjectResponse,
} from '../shared/rpc';
import { ProjectState } from '../shared/state';
import { asyncRPC } from './asyncRPC';
import { Alert } from './components/Alert';
import { Button } from './components/Button';
import { Input } from './components/Input';
import { Loading } from './components/Loading';
import { Select } from './components/Select';
import { Footer } from './Footer';
import { SAMPLES } from './samples';
export function MakeSelectProject() {
const [projectNameTmp, setProjectNameTmp] = React.useState('');
const [sample, setSample] = React.useState(null);
const [makeProjectError, setMakeProjectError] = React.useState('');
async function makeProject(projectId: string) {
try {
let project: ProjectState = null;
if (sample) {
outer: for (const group of SAMPLES) {
for (const groupSample of group.samples) {
if (sample === groupSample.name) {
project = groupSample.project(projectNameTmp);
break outer;
}
}
}
}
await asyncRPC<MakeProjectRequest, MakeProjectResponse>('makeProject', {
projectId,
project,
});
// So loading ui shows up while changing page.
setProjects([]);
window.location.href =
window.location.pathname + '?projectId=' + projectId;
} catch (e) {
setMakeProjectError(e.message);
}
}
const [projects, setProjects] = React.useState<GetProjectsResponse | null>(
null
);
React.useEffect(() => {
async function load() {
const loaded = await asyncRPC<GetProjectsRequest, GetProjectsResponse>(
'getProjects',
null
);
setProjects(loaded);
}
if (!projects) {
load();
}
}, [projects]);
if (!projects) {
return <Loading />;
}
return (
<div className="main-body">
<div className="project-name form">
<h1>New Project</h1>
<p>Pick a name for this project to get started.</p>
<div className="form-row">
<Input
noBuffer
value={projectNameTmp}
label="Project name"
onChange={(v) => setProjectNameTmp(v)}
/>
</div>
<div className="form-row">
<Select
value={sample ? sample : 'null'}
label="Create from template with sample data"
onChange={(v) => setSample(v === 'null' ? null : v)}
>
<option value="null">Blank, no template</option>
{SAMPLES.map((group) => (
<optgroup key={group.name} label={group.name}>
{group.samples.map((f) => (
<option key={f.name} value={f.name}>
{f.name}
</option>
))}
</optgroup>
))}
</Select>
</div>
<div className="form-row">
<Button
type="primary"
disabled={!projectNameTmp}
onClick={() => makeProject(projectNameTmp)}
>
{projectNameTmp ? 'Go!' : 'Pick a name'}
</Button>
</div>
{makeProjectError && <Alert type="error" children={makeProjectError} />}
{projects.length ? (
<div className="project-existing">
<p>Or open an existing project.</p>
{projects.map(({ name, createdAt }) => (
<div className="form-row" key={name}>
<h3 className="project-selector">{name}</h3>
<div className="project-timestamp">
Created{' '}
{formatDistanceToNow(new Date(createdAt), {
addSuffix: true,
})}
</div>
<a href={window.location.pathname + '?projectId=' + name}>
Open
</a>
</div>
))}
</div>
) : null}
</div>
<Footer />
</div>
);
}