import {Component, h} from 'https://unpkg.com/preact?module'; import htm from 'https://unpkg.com/htm?module'; 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'; const html = htm.bind(h); export default class Message extends Component { formatTimestamp(sentAt) { sentAt = new Date(sentAt); let diffInDays = ((new Date()) - sentAt) / (24 * 3600 * 1000); if (diffInDays >= -1) { return `${sentAt.toLocaleDateString('en-US', {dateStyle: 'medium'})} at ` + sentAt.toLocaleTimeString(); } return sentAt.toLocaleTimeString(); } 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 authorColor = messageBubbleColorForString(author); const avatarBgColor = { backgroundColor: authorColor }; const authorTextColor = { color: authorColor }; return ( html`
`); } else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) { const { oldName, newName, image } = message; return ( html` ` ); } } }