From 7b7389526ba6a4847999539a672e924c370dd43d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 21 Jul 2025 17:41:13 +0000 Subject: [PATCH] Add online players list component to instance view page Co-authored-by: danielv --- .../src/components/InstanceViewPage.tsx | 3 + .../src/components/OnlinePlayersList.tsx | 119 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 packages/web_ui/src/components/OnlinePlayersList.tsx diff --git a/packages/web_ui/src/components/InstanceViewPage.tsx b/packages/web_ui/src/components/InstanceViewPage.tsx index b2ad9d8d..5d594678 100644 --- a/packages/web_ui/src/components/InstanceViewPage.tsx +++ b/packages/web_ui/src/components/InstanceViewPage.tsx @@ -18,6 +18,7 @@ import AssignInstanceModal from "./AssignInstanceModal"; import StartStopInstanceButton from "./StartStopInstanceButton"; import LoadScenarioModal from "./LoadScenarioModal"; import SavesList from "./SavesList"; +import OnlinePlayersList from "./OnlinePlayersList"; import { notifyErrorHandler } from "../util/notify"; import { useInstance } from "../model/instance"; import { useHost } from "../model/host"; @@ -225,6 +226,8 @@ export default function InstanceViewPage() { /> + {account.hasPermission("core.user.list") && } + { account.hasAllPermission("core.instance.save.list", "core.instance.save.subscribe") && diff --git a/packages/web_ui/src/components/OnlinePlayersList.tsx b/packages/web_ui/src/components/OnlinePlayersList.tsx new file mode 100644 index 00000000..844c8033 --- /dev/null +++ b/packages/web_ui/src/components/OnlinePlayersList.tsx @@ -0,0 +1,119 @@ +import React from "react"; +import { Table, Tag, Space } from "antd"; +import { ColumnsType } from "antd/es/table"; + +import * as lib from "@clusterio/lib"; + +import { useUsers, formatFirstSeen } from "../model/user"; +import { useRoles } from "../model/roles"; +import { formatDuration } from "../util/time_format"; +import Link from "./Link"; + +type OnlinePlayersListProps = { + instanceId: number; +}; + +export default function OnlinePlayersList({ instanceId }: OnlinePlayersListProps) { + const [users] = useUsers(); + const [roles] = useRoles(); + + // Filter users to only show those online on this specific instance + const onlineUsers = [...users.values()].filter(user => user.instances && user.instances.has(instanceId)); + + const columns: ColumnsType = [ + { + title: "Player", + key: "name", + render: (_, user) => ( + + + {user.name} + + + {user.isAdmin && Admin} + + + ), + sorter: (a, b) => a.name.localeCompare(b.name), + defaultSortOrder: "ascend", + }, + { + title: "Roles", + key: "roles", + render: (_, user) => ( + [...user.roleIds] + .map(id => ( + e.stopPropagation()}> + {(roles.get(id) || { name: id }).name} + + )) + ), + }, + { + title: "Play Time", + key: "playTime", + render: (_, user) => { + const instanceStats = user.instanceStats.get(instanceId); + return instanceStats?.onlineTimeMs + ? formatDuration(instanceStats.onlineTimeMs) + : "-"; + }, + sorter: (a, b) => { + const statsA = a.instanceStats.get(instanceId); + const statsB = b.instanceStats.get(instanceId); + return (statsA?.onlineTimeMs ?? 0) - (statsB?.onlineTimeMs ?? 0); + }, + }, + { + title: "Join Count", + key: "joinCount", + render: (_, user) => { + const instanceStats = user.instanceStats.get(instanceId); + return instanceStats?.joinCount ?? 0; + }, + sorter: (a, b) => { + const statsA = a.instanceStats.get(instanceId); + const statsB = b.instanceStats.get(instanceId); + return (statsA?.joinCount ?? 0) - (statsB?.joinCount ?? 0); + }, + }, + { + title: "First Seen", + key: "firstSeen", + render: (_, user) => formatFirstSeen(user, instanceId), + sorter: (a, b) => { + const statsA = a.instanceStats.get(instanceId); + const statsB = b.instanceStats.get(instanceId); + const firstSeenA = statsA?.firstJoinAt?.getTime() ?? 0; + const firstSeenB = statsB?.firstJoinAt?.getTime() ?? 0; + return firstSeenA - firstSeenB; + }, + }, + ]; + + if (onlineUsers.length === 0) { + return null; + } + + return ( +
+

Online Players ({onlineUsers.length})

+ user.name} + pagination={false} + size="small" + onRow={(user) => ({ + onClick: (event) => { + // Don't navigate if clicking on a link + if ((event.target as HTMLElement).closest("a")) { + return; + } + window.location.href = `/users/${user.name}/view`; + }, + })} + /> + + ); +}