import { Button, Col, Collapse, Row, Spin, Table, Tag } from 'antd';
import { FC, useEffect, useState } from 'react';
import format from 'date-fns/format';
import { DeleteOutlined } from '@ant-design/icons';
import { ColumnsType } from 'antd/lib/table';
import ChatModeration from '../../../services/moderation-service';
import styles from './ChatModerationDetailsModal.module.scss';
import { formatUAstring } from '../../../utils/format';
const { Panel } = Collapse;
export type ChatModerationDetailsModalProps = {
userId: string;
accessToken: string;
};
export interface UserDetails {
user: User;
connectedClients: Client[];
messages: Message[];
}
export interface Client {
messageCount: number;
userAgent: string;
connectedAt: Date;
geo: string;
}
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 }) => (
{label}
{value}
);
const ConnectedClient = ({ client }: { client: Client }) => {
const { messageCount, connectedAt, geo } = client;
const connectedAtDate = format(new Date(connectedAt), 'PP pp');
return (
{geo !== 'N/A' && }
);
};
const UserColorBlock = ({ color }) => {
const bg = `var(--theme-color-users-${color})`;
return (
Color {color}
);
};
export const ChatModerationDetailsModal: FC = ({
userId,
accessToken,
}) => {
const [userDetails, setUserDetails] = useState(null);
const [loading, setLoading] = useState(true);
const getDetails = async () => {
try {
const response = await (
await fetch(`/api/moderation/chat/user/${userId}?accessToken=${accessToken}`)
).json();
setUserDetails(response);
setLoading(false);
} catch (e) {
console.error(e);
}
};
useEffect(() => {
getDetails();
}, []);
if (!userDetails) {
return null;
}
const { user, connectedClients, messages } = userDetails;
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 = [
{
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',
render: (text, record) => (
}
onClick={() => removeMessage(record.id, accessToken)}
/>
),
},
];
return (
{scopes.map(scope => (
{scope}
))}
{authenticated && Authenticated}
{isBot && Bot}
{connectedClients.map(client => (
))}
);
};