2022-10-02 21:02:21 +03:00
|
|
|
import { Button, Col, Collapse, Row, Spin, Table, Tag } from 'antd';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { FC, useEffect, useState } from 'react';
|
2022-10-02 21:02:21 +03:00
|
|
|
import format from 'date-fns/format';
|
|
|
|
import { ColumnsType } from 'antd/lib/table';
|
2023-01-16 09:31:36 +03:00
|
|
|
import dynamic from 'next/dynamic';
|
2023-03-13 09:10:20 +03:00
|
|
|
import { ErrorBoundary } from 'react-error-boundary';
|
2022-07-21 06:42:23 +03:00
|
|
|
import ChatModeration from '../../../services/moderation-service';
|
2022-09-07 10:00:28 +03:00
|
|
|
import styles from './ChatModerationDetailsModal.module.scss';
|
2022-10-02 21:02:21 +03:00
|
|
|
import { formatUAstring } from '../../../utils/format';
|
2023-03-13 09:10:20 +03:00
|
|
|
import { ComponentError } from '../../ui/ComponentError/ComponentError';
|
2022-10-02 21:02:21 +03:00
|
|
|
|
|
|
|
const { Panel } = Collapse;
|
2022-07-17 03:30:05 +03:00
|
|
|
|
2023-01-16 09:31:36 +03:00
|
|
|
// Lazy loaded components
|
|
|
|
|
|
|
|
const DeleteOutlined = dynamic(() => import('@ant-design/icons/DeleteOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type ChatModerationDetailsModalProps = {
|
2022-07-21 06:42:23 +03:00
|
|
|
userId: string;
|
|
|
|
accessToken: string;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2022-07-17 03:30:05 +03:00
|
|
|
|
2022-07-21 06:42:23 +03:00
|
|
|
export interface UserDetails {
|
|
|
|
user: User;
|
|
|
|
connectedClients: Client[];
|
|
|
|
messages: Message[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Client {
|
|
|
|
messageCount: number;
|
|
|
|
userAgent: string;
|
|
|
|
connectedAt: Date;
|
|
|
|
geo: string;
|
2022-12-27 22:57:44 +03:00
|
|
|
id: number;
|
2022-07-21 06:42:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface Message {
|
|
|
|
id: string;
|
|
|
|
timestamp: Date;
|
|
|
|
user: null;
|
|
|
|
body: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface User {
|
|
|
|
id: string;
|
|
|
|
displayName: string;
|
|
|
|
displayColor: number;
|
|
|
|
createdAt: Date;
|
|
|
|
previousNames: string[];
|
|
|
|
scopes: string[];
|
|
|
|
isBot: boolean;
|
|
|
|
authenticated: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const removeMessage = async (messageId: string, accessToken: string) => {
|
|
|
|
try {
|
|
|
|
ChatModeration.removeMessage(messageId, accessToken);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ValueRow = ({ label, value }: { label: string; value: string }) => (
|
|
|
|
<Row justify="space-around" align="middle">
|
|
|
|
<Col span={12}>{label}</Col>
|
|
|
|
<Col span={12}>{value}</Col>
|
|
|
|
</Row>
|
|
|
|
);
|
|
|
|
|
|
|
|
const ConnectedClient = ({ client }: { client: Client }) => {
|
2022-10-02 21:02:21 +03:00
|
|
|
const { messageCount, connectedAt, geo } = client;
|
|
|
|
const connectedAtDate = format(new Date(connectedAt), 'PP pp');
|
2022-07-21 06:42:23 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<ValueRow label="Messages Sent" value={`${messageCount}`} />
|
2022-10-02 21:02:21 +03:00
|
|
|
{geo !== 'N/A' && <ValueRow label="Geo" value={geo} />}
|
|
|
|
<ValueRow label="Connected At" value={connectedAtDate} />
|
2022-07-21 06:42:23 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const UserColorBlock = ({ color }) => {
|
2022-09-02 05:37:11 +03:00
|
|
|
const bg = `var(--theme-color-users-${color})`;
|
2022-07-21 06:42:23 +03:00
|
|
|
return (
|
2022-10-02 21:02:21 +03:00
|
|
|
<div className={styles.colorBlock} style={{ backgroundColor: bg }}>
|
|
|
|
Color {color}
|
|
|
|
</div>
|
2022-07-21 06:42:23 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export const ChatModerationDetailsModal: FC<ChatModerationDetailsModalProps> = ({
|
|
|
|
userId,
|
|
|
|
accessToken,
|
|
|
|
}) => {
|
2022-07-21 06:42:23 +03:00
|
|
|
const [userDetails, setUserDetails] = useState<UserDetails | null>(null);
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
|
|
|
const getDetails = async () => {
|
|
|
|
try {
|
2022-10-02 21:02:21 +03:00
|
|
|
const response = await (
|
|
|
|
await fetch(`/api/moderation/chat/user/${userId}?accessToken=${accessToken}`)
|
|
|
|
).json();
|
2022-07-21 06:42:23 +03:00
|
|
|
setUserDetails(response);
|
|
|
|
setLoading(false);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getDetails();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (!userDetails) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { user, connectedClients, messages } = userDetails;
|
2022-10-02 21:02:21 +03:00
|
|
|
const { displayColor, createdAt, previousNames, scopes, isBot, authenticated } = user;
|
|
|
|
|
|
|
|
const totalMessagesSent = connectedClients.reduce((acc, client) => acc + client.messageCount, 0);
|
|
|
|
const createdAtDate = format(new Date(createdAt), 'PP pp');
|
|
|
|
|
|
|
|
const chatMessageColumns: ColumnsType<Message> = [
|
|
|
|
{
|
|
|
|
title: 'Message',
|
|
|
|
dataIndex: 'body',
|
|
|
|
key: 'body',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Sent At',
|
|
|
|
dataIndex: 'timestamp',
|
|
|
|
key: 'timestamp',
|
|
|
|
render: timestamp => format(new Date(timestamp), 'PP pp'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Delete',
|
|
|
|
key: 'delete',
|
2023-05-21 07:15:25 +03:00
|
|
|
render: (_text, record) => (
|
2022-10-02 21:02:21 +03:00
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
ghost
|
|
|
|
icon={<DeleteOutlined />}
|
|
|
|
onClick={() => removeMessage(record.id, accessToken)}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
];
|
2022-07-17 03:30:05 +03:00
|
|
|
return (
|
2023-03-13 09:10:20 +03:00
|
|
|
<ErrorBoundary
|
|
|
|
// eslint-disable-next-line react/no-unstable-nested-components
|
|
|
|
fallbackRender={({ error, resetErrorBoundary }) => (
|
|
|
|
<ComponentError
|
|
|
|
componentName="ChatModerationDetailsModal"
|
|
|
|
message={error.message}
|
|
|
|
retryFunction={resetErrorBoundary}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Spin spinning={loading}>
|
|
|
|
<UserColorBlock color={displayColor} />
|
|
|
|
{scopes?.map(scope => (
|
|
|
|
<Tag key={scope}>{scope}</Tag>
|
|
|
|
))}
|
|
|
|
{authenticated && <Tag>Authenticated</Tag>}
|
|
|
|
{isBot && <Tag>Bot</Tag>}
|
|
|
|
<ValueRow label="Messages Sent Across Clients" value={totalMessagesSent.toString()} />
|
|
|
|
<ValueRow label="User Created" value={createdAtDate} />
|
|
|
|
<ValueRow label="Known As" value={previousNames.join(',')} />
|
2022-10-02 21:02:21 +03:00
|
|
|
<Collapse accordion>
|
2023-03-13 09:10:20 +03:00
|
|
|
<Panel header="Currently Connected Clients" key="connected-clients">
|
|
|
|
<Collapse accordion>
|
|
|
|
{connectedClients.map(client => (
|
|
|
|
<Panel header={formatUAstring(client.userAgent)} key={client.id}>
|
|
|
|
<ConnectedClient client={client} />
|
|
|
|
</Panel>
|
|
|
|
))}
|
|
|
|
</Collapse>
|
2022-10-02 21:02:21 +03:00
|
|
|
</Panel>
|
2023-03-13 09:10:20 +03:00
|
|
|
<Collapse accordion>
|
|
|
|
<Panel header="Recent Chat Messages" key="chat-messages">
|
|
|
|
<Table
|
|
|
|
size="small"
|
|
|
|
pagination={null}
|
|
|
|
columns={chatMessageColumns}
|
|
|
|
dataSource={messages}
|
|
|
|
rowKey="id"
|
|
|
|
/>
|
|
|
|
</Panel>
|
|
|
|
</Collapse>
|
2022-10-02 21:02:21 +03:00
|
|
|
</Collapse>
|
2023-03-13 09:10:20 +03:00
|
|
|
</Spin>
|
|
|
|
</ErrorBoundary>
|
2022-07-17 03:30:05 +03:00
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|