import React, { useContext, useState } from "react";
import { Button, List, Popconfirm, Progress, Space, Table, Tooltip, Upload } from "antd";
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
import LeftOutlined from "@ant-design/icons/LeftOutlined";
import libLink from "@clusterio/lib/link";
import ControlContext from "./ControlContext";
import CreateSaveModal from "./CreateSaveModal";
import SectionHeader from "./SectionHeader";
import { useSaves } from "../model/saves";
import { notifyErrorHandler } from "../util/notify";
function formatBytes(bytes) {
if (bytes === 0) {
return "0 Bytes";
}
let units = [" Bytes", " kB", " MB", " GB", " TB"];
let factor = 1000;
let power = Math.min(Math.floor(Math.log(bytes) / Math.log(factor)), units.length);
return (power > 0 ? (bytes / factor ** power).toFixed(2) : bytes) + units[power];
}
export default function SavesList(props) {
let control = useContext(ControlContext);
let saves = useSaves(props.instance.id);
let [starting, setStarting] = useState(false);
let [uploadingFiles, setUploadingFiles] = useState([]);
const saveTable =
<>
{save.name}
{save.loaded && }
{save.default && }
>,
sorter: (a, b) => a.name.localeCompare(b.name),
},
{
title: "Size",
key: "size",
responsive: ["sm"],
render: save => formatBytes(save.size),
align: "right",
sorter: (a, b) => a.size - b.size,
},
{
title: "Last Modified",
key: "mtime_ms",
render: save => new Date(save.mtime_ms).toLocaleString(),
sorter: (a, b) => a.mtime_ms - b.mtime_ms,
defaultSortOrder: "descend",
},
]}
dataSource={saves}
rowKey={save => save.name}
pagination={false}
expandable={{
columnWidth: 33,
expandRowByClick: true,
expandedRowRender: save =>
{
libLink.messages.deleteSave.send(
control, { instance_id: props.instance.id, save: save.name }
).catch(notifyErrorHandler("Error deleting save"));
}}
>
,
}}
/>;
function onChange(changeEvent) {
if (["done", "error"].includes(changeEvent.file.status)) {
if (changeEvent.file.status === "error") {
notifyErrorHandler("Error uploading file")(changeEvent.file.error);
}
let newUploadingFiles = [...uploadingFiles];
let index = newUploadingFiles.findIndex(f => f.uid === changeEvent.file.uid);
if (index !== -1) {
newUploadingFiles.splice(index, 1);
setUploadingFiles(newUploadingFiles);
}
return;
}
let file = {
uid: changeEvent.file.uid,
name: changeEvent.file.name,
percent: changeEvent.file.percent,
status: changeEvent.file.status,
};
let newUploadingFiles = [...uploadingFiles];
let index = newUploadingFiles.findIndex(f => f.uid === changeEvent.file.uid);
if (index !== -1) {
newUploadingFiles[index] = file;
} else {
newUploadingFiles.push(file);
}
setUploadingFiles(newUploadingFiles);
}
let uploadProps = {
name: "file",
accept: ".zip",
headers: {
"X-Access-Token": control.connector.token,
},
data: {
instance_id: props.instance.id,
},
showUploadList: false,
action: `${webRoot}api/upload-save`,
onChange,
};
return
/>
{saveTable}
{uploadingFiles.length ?
{uploadingFiles.map(file =>
{file.name}
)}
: null}
;
}