2020-08-13 10:28:25 +02:00
|
|
|
import { html, Component } from "https://unpkg.com/htm/preact/index.mjs?module";
|
2020-08-17 18:00:36 +02:00
|
|
|
import UsernameForm from './username.js';
|
2020-08-13 10:28:25 +02:00
|
|
|
import Chat from './chat.js';
|
2020-08-14 13:19:19 +02:00
|
|
|
import Websocket from '../websocket.js';
|
2020-08-13 10:28:25 +02:00
|
|
|
|
|
|
|
import { getLocalStorage, generateAvatar, generateUsername } from '../utils.js';
|
|
|
|
import { KEY_USERNAME, KEY_AVATAR } from '../utils/chat.js';
|
|
|
|
|
2020-08-14 13:19:19 +02:00
|
|
|
export default class StandaloneChat extends Component {
|
2020-08-13 10:28:25 +02:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = {
|
2020-08-14 13:19:19 +02:00
|
|
|
websocket: new Websocket(),
|
2020-08-13 10:28:25 +02: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 13:19:19 +02:00
|
|
|
this.websocket = null;
|
2020-08-13 10:28:25 +02:00
|
|
|
this.handleUsernameChange = this.handleUsernameChange.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUsernameChange(newName, newAvatar) {
|
|
|
|
this.setState({
|
|
|
|
username: newName,
|
|
|
|
userAvatarImage: newAvatar,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChatToggle() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
render(props, state) {
|
2020-08-14 13:19:19 +02:00
|
|
|
const { username, userAvatarImage, websocket } = state;
|
2020-08-13 10:28:25 +02:00
|
|
|
return (
|
|
|
|
html`
|
|
|
|
<div class="flex">
|
2020-08-17 18:00:36 +02:00
|
|
|
<${UsernameForm}
|
2020-08-13 10:28:25 +02:00
|
|
|
username=${username}
|
|
|
|
userAvatarImage=${userAvatarImage}
|
|
|
|
handleUsernameChange=${this.handleUsernameChange}
|
|
|
|
handleChatToggle=${this.handleChatToggle}
|
|
|
|
/>
|
2020-08-17 18:00:36 +02:00
|
|
|
|
2020-08-14 13:19:19 +02:00
|
|
|
<${Chat}
|
|
|
|
websocket=${websocket}
|
|
|
|
username=${username}
|
|
|
|
userAvatarImage=${userAvatarImage}
|
2020-08-17 18:00:36 +02:00
|
|
|
chatEnabled
|
|
|
|
/>
|
2020-08-13 10:28:25 +02:00
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|