2020-12-06 21:01:52 -08:00
|
|
|
import { h } from '/js/web_modules/preact.js';
|
2020-10-04 18:43:31 -07:00
|
|
|
import htm from '/js/web_modules/htm.js';
|
2020-10-04 09:01:46 +07:00
|
|
|
const html = htm.bind(h);
|
2020-08-13 01:28:25 -07:00
|
|
|
|
2020-10-16 17:36:11 -07:00
|
|
|
import ChatMessageView from './chat-message-view.js';
|
|
|
|
|
2020-10-04 09:08:05 +07:00
|
|
|
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
2021-11-02 19:27:41 -07:00
|
|
|
import { checkIsModerator } from '../../utils/chat.js';
|
|
|
|
|
|
|
|
function SystemMessage(props) {
|
|
|
|
const { contents } = props;
|
|
|
|
return html`
|
2021-11-03 02:28:19 +00:00
|
|
|
<div
|
|
|
|
class="message message-name-change flex items-center justify-start p-3"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
class="message-content flex flex-row items-center justify-center text-sm w-full"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
class="text-white text-center opacity-50 overflow-hidden break-words"
|
|
|
|
>
|
2021-11-02 19:27:41 -07:00
|
|
|
${contents}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
2020-10-03 20:29:29 +07:00
|
|
|
|
2020-12-06 21:01:52 -08:00
|
|
|
export default function Message(props) {
|
|
|
|
const { message } = props;
|
2021-11-02 19:27:41 -07:00
|
|
|
const { type, oldName, user, body } = message;
|
2021-07-20 02:23:06 +00:00
|
|
|
if (
|
|
|
|
type === SOCKET_MESSAGE_TYPES.CHAT ||
|
|
|
|
type === SOCKET_MESSAGE_TYPES.SYSTEM
|
|
|
|
) {
|
2020-12-06 21:01:52 -08:00
|
|
|
return html`<${ChatMessageView} ...${props} />`;
|
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) {
|
2021-07-20 02:23:06 +00:00
|
|
|
const { displayName } = user;
|
2021-11-02 19:27:41 -07:00
|
|
|
const contents = html`
|
|
|
|
<>
|
|
|
|
<span class="font-bold">${oldName}</span> is now known as ${' '}
|
|
|
|
<span class="font-bold">${displayName}</span>.
|
|
|
|
</>
|
2021-07-20 02:23:06 +00:00
|
|
|
`;
|
2021-11-03 02:28:19 +00:00
|
|
|
return html`<${SystemMessage} contents=${contents} />`;
|
2021-02-18 23:05:52 -08:00
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.USER_JOINED) {
|
2021-07-19 19:22:29 -07:00
|
|
|
const { displayName } = user;
|
2021-11-03 02:28:19 +00:00
|
|
|
const contents = html`<span class="font-bold">${displayName}</span> joined
|
|
|
|
the chat.`;
|
|
|
|
return html`<${SystemMessage} contents=${contents} />`;
|
2021-02-18 23:05:52 -08:00
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.CHAT_ACTION) {
|
2021-11-02 19:27:41 -07:00
|
|
|
const contents = html`<span
|
|
|
|
dangerouslySetInnerHTML=${{ __html: body }}
|
|
|
|
></span>`;
|
2021-11-03 02:28:19 +00:00
|
|
|
return html`<${SystemMessage} contents=${contents} />`;
|
2021-07-19 19:22:29 -07:00
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.CONNECTED_USER_INFO) {
|
2021-11-02 19:27:41 -07:00
|
|
|
// moderator message
|
|
|
|
const isModerator = checkIsModerator(message);
|
|
|
|
if (isModerator) {
|
2021-11-03 02:28:19 +00:00
|
|
|
const contents = html`<span class="font-bold moderator-flag"
|
|
|
|
>You are now a moderator.</span
|
|
|
|
>`;
|
|
|
|
return html`<${SystemMessage} contents=${contents} />`;
|
2021-11-02 19:27:41 -07:00
|
|
|
}
|
2020-12-06 21:01:52 -08:00
|
|
|
} else {
|
2021-07-20 02:23:06 +00:00
|
|
|
console.log('Unknown message type:', type);
|
2020-10-13 16:45:52 -07:00
|
|
|
}
|
|
|
|
}
|