2020-10-04 09:01:46 +07:00
|
|
|
import {Component, h} from 'https://unpkg.com/preact?module';
|
2020-08-23 21:41:02 -07:00
|
|
|
import htm from 'https://unpkg.com/htm?module';
|
2020-10-04 09:01:46 +07:00
|
|
|
import {messageBubbleColorForString} from '../../utils/user-colors.js';
|
|
|
|
import {formatMessageText} from '../../utils/chat.js';
|
|
|
|
import {generateAvatar} from '../../utils/helpers.js';
|
|
|
|
import {SOCKET_MESSAGE_TYPES} from '../../utils/websocket.js';
|
2020-08-13 01:28:25 -07:00
|
|
|
|
2020-10-04 09:01:46 +07:00
|
|
|
const html = htm.bind(h);
|
2020-08-13 01:28:25 -07:00
|
|
|
|
|
|
|
export default class Message extends Component {
|
2020-10-03 20:29:29 +07:00
|
|
|
formatTimestamp(sentAt) {
|
2020-10-04 09:01:46 +07:00
|
|
|
sentAt = new Date(sentAt);
|
2020-10-03 20:29:29 +07:00
|
|
|
|
2020-10-04 09:01:46 +07:00
|
|
|
let diffInDays = ((new Date()) - sentAt) / (24 * 3600 * 1000);
|
|
|
|
if (diffInDays >= -1) {
|
|
|
|
return `${sentAt.toLocaleDateString('en-US', {dateStyle: 'medium'})} at ` +
|
|
|
|
sentAt.toLocaleTimeString();
|
2020-10-03 20:29:29 +07:00
|
|
|
}
|
|
|
|
|
2020-10-04 09:01:46 +07:00
|
|
|
return sentAt.toLocaleTimeString();
|
2020-10-03 20:29:29 +07:00
|
|
|
}
|
|
|
|
|
2020-08-13 01:28:25 -07:00
|
|
|
render(props) {
|
2020-08-14 04:19:19 -07:00
|
|
|
const { message, username } = props;
|
2020-08-13 01:49:10 -07:00
|
|
|
const { type } = message;
|
|
|
|
|
|
|
|
if (type === SOCKET_MESSAGE_TYPES.CHAT) {
|
2020-10-01 16:11:43 +07:00
|
|
|
const { image, author, body, timestamp } = message;
|
2020-08-14 04:19:19 -07:00
|
|
|
const formattedMessage = formatMessageText(body, username);
|
2020-08-13 01:49:10 -07:00
|
|
|
const avatar = image || generateAvatar(author);
|
2020-08-19 00:47:41 -07:00
|
|
|
|
|
|
|
const authorColor = messageBubbleColorForString(author);
|
|
|
|
const avatarBgColor = { backgroundColor: authorColor };
|
|
|
|
const authorTextColor = { color: authorColor };
|
2020-08-13 01:49:10 -07:00
|
|
|
return (
|
|
|
|
html`
|
2020-08-23 21:49:26 -07:00
|
|
|
<div class="message flex flex-row items-start p-3">
|
2020-08-13 01:49:10 -07:00
|
|
|
<div
|
2020-08-21 23:44:10 -07:00
|
|
|
class="message-avatar rounded-full flex items-center justify-center mr-3"
|
2020-08-13 01:49:10 -07:00
|
|
|
style=${avatarBgColor}
|
|
|
|
>
|
2020-08-21 23:44:10 -07:00
|
|
|
<img src=${avatar} class="p-1" />
|
2020-08-13 01:49:10 -07:00
|
|
|
</div>
|
2020-08-27 12:25:46 -07:00
|
|
|
<div class="message-content text-sm break-words w-full">
|
2020-08-21 23:44:10 -07:00
|
|
|
<div class="message-author text-white font-bold" style=${authorTextColor}>
|
|
|
|
${author}
|
|
|
|
</div>
|
2020-08-14 04:19:19 -07:00
|
|
|
<div
|
2020-09-21 20:11:09 -07:00
|
|
|
class="message-text text-gray-300 font-normal overflow-y-hidden"
|
2020-10-03 20:29:29 +07:00
|
|
|
title=${`Sent at ${this.formatTimestamp(timestamp)}`}
|
2020-08-14 04:19:19 -07:00
|
|
|
dangerouslySetInnerHTML=${
|
|
|
|
{ __html: formattedMessage }
|
|
|
|
}
|
|
|
|
></div>
|
2020-08-13 01:49:10 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) {
|
|
|
|
const { oldName, newName, image } = message;
|
|
|
|
return (
|
|
|
|
html`
|
2020-08-23 21:49:26 -07:00
|
|
|
<div class="message message-name-change flex items-center justify-start p-3">
|
2020-08-27 12:25:46 -07:00
|
|
|
<div class="message-content flex flex-row items-center justify-center text-sm w-full">
|
2020-08-23 21:49:26 -07:00
|
|
|
<div
|
|
|
|
class="message-avatar rounded-full mr-3 bg-gray-900"
|
|
|
|
>
|
|
|
|
<img class="mr-2 p-1" src=${image} />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="text-white text-center opacity-50">
|
2020-08-21 15:55:52 -07:00
|
|
|
<span class="font-bold">${oldName}</span> is now known as <span class="font-bold">${newName}</span>.
|
|
|
|
</div>
|
2020-08-13 01:49:10 -07:00
|
|
|
</div>
|
2020-08-13 01:28:25 -07:00
|
|
|
</div>
|
2020-08-21 23:44:10 -07:00
|
|
|
`
|
|
|
|
);
|
2020-08-13 01:49:10 -07:00
|
|
|
}
|
2020-08-13 01:28:25 -07:00
|
|
|
}
|
|
|
|
}
|