owncast/web/pages/chat/messages.tsx

249 lines
7.1 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
2021-07-20 07:02:02 +02:00
import { Table, Typography, Button } from 'antd';
import { CheckCircleFilled, ExclamationCircleFilled } from '@ant-design/icons';
2020-12-27 06:36:48 +01:00
import classNames from 'classnames';
import { ColumnsType } from 'antd/es/table';
import format from 'date-fns/format';
2021-06-22 05:19:00 +02:00
import { CHAT_HISTORY, fetchData, FETCH_INTERVAL, UPDATE_CHAT_MESSGAE_VIZ } from '../../utils/apis';
import { MessageType } from '../../types/chat';
import { isEmptyObject } from '../../utils/format';
import MessageVisiblityToggle from '../../components/message-visiblity-toggle';
2021-07-20 07:02:02 +02:00
import UserPopover from '../../components/user-popover';
const { Title } = Typography;
function createUserNameFilters(messages: MessageType[]) {
const filtered = messages.reduce((acc, curItem) => {
2021-07-20 07:02:02 +02:00
const curAuthor = curItem.user.id;
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;
export default function Chat() {
const [messages, setMessages] = useState([]);
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;
const getInfo = async () => {
try {
const result = await fetchData(CHAT_HISTORY, { auth: true });
if (isEmptyObject(result)) {
setMessages([]);
} else {
setMessages(result);
}
} catch (error) {
console.log('==== error', error);
}
};
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
};
}, []);
const nameFilters = createUserNameFilters(messages);
const rowSelection = {
selectedRowKeys,
onChange: (selectedKeys: string[]) => {
setSelectedRows(selectedKeys);
},
};
const updateMessage = message => {
const messageIndex = messages.findIndex(m => m.id === message.id);
messages.splice(messageIndex, 1, message);
setMessages([...messages]);
};
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
};
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,
},
});
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);
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);
};
2020-12-27 06:36:48 +01:00
const handleSubmitBulkShow = () => {
setBulkAction('show');
handleSubmitBulk(true);
};
2020-12-27 06:36:48 +01:00
const handleSubmitBulkHide = () => {
setBulkAction('hide');
handleSubmitBulk(false);
};
const chatColumns: ColumnsType<MessageType> = [
{
title: 'Time',
dataIndex: 'timestamp',
key: 'timestamp',
2020-12-23 08:15:37 +01:00
className: 'timestamp-col',
defaultSortOrder: 'descend',
render: timestamp => {
const dateObject = new Date(timestamp);
2020-12-23 08:15:37 +01:00
return format(dateObject, 'PP pp');
},
sorter: (a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),
2020-12-27 06:36:48 +01:00
width: 90,
},
{
title: 'User',
2021-07-20 07:02:02 +02:00
dataIndex: 'user',
key: 'user',
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),
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,
},
{
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 }}
/>
),
},
{
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',
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} />
),
2020-12-27 06:36:48 +01:00
width: 30,
},
];
2020-12-27 06:36:48 +01:00
const bulkDivClasses = classNames({
'bulk-editor': true,
active: selectedRowKeys.length,
});
return (
2020-12-23 08:15:37 +01:00
<div className="chat-messages">
<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>
<Table
size="small"
2021-07-20 07:02:02 +02:00
className="table-container"
pagination={{ pageSize: 100 }}
scroll={{ y: 540 }}
2021-07-20 07:02:02 +02:00
rowClassName={record => (record.hiddenAt ? 'hidden' : '')}
dataSource={messages}
columns={chatColumns}
rowKey={row => row.id}
rowSelection={rowSelection}
/>
</div>
);
}