import { h } from '/js/web_modules/preact.js'; import htm from '/js/web_modules/htm.js'; const html = htm.bind(h); import ChatMessageView from './chat-message-view.js'; import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js'; import { checkIsModerator } from '../../utils/chat.js'; function SystemMessage(props) { const { contents } = props; return html`
${contents}
`; } export default function Message(props) { const { message } = props; const { type, oldName, user, body } = message; if ( type === SOCKET_MESSAGE_TYPES.CHAT || type === SOCKET_MESSAGE_TYPES.SYSTEM ) { return html`<${ChatMessageView} ...${props} />`; } else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) { const { displayName } = user; const contents = html` <> ${oldName} is now known as ${' '} ${displayName}. `; return html`<${SystemMessage} contents=${contents} />`; } else if (type === SOCKET_MESSAGE_TYPES.USER_JOINED) { const { displayName } = user; const contents = html`${displayName} joined the chat.`; return html`<${SystemMessage} contents=${contents} />`; } else if (type === SOCKET_MESSAGE_TYPES.CHAT_ACTION) { const contents = html``; return html`<${SystemMessage} contents=${contents} />`; } else if (type === SOCKET_MESSAGE_TYPES.CONNECTED_USER_INFO) { // moderator message const isModerator = checkIsModerator(message); if (isModerator) { const contents = html`
You are now a moderator.
`; return html`<${SystemMessage} contents=${contents} />`; } } else { console.log('Unknown message type:', type); } }