mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Persist table sort/filter state in the URL
Tables such as Hosts, Instances, Users, Roles, etc. now write their sort order, column filters and pagination into the URL query string, so reload, link-sharing and browser back/forward restore the same view (closes #927). Every parameter is namespaced with the table name so the URL is self-describing and two tables can share a page without colliding, e.g. ?user.sort=name&user.name=bob&user.admin=true or ?instance.name=foo. Adds two reusable hooks, exported from @clusterio/web_ui so plugins can use them too: - useTableQueryState: makes an antd Table controlled and round-trips its sort/filter/pagination through the URL via useSearchParams. The pure serialisation helpers live in util/tableQuery.ts and are unit tested. - useColumnSearch: a column-header text-search filter (matching the Users table style), persisted through the same filter mechanism. Applied to Hosts, Roles, Plugins, the instance list (standalone and on the host detail page), Users (main table and the online-players list on the instance detail page), instance saves, the per-instance user stats, the mod packs and stored mods tables, and the subspace_storage storage page. The storage page's resource search moves from a standalone box into the Resource column header, keeping its regex matching. The bundled user filter persists as separate readable params (user.name, user.admin, ...) via a per-column FilterCodec rather than one JSON blob. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9e727f9a13
commit
49eb594e5b
19 files changed
+639
-78
No files matched your search
@@ -169,7 +169,7 @@ export default function HostViewPage() {
|
||||
</Descriptions>
|
||||
{account.hasPermission("core.instance.list") && <>
|
||||
<Title level={5} style={{ marginTop: 16 }}>Instances</Title>
|
||||
<InstanceList instances={hostInstances} size="small" hideAssignedHost />
|
||||
<InstanceList instances={hostInstances} size="small" hideAssignedHost persistState />
|
||||
</>}
|
||||
{account.hasPermission("core.log.follow") && <>
|
||||
<Flex justify="space-between" align="baseline">
|
||||
|
||||
@@ -3,6 +3,8 @@ import { useNavigate } from "react-router-dom";
|
||||
import { Button, Flex, Form, InputNumber, Modal, Space, Switch, Table, Tooltip, Typography } from "antd";
|
||||
import { ExclamationCircleOutlined, CopyOutlined } from "@ant-design/icons";
|
||||
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import webUiPackage from "../../package.json";
|
||||
@@ -20,6 +22,8 @@ import {
|
||||
import { useHosts } from "../model/host";
|
||||
import { useSystems } from "../model/system";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "./useColumnSearch";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -205,6 +209,10 @@ export default function HostsPage() {
|
||||
const [showRatios, setShowRatios] = useState(true);
|
||||
const [showNumbers, setShowNumbers] = useState(false);
|
||||
const [showCpuModel, setShowCpuModel] = useState(false);
|
||||
const tableState = useTableQueryState<lib.HostDetails>({
|
||||
namespace: "host", defaultSortKey: "name", pagination: false,
|
||||
});
|
||||
const nameSearch = useColumnSearch<lib.HostDetails>(host => host.name, "Search hosts");
|
||||
|
||||
return <PageLayout nav={[{ name: "Hosts" }]}>
|
||||
<PageHeader
|
||||
@@ -213,12 +221,12 @@ export default function HostsPage() {
|
||||
/>
|
||||
<Table
|
||||
style={{ overflowX: "auto" }}
|
||||
columns={[
|
||||
columns={([
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
title: "CPU Model",
|
||||
@@ -302,10 +310,11 @@ export default function HostsPage() {
|
||||
</>,
|
||||
sorter: (a, b) => Number(a.connected) - Number(b.connected),
|
||||
},
|
||||
]}
|
||||
] satisfies ColumnsType<lib.HostDetails>).map(tableState.applyColumn)}
|
||||
dataSource={[...hosts.values()]}
|
||||
rowKey={host => host.id}
|
||||
pagination={false}
|
||||
pagination={tableState.pagination}
|
||||
onChange={tableState.onChange}
|
||||
onRow={(record, rowIndex) => ({
|
||||
onClick: event => {
|
||||
navigate(`/hosts/${record.id}/view`);
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useNavigate } from "react-router-dom";
|
||||
import { message, Button, Table, Space } from "antd";
|
||||
import CopyOutlined from "@ant-design/icons/CopyOutlined";
|
||||
import type { SizeType } from "antd/es/config-provider/SizeContext";
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
import type { ColumnType } from "antd/es/table/interface";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import { useSystems } from "../model/system";
|
||||
@@ -14,6 +14,8 @@ import InstanceControlButton, { InstanceControlButtonPermissions } from "./Insta
|
||||
import * as lib from "@clusterio/lib";
|
||||
import Link from "./Link";
|
||||
import { instancePublicAddress } from "../util/instance";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "./useColumnSearch";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -21,6 +23,8 @@ type InstanceListProps = {
|
||||
instances: ReadonlyMap<number, Readonly<lib.InstanceDetails>>;
|
||||
size?: SizeType;
|
||||
hideAssignedHost?: boolean;
|
||||
/** Persist sort/search state in the URL and show a search box (for the standalone Instances page). */
|
||||
persistState?: boolean;
|
||||
};
|
||||
|
||||
export default function InstanceList(props: InstanceListProps) {
|
||||
@@ -28,6 +32,11 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
let navigate = useNavigate();
|
||||
let [hosts] = useHosts();
|
||||
const [systems] = useSystems();
|
||||
const persist = props.persistState ?? false;
|
||||
const tableState = useTableQueryState<lib.InstanceDetails>({
|
||||
namespace: "instance", defaultSortKey: "name", pagination: false,
|
||||
});
|
||||
const nameSearch = useColumnSearch<lib.InstanceDetails>(instance => instance.name, "Search instances");
|
||||
|
||||
function hostName(hostId?: number) {
|
||||
if (hostId === undefined) {
|
||||
@@ -43,12 +52,13 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
return lib.integerPartialVersion(instance.factorioVersion);
|
||||
}
|
||||
|
||||
let columns: ColumnsType<lib.InstanceDetails> = [
|
||||
let columns: ColumnType<lib.InstanceDetails>[] = [
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
||||
...(persist ? nameSearch : {}),
|
||||
},
|
||||
{
|
||||
title: "Assigned Host",
|
||||
@@ -118,12 +128,15 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
columns.splice(1, 1);
|
||||
}
|
||||
|
||||
const finalColumns = persist ? columns.map(tableState.applyColumn) : columns;
|
||||
|
||||
return <Table
|
||||
size={props.size || "large"}
|
||||
columns={columns}
|
||||
columns={finalColumns}
|
||||
dataSource={[...props.instances.values()]}
|
||||
rowKey={instance => instance["id"]}
|
||||
pagination={false}
|
||||
pagination={persist ? tableState.pagination : false}
|
||||
onChange={persist ? tableState.onChange : undefined}
|
||||
onRow={(record, rowIndex) => ({
|
||||
onClick: event => {
|
||||
navigate(`/instances/${record.id}/view`);
|
||||
|
||||
@@ -111,7 +111,7 @@ export default function InstancesPage() {
|
||||
</Space>}
|
||||
/>
|
||||
|
||||
<InstanceList instances={instances} />
|
||||
<InstanceList instances={instances} persistState />
|
||||
<PluginExtra component="InstancesPage" />
|
||||
</PageLayout>;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import {
|
||||
ImportOutlined, PlusOutlined, SearchOutlined, DownloadOutlined,
|
||||
} from "@ant-design/icons";
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
@@ -18,6 +19,7 @@ import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import ModDetails from "./ModDetails";
|
||||
import { Dropzone } from "./Dropzone";
|
||||
import UploadButton from "./UploadButton";
|
||||
@@ -510,6 +512,12 @@ export default function ModsPage() {
|
||||
let navigate = useNavigate();
|
||||
let [mods] = useMods();
|
||||
let [modPacks] = useModPacks();
|
||||
const modPackTable = useTableQueryState<lib.ModPack>({
|
||||
namespace: "modPack", defaultSortKey: "name", pagination: false,
|
||||
});
|
||||
const modTable = useTableQueryState<lib.ModInfo>({
|
||||
namespace: "mod", defaultSortKey: "title", pagination: false,
|
||||
});
|
||||
|
||||
function actions(mod: lib.ModInfo) {
|
||||
return <Space>
|
||||
@@ -571,11 +579,10 @@ export default function ModsPage() {
|
||||
</Space>}
|
||||
/>
|
||||
<Table
|
||||
columns={[
|
||||
columns={([
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
},
|
||||
{
|
||||
@@ -588,9 +595,10 @@ export default function ModsPage() {
|
||||
key: "mods",
|
||||
render: (_, modPack) => modPack.mods.size,
|
||||
},
|
||||
]}
|
||||
] satisfies ColumnsType<lib.ModPack>).map(modPackTable.applyColumn)}
|
||||
dataSource={[...modPacks.values()]}
|
||||
pagination={false}
|
||||
pagination={modPackTable.pagination}
|
||||
onChange={modPackTable.onChange}
|
||||
rowKey={modPack => Number(modPack.id)}
|
||||
onRow={(modPack, rowIndex) => ({
|
||||
onClick: event => {
|
||||
@@ -617,11 +625,10 @@ export default function ModsPage() {
|
||||
>
|
||||
<Dropzone />
|
||||
<Table
|
||||
columns={[
|
||||
columns={([
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "title",
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => (
|
||||
strcmp(a.name, b.name) || a.integerVersion - b.integerVersion
|
||||
),
|
||||
@@ -655,13 +662,14 @@ export default function ModsPage() {
|
||||
responsive: ["lg"],
|
||||
render: (_, mod) => actions(mod),
|
||||
},
|
||||
]}
|
||||
] satisfies ColumnsType<lib.ModInfo>).map(modTable.applyColumn)}
|
||||
expandable={{
|
||||
expandedRowRender: (mod: lib.ModInfo) => <ModDetails mod={mod} actions={actions} />,
|
||||
expandedRowClassName: () => "no-expanded-padding",
|
||||
}}
|
||||
dataSource={[...mods.values()]}
|
||||
pagination={false}
|
||||
pagination={modTable.pagination}
|
||||
onChange={modTable.onChange}
|
||||
rowKey={mod => mod.id}
|
||||
/>
|
||||
</Upload.Dragger>
|
||||
|
||||
@@ -20,7 +20,7 @@ export default function OnlinePlayersList({ instanceId }: OnlinePlayersListProps
|
||||
return (
|
||||
<div style={{ marginTop: 16 }}>
|
||||
<h4>Online Players ({onlineUsers.length})</h4>
|
||||
<UsersTable instanceId={instanceId} onlyOnline size="small" pagination={{
|
||||
<UsersTable instanceId={instanceId} onlyOnline size="small" persistState pagination={{
|
||||
defaultPageSize: 10,
|
||||
showSizeChanger: true,
|
||||
pageSizeOptions: ["10", "20", "50", "100", "200"],
|
||||
|
||||
@@ -1,23 +1,38 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { Table } from "antd";
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
import CloseCircleFilled from "@ant-design/icons/CloseCircleFilled";
|
||||
import InfoCircleFilled from "@ant-design/icons/InfoCircleFilled";
|
||||
|
||||
import type { PluginWebApi } from "@clusterio/lib";
|
||||
import type { PluginWebApi, PluginWebpackEnvInfo } from "@clusterio/lib";
|
||||
|
||||
import notify from "../util/notify";
|
||||
import ControlContext from "./ControlContext";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PageHeader from "./PageHeader";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "./useColumnSearch";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
type PluginRow = {
|
||||
meta: PluginWebApi;
|
||||
info?: PluginWebpackEnvInfo;
|
||||
package?: any;
|
||||
};
|
||||
|
||||
|
||||
export default function PluginsPage() {
|
||||
const control = useContext(ControlContext);
|
||||
let navigate = useNavigate();
|
||||
let [pluginList, setPluginList] = useState<PluginWebApi[]>([]);
|
||||
const tableState = useTableQueryState<PluginRow>({
|
||||
namespace: "plugin", defaultSortKey: "name", pagination: false,
|
||||
});
|
||||
const nameSearch = useColumnSearch<PluginRow>(
|
||||
plugin => (plugin.info ? plugin.info.title : plugin.meta.name), "Search plugins"
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
@@ -30,7 +45,7 @@ export default function PluginsPage() {
|
||||
})();
|
||||
}, []);
|
||||
|
||||
let tableContents = [];
|
||||
let tableContents: PluginRow[] = [];
|
||||
for (let meta of pluginList) {
|
||||
if (control.plugins.has(meta.name)) {
|
||||
let plugin = control.plugins.get(meta.name)!;
|
||||
@@ -50,13 +65,13 @@ export default function PluginsPage() {
|
||||
return <PageLayout nav={[{ name: "Plugins" }]}>
|
||||
<PageHeader title="Plugins" />
|
||||
<Table
|
||||
columns={[
|
||||
columns={([
|
||||
{
|
||||
title: "Name",
|
||||
key: "name",
|
||||
render: (_, plugin) => (plugin.info ? plugin.info.title : plugin.meta.name),
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a.info ? a.info.title : a.meta.name, b.info ? b.info.title : b.meta.name),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
title: "Version",
|
||||
@@ -82,10 +97,11 @@ export default function PluginsPage() {
|
||||
sorter: (a, b) => Number(Boolean(a.package)) - Number(Boolean(b.package)),
|
||||
responsive: ["sm"],
|
||||
},
|
||||
]}
|
||||
] satisfies ColumnsType<PluginRow>).map(tableState.applyColumn)}
|
||||
dataSource={tableContents}
|
||||
rowKey={plugin => plugin.meta.name}
|
||||
pagination={false}
|
||||
pagination={tableState.pagination}
|
||||
onChange={tableState.onChange}
|
||||
onRow={plugin => ({
|
||||
onClick: event => {
|
||||
navigate(`/plugins/${plugin.meta.name}/view`);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { Button, Form, Input, Modal, Table } from "antd";
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
@@ -11,6 +12,8 @@ import { notifyErrorHandler } from "../util/notify";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "./useColumnSearch";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -63,6 +66,8 @@ export default function RolesPage() {
|
||||
let account = useAccount();
|
||||
let navigate = useNavigate();
|
||||
const [roles] = useRoles();
|
||||
const tableState = useTableQueryState<lib.Role>({ namespace: "role", pagination: false });
|
||||
const nameSearch = useColumnSearch<lib.Role>(role => role.name, "Search roles");
|
||||
|
||||
return <PageLayout nav={[{ name: "Roles" }]}>
|
||||
<PageHeader
|
||||
@@ -70,20 +75,22 @@ export default function RolesPage() {
|
||||
extra={account.hasPermission("core.role.create") ? <CreateRoleButton /> : undefined}
|
||||
/>
|
||||
<Table
|
||||
columns={[
|
||||
columns={([
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
title: "Description",
|
||||
dataIndex: "description",
|
||||
},
|
||||
]}
|
||||
] satisfies ColumnsType<lib.Role>).map(tableState.applyColumn)}
|
||||
dataSource={[...roles.values()]}
|
||||
pagination={false}
|
||||
pagination={tableState.pagination}
|
||||
rowKey={role => role.id}
|
||||
onChange={tableState.onChange}
|
||||
onRow={(role, rowIndex) => ({
|
||||
onClick: event => {
|
||||
navigate(`/roles/${role.id}/view`);
|
||||
|
||||
@@ -5,10 +5,12 @@ import {
|
||||
} from "antd";
|
||||
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
|
||||
import LeftOutlined from "@ant-design/icons/LeftOutlined";
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import ControlContext from "./ControlContext";
|
||||
import CreateSaveModal from "./CreateSaveModal";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
@@ -204,12 +206,15 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
let [saves] = useSavesOfInstance(props.instance.id);
|
||||
let [starting, setStarting] = useState(false);
|
||||
let [uploadingFiles, setUploadingFiles] = useState<File[]>([]);
|
||||
const tableState = useTableQueryState<lib.SaveDetails>({
|
||||
namespace: "save", defaultSortKey: "mtimeMs", defaultSortOrder: "descend", pagination: false,
|
||||
});
|
||||
|
||||
let hostOffline = ["unassigned", "unknown"].includes(props.instance.status!);
|
||||
const saveTable = <Table
|
||||
className="save-list-table"
|
||||
size="small"
|
||||
columns={[
|
||||
columns={([
|
||||
{
|
||||
title: "Name",
|
||||
key: "name",
|
||||
@@ -233,10 +238,11 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
key: "mtimeMs",
|
||||
render: (_, save) => new Date(save.mtimeMs).toLocaleString(),
|
||||
sorter: (a, b) => a.mtimeMs - b.mtimeMs,
|
||||
defaultSortOrder: "descend",
|
||||
},
|
||||
]}
|
||||
] satisfies ColumnsType<lib.SaveDetails>).map(tableState.applyColumn)}
|
||||
dataSource={[...saves.values()]}
|
||||
pagination={tableState.pagination}
|
||||
onChange={tableState.onChange}
|
||||
rowKey={save => save.name}
|
||||
expandable={{
|
||||
columnWidth: 33,
|
||||
|
||||
@@ -5,10 +5,12 @@ import {
|
||||
Row, Table, Tag, Select, Space, Spin, Switch,
|
||||
} from "antd";
|
||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import { useRoles } from "../model/roles";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import { useAccount } from "../model/account";
|
||||
import { useInstances } from "../model/instance";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -38,6 +40,9 @@ export default function UserViewPage() {
|
||||
const [user, synced] = useUser(userName);
|
||||
let [roles] = useRoles();
|
||||
let [form] = Form.useForm();
|
||||
const statsTable = useTableQueryState<[number, lib.PlayerStats]>({
|
||||
namespace: "stats", defaultSortKey: "instance", pagination: false,
|
||||
});
|
||||
let [rolesDirty, setRolesDirty] = useState<boolean>(false);
|
||||
let [banReasonDirty, setBanReasonDirty] = useState<boolean>(false);
|
||||
let [applyingRoles, setApplyingRoles] = useState<boolean>(false);
|
||||
@@ -294,12 +299,11 @@ export default function UserViewPage() {
|
||||
<SectionHeader title="Instance stats" />
|
||||
<Table
|
||||
size="small"
|
||||
columns={[
|
||||
columns={([
|
||||
{
|
||||
title: "Instance",
|
||||
key: "instance",
|
||||
render: (_, [id]) => instanceName(id),
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(instanceName(a[0]), instanceName(b[0])),
|
||||
},
|
||||
{
|
||||
@@ -327,9 +331,10 @@ export default function UserViewPage() {
|
||||
render: (_, [id]) => formatLastSeen(user, id),
|
||||
sorter: (a, b) => sortLastSeen(user, user, a[0], b[0]),
|
||||
},
|
||||
]}
|
||||
] satisfies ColumnsType<[number, lib.PlayerStats]>).map(statsTable.applyColumn)}
|
||||
dataSource={[...(user.instanceStats || []).entries()]}
|
||||
pagination={false}
|
||||
pagination={statsTable.pagination}
|
||||
onChange={statsTable.onChange}
|
||||
rowKey={([id]) => id}
|
||||
onRow={([id], rowIndex) => ({
|
||||
onClick: event => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { InputRef, Input, Space, Segmented, Button, Typography, Tag } from "antd
|
||||
import type { FilterDropdownProps } from "antd/es/table/interface";
|
||||
import { CheckOutlined, CloseOutlined, MinusOutlined } from "@ant-design/icons";
|
||||
import { UserDetails } from "@clusterio/lib";
|
||||
import type { FilterCodec } from "../util/tableQuery";
|
||||
const { Text } = Typography;
|
||||
|
||||
export interface UserFilter {
|
||||
@@ -24,6 +25,43 @@ function decodeFilter(value: string): UserFilter {
|
||||
}
|
||||
}
|
||||
|
||||
function triFromParam(value: string | null): boolean | undefined {
|
||||
if (value === "true") { return true; }
|
||||
if (value === "false") { return false; }
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists the bundled user filter as separate readable query parameters
|
||||
* (`filter.name` for the text search plus `filter.admin`/`filter.whitelisted`/
|
||||
* `filter.banned`) instead of one opaque JSON blob. Plug into
|
||||
* useTableQueryState via `filterCodecs: { name: userFilterCodec }`.
|
||||
*/
|
||||
export const userFilterCodec: FilterCodec = {
|
||||
decode(params, prefix) {
|
||||
const filter: UserFilter = {};
|
||||
const search = params.get(`${prefix}name`);
|
||||
if (search) { filter.search = search; }
|
||||
const admin = triFromParam(params.get(`${prefix}admin`));
|
||||
if (admin !== undefined) { filter.admin = admin; }
|
||||
const whitelisted = triFromParam(params.get(`${prefix}whitelisted`));
|
||||
if (whitelisted !== undefined) { filter.whitelisted = whitelisted; }
|
||||
const banned = triFromParam(params.get(`${prefix}banned`));
|
||||
if (banned !== undefined) { filter.banned = banned; }
|
||||
return Object.keys(filter).length ? [encodeFilter(filter)] : null;
|
||||
},
|
||||
encode(params, value, prefix) {
|
||||
const filter = value && value[0] ? decodeFilter(String(value[0])) : {};
|
||||
const set = (key: string, val: string | undefined) => (
|
||||
val !== undefined ? params.set(prefix + key, val) : params.delete(prefix + key)
|
||||
);
|
||||
set("name", filter.search || undefined);
|
||||
set("admin", filter.admin === undefined ? undefined : String(filter.admin));
|
||||
set("whitelisted", filter.whitelisted === undefined ? undefined : String(filter.whitelisted));
|
||||
set("banned", filter.banned === undefined ? undefined : String(filter.banned));
|
||||
},
|
||||
};
|
||||
|
||||
function triStateToSegmented(value?: boolean) {
|
||||
if (value === true) { return "yes"; }
|
||||
if (value === false) { return "no"; }
|
||||
|
||||
@@ -386,7 +386,7 @@ export default function UsersPage() {
|
||||
&& <BulkUserActionButton />}
|
||||
</Space>}
|
||||
/>
|
||||
<UsersTable instanceId={instanceId ?? undefined}/>
|
||||
<UsersTable instanceId={instanceId ?? undefined} persistState/>
|
||||
<PluginExtra component="UsersPage" />
|
||||
</PageLayout>;
|
||||
}
|
||||
@@ -13,8 +13,9 @@ import {
|
||||
useUsers, getUserStats, isUserOnline,
|
||||
} from "../model/user";
|
||||
|
||||
import { onFilterUser, Username, useUserFilter } from "./UsersFilters";
|
||||
import { onFilterUser, Username, useUserFilter, userFilterCodec } from "./UsersFilters";
|
||||
import Link from "./Link";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
|
||||
export interface UsersTableProps {
|
||||
/** Optional instance id. If provided, stats will be filtered to that instance only */
|
||||
@@ -25,14 +26,26 @@ export interface UsersTableProps {
|
||||
pagination?: false | object;
|
||||
/** Ant Design size prop */
|
||||
size?: "small" | "middle" | "large";
|
||||
/** Persist sort/filter/pagination state in the URL (for the standalone Users page). */
|
||||
persistState?: boolean;
|
||||
}
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
export default function UsersTable({ instanceId, onlyOnline = false, pagination, size }: UsersTableProps) {
|
||||
export default function UsersTable(
|
||||
{ instanceId, onlyOnline = false, pagination, size, persistState }: UsersTableProps
|
||||
) {
|
||||
const [roles, rolesSynced] = useRoles();
|
||||
const [users, usersSynced] = useUsers();
|
||||
const navigate = useNavigate();
|
||||
const callerPagination = pagination && typeof pagination === "object" ? pagination : undefined;
|
||||
const defaultPageSize = (callerPagination as { defaultPageSize?: number })?.defaultPageSize ?? 50;
|
||||
const tableState = useTableQueryState<lib.UserDetails>({
|
||||
namespace: "user",
|
||||
defaultSortKey: "name",
|
||||
pagination: pagination === false ? false : { defaultPageSize },
|
||||
filterCodecs: { name: userFilterCodec },
|
||||
});
|
||||
|
||||
const {filterDropdown, filterDropdownProps} = useUserFilter(true);
|
||||
|
||||
@@ -102,7 +115,7 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
||||
filters: roleFilters,
|
||||
filterMultiple: true,
|
||||
onFilter: (value: string | number | boolean, record: lib.UserDetails) => (
|
||||
record.roleIds.has(value as number)
|
||||
record.roleIds.has(Number(value))
|
||||
),
|
||||
render: (_: any, user: lib.UserDetails) => (
|
||||
[...user.roleIds].map((id) => (
|
||||
@@ -160,21 +173,36 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
||||
},
|
||||
];
|
||||
|
||||
const defaultPagination = pagination === undefined
|
||||
? {
|
||||
defaultPageSize: 50,
|
||||
showSizeChanger: true,
|
||||
pageSizeOptions: ["10", "20", "50", "100", "200"],
|
||||
showTotal: (total: number) => `${total} Users`,
|
||||
}
|
||||
: pagination;
|
||||
const displayPagination = {
|
||||
showSizeChanger: true,
|
||||
pageSizeOptions: ["10", "20", "50", "100", "200"],
|
||||
showTotal: (total: number) => `${total} Users`,
|
||||
};
|
||||
let tablePagination: false | object;
|
||||
if (pagination === false) {
|
||||
tablePagination = false;
|
||||
} else if (persistState) {
|
||||
// Controlled current/pageSize from the URL win over the caller's display props.
|
||||
tablePagination = { ...displayPagination, ...callerPagination, ...(tableState.pagination || {}) };
|
||||
} else {
|
||||
tablePagination = { ...displayPagination, ...(callerPagination ?? { defaultPageSize: 50 }) };
|
||||
}
|
||||
|
||||
let tableColumns = persistState ? columns.map(tableState.applyColumn) : columns;
|
||||
// Seed the online-only default when nothing for the Last Seen column is in the URL yet.
|
||||
if (persistState && onlyOnline && !("lastSeen" in tableState.filters)) {
|
||||
tableColumns = tableColumns.map(column => (
|
||||
column.key === "lastSeen" ? { ...column, filteredValue: ["online"] } : column
|
||||
));
|
||||
}
|
||||
|
||||
return (
|
||||
<Table
|
||||
columns={columns}
|
||||
columns={tableColumns}
|
||||
dataSource={data}
|
||||
rowKey={(user) => user.name}
|
||||
pagination={defaultPagination}
|
||||
pagination={tablePagination}
|
||||
onChange={persistState ? tableState.onChange : undefined}
|
||||
size={size}
|
||||
scroll={{ x: "max-content" }}
|
||||
loading={!usersSynced || !rolesSynced}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import React, { useRef } from "react";
|
||||
import { Input, InputRef } from "antd";
|
||||
import { SearchOutlined } from "@ant-design/icons";
|
||||
import type { FilterDropdownProps } from "antd/es/table/interface";
|
||||
|
||||
/**
|
||||
* Column props for a free-text search filter rendered in the column header
|
||||
* dropdown (the same style as the Users table). Spread the result onto a
|
||||
* column; the search value is persisted through the table's filter state by
|
||||
* {@link useTableQueryState}.
|
||||
*
|
||||
* @param getText Returns the searchable text for a row.
|
||||
* @param placeholder Placeholder shown in the search box.
|
||||
*/
|
||||
export default function useColumnSearch<T>(getText: (record: T) => string, placeholder = "Search") {
|
||||
const searchInput = useRef<InputRef>(null);
|
||||
|
||||
return {
|
||||
filterIcon: (filtered: boolean) => (
|
||||
<SearchOutlined style={{ color: filtered ? "#1677ff" : undefined }} />
|
||||
),
|
||||
filterDropdown: ({ selectedKeys, setSelectedKeys, confirm, clearFilters }: FilterDropdownProps) => (
|
||||
<div style={{ padding: 8, width: 240 }} onKeyDown={e => e.stopPropagation()}>
|
||||
<Input.Search
|
||||
ref={searchInput}
|
||||
placeholder={placeholder}
|
||||
value={selectedKeys[0] as string | undefined}
|
||||
allowClear
|
||||
onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
|
||||
onSearch={() => confirm({ closeDropdown: true })}
|
||||
onClear={() => {
|
||||
clearFilters?.({ closeDropdown: false });
|
||||
confirm({ closeDropdown: true });
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
),
|
||||
filterDropdownProps: {
|
||||
onOpenChange: (open: boolean) => {
|
||||
if (open) {
|
||||
setTimeout(() => searchInput.current?.select(), 100);
|
||||
}
|
||||
},
|
||||
},
|
||||
onFilter: (value: React.Key | boolean, record: T) => (
|
||||
getText(record).toLowerCase().includes(String(value).toLowerCase())
|
||||
),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user