mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-27 00:37:14 +09:00
Add proper filters
This commit is contained in:
1 parent
e7519975ec
commit
2053423864
2 files changed
+83
-23
No files matched your search
@@ -2,8 +2,9 @@ import React, { useContext, useState } from "react";
|
|||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { Static } from "@sinclair/typebox";
|
import { Static } from "@sinclair/typebox";
|
||||||
|
|
||||||
import { Button, Form, FormInstance, Input, Modal,
|
import {
|
||||||
Radio, Upload, UploadProps,
|
Button, Form, FormInstance, Input, Modal,
|
||||||
|
Radio, Upload, UploadProps
|
||||||
} from "antd";
|
} from "antd";
|
||||||
|
|
||||||
import { InboxOutlined } from "@ant-design/icons";
|
import { InboxOutlined } from "@ant-design/icons";
|
||||||
@@ -313,7 +314,7 @@ function BulkUserActionButton() {
|
|||||||
const [formAction, setFormAction] = useState<string | undefined>(undefined);
|
const [formAction, setFormAction] = useState<string | undefined>(undefined);
|
||||||
const [form] = Form.useForm();
|
const [form] = Form.useForm();
|
||||||
|
|
||||||
function onValuesChange({ action } : { action?: string }) {
|
function onValuesChange({ action }: { action?: string }) {
|
||||||
if (action) {
|
if (action) {
|
||||||
setFormAction(action);
|
setFormAction(action);
|
||||||
}
|
}
|
||||||
@@ -340,7 +341,7 @@ function BulkUserActionButton() {
|
|||||||
title="Bulk Actions"
|
title="Bulk Actions"
|
||||||
okText="Apply"
|
okText="Apply"
|
||||||
open={open}
|
open={open}
|
||||||
okButtonProps={{disabled: formAction === undefined}}
|
okButtonProps={{ disabled: formAction === undefined }}
|
||||||
onOk={() => { onOk().catch(notifyErrorHandler(`Error running ${formAction}`)); }}
|
onOk={() => { onOk().catch(notifyErrorHandler(`Error running ${formAction}`)); }}
|
||||||
onCancel={() => { setOpen(false); }}
|
onCancel={() => { setOpen(false); }}
|
||||||
destroyOnClose
|
destroyOnClose
|
||||||
@@ -356,9 +357,9 @@ function BulkUserActionButton() {
|
|||||||
? <Radio.Button value="restore">Restore</Radio.Button> : undefined}
|
? <Radio.Button value="restore">Restore</Radio.Button> : undefined}
|
||||||
</Radio.Group>
|
</Radio.Group>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
{formAction === "import" ? <UserBulkActionImport {...{setApplyAction, form}}/> : undefined}
|
{formAction === "import" ? <UserBulkActionImport {...{ setApplyAction, form }} /> : undefined}
|
||||||
{formAction === "export" ? <UserBulkActionExport {...{setApplyAction, form}}/> : undefined}
|
{formAction === "export" ? <UserBulkActionExport {...{ setApplyAction, form }} /> : undefined}
|
||||||
{formAction === "restore" ? <UserBulkActionImport restore {...{setApplyAction, form}}/> : undefined}
|
{formAction === "restore" ? <UserBulkActionImport restore {...{ setApplyAction, form }} /> : undefined}
|
||||||
</Form>
|
</Form>
|
||||||
</Modal>
|
</Modal>
|
||||||
</>;
|
</>;
|
||||||
|
|||||||
@@ -6,10 +6,13 @@ import { useNavigate } from "react-router-dom";
|
|||||||
import * as lib from "@clusterio/lib";
|
import * as lib from "@clusterio/lib";
|
||||||
|
|
||||||
import { useRoles } from "../model/roles";
|
import { useRoles } from "../model/roles";
|
||||||
import { useUsers } from "../model/user";
|
|
||||||
import { formatDuration } from "../util/time_format";
|
import { formatDuration } from "../util/time_format";
|
||||||
import {
|
import {
|
||||||
formatFirstSeen, formatLastSeen, sortFirstSeen, sortLastSeen,
|
formatFirstSeen,
|
||||||
|
formatLastSeen,
|
||||||
|
sortFirstSeen,
|
||||||
|
sortLastSeen,
|
||||||
|
useUsers,
|
||||||
} from "../model/user";
|
} from "../model/user";
|
||||||
import Link from "./Link";
|
import Link from "./Link";
|
||||||
|
|
||||||
@@ -20,7 +23,7 @@ export interface UsersTableProps {
|
|||||||
* – If omitted, global player statistics columns (Online Time, First/Last Seen) are shown.
|
* – If omitted, global player statistics columns (Online Time, First/Last Seen) are shown.
|
||||||
*/
|
*/
|
||||||
instanceId?: number;
|
instanceId?: number;
|
||||||
/** Show only players that are currently online. When instanceId is provided players must be online on that instance. */
|
/** Show only players that are currently online, works with instanceId to show online of a particular instance */
|
||||||
onlyOnline?: boolean;
|
onlyOnline?: boolean;
|
||||||
/** Ant Design pagination prop. Pass `false` to disable pagination. */
|
/** Ant Design pagination prop. Pass `false` to disable pagination. */
|
||||||
pagination?: false | object;
|
pagination?: false | object;
|
||||||
@@ -37,15 +40,18 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
|||||||
|
|
||||||
const [users] = useUsers();
|
const [users] = useUsers();
|
||||||
|
|
||||||
let data = [...users.values()];
|
const data = [...users.values()];
|
||||||
|
|
||||||
if (onlyOnline) {
|
// Determine online predicate based on instanceId
|
||||||
|
const isUserOnline = (user: lib.User) => {
|
||||||
if (instanceId !== undefined) {
|
if (instanceId !== undefined) {
|
||||||
data = data.filter(u => u.instances && u.instances.has(instanceId));
|
return user.instances && user.instances.has(instanceId);
|
||||||
} else {
|
|
||||||
data = data.filter(u => u.instances && u.instances.size > 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return user.instances && user.instances.size > 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prepare filters for roles column
|
||||||
|
const roleFilters = [...roles.values()].map(role => ({ text: role.name, value: role.id }));
|
||||||
|
|
||||||
const columns: any[] = [
|
const columns: any[] = [
|
||||||
{
|
{
|
||||||
@@ -66,8 +72,9 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
|||||||
filterIcon: (filtered: boolean) => (
|
filterIcon: (filtered: boolean) => (
|
||||||
<SearchOutlined style={{ color: filtered ? "#1677ff" : undefined }} />
|
<SearchOutlined style={{ color: filtered ? "#1677ff" : undefined }} />
|
||||||
),
|
),
|
||||||
onFilter: (value: string | number | boolean, record: lib.User) =>
|
onFilter: (value: string | number | boolean, record: lib.User) => record.name
|
||||||
record.name.toLowerCase().includes((value as string).toLowerCase()),
|
.toLowerCase()
|
||||||
|
.includes((value as string).toLowerCase()),
|
||||||
filterDropdownProps: {
|
filterDropdownProps: {
|
||||||
onOpenChange: (open: boolean) => open && setTimeout(() => searchInput.current?.select(), 100),
|
onOpenChange: (open: boolean) => open && setTimeout(() => searchInput.current?.select(), 100),
|
||||||
},
|
},
|
||||||
@@ -88,6 +95,9 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
|||||||
{
|
{
|
||||||
title: "Roles",
|
title: "Roles",
|
||||||
key: "roles",
|
key: "roles",
|
||||||
|
filters: roleFilters,
|
||||||
|
filterMultiple: true,
|
||||||
|
onFilter: (value: string | number | boolean, record: lib.User) => record.roleIds.has(value as number),
|
||||||
render: (_: any, user: lib.User) => (
|
render: (_: any, user: lib.User) => (
|
||||||
[...user.roleIds].map((id) => (
|
[...user.roleIds].map((id) => (
|
||||||
<Link key={id} to={`/roles/${id}/view`} onClick={(e) => e.stopPropagation()}>
|
<Link key={id} to={`/roles/${id}/view`} onClick={(e) => e.stopPropagation()}>
|
||||||
@@ -98,6 +108,20 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
// Helper to get last seen timestamp
|
||||||
|
function getLastSeenTimestamp(user: lib.User): number | undefined {
|
||||||
|
const stats = instanceId !== undefined ? user.instanceStats.get(instanceId) : user.playerStats;
|
||||||
|
if (!stats) { return undefined; }
|
||||||
|
if (stats.lastLeaveAt && stats.lastLeaveAt.getTime() > (stats.lastJoinAt?.getTime() ?? 0)) {
|
||||||
|
return stats.lastLeaveAt.getTime();
|
||||||
|
}
|
||||||
|
if (stats.lastJoinAt) {
|
||||||
|
return stats.lastJoinAt.getTime();
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
if (instanceId !== undefined) {
|
if (instanceId !== undefined) {
|
||||||
columns.push(
|
columns.push(
|
||||||
{
|
{
|
||||||
@@ -144,8 +168,10 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
|||||||
{
|
{
|
||||||
title: "Online time",
|
title: "Online time",
|
||||||
key: "onlineTime",
|
key: "onlineTime",
|
||||||
render: (_: any, user: lib.User) =>
|
render: (_: any, user: lib.User) => (user.playerStats?.onlineTimeMs
|
||||||
user.playerStats?.onlineTimeMs ? formatDuration(user.playerStats.onlineTimeMs) : null,
|
? formatDuration(user.playerStats.onlineTimeMs)
|
||||||
|
: null),
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
sorter: (a: lib.User, b: lib.User) => (a.playerStats?.onlineTimeMs ?? 0) - (b.playerStats?.onlineTimeMs ?? 0),
|
sorter: (a: lib.User, b: lib.User) => (a.playerStats?.onlineTimeMs ?? 0) - (b.playerStats?.onlineTimeMs ?? 0),
|
||||||
responsive: ["lg"],
|
responsive: ["lg"],
|
||||||
},
|
},
|
||||||
@@ -155,15 +181,48 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
|||||||
render: (_: any, user: lib.User) => formatFirstSeen(user),
|
render: (_: any, user: lib.User) => formatFirstSeen(user),
|
||||||
sorter: (a: lib.User, b: lib.User) => sortFirstSeen(a, b),
|
sorter: (a: lib.User, b: lib.User) => sortFirstSeen(a, b),
|
||||||
},
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
columns.push(
|
||||||
{
|
{
|
||||||
title: "Last seen",
|
title: "Last seen",
|
||||||
key: "lastSeen",
|
key: "lastSeen",
|
||||||
render: (_: any, user: lib.User) => formatLastSeen(user),
|
filterMultiple: false,
|
||||||
sorter: (a: lib.User, b: lib.User) => sortLastSeen(a, b),
|
defaultFilteredValue: onlyOnline ? ["online"] : undefined,
|
||||||
|
filters: [
|
||||||
|
{ text: "Online", value: "online" },
|
||||||
|
{ text: "24h", value: "24h" },
|
||||||
|
{ text: "7d", value: "7d" },
|
||||||
|
{ text: "30d", value: "30d" },
|
||||||
|
],
|
||||||
|
onFilter: (value: string | number | boolean, record: lib.User) => {
|
||||||
|
const online = isUserOnline(record);
|
||||||
|
if (value === "online") {
|
||||||
|
return online;
|
||||||
|
}
|
||||||
|
// Online players should appear in all buckets
|
||||||
|
if (online) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const ts = getLastSeenTimestamp(record);
|
||||||
|
if (!ts) { return false; }
|
||||||
|
const diff = Date.now() - ts;
|
||||||
|
switch (value) {
|
||||||
|
case "24h":
|
||||||
|
return diff <= 24 * 60 * 60 * 1000;
|
||||||
|
case "7d":
|
||||||
|
return diff <= 7 * 24 * 60 * 60 * 1000;
|
||||||
|
case "30d":
|
||||||
|
return diff <= 30 * 24 * 60 * 60 * 1000;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
sorter: (a: lib.User, b: lib.User) => sortLastSeen(a, b, instanceId, instanceId),
|
||||||
|
render: (_: any, user: lib.User) => formatLastSeen(user, instanceId),
|
||||||
responsive: ["lg"],
|
responsive: ["lg"],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
const defaultPagination = pagination === undefined
|
const defaultPagination = pagination === undefined
|
||||||
? {
|
? {
|
||||||
|
|||||||
Reference in new issue
Block a user