2020-08-24 07:41:02 +03:00
|
|
|
import { h, Component } from 'https://unpkg.com/preact?module';
|
2020-08-19 10:16:35 +03:00
|
|
|
import htm from 'https://unpkg.com/htm?module';
|
|
|
|
const html = htm.bind(h);
|
|
|
|
|
2020-08-24 05:37:06 +03:00
|
|
|
import Chat from './components/chat/chat.js';
|
2020-08-24 05:06:58 +03:00
|
|
|
import Websocket from './utils/websocket.js';
|
|
|
|
import { getLocalStorage, generateAvatar, generateUsername } from './utils/helpers.js';
|
2020-08-24 05:37:06 +03:00
|
|
|
import { KEY_USERNAME, KEY_AVATAR } from './utils/constants.js';
|
2020-08-13 11:28:25 +03:00
|
|
|
|
2020-08-14 14:19:19 +03:00
|
|
|
export default class StandaloneChat extends Component {
|
2020-08-13 11:28:25 +03:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = {
|
2020-08-14 14:19:19 +03:00
|
|
|
websocket: new Websocket(),
|
2020-08-13 11:28:25 +03:00
|
|
|
chatEnabled: true, // always true for standalone chat
|
|
|
|
username: getLocalStorage(KEY_USERNAME) || generateUsername(),
|
|
|
|
userAvatarImage: getLocalStorage(KEY_AVATAR) || generateAvatar(`${this.username}${Date.now()}`),
|
|
|
|
};
|
|
|
|
|
2020-08-14 14:19:19 +03:00
|
|
|
this.websocket = null;
|
2020-08-13 11:28:25 +03:00
|
|
|
this.handleUsernameChange = this.handleUsernameChange.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUsernameChange(newName, newAvatar) {
|
|
|
|
this.setState({
|
|
|
|
username: newName,
|
|
|
|
userAvatarImage: newAvatar,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render(props, state) {
|
2020-08-14 14:19:19 +03:00
|
|
|
const { username, userAvatarImage, websocket } = state;
|
2020-08-19 10:16:35 +03:00
|
|
|
|
2020-08-24 05:37:06 +03:00
|
|
|
return (
|
2020-08-19 10:16:35 +03:00
|
|
|
html`
|
|
|
|
<${Chat}
|
|
|
|
websocket=${websocket}
|
|
|
|
username=${username}
|
|
|
|
userAvatarImage=${userAvatarImage}
|
|
|
|
messagesOnly
|
|
|
|
/>
|
2020-08-24 05:37:06 +03:00
|
|
|
`
|
|
|
|
);
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
}
|