Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

feat(admin): open the user detail modal from a users table row

Gabe Kangas5d3d444ae61d
diff --git a/web/components/admin/UserDetailsModal.tsx b/web/components/admin/UserDetailsModal.tsx
new file mode 100644
index 0000000000..e087ef7b6e
--- /dev/null
+++ b/web/components/admin/UserDetailsModal.tsx
@@ -0,0 +1,123 @@
+// Modal showing the full metadata for a single user (creation date, previous
+// names, connection info when available, ban status) plus ban/moderator
+// actions. Rendered by UserPopover (opened by clicking a user's name) and by
+// the admin users table (opened by clicking a row).
+
+import { FC } from 'react';
+import { Divider, Modal, Typography, Row, Col, Space } from 'antd';
+import { format, formatDistanceToNow } from 'date-fns';
+import { uniq } from 'lodash';
+
+import { BanUserButton } from './BanUserButton';
+import { ModeratorUserButton } from './ModeratorUserButton';
+
+import { User, UserConnectionInfo } from '../../types/chat';
+import { formatDisplayDate, formatUAstring } from '../../utils/format';
+
+export type UserDetailsModalProps = {
+ user: User;
+ open: boolean;
+ onClose: () => void;
+ connectionInfo?: UserConnectionInfo | null;
+};
+
+export const UserDetailsModal: FC<UserDetailsModalProps> = ({
+ user,
+ open,
+ onClose,
+ connectionInfo,
+}) => {
+ const { displayName, createdAt, previousNames, nameChangedAt, disabledAt } = user;
+ const { connectedAt, messageCount, userAgent } = connectionInfo || {};
+
+ let lastNameChangeDate = null;
+ const nameList = previousNames && [...previousNames];
+
+ if (previousNames && previousNames.length > 1 && nameChangedAt) {
+ lastNameChangeDate = new Date(nameChangedAt);
+ // reverse prev names for display purposes
+ nameList.reverse();
+ }
+
+ const createdAtDate = format(new Date(createdAt), 'PP pp');
+
+ const lastNameChangeDuration = lastNameChangeDate
+ ? formatDistanceToNow(lastNameChangeDate)
+ : null;
+
+ return (
+ <Modal
+ destroyOnClose
+ width={600}
+ cancelText="Close"
+ okButtonProps={{ style: { display: 'none' } }}
+ title={`User details: ${displayName}`}
+ open={open}
+ onOk={onClose}
+ onCancel={onClose}
+ >
+ <div className="user-details">
+ <Typography.Title level={4}>{displayName}</Typography.Title>
+ <p className="created-at">User created at {createdAtDate}.</p>
+ <Row gutter={16}>
+ {connectionInfo && (
+ <Col md={lastNameChangeDate ? 12 : 24}>
+ <Typography.Title level={5}>
+ This user is currently connected to Chat.
+ </Typography.Title>
+ <ul className="connection-info">
+ <li>
+ <strong>Active for:</strong> {formatDistanceToNow(new Date(connectedAt))}
+ </li>
+ <li>
+ <strong>Messages sent:</strong> {messageCount}
+ </li>
+ <li>
+ <strong>User Agent:</strong>
+ <br />
+ {formatUAstring(userAgent)}
+ </li>
+ </ul>
+ </Col>
+ )}
+ {lastNameChangeDate && (
+ <Col md={connectionInfo ? 12 : 24}>
+ <Typography.Title level={5}>This user is also seen as:</Typography.Title>
+ <ul className="previous-names-list">
+ {uniq(nameList).map((name, index) => (
+ <li className={index === 0 ? 'latest' : ''}>
+ <span className="user-name-item">{name}</span>
+ {index === 0 ? ` (Changed ${lastNameChangeDuration} ago)` : ''}
+ </li>
+ ))}
+ </ul>
+ </Col>
+ )}
+ </Row>
+ <Divider />
+ <Space direction="horizontal">
+ {disabledAt ? (
+ <>
+ This user was banned on <code>{formatDisplayDate(disabledAt)}</code>.
+ <br />
+ <br />
+ <BanUserButton
+ label="Unban this user"
+ user={user}
+ isEnabled={false}
+ onClick={onClose}
+ />
+ </>
+ ) : (
+ <BanUserButton label="Ban this user" user={user} isEnabled onClick={onClose} />
+ )}
+ <ModeratorUserButton user={user} onClick={onClose} />
+ </Space>
+ </div>
+ </Modal>
+ );
+};
+
+UserDetailsModal.defaultProps = {
+ connectionInfo: null,
+};
diff --git a/web/components/admin/UserPopover.tsx b/web/components/admin/UserPopover.tsx
index ff014bff6a..b64c26bcb0 100644
--- a/web/components/admin/UserPopover.tsx
+++ b/web/components/admin/UserPopover.tsx
@@ -1,15 +1,12 @@
// This displays a clickable user name (or whatever children element you provide), and displays a simple tooltip of created time. OnClick a modal with more information about the user is displayed.
import { useState, ReactNode, FC } from 'react';
-import { Divider, Modal, Typography, Row, Col, Space, Tooltip } from 'antd';
-import { format, formatDistanceToNow } from 'date-fns';
-import { uniq } from 'lodash';
+import { Tooltip } from 'antd';
+import { format } from 'date-fns';
-import { BanUserButton } from './BanUserButton';
-import { ModeratorUserButton } from './ModeratorUserButton';
+import { UserDetailsModal } from './UserDetailsModal';
import { User, UserConnectionInfo } from '../../types/chat';
-import { formatDisplayDate, formatUAstring } from '../../utils/format';
export type UserPopoverProps = {
user: User;
@@ -26,24 +23,7 @@ export const UserPopover: FC<UserPopoverProps> = ({ user, connectionInfo, childr
setIsModalOpen(false);
};
- const { displayName, createdAt, previousNames, nameChangedAt, disabledAt } = user;
- const { connectedAt, messageCount, userAgent } = connectionInfo || {};
-
- let lastNameChangeDate = null;
- const nameList = previousNames && [...previousNames];
-
- if (previousNames && previousNames.length > 1 && nameChangedAt) {
- lastNameChangeDate = new Date(nameChangedAt);
- // reverse prev names for display purposes
- nameList.reverse();
- }
-
- const dateObject = new Date(createdAt);
- const createdAtDate = format(dateObject, 'PP pp');
-
- const lastNameChangeDuration = lastNameChangeDate
- ? formatDistanceToNow(lastNameChangeDate)
- : null;
+ const createdAtDate = format(new Date(user.createdAt), 'PP pp');
return (
<>
@@ -66,80 +46,12 @@ export const UserPopover: FC<UserPopoverProps> = ({ user, connectionInfo, childr
</button>
</Tooltip>
- <Modal
- destroyOnClose
- width={600}
- cancelText="Close"
- okButtonProps={{ style: { display: 'none' } }}
- title={`User details: ${displayName}`}
+ <UserDetailsModal
+ user={user}
+ connectionInfo={connectionInfo}
open={isModalOpen}
- onOk={handleCloseModal}
- onCancel={handleCloseModal}
- >
- <div className="user-details">
- <Typography.Title level={4}>{displayName}</Typography.Title>
- <p className="created-at">User created at {createdAtDate}.</p>
- <Row gutter={16}>
- {connectionInfo && (
- <Col md={lastNameChangeDate ? 12 : 24}>
- <Typography.Title level={5}>
- This user is currently connected to Chat.
- </Typography.Title>
- <ul className="connection-info">
- <li>
- <strong>Active for:</strong> {formatDistanceToNow(new Date(connectedAt))}
- </li>
- <li>
- <strong>Messages sent:</strong> {messageCount}
- </li>
- <li>
- <strong>User Agent:</strong>
- <br />
- {formatUAstring(userAgent)}
- </li>
- </ul>
- </Col>
- )}
- {lastNameChangeDate && (
- <Col md={connectionInfo ? 12 : 24}>
- <Typography.Title level={5}>This user is also seen as:</Typography.Title>
- <ul className="previous-names-list">
- {uniq(nameList).map((name, index) => (
- <li className={index === 0 ? 'latest' : ''}>
- <span className="user-name-item">{name}</span>
- {index === 0 ? ` (Changed ${lastNameChangeDuration} ago)` : ''}
- </li>
- ))}
- </ul>
- </Col>
- )}
- </Row>
- <Divider />
- <Space direction="horizontal">
- {disabledAt ? (
- <>
- This user was banned on <code>{formatDisplayDate(disabledAt)}</code>.
- <br />
- <br />
- <BanUserButton
- label="Unban this user"
- user={user}
- isEnabled={false}
- onClick={handleCloseModal}
- />
- </>
- ) : (
- <BanUserButton
- label="Ban this user"
- user={user}
- isEnabled
- onClick={handleCloseModal}
- />
- )}
- <ModeratorUserButton user={user} onClick={handleCloseModal} />
- </Space>
- </div>
- </Modal>
+ onClose={handleCloseModal}
+ />
</>
);
};
diff --git a/web/pages/admin/users.tsx b/web/pages/admin/users.tsx
index 8b595f3907..ad78de0682 100644
--- a/web/pages/admin/users.tsx
+++ b/web/pages/admin/users.tsx
@@ -17,6 +17,7 @@ import { formatDateOnly } from '../../utils/format';
import { ClientTable } from '../../components/admin/ClientTable';
import { BannedIPsTable } from '../../components/admin/BannedIPsTable';
import { UserActionsDropdown } from '../../components/admin/UserActionsDropdown';
+import { UserDetailsModal } from '../../components/admin/UserDetailsModal';
import { Translation } from '../../components/ui/Translation/Translation';
import { Localization } from '../../types/localization';
@@ -54,6 +55,10 @@ export default function UsersAdmin() {
const [clients, setClients] = useState<Client[]>([]);
const [ipBans, setIPBans] = useState([]);
+ // The user whose detail modal is open (null when closed). Set by clicking a
+ // table row.
+ const [selectedUser, setSelectedUser] = useState<User | null>(null);
+
const getUsers = async () => {
setLoading(true);
try {
@@ -186,10 +191,26 @@ export default function UsersAdmin() {
title: '',
key: 'actions',
className: 'actions-col',
- render: (_, user) => <UserActionsDropdown user={user} onChanged={refresh} />,
+ // Stop clicks in the actions cell from also opening the row's detail modal.
+ render: (_, user) => (
+ <span
+ onClick={e => e.stopPropagation()}
+ onKeyDown={e => e.stopPropagation()}
+ role="presentation"
+ >
+ <UserActionsDropdown user={user} onChanged={refresh} />
+ </span>
+ ),
},
];
+ // Clicking a row opens that user's detail modal. Shared by the paginated "All
+ // Users" table and the Moderators/Banned list tables.
+ const rowInteraction = (record: User) => ({
+ onClick: () => setSelectedUser(record),
+ style: { cursor: 'pointer' },
+ });
+
// The "All Users" table is server-paginated, so its Created column sorts via
// the API across every page rather than only the rows in hand. The dedicated
// Moderators/Banned tables hold complete sets, so they keep the shared
@@ -238,6 +259,7 @@ export default function UsersAdmin() {
dataSource={users}
size="small"
rowKey="id"
+ onRow={rowInteraction}
pagination={{
current: currentPage,
pageSize: PAGE_SIZE,
@@ -294,6 +316,7 @@ export default function UsersAdmin() {
dataSource={data}
size="small"
rowKey="id"
+ onRow={rowInteraction}
pagination={{ pageSize: PAGE_SIZE, showSizeChanger: false, hideOnSinglePage: true }}
/>
);
@@ -328,6 +351,18 @@ export default function UsersAdmin() {
<Translation translationKey={Localization.Admin.Users.pageTitle} defaultText="Users" />
</Typography.Title>
<Tabs defaultActiveKey="all" items={items} />
+ {selectedUser && (
+ <UserDetailsModal
+ user={selectedUser}
+ open
+ onClose={() => {
+ setSelectedUser(null);
+ // A ban or moderator change made from the modal should be reflected
+ // in the lists.
+ refresh();
+ }}
+ />
+ )}
</div>
);
}