import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Crypto from 'utils/crypto'
import { connect as connectSocket } from 'utils/socket'
import Nav from 'components/Nav'
import shortId from 'shortid'
import Connecting from 'components/Connecting'
import Modal from 'react-modal'
import About from 'components/About'
import Settings from 'components/Settings'
import Welcome from 'components/Welcome'
import RoomLocked from 'components/RoomLocked'
import { X, AlertCircle } from 'react-feather'
import Tinycon from 'tinycon'
import beepFile from 'audio/beep.mp3'
import classNames from 'classnames'
import ActivityList from './ActivityList'
import styles from './styles.module.scss'
const crypto = new Crypto()
Modal.setAppElement('#root');
class Home extends Component {
async componentWillMount() {
const roomId = encodeURI(this.props.match.params.roomId)
const user = await this.createUser()
const socket = connectSocket(roomId)
this.socket = socket;
socket.on('disconnect', () => {
this.props.toggleSocketConnected(false)
})
socket.on('connect', () => {
this.initApp(user)
this.props.toggleSocketConnected(true)
})
socket.on('USER_ENTER', (payload) => {
this.props.receiveUnencryptedMessage('USER_ENTER', payload)
this.props.sendEncryptedMessage({
type: 'ADD_USER',
payload: {
username: this.props.username,
publicKey: this.props.publicKey,
isOwner: this.props.iAmOwner,
id: this.props.userId,
},
})
if (payload.users.length === 1) {
this.props.openModal('Welcome');
}
})
socket.on('USER_EXIT', (payload) => {
this.props.receiveUnencryptedMessage('USER_EXIT', payload)
})
socket.on('ENCRYPTED_MESSAGE', (payload) => {
this.props.receiveEncryptedMessage(payload)
})
socket.on('TOGGLE_LOCK_ROOM', (payload) => {
this.props.receiveUnencryptedMessage('TOGGLE_LOCK_ROOM', payload)
})
socket.on('ROOM_LOCKED', (payload) => {
this.props.openModal('Room Locked')
});
window.addEventListener('beforeunload', (evt) => {
socket.emit('USER_DISCONNECT')
});
}
componentDidMount() {
this.bindEvents()
this.beep = window.Audio && new window.Audio(beepFile)
}
componentWillReceiveProps(nextProps) {
Tinycon.setBubble(nextProps.faviconCount)
if (nextProps.faviconCount !== 0 && nextProps.faviconCount !== this.props.faviconCount && this.props.soundIsEnabled) {
this.beep.play()
}
}
getModal() {
switch (this.props.modalComponent) {
case 'Connecting':
return {
component: