mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 23:30:41 +09:00
Add notes to hosts, instances, users and other resources (#1000)
This commit is contained in:
1 parent
ee39945c83
commit
cbf2b10782
25 files changed
+830
-52
No files matched your search
@@ -16,6 +16,7 @@ import { useHost } from "../model/host";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import ResourceNotes from "./ResourceNotes";
|
||||
import {
|
||||
MetricCpuRatio, MetricCpuUsed, MetricMemoryRatio, MetricMemoryUsed,
|
||||
MetricDiskUsed, MetricDiskRatio, MetricRelativeDate,
|
||||
@@ -182,6 +183,7 @@ export default function HostViewPage() {
|
||||
: null
|
||||
}
|
||||
</Descriptions>
|
||||
<ResourceNotes resourceType="host" resourceId={hostId} />
|
||||
{account.hasPermission("core.instance.list") && <>
|
||||
<Title level={5} style={{ marginTop: 16 }}>Instances</Title>
|
||||
<InstanceList instances={hostInstances} size="small" hideAssignedHost />
|
||||
|
||||
@@ -15,6 +15,7 @@ import ControlContext from "./ControlContext";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import ResourceNotes from "./ResourceNotes";
|
||||
import InstanceConfigTree from "./InstanceConfigTree";
|
||||
import LogConsole, { SelectMaxLogLevel } from "./LogConsole";
|
||||
import InstanceRcon from "./InstanceRcon";
|
||||
@@ -236,6 +237,7 @@ export default function InstanceViewPage() {
|
||||
extra={<InstanceButtons instance={instance} />}
|
||||
/>
|
||||
<InstanceDescription host={host} instance={instance} />
|
||||
<ResourceNotes resourceType="instance" resourceId={instanceId} />
|
||||
|
||||
{account.hasPermission("core.user.list") && <OnlinePlayersList instanceId={instanceId} />}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import FileExclamationOutlined from "@ant-design/icons/FileExclamationOutlined";
|
||||
import FileSyncOutlined from "@ant-design/icons/FileSyncOutlined";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import ResourceNotes from "./ResourceNotes";
|
||||
|
||||
const { useBreakpoint } = Grid;
|
||||
|
||||
@@ -58,6 +59,9 @@ export default function ModDetails<T extends lib.ModInfo | lib.ModRecord>(props:
|
||||
&& <Descriptions.Item label="Description" span={2}>{mod.description}</Descriptions.Item>
|
||||
}
|
||||
{Boolean(mod.author) && <Descriptions.Item label="Author" span={2}>{mod.author}</Descriptions.Item>}
|
||||
<Descriptions.Item label="Notes" span={2}>
|
||||
<ResourceNotes resourceType="mod" resourceId={`${mod.name}_${mod.version}`} compact />
|
||||
</Descriptions.Item>
|
||||
|
||||
<Descriptions.Item label="Mod Portal" span={2}>
|
||||
<Typography.Link href={`https://mods.factorio.com/mod/${mod.name}`}>
|
||||
|
||||
@@ -26,6 +26,7 @@ import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
import ResourceNotes from "./ResourceNotes";
|
||||
import ModDetails from "./ModDetails";
|
||||
import { InputPartialVersion } from "./InputVersion";
|
||||
import Link from "./Link";
|
||||
@@ -1399,5 +1400,6 @@ export default function ModPackViewPage() {
|
||||
</Col>
|
||||
</Row>
|
||||
</div>
|
||||
<ResourceNotes resourceType="mod_pack" resourceId={modPackId} />
|
||||
</PageLayout>;
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import { Button, Input, Space, Typography } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import { useAccount } from "../model/account";
|
||||
import { useNote } from "../model/notes";
|
||||
import { formatTimestamp } from "../util/time_format";
|
||||
import ControlContext from "./ControlContext";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
type ResourceNotesProps = {
|
||||
/** Type of resource, such as "host" or "instance". */
|
||||
resourceType: string;
|
||||
/** Id of the resource within its type. */
|
||||
resourceId: string | number;
|
||||
/** Render without the section header, for use inside tables and modals. */
|
||||
compact?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Shows the note attached to a resource with an editor for users that
|
||||
* have the core.note.update permission.
|
||||
*/
|
||||
export default function ResourceNotes(props: ResourceNotesProps) {
|
||||
// Remount on resource change so a draft is never saved to another resource
|
||||
return <ResourceNotesEditor key={lib.Note.idFor(props.resourceType, props.resourceId)} {...props} />;
|
||||
}
|
||||
|
||||
function ResourceNotesEditor(props: ResourceNotesProps) {
|
||||
const control = useContext(ControlContext);
|
||||
const account = useAccount();
|
||||
const [note, synced] = useNote(props.resourceType, props.resourceId);
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [draft, setDraft] = useState("");
|
||||
const [saving, setSaving] = useState(false);
|
||||
const canUpdate = account.hasPermission("core.note.update");
|
||||
const canView = account.hasPermission("core.note.subscribe");
|
||||
|
||||
// Pick up changes made by others while not editing
|
||||
const content = note?.content ?? "";
|
||||
useEffect(() => {
|
||||
if (!editing) {
|
||||
setDraft(content);
|
||||
}
|
||||
}, [content, editing]);
|
||||
|
||||
if (!canView && !canUpdate) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function save() {
|
||||
setSaving(true);
|
||||
control.send(new lib.NoteSetRequest(props.resourceType, String(props.resourceId), draft))
|
||||
.then(() => setEditing(false))
|
||||
.catch(notifyErrorHandler("Error saving note"))
|
||||
.finally(() => setSaving(false));
|
||||
}
|
||||
|
||||
function cancel() {
|
||||
setDraft(content);
|
||||
setEditing(false);
|
||||
}
|
||||
|
||||
const buttons = editing
|
||||
? <Space>
|
||||
<Button size="small" onClick={cancel} disabled={saving}>Cancel</Button>
|
||||
<Button size="small" type="primary" onClick={save} loading={saving}>Save</Button>
|
||||
</Space>
|
||||
: <Button size="small" onClick={() => setEditing(true)} disabled={!synced}>
|
||||
{note ? "Edit" : "Add note"}
|
||||
</Button>;
|
||||
|
||||
const body = editing
|
||||
? <Input.TextArea
|
||||
autoSize={{ minRows: 3 }}
|
||||
value={draft}
|
||||
onChange={e => setDraft(e.target.value)}
|
||||
onKeyDown={e => {
|
||||
if (e.key === "Escape") {
|
||||
cancel();
|
||||
} else if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
|
||||
save();
|
||||
}
|
||||
}}
|
||||
placeholder="Write a note visible to everyone with access to this page"
|
||||
autoFocus
|
||||
/>
|
||||
: <>
|
||||
{note
|
||||
? <div style={{ whiteSpace: "pre-wrap", overflowWrap: "anywhere" }}>{note.content}</div>
|
||||
: <Text type="secondary">{synced ? "No note" : "Loading"}</Text>
|
||||
}
|
||||
{note && (note.updatedBy || note.updatedAtMs) && <div>
|
||||
<Text type="secondary" style={{ fontSize: "0.85em" }}>
|
||||
Last edited{note.updatedBy ? ` by ${note.updatedBy}` : ""}
|
||||
{note.updatedAtMs ? ` on ${formatTimestamp(note.updatedAtMs)}` : ""}
|
||||
</Text>
|
||||
</div>}
|
||||
</>;
|
||||
|
||||
if (props.compact) {
|
||||
return <Space direction="vertical" style={{ width: "100%" }}>
|
||||
{body}
|
||||
{canUpdate && buttons}
|
||||
</Space>;
|
||||
}
|
||||
|
||||
return <>
|
||||
<SectionHeader title="Notes" extra={canUpdate ? buttons : undefined} />
|
||||
{body}
|
||||
</>;
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import ControlContext from "./ControlContext";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import ResourceNotes from "./ResourceNotes";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
|
||||
|
||||
@@ -327,6 +328,8 @@ export default function RoleViewPage() {
|
||||
<Input value={description} disabled={!canUpdate} onChange={e => setDescription(e.target.value)} />
|
||||
</div>
|
||||
|
||||
<ResourceNotes resourceType="role" resourceId={roleId} />
|
||||
|
||||
<PluginExtra component="RoleViewPageBeforePermissions" role={role} />
|
||||
|
||||
<h3>Permissions</h3>
|
||||
|
||||
@@ -13,6 +13,7 @@ import useTableQueryState from "../util/useTableQueryState";
|
||||
import ControlContext from "./ControlContext";
|
||||
import CreateSaveModal from "./CreateSaveModal";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
import ResourceNotes from "./ResourceNotes";
|
||||
import { useInstances } from "../model/instance";
|
||||
import { useSavesOfInstance } from "../model/saves";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
@@ -249,58 +250,61 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
expandable={{
|
||||
columnWidth: 33,
|
||||
expandRowByClick: true,
|
||||
expandedRowRender: save => <Space wrap style={{ marginBottom: 0 }}>
|
||||
{account.hasPermission("core.instance.start") && <Button
|
||||
loading={starting}
|
||||
disabled={props.instance.status !== "stopped"}
|
||||
onClick={() => {
|
||||
setStarting(true);
|
||||
control.sendTo(
|
||||
{ instanceId: props.instance.id! },
|
||||
new lib.InstanceStartRequest(save.name),
|
||||
).catch(
|
||||
notifyErrorHandler("Error loading save")
|
||||
).finally(
|
||||
() => { setStarting(false); }
|
||||
);
|
||||
}}
|
||||
>Load save</Button>}
|
||||
{account.hasPermission("core.instance.save.rename") && <RenameModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} save={save}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.copy") && <CopyModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} save={save}
|
||||
/>}
|
||||
{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(
|
||||
notifyErrorHandler("Error downloading save")
|
||||
);
|
||||
}}
|
||||
>Download</Button>}
|
||||
{account.hasPermission("core.instance.save.transfer") && <TransferModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} save={save}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.delete") && <Popconfirm
|
||||
title="Permanently delete save?"
|
||||
okText="Delete"
|
||||
placement="top"
|
||||
okButtonProps={{ danger: true }}
|
||||
onConfirm={() => {
|
||||
control.send(
|
||||
new lib.InstanceDeleteSaveRequest(props.instance.id!, save.name)
|
||||
).catch(notifyErrorHandler("Error deleting save"));
|
||||
}}
|
||||
>
|
||||
<Button danger disabled={hostOffline}>Delete</Button>
|
||||
</Popconfirm>}
|
||||
expandedRowRender: save => <Space direction="vertical" style={{ width: "100%" }}>
|
||||
<Space wrap>
|
||||
{account.hasPermission("core.instance.start") && <Button
|
||||
loading={starting}
|
||||
disabled={props.instance.status !== "stopped"}
|
||||
onClick={() => {
|
||||
setStarting(true);
|
||||
control.sendTo(
|
||||
{ instanceId: props.instance.id! },
|
||||
new lib.InstanceStartRequest(save.name),
|
||||
).catch(
|
||||
notifyErrorHandler("Error loading save")
|
||||
).finally(
|
||||
() => { setStarting(false); }
|
||||
);
|
||||
}}
|
||||
>Load save</Button>}
|
||||
{account.hasPermission("core.instance.save.rename") && <RenameModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} save={save}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.copy") && <CopyModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} save={save}
|
||||
/>}
|
||||
{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(
|
||||
notifyErrorHandler("Error downloading save")
|
||||
);
|
||||
}}
|
||||
>Download</Button>}
|
||||
{account.hasPermission("core.instance.save.transfer") && <TransferModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} save={save}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.delete") && <Popconfirm
|
||||
title="Permanently delete save?"
|
||||
okText="Delete"
|
||||
placement="top"
|
||||
okButtonProps={{ danger: true }}
|
||||
onConfirm={() => {
|
||||
control.send(
|
||||
new lib.InstanceDeleteSaveRequest(props.instance.id!, save.name)
|
||||
).catch(notifyErrorHandler("Error deleting save"));
|
||||
}}
|
||||
>
|
||||
<Button danger disabled={hostOffline}>Delete</Button>
|
||||
</Popconfirm>}
|
||||
</Space>
|
||||
<ResourceNotes resourceType="save" resourceId={save.id} compact />
|
||||
</Space>,
|
||||
}}
|
||||
/>;
|
||||
|
||||
@@ -17,6 +17,7 @@ import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
import ResourceNotes from "./ResourceNotes";
|
||||
import useRowNavigation from "../util/useRowNavigation";
|
||||
import Link from "./Link";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
@@ -290,6 +291,7 @@ export default function UserViewPage() {
|
||||
&& <Form.Item label="Ban reason">{user.banReason}</Form.Item>
|
||||
}
|
||||
</Form>
|
||||
<ResourceNotes resourceType="user" resourceId={user.id} />
|
||||
<SectionHeader title="Player stats" />
|
||||
<Descriptions size="small" bordered column={{ xs: 1, sm: 2, md: 2, lg: 2, xl: 2, xxl: 2 }}>
|
||||
<Descriptions.Item label="Total online time">
|
||||
|
||||
@@ -12,6 +12,7 @@ export * from "./model/instance";
|
||||
export * from "./model/is_dragging";
|
||||
export * from "./model/mod_pack";
|
||||
export * from "./model/mods";
|
||||
export * from "./model/notes";
|
||||
export * from "./model/roles";
|
||||
export * from "./model/saves";
|
||||
export * from "./model/system";
|
||||
@@ -38,6 +39,7 @@ export { default as PageHeader } from "./components/PageHeader";
|
||||
export { default as PageLayout } from "./components/PageLayout";
|
||||
export { default as SavesList } from "./components/SavesList";
|
||||
export { default as SectionHeader } from "./components/SectionHeader";
|
||||
export { default as ResourceNotes } from "./components/ResourceNotes";
|
||||
|
||||
export { default as Link } from "./components/Link";
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useCallback, useContext, useSyncExternalStore } from "react";
|
||||
import ControlContext from "../components/ControlContext";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
export function useNote(resourceType: string, resourceId?: string | number) {
|
||||
const [notes, synced] = useNotes();
|
||||
const note = resourceId !== undefined ? notes.get(lib.Note.idFor(resourceType, resourceId)) : undefined;
|
||||
return [note, synced] as const;
|
||||
}
|
||||
|
||||
export function useNotes() {
|
||||
const control = useContext(ControlContext);
|
||||
const subscribe = useCallback((callback: () => void) => control.notes.subscribe(callback), [control]);
|
||||
return useSyncExternalStore(subscribe, () => control.notes.getSnapshot());
|
||||
}
|
||||
@@ -73,6 +73,7 @@ export class Control extends lib.Link {
|
||||
mods = new lib.MapSubscriber(lib.ModUpdatesEvent, this);
|
||||
users = new lib.MapSubscriber(lib.UserUpdatesEvent, this);
|
||||
roles = new lib.MapSubscriber(lib.RoleUpdatesEvent, this);
|
||||
notes = new lib.MapSubscriber(lib.NoteUpdatesEvent, this);
|
||||
|
||||
/** Cache for factorio versions to avoid repeat calls to the controller */
|
||||
factorioVersions = new lib.ValueCache(this.requestFactorioVersions.bind(this));
|
||||
|
||||
Reference in new issue
Block a user