2020-08-13 18:28:47 +02:00
|
|
|
import { html, Component } from "https://unpkg.com/htm/preact/index.mjs?module";
|
2020-08-13 10:28:25 +02:00
|
|
|
|
2020-08-13 10:49:10 +02:00
|
|
|
import { messageBubbleColorForString } from '../utils/user-colors.js';
|
|
|
|
import { formatMessageText } from '../utils/chat.js';
|
|
|
|
import { generateAvatar } from '../utils.js';
|
2020-08-13 11:08:14 +02:00
|
|
|
import SOCKET_MESSAGE_TYPES from '../utils/socket-message-types.js';
|
2020-08-13 10:28:25 +02:00
|
|
|
|
|
|
|
export default class Message extends Component {
|
|
|
|
render(props) {
|
2020-08-14 13:19:19 +02:00
|
|
|
const { message, username } = props;
|
2020-08-13 10:49:10 +02:00
|
|
|
const { type } = message;
|
|
|
|
|
|
|
|
if (type === SOCKET_MESSAGE_TYPES.CHAT) {
|
2020-08-13 18:28:47 +02:00
|
|
|
const { image, author, body } = message;
|
2020-08-14 13:19:19 +02:00
|
|
|
const formattedMessage = formatMessageText(body, username);
|
2020-08-13 10:49:10 +02:00
|
|
|
const avatar = image || generateAvatar(author);
|
2020-08-19 09:47:41 +02:00
|
|
|
|
|
|
|
const authorColor = messageBubbleColorForString(author);
|
|
|
|
const avatarBgColor = { backgroundColor: authorColor };
|
|
|
|
const authorTextColor = { color: authorColor };
|
2020-08-13 10:49:10 +02:00
|
|
|
return (
|
|
|
|
html`
|
|
|
|
<div class="message flex">
|
|
|
|
<div
|
|
|
|
class="message-avatar rounded-full flex items-center justify-center"
|
|
|
|
style=${avatarBgColor}
|
|
|
|
>
|
|
|
|
<img src=${avatar} />
|
|
|
|
</div>
|
|
|
|
<div class="message-content">
|
|
|
|
<p class="message-author text-white font-bold">${author}</p>
|
2020-08-14 13:19:19 +02:00
|
|
|
<div
|
|
|
|
class="message-text text-gray-400 font-thin"
|
|
|
|
dangerouslySetInnerHTML=${
|
|
|
|
{ __html: formattedMessage }
|
|
|
|
}
|
|
|
|
></div>
|
2020-08-13 10:49:10 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) {
|
|
|
|
const { oldName, newName, image } = message;
|
|
|
|
return (
|
|
|
|
html`
|
|
|
|
<div class="message flex">
|
|
|
|
<img class="mr-2" src=${image} />
|
|
|
|
<div class="text-white text-center">
|
|
|
|
<span class="font-bold">${oldName}</span> is now known as <span class="font-bold">${newName}</span>.
|
|
|
|
</div>
|
2020-08-13 10:28:25 +02:00
|
|
|
</div>
|
2020-08-13 18:28:47 +02:00
|
|
|
`);
|
2020-08-13 10:49:10 +02:00
|
|
|
}
|
2020-08-13 10:28:25 +02:00
|
|
|
}
|
|
|
|
}
|