forked from clusterio/clusterio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModsPage.tsx
355 lines (345 loc) · 9.52 KB
/
ModsPage.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import React, { Fragment, useState, useContext } from "react";
import { useNavigate } from "react-router-dom";
import { Button, Form, Input, Modal, Popconfirm, Space, Table, Typography, Upload } from "antd";
import ImportOutlined from "@ant-design/icons/ImportOutlined";
import PlusOutlined from "@ant-design/icons/PlusOutlined";
import UploadOutlined from "@ant-design/icons/UploadOutlined";
import * as lib from "@clusterio/lib";
import { useAccount } from "../model/account";
import { useMods } from "../model/mods";
import { useModPacks } from "../model/mod_pack";
import { notifyErrorHandler } from "../util/notify";
import ControlContext from "./ControlContext";
import PageHeader from "./PageHeader";
import PageLayout from "./PageLayout";
import PluginExtra from "./PluginExtra";
import SectionHeader from "./SectionHeader";
import ModDetails from "./ModDetails";
import { InboxOutlined } from "@ant-design/icons";
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
function ImportModPackButton() {
let control = useContext(ControlContext);
let navigate = useNavigate();
let [open, setOpen] = useState(false);
let [form] = Form.useForm();
function close() {
setOpen(false);
}
return <>
<Button icon={<ImportOutlined />} onClick={() => { setOpen(true); }}>Import string</Button>
<Modal
title="Import Mod Pack String"
open={open}
okText="Import"
cancelText="Cancel"
onCancel={() => { setOpen(false); }}
onOk={() => {
(async () => {
let values;
try {
values = await form.validateFields();
} catch {
return; // Validation failed
}
const modPack = lib.ModPack.fromModPackString(values.string);
await control.send(new lib.ModPackCreateRequest(modPack));
navigate(`/mods/mod-packs/${modPack.id}/view`);
})().catch(notifyErrorHandler("Error creating mod pack"));
}}
>
<Form form={form} layout="vertical">
<Form.Item
name="string"
rules={[
{ required: true },
{
async validator(rule, value) {
if (value) {
lib.ModPack.fromModPackString(value);
}
},
},
]}
>
<Input.TextArea autoSize={{ minRows: 6, maxRows: 6 }} />
</Form.Item>
</Form>
</Modal>
</>;
}
function CreateModPackButton() {
let control = useContext(ControlContext);
let navigate = useNavigate();
let [open, setOpen] = useState(false);
let [form] = Form.useForm();
function close() {
setOpen(false);
}
return <>
<Button type="primary" icon={<PlusOutlined />} onClick={() => { setOpen(true); }}>Create</Button>
<Modal
title="Create Mod Pack"
open={open}
okText="Create"
cancelText="Cancel"
onCancel={() => { setOpen(false); }}
onOk={() => {
(async () => {
let values;
try {
values = await form.validateFields();
} catch {
return; // Validation failed
}
const modPack = lib.ModPack.fromJSON({} as any);
modPack.name = values.name;
modPack.factorioVersion = values.factorioVersion;
if (values.description) { modPack.description = values.description; }
await control.send(new lib.ModPackCreateRequest(modPack));
navigate(`/mods/mod-packs/${modPack.id}/view`);
})().catch(notifyErrorHandler("Error creating mod pack"));
}}
>
<Form form={form} layout="vertical" requiredMark="optional">
<Form.Item name="name" label="Name" rules={[{ required: true }]}>
<Input />
</Form.Item>
<Form.Item name="description" label="Description">
<Input.TextArea autoSize={{ minRows: 2, maxRows: 6 }} />
</Form.Item>
<Form.Item
name="factorioVersion"
label="Factorio Version"
rules={[{
required: true,
pattern: /^\d+\.\d+(\.\d+)?$/,
message: "Must be an a.b or a.b.c version number.",
}]}
>
<Input />
</Form.Item>
</Form>
</Modal>
</>;
}
export default function ModsPage() {
let account = useAccount();
let control = useContext(ControlContext);
let navigate = useNavigate();
let [mods] = useMods();
let [modPacks] = useModPacks();
const [isDroppingFile, setIsDroppingFile] = useState(false);
function actions(mod: lib.ModInfo) {
return <Space>
{account.hasPermission("core.mod.download")
&& <Typography.Link
onClick={() => {
control.send(
new lib.ModDownloadRequest(mod.name, mod.version)
).then((streamId: string) => {
let url = new URL(webRoot, document.location.origin);
url.pathname += `api/stream/${streamId}`;
document.location = url.href;
}).catch(
notifyErrorHandler("Error downloading save")
);
}}
>download</Typography.Link>
}
{account.hasPermission("core.mod.delete")
&& <Popconfirm
title={`Delete ${mod.filename}?`}
onConfirm={event => {
control.send(
new lib.ModDeleteRequest(mod.name, mod.version)
).catch(notifyErrorHandler("Error deleting mod"));
}}
okText="Delete"
okButtonProps={{ danger: true }}
>
<Typography.Link type="danger">delete</Typography.Link>
</Popconfirm>
}
</Space>;
}
let uploadButton;
if (account.hasPermission("core.mod.upload")) {
uploadButton = <Upload
name="file"
accept=".zip"
multiple
headers={{
"X-Access-Token": control.connector.token || "",
}}
action={`${webRoot}api/upload-mod`}
>
<Button
onDragEnter={e => setIsDroppingFile(true)}
onDragLeave={e => {
// Only process if we are leaving the dropzone for an element that is not a child of the button
if ((e.currentTarget as Node).contains(e.relatedTarget as Node)) {
return;
}
setIsDroppingFile(false);
}}
onDrop={e => setIsDroppingFile(false)}
style={{
// Distinct border when dropping file
border: isDroppingFile ? "dashed 2px rgb(22, 119, 255)" : undefined,
}}
icon={isDroppingFile ? <InboxOutlined /> : <UploadOutlined />}
>Upload</Button>
</Upload>;
}
return <PageLayout nav={[{ name: "Mods" }]}>
<PageHeader title="Mods" />
<SectionHeader
title="Mod Packs"
extra={<Space>
{account.hasPermission("core.mod_pack.create") && <ImportModPackButton />}
{account.hasPermission("core.mod_pack.create") && <CreateModPackButton />}
</Space>}
/>
<Table
columns={[
{
title: "Name",
dataIndex: "name",
defaultSortOrder: "ascend",
sorter: (a, b) => strcmp(a.name, b.name),
},
{
title: "Factorio Version",
dataIndex: "factorioVersion",
sorter: (a, b) => a.integerFactorioVersion - b.integerFactorioVersion,
},
{
title: "Mods",
key: "roles",
render: (_, modPack) => modPack.mods.size,
},
]}
dataSource={[...modPacks.values()]}
pagination={false}
rowKey={modPack => Number(modPack.id)}
onRow={(modPack, rowIndex) => ({
onClick: event => {
navigate(`/mods/mod-packs/${modPack.id}/view`);
},
})}
/>
<SectionHeader title="Stored Mods" extra={uploadButton} />
<Upload.Dragger
className="save-list-dragger"
openFileDialogOnClick={false}
name="file"
accept=".zip"
multiple
headers={{
"X-Access-Token": control.connector.token || "",
}}
action={`${webRoot}api/upload-mod`}
showUploadList={false}
>
<div
style={{
position: "relative",
zIndex: "1010",
}}
onDragEnter={e => {
setIsDroppingFile(true);
}}
onDragLeave={e => {
if ((e.currentTarget as Node).contains(e.relatedTarget as Node)) {
return;
}
setIsDroppingFile(false);
}}
onDrop={e => setIsDroppingFile(false)}
>
<div
style={{
position: "absolute",
top: "0",
left: "0",
width: "100%",
height: "100%",
zIndex: "90",
backgroundColor: "#88888844",
borderRadius: "20px",
border: "dashed 2px rgb(22, 119, 255)",
display: isDroppingFile ? "block" : "none",
}}
>
<div
id="dropzone-icon"
style={{
fontSize: "72px",
color: "rgb(22, 119, 255)",
display: "flex",
zIndex: "100",
alignItems: "center",
justifyContent: "center",
flexDirection: "column",
height: "100%",
}}
>
<InboxOutlined />
<p style={{ fontSize: "24px", display: "block", textAlign: "center", marginTop: "8px" }}>
Drop to upload
</p>
</div>
</div>
<Table
columns={[
{
title: "Name",
dataIndex: "title",
defaultSortOrder: "ascend",
sorter: (a, b) => (
strcmp(a.name, b.name) || a.integerVersion - b.integerVersion
),
},
{
title: "Version",
key: "version",
align: "right",
render: (_, mod) => <>
{`${mod.version} `}
<Typography.Text type="secondary">{`/ ${mod.factorioVersion}`}</Typography.Text>
</>,
},
{
title: "Filename",
dataIndex: "filename",
responsive: ["xl"],
sorter: (a, b) => strcmp(a.filename, b.filename),
},
{
title: "Size",
key: "size",
responsive: ["lg"],
render: (_, mod) => lib.formatBytes(mod.size),
align: "right",
sorter: (a, b) => a.size - b.size,
},
{
title: "Action",
key: "action",
responsive: ["lg"],
render: (_, mod) => actions(mod),
},
]}
expandable={{
expandedRowRender: (mod: lib.ModInfo) => <ModDetails mod={mod} actions={actions} />,
expandedRowClassName: () => "no-expanded-padding",
}}
dataSource={[...mods.values()]}
pagination={false}
rowKey={mod => mod.id}
/>
</div>
</Upload.Dragger>
<PluginExtra component="ModsPage" />
</PageLayout>;
}