2021-02-07 06:38:58 +03:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-07-20 08:02:02 +03:00
|
|
|
import { Table, Typography, Button } from 'antd';
|
2021-02-07 06:38:58 +03:00
|
|
|
import { CheckCircleFilled, ExclamationCircleFilled } from '@ant-design/icons';
|
2020-12-27 08:36:48 +03:00
|
|
|
import classNames from 'classnames';
|
2020-12-23 09:05:17 +03:00
|
|
|
import { ColumnsType } from 'antd/es/table';
|
2021-02-07 06:38:58 +03:00
|
|
|
import format from 'date-fns/format';
|
2020-12-23 09:05:17 +03:00
|
|
|
|
2022-04-20 05:57:27 +03:00
|
|
|
import { MessageType } from '../../../types/chat';
|
|
|
|
import {
|
|
|
|
CHAT_HISTORY,
|
|
|
|
fetchData,
|
|
|
|
FETCH_INTERVAL,
|
|
|
|
UPDATE_CHAT_MESSGAE_VIZ,
|
|
|
|
} from '../../../utils/apis';
|
|
|
|
import { isEmptyObject } from '../../../utils/format';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { MessageVisiblityToggle } from '../../../components/MessageVisiblityToggle';
|
|
|
|
import { UserPopover } from '../../../components/UserPopover';
|
2020-12-23 09:05:17 +03:00
|
|
|
|
2020-12-26 07:29:15 +03:00
|
|
|
const { Title } = Typography;
|
2020-12-23 09:05:17 +03:00
|
|
|
|
2020-12-26 07:29:15 +03:00
|
|
|
function createUserNameFilters(messages: MessageType[]) {
|
2020-12-23 09:05:17 +03:00
|
|
|
const filtered = messages.reduce((acc, curItem) => {
|
2021-07-20 08:02:02 +03:00
|
|
|
const curAuthor = curItem.user.id;
|
2020-12-23 09:05:17 +03:00
|
|
|
if (!acc.some(item => item.text === curAuthor)) {
|
|
|
|
acc.push({ text: curAuthor, value: curAuthor });
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// sort by name
|
|
|
|
return filtered.sort((a, b) => {
|
|
|
|
const nameA = a.text.toUpperCase(); // ignore upper and lowercase
|
|
|
|
const nameB = b.text.toUpperCase(); // ignore upper and lowercase
|
|
|
|
if (nameA < nameB) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nameA > nameB) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// names must be equal
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
2020-12-26 10:14:27 +03:00
|
|
|
export const OUTCOME_TIMEOUT = 3000;
|
2020-12-23 09:05:17 +03:00
|
|
|
|
|
|
|
export default function Chat() {
|
|
|
|
const [messages, setMessages] = useState([]);
|
2020-12-26 07:29:15 +03:00
|
|
|
const [selectedRowKeys, setSelectedRows] = useState([]);
|
2020-12-26 10:14:27 +03:00
|
|
|
const [bulkProcessing, setBulkProcessing] = useState(false);
|
|
|
|
const [bulkOutcome, setBulkOutcome] = useState(null);
|
2020-12-27 08:36:48 +03:00
|
|
|
const [bulkAction, setBulkAction] = useState('');
|
2020-12-26 10:14:27 +03:00
|
|
|
let outcomeTimeout = null;
|
2021-01-02 16:06:14 +03:00
|
|
|
let chatReloadInterval = null;
|
2020-12-23 09:05:17 +03:00
|
|
|
|
|
|
|
const getInfo = async () => {
|
|
|
|
try {
|
2020-12-27 04:36:46 +03:00
|
|
|
const result = await fetchData(CHAT_HISTORY, { auth: true });
|
2020-12-30 01:59:43 +03:00
|
|
|
if (isEmptyObject(result)) {
|
|
|
|
setMessages([]);
|
|
|
|
} else {
|
|
|
|
setMessages(result);
|
|
|
|
}
|
2020-12-23 09:05:17 +03:00
|
|
|
} catch (error) {
|
2021-02-07 06:38:58 +03:00
|
|
|
console.log('==== error', error);
|
2020-12-23 09:05:17 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getInfo();
|
2021-01-02 16:06:14 +03:00
|
|
|
|
|
|
|
chatReloadInterval = setInterval(() => {
|
|
|
|
getInfo();
|
|
|
|
}, FETCH_INTERVAL);
|
|
|
|
|
2020-12-26 10:14:27 +03:00
|
|
|
return () => {
|
|
|
|
clearTimeout(outcomeTimeout);
|
2021-01-02 16:06:14 +03:00
|
|
|
clearTimeout(chatReloadInterval);
|
2020-12-26 10:14:27 +03:00
|
|
|
};
|
2020-12-23 09:05:17 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const nameFilters = createUserNameFilters(messages);
|
2021-02-07 06:38:58 +03:00
|
|
|
|
2020-12-30 01:59:43 +03:00
|
|
|
const rowSelection = {
|
2020-12-26 07:29:15 +03:00
|
|
|
selectedRowKeys,
|
2020-12-30 01:59:43 +03:00
|
|
|
onChange: (selectedKeys: string[]) => {
|
2020-12-26 07:29:15 +03:00
|
|
|
setSelectedRows(selectedKeys);
|
2020-12-23 09:05:17 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-02-07 06:38:58 +03:00
|
|
|
const updateMessage = message => {
|
|
|
|
const messageIndex = messages.findIndex(m => m.id === message.id);
|
|
|
|
messages.splice(messageIndex, 1, message);
|
|
|
|
setMessages([...messages]);
|
2021-01-10 02:17:06 +03:00
|
|
|
};
|
2020-12-27 08:36:48 +03:00
|
|
|
|
2020-12-26 10:14:27 +03:00
|
|
|
const resetBulkOutcome = () => {
|
2020-12-27 08:36:48 +03:00
|
|
|
outcomeTimeout = setTimeout(() => {
|
|
|
|
setBulkOutcome(null);
|
|
|
|
setBulkAction('');
|
|
|
|
}, OUTCOME_TIMEOUT);
|
2020-12-26 10:14:27 +03:00
|
|
|
};
|
2021-02-07 06:38:58 +03:00
|
|
|
const handleSubmitBulk = async bulkVisibility => {
|
2020-12-26 10:14:27 +03:00
|
|
|
setBulkProcessing(true);
|
|
|
|
const result = await fetchData(UPDATE_CHAT_MESSGAE_VIZ, {
|
|
|
|
auth: true,
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
visible: bulkVisibility,
|
|
|
|
idArray: selectedRowKeys,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-07 06:38:58 +03:00
|
|
|
if (result.success && result.message === 'changed') {
|
2020-12-26 10:14:27 +03:00
|
|
|
setBulkOutcome(<CheckCircleFilled />);
|
|
|
|
resetBulkOutcome();
|
|
|
|
|
|
|
|
// update messages
|
|
|
|
const updatedList = [...messages];
|
|
|
|
selectedRowKeys.map(key => {
|
|
|
|
const messageIndex = updatedList.findIndex(m => m.id === key);
|
2021-02-07 06:38:58 +03:00
|
|
|
const newMessage = { ...messages[messageIndex], visible: bulkVisibility };
|
2020-12-26 10:14:27 +03:00
|
|
|
updatedList.splice(messageIndex, 1, newMessage);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
setMessages(updatedList);
|
2020-12-26 12:01:51 +03:00
|
|
|
setSelectedRows([]);
|
2020-12-26 10:14:27 +03:00
|
|
|
} else {
|
|
|
|
setBulkOutcome(<ExclamationCircleFilled />);
|
|
|
|
resetBulkOutcome();
|
|
|
|
}
|
|
|
|
setBulkProcessing(false);
|
2021-02-07 06:38:58 +03:00
|
|
|
};
|
2020-12-27 08:36:48 +03:00
|
|
|
const handleSubmitBulkShow = () => {
|
|
|
|
setBulkAction('show');
|
|
|
|
handleSubmitBulk(true);
|
2021-02-07 06:38:58 +03:00
|
|
|
};
|
2020-12-27 08:36:48 +03:00
|
|
|
const handleSubmitBulkHide = () => {
|
|
|
|
setBulkAction('hide');
|
|
|
|
handleSubmitBulk(false);
|
2021-02-07 06:38:58 +03:00
|
|
|
};
|
2020-12-26 07:29:15 +03:00
|
|
|
|
|
|
|
const chatColumns: ColumnsType<MessageType> = [
|
2020-12-23 09:05:17 +03:00
|
|
|
{
|
|
|
|
title: 'Time',
|
|
|
|
dataIndex: 'timestamp',
|
|
|
|
key: 'timestamp',
|
2020-12-23 10:15:37 +03:00
|
|
|
className: 'timestamp-col',
|
2020-12-23 09:05:17 +03:00
|
|
|
defaultSortOrder: 'descend',
|
2021-02-07 06:38:58 +03:00
|
|
|
render: timestamp => {
|
2020-12-23 09:05:17 +03:00
|
|
|
const dateObject = new Date(timestamp);
|
2020-12-23 10:15:37 +03:00
|
|
|
return format(dateObject, 'PP pp');
|
2020-12-23 09:05:17 +03:00
|
|
|
},
|
|
|
|
sorter: (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),
|
2020-12-27 08:36:48 +03:00
|
|
|
width: 90,
|
2020-12-23 09:05:17 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'User',
|
2021-07-20 08:02:02 +03:00
|
|
|
dataIndex: 'user',
|
|
|
|
key: 'user',
|
2020-12-23 09:05:17 +03:00
|
|
|
className: 'name-col',
|
|
|
|
filters: nameFilters,
|
2021-07-20 08:02:02 +03:00
|
|
|
onFilter: (value, record) => record.user.id === value,
|
|
|
|
sorter: (a, b) => a.user.displayName.localeCompare(b.user.displayName),
|
2021-02-07 06:38:58 +03:00
|
|
|
sortDirections: ['ascend', 'descend'],
|
2020-12-23 10:15:37 +03:00
|
|
|
ellipsis: true,
|
2021-07-20 08:02:02 +03:00
|
|
|
render: user => {
|
|
|
|
const { displayName } = user;
|
|
|
|
return <UserPopover user={user}>{displayName}</UserPopover>;
|
|
|
|
},
|
2020-12-27 08:36:48 +03:00
|
|
|
width: 110,
|
2020-12-23 09:05:17 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Message',
|
|
|
|
dataIndex: 'body',
|
|
|
|
key: 'body',
|
|
|
|
className: 'message-col',
|
2020-12-27 08:36:48 +03:00
|
|
|
width: 320,
|
2020-12-27 08:46:40 +03:00
|
|
|
render: body => (
|
|
|
|
<div
|
|
|
|
className="message-contents"
|
|
|
|
// eslint-disable-next-line react/no-danger
|
|
|
|
dangerouslySetInnerHTML={{ __html: body }}
|
|
|
|
/>
|
2021-02-07 06:38:58 +03:00
|
|
|
),
|
2020-12-23 09:05:17 +03:00
|
|
|
},
|
|
|
|
{
|
2020-12-27 08:36:48 +03:00
|
|
|
title: '',
|
2021-07-20 08:02:02 +03:00
|
|
|
dataIndex: 'hiddenAt',
|
|
|
|
key: 'hiddenAt',
|
2020-12-27 08:36:48 +03:00
|
|
|
className: 'toggle-col',
|
2021-02-07 06:38:58 +03:00
|
|
|
filters: [
|
|
|
|
{ text: 'Visible messages', value: true },
|
|
|
|
{ text: 'Hidden messages', value: false },
|
|
|
|
],
|
2020-12-23 10:15:37 +03:00
|
|
|
onFilter: (value, record) => record.visible === value,
|
2021-07-20 08:02:02 +03:00
|
|
|
render: (hiddenAt, record) => (
|
|
|
|
<MessageVisiblityToggle isVisible={!hiddenAt} message={record} setMessage={updateMessage} />
|
2021-01-10 02:17:06 +03:00
|
|
|
),
|
2020-12-27 08:36:48 +03:00
|
|
|
width: 30,
|
2020-12-23 09:05:17 +03:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-12-27 08:36:48 +03:00
|
|
|
const bulkDivClasses = classNames({
|
|
|
|
'bulk-editor': true,
|
|
|
|
active: selectedRowKeys.length,
|
|
|
|
});
|
2021-02-07 06:38:58 +03:00
|
|
|
|
2020-12-23 09:05:17 +03:00
|
|
|
return (
|
2020-12-23 10:15:37 +03:00
|
|
|
<div className="chat-messages">
|
2021-02-15 09:20:25 +03:00
|
|
|
<Title>Chat Messages</Title>
|
2020-12-27 12:20:09 +03:00
|
|
|
<p>Manage the messages from viewers that show up on your stream.</p>
|
2020-12-27 08:36:48 +03:00
|
|
|
<div className={bulkDivClasses}>
|
2020-12-26 10:14:27 +03:00
|
|
|
<span className="label">Check multiple messages to change their visibility to: </span>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
size="small"
|
2020-12-27 08:36:48 +03:00
|
|
|
shape="round"
|
|
|
|
className="button"
|
|
|
|
loading={bulkAction === 'show' && bulkProcessing}
|
|
|
|
icon={bulkAction === 'show' && bulkOutcome}
|
|
|
|
disabled={!selectedRowKeys.length || (bulkAction && bulkAction !== 'show')}
|
|
|
|
onClick={handleSubmitBulkShow}
|
|
|
|
>
|
|
|
|
Show
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
size="small"
|
|
|
|
shape="round"
|
|
|
|
className="button"
|
|
|
|
loading={bulkAction === 'hide' && bulkProcessing}
|
|
|
|
icon={bulkAction === 'hide' && bulkOutcome}
|
|
|
|
disabled={!selectedRowKeys.length || (bulkAction && bulkAction !== 'hide')}
|
|
|
|
onClick={handleSubmitBulkHide}
|
2020-12-26 10:14:27 +03:00
|
|
|
>
|
2020-12-27 08:36:48 +03:00
|
|
|
Hide
|
2020-12-26 10:14:27 +03:00
|
|
|
</Button>
|
|
|
|
</div>
|
2020-12-23 09:05:17 +03:00
|
|
|
<Table
|
|
|
|
size="small"
|
2021-07-20 08:02:02 +03:00
|
|
|
className="table-container"
|
2021-08-14 22:22:05 +03:00
|
|
|
pagination={{ defaultPageSize: 100, showSizeChanger: true }}
|
2020-12-23 09:05:17 +03:00
|
|
|
scroll={{ y: 540 }}
|
2021-07-20 08:02:02 +03:00
|
|
|
rowClassName={record => (record.hiddenAt ? 'hidden' : '')}
|
2020-12-23 09:05:17 +03:00
|
|
|
dataSource={messages}
|
|
|
|
columns={chatColumns}
|
2021-02-07 06:38:58 +03:00
|
|
|
rowKey={row => row.id}
|
2020-12-26 07:29:15 +03:00
|
|
|
rowSelection={rowSelection}
|
2020-12-23 09:05:17 +03:00
|
|
|
/>
|
2021-02-07 06:38:58 +03:00
|
|
|
</div>
|
|
|
|
);
|
2020-12-23 09:05:17 +03:00
|
|
|
}
|