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