2020-10-17 03:50:00 +03:00
|
|
|
import { h, Component } from '/js/web_modules/preact.js';
|
|
|
|
import htm from '/js/web_modules/htm.js';
|
2020-11-18 04:29:03 +03:00
|
|
|
import Mark from '/js/web_modules/markjs/dist/mark.es6.min.js';
|
2020-10-17 03:50:00 +03:00
|
|
|
const html = htm.bind(h);
|
|
|
|
|
|
|
|
import {
|
2021-07-20 05:22:29 +03:00
|
|
|
messageBubbleColorForHue,
|
|
|
|
textColorForHue,
|
2020-10-17 03:50:00 +03:00
|
|
|
} from '../../utils/user-colors.js';
|
|
|
|
import { convertToText } from '../../utils/chat.js';
|
|
|
|
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
2021-06-20 20:49:16 +03:00
|
|
|
import { getDiffInDaysFromNow } from '../../utils/helpers.js';
|
2020-10-17 03:50:00 +03:00
|
|
|
|
|
|
|
export default class ChatMessageView extends Component {
|
2020-12-07 08:01:52 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
formattedMessage: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { formattedMessage } = this.state;
|
|
|
|
const { formattedMessage: nextFormattedMessage } = nextState;
|
|
|
|
|
2021-03-14 22:08:32 +03:00
|
|
|
return formattedMessage !== nextFormattedMessage;
|
2020-12-07 08:01:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2020-10-17 03:50:00 +03:00
|
|
|
const { message, username } = this.props;
|
2021-07-20 05:22:29 +03:00
|
|
|
const { body } = message;
|
2021-07-20 05:23:06 +03:00
|
|
|
|
2020-12-07 08:01:52 +03:00
|
|
|
if (message && username) {
|
|
|
|
const formattedMessage = await formatMessageText(body, username);
|
|
|
|
this.setState({
|
|
|
|
formattedMessage,
|
|
|
|
});
|
2020-11-18 04:29:03 +03:00
|
|
|
}
|
2020-12-07 08:01:52 +03:00
|
|
|
}
|
2020-10-17 03:50:00 +03:00
|
|
|
|
2020-11-18 04:29:03 +03:00
|
|
|
render() {
|
|
|
|
const { message } = this.props;
|
2021-07-20 05:22:29 +03:00
|
|
|
const { user, timestamp } = message;
|
|
|
|
const { displayName, displayColor, createdAt } = user;
|
2020-11-18 04:29:03 +03:00
|
|
|
|
|
|
|
const { formattedMessage } = this.state;
|
|
|
|
if (!formattedMessage) {
|
2020-12-07 08:01:52 +03:00
|
|
|
return null;
|
2020-11-18 04:29:03 +03:00
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
const formattedTimestamp = `Sent at ${formatTimestamp(timestamp)}`;
|
2021-07-26 08:41:48 +03:00
|
|
|
const userMetadata = createdAt
|
|
|
|
? `${displayName} first joined ${formatTimestamp(createdAt)}`
|
|
|
|
: null;
|
2020-10-17 03:50:00 +03:00
|
|
|
|
|
|
|
const isSystemMessage = message.type === SOCKET_MESSAGE_TYPES.SYSTEM;
|
2020-10-18 05:21:43 +03:00
|
|
|
|
|
|
|
const authorTextColor = isSystemMessage
|
|
|
|
? { color: '#fff' }
|
2021-07-20 05:22:29 +03:00
|
|
|
: { color: textColorForHue(displayColor) };
|
2020-10-17 03:50:00 +03:00
|
|
|
const backgroundStyle = isSystemMessage
|
|
|
|
? { backgroundColor: '#667eea' }
|
2021-07-20 05:22:29 +03:00
|
|
|
: { backgroundColor: messageBubbleColorForHue(displayColor) };
|
2020-10-18 05:21:43 +03:00
|
|
|
const messageClassString = isSystemMessage
|
|
|
|
? getSystemMessageClassString()
|
|
|
|
: getChatMessageClassString();
|
2020-10-17 03:50:00 +03:00
|
|
|
|
|
|
|
return html`
|
|
|
|
<div
|
|
|
|
style=${backgroundStyle}
|
2020-10-18 05:21:43 +03:00
|
|
|
class=${messageClassString}
|
2020-10-17 03:50:00 +03:00
|
|
|
title=${formattedTimestamp}
|
|
|
|
>
|
|
|
|
<div class="message-content break-words w-full">
|
2021-07-20 05:23:06 +03:00
|
|
|
<div
|
|
|
|
style=${authorTextColor}
|
|
|
|
class="message-author font-bold"
|
|
|
|
title=${userMetadata}
|
|
|
|
>
|
2021-07-20 05:22:29 +03:00
|
|
|
${displayName}
|
2020-10-17 03:50:00 +03:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
class="message-text text-gray-300 font-normal overflow-y-hidden pt-2"
|
|
|
|
dangerouslySetInnerHTML=${{ __html: formattedMessage }}
|
|
|
|
></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSystemMessageClassString() {
|
|
|
|
return 'message flex flex-row items-start p-4 m-2 rounded-lg shadow-l border-solid border-indigo-700 border-2 border-opacity-60 text-l';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getChatMessageClassString() {
|
|
|
|
return 'message flex flex-row items-start p-3 m-3 rounded-lg shadow-s text-sm';
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:23:06 +03:00
|
|
|
export async function formatMessageText(message, username) {
|
2020-11-18 04:29:03 +03:00
|
|
|
let formattedText = getMessageWithEmbeds(message);
|
|
|
|
formattedText = convertToMarkup(formattedText);
|
|
|
|
return await highlightUsername(formattedText, username);
|
2020-10-17 03:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function highlightUsername(message, username) {
|
2020-11-18 04:29:03 +03:00
|
|
|
// https://github.com/julmot/mark.js/issues/115
|
|
|
|
const node = document.createElement('span');
|
|
|
|
node.innerHTML = message;
|
2021-03-14 22:08:32 +03:00
|
|
|
return new Promise((res) => {
|
2020-11-18 04:29:03 +03:00
|
|
|
new Mark(node).mark(username, {
|
|
|
|
element: 'span',
|
|
|
|
className: 'highlighted px-1 rounded font-bold bg-orange-500',
|
|
|
|
separateWordSearch: false,
|
|
|
|
accuracy: {
|
|
|
|
value: 'exactly',
|
2021-03-14 22:08:32 +03:00
|
|
|
limiters: [',', '.', "'", '?', '@'],
|
2020-11-18 04:29:03 +03:00
|
|
|
},
|
|
|
|
done() {
|
|
|
|
res(node.innerHTML);
|
2021-03-14 22:08:32 +03:00
|
|
|
},
|
2020-11-18 04:29:03 +03:00
|
|
|
});
|
|
|
|
});
|
2020-10-17 03:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMessageWithEmbeds(message) {
|
|
|
|
var embedText = '';
|
|
|
|
// Make a temporary element so we can actually parse the html and pull anchor tags from it.
|
|
|
|
// This is a better approach than regex.
|
|
|
|
var container = document.createElement('p');
|
|
|
|
container.innerHTML = message;
|
|
|
|
|
|
|
|
var anchors = container.getElementsByTagName('a');
|
|
|
|
for (var i = 0; i < anchors.length; i++) {
|
|
|
|
const url = anchors[i].href;
|
2021-06-07 20:57:13 +03:00
|
|
|
if (url.indexOf('instagram.com/p/') > -1) {
|
2020-10-17 03:50:00 +03:00
|
|
|
embedText += getInstagramEmbedFromURL(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this message only consists of a single embeddable link
|
|
|
|
// then only return the embed and strip the link url from the text.
|
|
|
|
if (
|
|
|
|
embedText !== '' &&
|
|
|
|
anchors.length == 1 &&
|
|
|
|
isMessageJustAnchor(message, anchors[0])
|
|
|
|
) {
|
|
|
|
return embedText;
|
|
|
|
}
|
|
|
|
return message + embedText;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInstagramEmbedFromURL(url) {
|
|
|
|
const urlObject = new URL(url.replace(/\/$/, ''));
|
|
|
|
urlObject.pathname += '/embed';
|
|
|
|
return `<iframe class="chat-embed instagram-embed" src="${urlObject.href}" frameborder="0" allowfullscreen></iframe>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMessageJustAnchor(message, anchor) {
|
|
|
|
return stripTags(message) === stripTags(anchor.innerHTML);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTimestamp(sentAt) {
|
|
|
|
sentAt = new Date(sentAt);
|
|
|
|
if (isNaN(sentAt)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
let diffInDays = getDiffInDaysFromNow(sentAt);
|
2020-10-17 03:50:00 +03:00
|
|
|
if (diffInDays >= 1) {
|
|
|
|
return (
|
2021-08-06 07:40:32 +03:00
|
|
|
`at ${sentAt.toLocaleDateString('en-US', {
|
2020-10-17 03:50:00 +03:00
|
|
|
dateStyle: 'medium',
|
|
|
|
})} at ` + sentAt.toLocaleTimeString()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
return `${sentAt.toLocaleTimeString()}`;
|
2020-10-17 03:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
You would call this when receiving a plain text
|
|
|
|
value back from an API, and before inserting the
|
|
|
|
text into the `contenteditable` area on a page.
|
|
|
|
*/
|
|
|
|
function convertToMarkup(str = '') {
|
2021-08-19 02:11:00 +03:00
|
|
|
return str;// convertToText(str).replace(/\n/g, '<br>');
|
2020-10-17 03:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function stripTags(str) {
|
|
|
|
return str.replace(/<\/?[^>]+(>|$)/g, '');
|
|
|
|
}
|