mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 08:13:12 +09:00
Add bulk save actions and name search to the saves table (#1256)
* Add save selection, name search and bulk transfer to the saves table Rows get checkboxes and the header checkbox selects every save matching the name search, including those on other pages. Selected saves can be copied or moved to another instance in one go, with copy on by default. Closes #1253 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add bulk copy, download and delete for selected saves Copy picks name-2.zip, name-3.zip and so on, skipping names already taken, because the host refuses to overwrite on copy. Downloads use a link with the download attribute so several in a row do not cancel each other. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Drop bulk copy from the selected saves actions Copy on the same instance is rarely needed in bulk, and Transfer with Copy ticked covers copying to another instance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
8865038af5
1 file changed
+154
-31
@@ -1,7 +1,7 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import {
|
||||
message, Button, Checkbox, Form, Input, Modal,
|
||||
Popconfirm, Select, Space, Table, Tooltip, Upload,
|
||||
Popconfirm, Select, Space, Table, Tooltip, Typography, Upload,
|
||||
} from "antd";
|
||||
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
|
||||
import LeftOutlined from "@ant-design/icons/LeftOutlined";
|
||||
@@ -10,6 +10,8 @@ import * as lib from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "../util/useColumnSearch";
|
||||
import type { Control } from "../util/websocket";
|
||||
import ControlContext from "./ControlContext";
|
||||
import CreateSaveModal from "./CreateSaveModal";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
@@ -107,17 +109,66 @@ function CopyModal(props: ModalProps) {
|
||||
}
|
||||
|
||||
|
||||
function TransferModal(props: ModalProps) {
|
||||
/** Runs action on each save in turn, returning the names that failed. */
|
||||
async function forEachSave(
|
||||
saves: lib.SaveDetails[],
|
||||
action: (save: lib.SaveDetails) => Promise<unknown>,
|
||||
errorText: string,
|
||||
) {
|
||||
const failed: string[] = [];
|
||||
for (const save of saves) {
|
||||
try {
|
||||
await action(save);
|
||||
} catch (err: any) {
|
||||
failed.push(save.name);
|
||||
notifyErrorHandler(`${errorText} ${save.name}`)(err);
|
||||
}
|
||||
}
|
||||
return failed;
|
||||
}
|
||||
|
||||
function downloadSave(control: Control, instanceId: number, name: string) {
|
||||
return control.send(new lib.InstanceDownloadSaveRequest(instanceId, name)).then(streamId => {
|
||||
let url = new URL(webRoot, document.location.href);
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
// A download link instead of navigating, so several downloads do not cancel each other.
|
||||
const link = document.createElement("a");
|
||||
link.href = url.href;
|
||||
link.download = name;
|
||||
link.click();
|
||||
});
|
||||
}
|
||||
|
||||
type TransferModalProps = {
|
||||
disabled: boolean;
|
||||
saves: lib.SaveDetails[];
|
||||
instanceId: number;
|
||||
onDone?: (failed: string[]) => void;
|
||||
};
|
||||
|
||||
function TransferModal(props: TransferModalProps) {
|
||||
let account = useAccount();
|
||||
let control = useContext(ControlContext);
|
||||
let [open, setOpen] = useState(false);
|
||||
let [form] = Form.useForm();
|
||||
let [instances] = useInstances();
|
||||
const single = props.saves.length === 1 ? props.saves[0] : undefined;
|
||||
const canCopy = account.hasPermission("core.instance.save.copy");
|
||||
|
||||
function transfer(values: { targetInstanceId: number, transferredName?: string, copy: boolean }) {
|
||||
return forEachSave(props.saves, save => control.send(new lib.InstanceTransferSaveRequest(
|
||||
props.instanceId,
|
||||
save.name,
|
||||
values.targetInstanceId,
|
||||
(single && values.transferredName) || save.name,
|
||||
values.copy,
|
||||
)), "Error transfering");
|
||||
}
|
||||
|
||||
return <>
|
||||
<Button disabled={props.disabled} onClick={() => setOpen(true)}>Transfer</Button>
|
||||
<Modal
|
||||
title="Transfer save"
|
||||
title={single ? "Transfer save" : `Transfer ${props.saves.length} saves`}
|
||||
okText="Transfer"
|
||||
open={open}
|
||||
onOk={() => form.submit()}
|
||||
@@ -128,22 +179,17 @@ function TransferModal(props: ModalProps) {
|
||||
form={form}
|
||||
labelCol={{ span: 6 }}
|
||||
wrapperCol={{ span: 18 }}
|
||||
initialValues={{ transferredName: props.save.name, copy: false }}
|
||||
initialValues={{ transferredName: single?.name, copy: !single && canCopy }}
|
||||
onFinish={values => {
|
||||
setOpen(false);
|
||||
let hide = message.loading("Transferring save...", 0);
|
||||
control.send(new lib.InstanceTransferSaveRequest(
|
||||
props.instanceId,
|
||||
props.save.name,
|
||||
values.targetInstanceId,
|
||||
values.transferredName || props.save.name,
|
||||
values.copy,
|
||||
)).then(() => {
|
||||
message.success("Transfer complete");
|
||||
let hide = message.loading(single ? "Transferring save..." : "Transferring saves...", 0);
|
||||
transfer(values).then(failed => {
|
||||
if (failed.length < props.saves.length) {
|
||||
message.success(failed.length ? "Transfer partially complete" : "Transfer complete");
|
||||
}
|
||||
form.resetFields();
|
||||
}).catch(
|
||||
notifyErrorHandler("Error transfering save")
|
||||
).finally(() => hide());
|
||||
props.onDone?.(failed);
|
||||
}).finally(() => hide());
|
||||
}}
|
||||
>
|
||||
<Form.Item
|
||||
@@ -170,23 +216,21 @@ function TransferModal(props: ModalProps) {
|
||||
</Select.Option>)}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
{single && <Form.Item
|
||||
name="transferredName"
|
||||
label="Transferred name"
|
||||
>
|
||||
<Input
|
||||
disabled={!account.hasAnyPermission("core.instance.save.rename", "core.instance.save.copy")}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form.Item>}
|
||||
<Form.Item
|
||||
name="copy"
|
||||
valuePropName="checked"
|
||||
label="Copy"
|
||||
tooltip="Copy instead of moving the save to the new instance."
|
||||
>
|
||||
<Checkbox
|
||||
disabled={!account.hasPermission("core.instance.save.copy")}
|
||||
/>
|
||||
<Checkbox disabled={!canCopy} />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
@@ -194,6 +238,53 @@ function TransferModal(props: ModalProps) {
|
||||
}
|
||||
|
||||
|
||||
type BulkActionsProps = {
|
||||
disabled: boolean;
|
||||
instanceId: number;
|
||||
saves: lib.SaveDetails[];
|
||||
onDone: (failed: string[]) => void;
|
||||
};
|
||||
|
||||
function BulkActions(props: BulkActionsProps) {
|
||||
const account = useAccount();
|
||||
const control = useContext(ControlContext);
|
||||
const [running, setRunning] = useState<string>();
|
||||
const count = props.saves.length;
|
||||
|
||||
function run(name: string, action: (save: lib.SaveDetails) => Promise<unknown>, errorText: string) {
|
||||
setRunning(name);
|
||||
forEachSave(props.saves, action, errorText)
|
||||
.then(props.onDone)
|
||||
.finally(() => setRunning(undefined));
|
||||
}
|
||||
|
||||
const disabled = props.disabled || running !== undefined;
|
||||
return <Space wrap>
|
||||
<Typography.Text type="secondary">{count} selected</Typography.Text>
|
||||
{account.hasPermission("core.instance.save.download") && <Button
|
||||
disabled={disabled}
|
||||
loading={running === "download"}
|
||||
onClick={() => run(
|
||||
"download", save => downloadSave(control, props.instanceId, save.name), "Error downloading"
|
||||
)}
|
||||
>Download</Button>}
|
||||
{account.hasPermission("core.instance.save.transfer") && <TransferModal
|
||||
disabled={disabled} instanceId={props.instanceId} saves={props.saves} onDone={props.onDone}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.delete") && <Popconfirm
|
||||
title={`Permanently delete ${count} ${count === 1 ? "save" : "saves"}?`}
|
||||
okText="Delete"
|
||||
okButtonProps={{ danger: true }}
|
||||
onConfirm={() => run("delete", save => control.send(
|
||||
new lib.InstanceDeleteSaveRequest(props.instanceId, save.name)
|
||||
), "Error deleting")}
|
||||
>
|
||||
<Button danger disabled={disabled} loading={running === "delete"}>Delete</Button>
|
||||
</Popconfirm>}
|
||||
</Space>;
|
||||
}
|
||||
|
||||
|
||||
export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
let account = useAccount();
|
||||
let control = useContext(ControlContext);
|
||||
@@ -203,6 +294,22 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
const tableState = useTableQueryState<lib.SaveDetails>({
|
||||
namespace: "save", defaultSortKey: "mtimeMs", defaultSortOrder: "descend", pagination: { defaultPageSize: 10 },
|
||||
});
|
||||
const nameSearch = useColumnSearch<lib.SaveDetails>(tableState, "name", save => save.name, "Search saves");
|
||||
const [selectedNames, setSelectedNames] = useState<string[]>([]);
|
||||
|
||||
const saveList = [...saves.values()];
|
||||
const selectedSaves = saveList.filter(save => selectedNames.includes(save.name));
|
||||
const nameFilter = tableState.filteredValue("name")?.[0]?.toLowerCase();
|
||||
const visibleNames = saveList
|
||||
.filter(save => !nameFilter || save.name.toLowerCase().includes(nameFilter))
|
||||
.map(save => save.name);
|
||||
const visibleSelected = visibleNames.filter(name => selectedNames.includes(name)).length;
|
||||
const canTransfer = account.hasPermission("core.instance.save.transfer");
|
||||
const canSelect = account.hasAnyPermission(
|
||||
"core.instance.save.download",
|
||||
"core.instance.save.transfer",
|
||||
"core.instance.save.delete",
|
||||
);
|
||||
|
||||
let hostOffline = ["unassigned", "unknown"].includes(props.instance.status!);
|
||||
const saveTable = <Table
|
||||
@@ -219,6 +326,8 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
</>,
|
||||
sorter: (a, b) => a.name.localeCompare(b.name),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
filteredValue: tableState.filteredValue("name"),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
title: "Size",
|
||||
@@ -237,10 +346,24 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
sortOrder: tableState.sortOrder("mtimeMs"),
|
||||
},
|
||||
]}
|
||||
dataSource={[...saves.values()]}
|
||||
dataSource={saveList}
|
||||
pagination={tableState.pagination}
|
||||
onChange={tableState.onChange}
|
||||
rowKey={save => save.name}
|
||||
rowSelection={canSelect ? {
|
||||
selectedRowKeys: selectedSaves.map(save => save.name),
|
||||
onChange: keys => setSelectedNames(keys as string[]),
|
||||
// Select all covers every save matching the search, not just the current page.
|
||||
columnTitle: <Checkbox
|
||||
disabled={!visibleNames.length}
|
||||
checked={visibleNames.length > 0 && visibleSelected === visibleNames.length}
|
||||
indeterminate={visibleSelected > 0 && visibleSelected < visibleNames.length}
|
||||
onChange={e => setSelectedNames(e.target.checked
|
||||
? [...new Set([...selectedNames, ...visibleNames])]
|
||||
: selectedNames.filter(name => !visibleNames.includes(name))
|
||||
)}
|
||||
/>,
|
||||
} : undefined}
|
||||
expandable={{
|
||||
columnWidth: 33,
|
||||
expandRowByClick: true,
|
||||
@@ -270,19 +393,13 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
{account.hasPermission("core.instance.save.download") && <Button
|
||||
disabled={hostOffline}
|
||||
onClick={() => {
|
||||
control.send(
|
||||
new lib.InstanceDownloadSaveRequest(props.instance.id!, save.name)
|
||||
).then(streamId => {
|
||||
let url = new URL(webRoot, document.location.href);
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
document.location.assign(url);
|
||||
}).catch(
|
||||
downloadSave(control, props.instance.id!, save.name).catch(
|
||||
notifyErrorHandler("Error downloading save")
|
||||
);
|
||||
}}
|
||||
>Download</Button>}
|
||||
{account.hasPermission("core.instance.save.transfer") && <TransferModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} save={save}
|
||||
{canTransfer && <TransferModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} saves={[save]}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.delete") && <Popconfirm
|
||||
title="Permanently delete save?"
|
||||
@@ -320,6 +437,12 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
|
||||
return <div>
|
||||
<SectionHeader title="Saves" extra={<Space wrap>
|
||||
{selectedSaves.length > 0 && <BulkActions
|
||||
disabled={hostOffline}
|
||||
instanceId={props.instance.id!}
|
||||
saves={selectedSaves}
|
||||
onDone={setSelectedNames}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.upload") && <Upload {...uploadProps} >
|
||||
<UploadButton disabled={hostOffline}>
|
||||
Upload save
|
||||
|
||||
Reference in new issue
Block a user