import { h, Component } from '/js/web_modules/preact.js'; import htm from '/js/web_modules/htm.js'; const html = htm.bind(h); import { messageBubbleColorForString } from '../../utils/user-colors.js'; import { formatMessageText, formatTimestamp } from '../../utils/chat.js'; import { generateAvatar } from '../../utils/helpers.js'; import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js'; export default class Message extends Component { render(props) { const { message, username } = props; const { type } = message; if (type === SOCKET_MESSAGE_TYPES.CHAT) { const { image, author, body, timestamp } = message; const formattedMessage = formatMessageText(body, username); const avatar = image || generateAvatar(author); const formattedTimestamp = formatTimestamp(timestamp); const authorColor = messageBubbleColorForString(author); const avatarBgColor = { backgroundColor: authorColor }; const authorTextColor = { color: authorColor }; return ( html`
${author}
`); } else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) { const { oldName, newName, image } = message; return ( html`
${oldName} is now known as ${newName}.
` ); } } }