import React, { Fragment, useState, useContext } from "react";
import { useHistory } from "react-router-dom";
import { Button, Form, Input, Modal, PageHeader, 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 { libData, libHelpers, libLogging } from "@clusterio/lib";
import { useAccount } from "../model/account";
import { useModList } from "../model/mods";
import { useModPackList } from "../model/mod_pack";
import { notifyErrorHandler } from "../util/notify";
import ControlContext from "./ControlContext";
import PageLayout from "./PageLayout";
import PluginExtra from "./PluginExtra";
import SectionHeader from "./SectionHeader";
import ModDetails from "./ModDetails";
const strcmp = new Intl.Collator(undefined, { numerice: "true", sensitivity: "base" }).compare;
const { logger } = libLogging;
function ImportModPackButton() {
let control = useContext(ControlContext);
let history = useHistory();
let [visible, setVisible] = useState(false);
let [form] = Form.useForm();
function close() {
setVisible(false);
}
return <>
} onClick={() => { setVisible(true); }}>Import string
{ setVisible(false); }}
onOk={() => {
(async () => {
let values;
try {
values = await form.validateFields();
} catch {
return; // Validation failed
}
const modPack = libData.ModPack.fromModPackString(values.string);
await control.send(new libData.ModPackCreateRequest(modPack));
history.push(`/mods/mod-packs/${modPack.id}/view`);
})().catch(notifyErrorHandler("Error creating mod pack"));
}}
>
>;
}
function CreateModPackButton() {
let control = useContext(ControlContext);
let history = useHistory();
let [visible, setVisible] = useState(false);
let [form] = Form.useForm();
function close() {
setVisible(false);
}
return <>
} onClick={() => { setVisible(true); }}>Create
{ setVisible(false); }}
onOk={() => {
(async () => {
let values;
try {
values = await form.validateFields();
} catch {
return; // Validation failed
}
const modPack = libData.ModPack.fromJSON({});
modPack.name = values.name;
modPack.factorioVersion = values.factorioVersion;
if (values.description) { modPack.description = values.description; }
await control.send(new libData.ModPackCreateRequest(modPack.toJSON()));
history.push(`/mods/mod-packs/${modPack.id}/view`);
})().catch(notifyErrorHandler("Error creating mod pack"));
}}
>
>;
}
export default function ModsPage() {
let account = useAccount();
let control = useContext(ControlContext);
let history = useHistory();
let [modList] = useModList();
let [modPackList] = useModPackList();
function actions(mod) {
return
{account.hasPermission("core.mod.download")
&& {
control.send(
new libData.ModDownloadRequest(mod.name, mod.version)
).then(streamId => {
let url = new URL(webRoot, document.location);
url.pathname += `api/stream/${streamId}`;
document.location = url;
}).catch(
notifyErrorHandler("Error downloading save")
);
}}
>download
}
{account.hasPermission("core.mod.delete")
&& {
control.send(
new libData.ModDeleteRequest(mod.name, mod.version)
).catch(notifyErrorHandler("Error deleting mod"));
}}
okText="Delete"
okButtonProps={{ danger: true }}
>
delete
}
;
}
let uploadButton;
if (account.hasPermission("core.mod.upload")) {
uploadButton =
;
}
return
{account.hasPermission("core.mod-pack.create") && }
{account.hasPermission("core.mod-pack.create") && }
}
/>
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={modPackList}
pagination={false}
rowKey={modPack => modPack.id}
onRow={(modPack, rowIndex) => ({
onClick: event => {
history.push(`/mods/mod-packs/${modPack.id}/view`);
},
})}
/>
(
strcmp(a.name, b.name) || a.integerVersion - b.integerVersion
),
},
{
title: "Version",
key: "version",
align: "right",
render: mod => <>
{`${mod.version} `}
{`/ ${mod.factorioVersion}`}
>,
},
{
title: "Filename",
dataIndex: "filename",
responsive: ["xl"],
sorter: (a, b) => strcmp(a.filename, b.filename),
},
{
title: "Size",
key: "size",
responsive: ["lg"],
render: mod => libHelpers.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 => ,
expandedRowClassName: () => "no-expanded-padding",
}}
dataSource={modList}
pagination={false}
rowKey={mod => `${mod.name}_${mod.version}`}
/>
;
}