mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 15:33:12 +09:00
Use inline sort/filter getters instead of mapping columns
Replace the satisfies-ColumnsType + .map(applyColumn) approach with inline tableState.sortOrder()/filteredValue() getters set per column, so the column arrays stay plain literals (no satisfies, no constraints; UsersTable stays any[]). Move useColumnSearch into util/ next to useTableQueryState. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4e94092730
commit
8cacdc5112
12 files changed
+86
-67
No files matched your search
@@ -3,8 +3,6 @@ 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";
|
||||
@@ -23,7 +21,7 @@ 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";
|
||||
import useColumnSearch from "../util/useColumnSearch";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -221,17 +219,20 @@ export default function HostsPage() {
|
||||
/>
|
||||
<Table
|
||||
style={{ overflowX: "auto" }}
|
||||
columns={([
|
||||
columns={[
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
filteredValue: tableState.filteredValue("name"),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
title: "CPU Model",
|
||||
dataIndex: "cpuModel",
|
||||
sorter: (a, b) => strcmp(systems.get(a.id)?.cpuModel ?? "", systems.get(b.id)?.cpuModel ?? ""),
|
||||
sortOrder: tableState.sortOrder("cpuModel"),
|
||||
render: (_, host) => systems.get(host.id)?.cpuModel,
|
||||
hidden: !showCpuModel,
|
||||
},
|
||||
@@ -239,6 +240,7 @@ export default function HostsPage() {
|
||||
title: "CPU%",
|
||||
key: "cpuPct",
|
||||
sorter: (a, b) => (systems.get(a.id)?.cpuRatio ?? 0) - (systems.get(b.id)?.cpuRatio ?? 0),
|
||||
sortOrder: tableState.sortOrder("cpuPct"),
|
||||
render: (_, host) => <MetricCpuRatio system={systems.get(host.id)} />,
|
||||
hidden: !showRatios,
|
||||
},
|
||||
@@ -248,6 +250,7 @@ export default function HostsPage() {
|
||||
sorter: (a, b) => (
|
||||
(systems.get(a.id)?.cpuUsed ?? 0) - (systems.get(b.id)?.cpuUsed ?? 0)
|
||||
),
|
||||
sortOrder: tableState.sortOrder("cores"),
|
||||
render: (_, host) => <MetricCpuUsed system={systems.get(host.id)} />,
|
||||
hidden: !showNumbers,
|
||||
},
|
||||
@@ -257,6 +260,7 @@ export default function HostsPage() {
|
||||
sorter: (a, b) => (
|
||||
(systems.get(a.id)?.memoryRatio ?? 0) - (systems.get(b.id)?.memoryRatio ?? 0)
|
||||
),
|
||||
sortOrder: tableState.sortOrder("memPct"),
|
||||
render: (_, host) => <MetricMemoryRatio system={systems.get(host.id)} />,
|
||||
hidden: !showRatios,
|
||||
},
|
||||
@@ -266,6 +270,7 @@ export default function HostsPage() {
|
||||
sorter: (a, b) => (
|
||||
(systems.get(a.id)?.memoryUsed ?? 0) - (systems.get(b.id)?.memoryUsed ?? 0)
|
||||
),
|
||||
sortOrder: tableState.sortOrder("memory"),
|
||||
render: (_, host) => <MetricMemoryUsed system={systems.get(host.id)} />,
|
||||
hidden: !showNumbers,
|
||||
},
|
||||
@@ -273,6 +278,7 @@ export default function HostsPage() {
|
||||
title: "Disk%",
|
||||
key: "diskPct",
|
||||
sorter: (a, b) => (systems.get(a.id)?.diskAvailable ?? 0) - (systems.get(b.id)?.diskAvailable ?? 0),
|
||||
sortOrder: tableState.sortOrder("diskPct"),
|
||||
render: (_, host) => <MetricDiskRatio system={systems.get(host.id)} />,
|
||||
hidden: !showRatios,
|
||||
},
|
||||
@@ -282,6 +288,7 @@ export default function HostsPage() {
|
||||
sorter: (a, b) => (
|
||||
(systems.get(a.id)?.diskUsed ?? 0) - (systems.get(b.id)?.diskUsed ?? 0)
|
||||
),
|
||||
sortOrder: tableState.sortOrder("disk"),
|
||||
render: (_, host) => <MetricDiskUsed system={systems.get(host.id)} />,
|
||||
hidden: !showNumbers,
|
||||
},
|
||||
@@ -295,11 +302,13 @@ export default function HostsPage() {
|
||||
</Tooltip>}
|
||||
</Space>,
|
||||
sorter: (a, b) => strcmp(a.version, b.version),
|
||||
sortOrder: tableState.sortOrder("version"),
|
||||
},
|
||||
{
|
||||
title: "Public address",
|
||||
dataIndex: "publicAddress",
|
||||
sorter: (a, b) => strcmp(a.publicAddress??"", b.publicAddress??""),
|
||||
sortOrder: tableState.sortOrder("publicAddress"),
|
||||
},
|
||||
{
|
||||
title: "Connected",
|
||||
@@ -309,8 +318,9 @@ export default function HostsPage() {
|
||||
<RestartRequired system={systems.get(host.id)}/>
|
||||
</>,
|
||||
sorter: (a, b) => Number(a.connected) - Number(b.connected),
|
||||
sortOrder: tableState.sortOrder("connected"),
|
||||
},
|
||||
] satisfies ColumnsType<lib.HostDetails>).map(tableState.applyColumn)}
|
||||
]}
|
||||
dataSource={[...hosts.values()]}
|
||||
rowKey={host => host.id}
|
||||
pagination={tableState.pagination}
|
||||
|
||||
@@ -15,7 +15,7 @@ import * as lib from "@clusterio/lib";
|
||||
import Link from "./Link";
|
||||
import { instancePublicAddress } from "../util/instance";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "./useColumnSearch";
|
||||
import useColumnSearch from "../util/useColumnSearch";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -53,8 +53,9 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
filteredValue: tableState.filteredValue("name"),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
@@ -70,6 +71,7 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
<RestartRequired system={instance.assignedHost ? systems.get(instance.assignedHost) : undefined}/>
|
||||
</Space>,
|
||||
sorter: (a, b) => strcmp(hostName(a.assignedHost), hostName(b.assignedHost)),
|
||||
sortOrder: tableState.sortOrder("assignedHost"),
|
||||
responsive: ["sm"],
|
||||
},
|
||||
{
|
||||
@@ -94,6 +96,7 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
instancePublicAddress(a, hosts.get(a.assignedHost!) ?? null),
|
||||
instancePublicAddress(b, hosts.get(b.assignedHost!) ?? null)
|
||||
),
|
||||
sortOrder: tableState.sortOrder("publicAddress"),
|
||||
responsive: ["lg"],
|
||||
},
|
||||
{
|
||||
@@ -101,6 +104,7 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
key: "version",
|
||||
render: (_, instance) => instance.factorioVersion ?? "unknown",
|
||||
sorter: (a, b) => integerFactorioVersionOrDefault(a) - integerFactorioVersionOrDefault(b),
|
||||
sortOrder: tableState.sortOrder("version"),
|
||||
responsive: ["xl"],
|
||||
},
|
||||
{
|
||||
@@ -108,6 +112,7 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
key: "status",
|
||||
render: (_, instance) => <InstanceStatusTag status={instance["status"]} />,
|
||||
sorter: (a, b) => strcmp(a["status"], b["status"]),
|
||||
sortOrder: tableState.sortOrder("status"),
|
||||
},
|
||||
];
|
||||
|
||||
@@ -125,11 +130,9 @@ export default function InstanceList(props: InstanceListProps) {
|
||||
columns.splice(1, 1);
|
||||
}
|
||||
|
||||
const finalColumns = columns.map(tableState.applyColumn);
|
||||
|
||||
return <Table
|
||||
size={props.size || "large"}
|
||||
columns={finalColumns}
|
||||
columns={columns}
|
||||
dataSource={[...props.instances.values()]}
|
||||
rowKey={instance => instance["id"]}
|
||||
pagination={tableState.pagination}
|
||||
|
||||
@@ -6,8 +6,6 @@ import {
|
||||
import {
|
||||
ImportOutlined, PlusOutlined, SearchOutlined, DownloadOutlined,
|
||||
} from "@ant-design/icons";
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
@@ -579,23 +577,25 @@ export default function ModsPage() {
|
||||
</Space>}
|
||||
/>
|
||||
<Table
|
||||
columns={([
|
||||
columns={[
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
sortOrder: modPackTable.sortOrder("name"),
|
||||
},
|
||||
{
|
||||
title: "Factorio Version",
|
||||
dataIndex: "factorioVersion",
|
||||
sorter: (a, b) => a.integerFactorioVersion - b.integerFactorioVersion,
|
||||
sortOrder: modPackTable.sortOrder("factorioVersion"),
|
||||
},
|
||||
{
|
||||
title: "Mods",
|
||||
key: "mods",
|
||||
render: (_, modPack) => modPack.mods.size,
|
||||
},
|
||||
] satisfies ColumnsType<lib.ModPack>).map(modPackTable.applyColumn)}
|
||||
]}
|
||||
dataSource={[...modPacks.values()]}
|
||||
pagination={modPackTable.pagination}
|
||||
onChange={modPackTable.onChange}
|
||||
@@ -625,13 +625,14 @@ export default function ModsPage() {
|
||||
>
|
||||
<Dropzone />
|
||||
<Table
|
||||
columns={([
|
||||
columns={[
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "title",
|
||||
sorter: (a, b) => (
|
||||
strcmp(a.name, b.name) || a.integerVersion - b.integerVersion
|
||||
),
|
||||
sortOrder: modTable.sortOrder("title"),
|
||||
},
|
||||
{
|
||||
title: "Version",
|
||||
@@ -647,6 +648,7 @@ export default function ModsPage() {
|
||||
dataIndex: "filename",
|
||||
responsive: ["xl"],
|
||||
sorter: (a, b) => strcmp(a.filename, b.filename),
|
||||
sortOrder: modTable.sortOrder("filename"),
|
||||
},
|
||||
{
|
||||
title: "Size",
|
||||
@@ -655,6 +657,7 @@ export default function ModsPage() {
|
||||
render: (_, mod) => lib.formatBytes(mod.size),
|
||||
align: "right",
|
||||
sorter: (a, b) => a.size - b.size,
|
||||
sortOrder: modTable.sortOrder("size"),
|
||||
},
|
||||
{
|
||||
title: "Action",
|
||||
@@ -662,7 +665,7 @@ 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",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
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";
|
||||
|
||||
@@ -12,7 +11,7 @@ import ControlContext from "./ControlContext";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PageHeader from "./PageHeader";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "./useColumnSearch";
|
||||
import useColumnSearch from "../util/useColumnSearch";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -65,12 +64,14 @@ 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),
|
||||
sorter: (a, b) => strcmp(a.info ? a.info.title : a.meta.name, b.info ? b.info.title : b.meta.name),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
filteredValue: tableState.filteredValue("name"),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
@@ -89,15 +90,17 @@ export default function PluginsPage() {
|
||||
return plugin.package.version;
|
||||
},
|
||||
sorter: (a, b) => strcmp(a.meta.version, b.meta.version),
|
||||
sortOrder: tableState.sortOrder("version"),
|
||||
},
|
||||
{
|
||||
title: "Loaded",
|
||||
key: "loaded",
|
||||
render: (_, plugin) => (plugin.package ? "Yes" : null),
|
||||
sorter: (a, b) => Number(Boolean(a.package)) - Number(Boolean(b.package)),
|
||||
sortOrder: tableState.sortOrder("loaded"),
|
||||
responsive: ["sm"],
|
||||
},
|
||||
] satisfies ColumnsType<PluginRow>).map(tableState.applyColumn)}
|
||||
]}
|
||||
dataSource={tableContents}
|
||||
rowKey={plugin => plugin.meta.name}
|
||||
pagination={tableState.pagination}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
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";
|
||||
|
||||
@@ -13,7 +12,7 @@ import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "./useColumnSearch";
|
||||
import useColumnSearch from "../util/useColumnSearch";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -75,18 +74,20 @@ 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),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
filteredValue: tableState.filteredValue("name"),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
title: "Description",
|
||||
dataIndex: "description",
|
||||
},
|
||||
] satisfies ColumnsType<lib.Role>).map(tableState.applyColumn)}
|
||||
]}
|
||||
dataSource={[...roles.values()]}
|
||||
pagination={tableState.pagination}
|
||||
rowKey={role => role.id}
|
||||
|
||||
@@ -5,7 +5,6 @@ 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";
|
||||
|
||||
@@ -214,7 +213,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
const saveTable = <Table
|
||||
className="save-list-table"
|
||||
size="small"
|
||||
columns={([
|
||||
columns={[
|
||||
{
|
||||
title: "Name",
|
||||
key: "name",
|
||||
@@ -224,6 +223,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
{save.loadByDefault && <Tooltip title="Save loaded by default"><LeftOutlined /></Tooltip>}
|
||||
</>,
|
||||
sorter: (a, b) => a.name.localeCompare(b.name),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
},
|
||||
{
|
||||
title: "Size",
|
||||
@@ -232,14 +232,16 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
render: (_, save) => lib.formatBytes(save.size),
|
||||
align: "right",
|
||||
sorter: (a, b) => a.size - b.size,
|
||||
sortOrder: tableState.sortOrder("size"),
|
||||
},
|
||||
{
|
||||
title: "Last Modified",
|
||||
key: "mtimeMs",
|
||||
render: (_, save) => new Date(save.mtimeMs).toLocaleString(),
|
||||
sorter: (a, b) => a.mtimeMs - b.mtimeMs,
|
||||
sortOrder: tableState.sortOrder("mtimeMs"),
|
||||
},
|
||||
] satisfies ColumnsType<lib.SaveDetails>).map(tableState.applyColumn)}
|
||||
]}
|
||||
dataSource={[...saves.values()]}
|
||||
pagination={tableState.pagination}
|
||||
onChange={tableState.onChange}
|
||||
|
||||
@@ -5,7 +5,6 @@ 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";
|
||||
|
||||
@@ -299,24 +298,27 @@ export default function UserViewPage() {
|
||||
<SectionHeader title="Instance stats" />
|
||||
<Table
|
||||
size="small"
|
||||
columns={([
|
||||
columns={[
|
||||
{
|
||||
title: "Instance",
|
||||
key: "instance",
|
||||
render: (_, [id]) => instanceName(id),
|
||||
sorter: (a, b) => strcmp(instanceName(a[0]), instanceName(b[0])),
|
||||
sortOrder: statsTable.sortOrder("instance"),
|
||||
},
|
||||
{
|
||||
title: "Online time",
|
||||
key: "onlineTime",
|
||||
render: (_, [, stats]) => formatDuration(stats.onlineTimeMs || 0),
|
||||
sorter: (a, b) => (a[1].onlineTimeMs || 0) - (b[1].onlineTimeMs || 0),
|
||||
sortOrder: statsTable.sortOrder("onlineTime"),
|
||||
},
|
||||
{
|
||||
title: "Join count",
|
||||
key: "joinCoint",
|
||||
render: (_, [, stats]) => stats.joinCount || 0,
|
||||
sorter: (a, b) => (a[1].joinCount || 0) - (b[1].joinCount || 0),
|
||||
sortOrder: statsTable.sortOrder("joinCoint"),
|
||||
responsive: ["sm"],
|
||||
},
|
||||
{
|
||||
@@ -324,14 +326,16 @@ export default function UserViewPage() {
|
||||
key: "firstSeen",
|
||||
render: (_, [id]) => formatFirstSeen(user, id),
|
||||
sorter: (a, b) => sortFirstSeen(user, user, a[0], b[0]),
|
||||
sortOrder: statsTable.sortOrder("firstSeen"),
|
||||
},
|
||||
{
|
||||
title: "Last seen",
|
||||
key: "lastSeen",
|
||||
render: (_, [id]) => formatLastSeen(user, id),
|
||||
sorter: (a, b) => sortLastSeen(user, user, a[0], b[0]),
|
||||
sortOrder: statsTable.sortOrder("lastSeen"),
|
||||
},
|
||||
] satisfies ColumnsType<[number, lib.PlayerStats]>).map(statsTable.applyColumn)}
|
||||
]}
|
||||
dataSource={[...(user.instanceStats || []).entries()]}
|
||||
pagination={statsTable.pagination}
|
||||
onChange={statsTable.onChange}
|
||||
|
||||
@@ -2,7 +2,6 @@ import React from "react";
|
||||
import { Table, Tag } from "antd";
|
||||
import { SearchOutlined } from "@ant-design/icons";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import type { ColumnType } from "antd/es/table/interface";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
@@ -92,15 +91,16 @@ export default function UsersTable(
|
||||
}
|
||||
}
|
||||
|
||||
const columns: ColumnType<lib.UserDetails>[] = [
|
||||
const columns: any[] = [
|
||||
{
|
||||
title: "Name",
|
||||
key: "name",
|
||||
render: (_: any, user: lib.UserDetails) => (
|
||||
<Username user={user} withStatus />
|
||||
),
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a: lib.UserDetails, b: lib.UserDetails) => strcmp(a.name, b.name),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
filteredValue: tableState.filteredValue("name"),
|
||||
filterIcon: (filtered: boolean) => (
|
||||
<SearchOutlined style={{ color: filtered ? "#1677ff" : undefined }} />
|
||||
),
|
||||
@@ -113,6 +113,7 @@ export default function UsersTable(
|
||||
key: "roles",
|
||||
filters: roleFilters,
|
||||
filterMultiple: true,
|
||||
filteredValue: tableState.filteredValue("roles"),
|
||||
onFilter: (value: boolean | React.Key, record: lib.UserDetails) => (
|
||||
record.roleIds.has(Number(value))
|
||||
),
|
||||
@@ -136,6 +137,7 @@ export default function UsersTable(
|
||||
const statsB = getUserStats(b, instanceId);
|
||||
return (statsA?.onlineTimeMs ?? 0) - (statsB?.onlineTimeMs ?? 0);
|
||||
},
|
||||
sortOrder: tableState.sortOrder("onlineTime"),
|
||||
},
|
||||
{
|
||||
title: "Join Count",
|
||||
@@ -148,27 +150,30 @@ export default function UsersTable(
|
||||
const statsB = getUserStats(b, instanceId);
|
||||
return (statsA?.joinCount ?? 0) - (statsB?.joinCount ?? 0);
|
||||
},
|
||||
sortOrder: tableState.sortOrder("joinCount"),
|
||||
},
|
||||
{
|
||||
title: "First Seen",
|
||||
key: "firstSeen",
|
||||
filterMultiple: false,
|
||||
filters: durationFilters,
|
||||
filteredValue: tableState.filteredValue("firstSeen"),
|
||||
onFilter: (value: any, record: lib.UserDetails) => onFilterDuration(value, record, calculateFirstSeen),
|
||||
render: (_: any, user: lib.UserDetails) => formatFirstSeen(user, instanceId),
|
||||
sorter: (a: lib.UserDetails, b: lib.UserDetails) => sortFirstSeen(a, b, instanceId, instanceId),
|
||||
sortOrder: tableState.sortOrder("firstSeen"),
|
||||
},
|
||||
{
|
||||
title: "Last Seen",
|
||||
key: "lastSeen",
|
||||
filterMultiple: false,
|
||||
filters: durationFilters,
|
||||
defaultFilteredValue: onlyOnline ? ["online"] : undefined,
|
||||
// Default to online-only when nothing for this column is in the URL yet.
|
||||
filteredValue: tableState.filteredValue("lastSeen") ?? (onlyOnline ? ["online"] : null),
|
||||
onFilter: (value: any, record: lib.UserDetails) => onFilterDuration(value, record, calculateLastSeen),
|
||||
sorter: (a: lib.UserDetails, b: lib.UserDetails) => sortLastSeen(a, b, instanceId, instanceId),
|
||||
sortOrder: tableState.sortOrder("lastSeen"),
|
||||
render: (_: any, user: lib.UserDetails) => formatLastSeen(user, instanceId),
|
||||
// Responsive breaks defaultFilteredValue, see: https://github.com/ant-design/ant-design/issues/32847
|
||||
// responsive: ["lg"],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -185,17 +190,9 @@ export default function UsersTable(
|
||||
tablePagination = { ...displayPagination, ...callerPagination, ...(tableState.pagination || {}) };
|
||||
}
|
||||
|
||||
let tableColumns = columns.map(tableState.applyColumn);
|
||||
// Seed the online-only default when nothing for the Last Seen column is in the URL yet.
|
||||
if (onlyOnline && !("lastSeen" in tableState.filters)) {
|
||||
tableColumns = tableColumns.map(column => (
|
||||
column.key === "lastSeen" ? { ...column, filteredValue: ["online"] } : column
|
||||
));
|
||||
}
|
||||
|
||||
return (
|
||||
<Table
|
||||
columns={tableColumns}
|
||||
columns={columns}
|
||||
dataSource={data}
|
||||
rowKey={(user) => user.name}
|
||||
pagination={tablePagination}
|
||||
|
||||
@@ -44,4 +44,4 @@ export { default as Link } from "./components/Link";
|
||||
export { default as FactorioIcon } from "./components/FactorioIcon";
|
||||
|
||||
export { default as useTableQueryState } from "./util/useTableQueryState";
|
||||
export { default as useColumnSearch } from "./components/useColumnSearch";
|
||||
export { default as useColumnSearch } from "./util/useColumnSearch";
|
||||
File renamed without changes.
@@ -1,6 +1,6 @@
|
||||
import { useSearchParams } from "react-router-dom";
|
||||
import type { TablePaginationConfig } from "antd";
|
||||
import type { ColumnType, FilterValue, SorterResult } from "antd/es/table/interface";
|
||||
import type { FilterValue, SorterResult } from "antd/es/table/interface";
|
||||
|
||||
import {
|
||||
applyFilters, applyPagination, applySort,
|
||||
@@ -28,12 +28,12 @@ export interface TableQueryState<T> {
|
||||
filters: Record<string, FilterValue | null>,
|
||||
sorter: SorterResult<T> | SorterResult<T>[],
|
||||
) => void;
|
||||
/** Wrap each column to inject controlled `sortOrder`/`filteredValue` from the URL. */
|
||||
applyColumn: (column: ColumnType<T>) => ColumnType<T>;
|
||||
/** Controlled `sortOrder` for the column with this key (null unless it's the active sort). */
|
||||
sortOrder: (columnKey: string) => SortOrder | null;
|
||||
/** Controlled `filteredValue` for the column with this key (null when no filter is active). */
|
||||
filteredValue: (columnKey: string) => string[] | null;
|
||||
/** Controlled pagination ({@link current}/{@link pageSize}), or `false` when disabled. */
|
||||
pagination: false | TablePaginationConfig;
|
||||
/** The active column filters parsed from the URL, keyed by column key. */
|
||||
filters: Record<string, string[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,23 +83,17 @@ export default function useTableQueryState<T>(options: TableQueryStateOptions):
|
||||
setParams(next);
|
||||
},
|
||||
|
||||
applyColumn(column) {
|
||||
const key = String(column.key ?? column.dataIndex ?? "");
|
||||
const next: ColumnType<T> = { ...column };
|
||||
if (column.sorter) {
|
||||
next.sortOrder = key === sort.columnKey ? (sort.order ?? null) : null;
|
||||
}
|
||||
if (column.filters || column.onFilter || column.filterDropdown) {
|
||||
const codec = codecs[key];
|
||||
next.filteredValue = codec ? codec.decode(params, prefix) : (filters[key] ?? null);
|
||||
}
|
||||
return next;
|
||||
sortOrder(columnKey) {
|
||||
return columnKey === sort.columnKey ? (sort.order ?? null) : null;
|
||||
},
|
||||
|
||||
filteredValue(columnKey) {
|
||||
const codec = codecs[columnKey];
|
||||
return codec ? codec.decode(params, prefix) : (filters[columnKey] ?? null);
|
||||
},
|
||||
|
||||
pagination: paginationEnabled && page
|
||||
? { current: page.page, pageSize: page.pageSize }
|
||||
: false,
|
||||
|
||||
filters,
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { useContext, useEffect, useState } from "react";
|
||||
import { Table } from "antd";
|
||||
import type { ColumnsType } from "antd/es/table";
|
||||
|
||||
import {
|
||||
BaseWebPlugin, PageLayout, PageHeader, Control, ControlContext, notifyErrorHandler,
|
||||
@@ -87,12 +86,14 @@ function StoragePage() {
|
||||
<PageHeader title="Storage" />
|
||||
<Table
|
||||
className="subspace-storage-storage"
|
||||
columns={([
|
||||
columns={[
|
||||
{
|
||||
title: "Resource",
|
||||
key: "resource",
|
||||
...resourceSearch,
|
||||
onFilter: matchesResource,
|
||||
filteredValue: tableState.filteredValue("resource"),
|
||||
sortOrder: tableState.sortOrder("resource"),
|
||||
sorter: (a, b) => {
|
||||
let aName = getLocaleName(a[1].name);
|
||||
let bName = getLocaleName(b[1].name);
|
||||
@@ -121,9 +122,10 @@ function StoragePage() {
|
||||
key: "quantity",
|
||||
align: "right",
|
||||
sorter: (a, b) => a[1].count - b[1].count,
|
||||
sortOrder: tableState.sortOrder("quantity"),
|
||||
render: (_, item) => numberFormat.format(item[1].count),
|
||||
},
|
||||
] satisfies ColumnsType<[string, Item]>).map(tableState.applyColumn)}
|
||||
]}
|
||||
dataSource={storage}
|
||||
rowKey={item => item[0]}
|
||||
pagination={tableState.pagination}
|
||||
|
||||
Reference in new issue
Block a user