import { h, Component, render } from 'https://unpkg.com/preact?module'; import htm from 'https://unpkg.com/htm?module'; // Initialize htm with Preact const html = htm.bind(h); import SOCKET_MESSAGE_TYPES from '../utils/socket-message-types.js'; import Message from './message.js'; export default class Chat extends Component { constructor(props, context) { super(props, context); this.messageCharCount = 0; this.maxMessageLength = 500; this.maxMessageBuffer = 20; this.state = { inputEnabled: false, messages: [], chatUserNames: [], } } componentDidMount() { } componentDidUpdate(prevProps) { const { username: prevName } = prevProps; const { username, userAvatarImage } = this.props; // if username updated, send a message if (prevName !== username) { this.sendUsernameChange(prevName, username, userAvatarImage); } } sendUsernameChange(oldName, newName, image) { const nameChange = { type: SOCKET_MESSAGE_TYPES.NAME_CHANGE, oldName: oldName, newName: newName, image: image, }; this.send(nameChange); } render(props, state) { const { username, userAvatarImage } = props; const { messages } = state; return ( html`
${ messages.map(message => (html`<${Message} message=${message} />`)) } messages..
😏
`); } }