import classnames from 'classnames' import React from 'react' import screenfull from 'screenfull' import { AddStreamPayload, removeStream } from '../actions/StreamActions' import { ME_DESKTOP } from '../constants' import { getDesktopStream } from '../actions/MediaActions' const hidden = { display: 'none', } export interface ToolbarProps { messagesCount: number stream: AddStreamPayload desktopStream: AddStreamPayload | undefined onToggleChat: () => void onGetDesktopStream: typeof getDesktopStream onRemoveStream: typeof removeStream onSendFile: (file: File) => void onHangup: () => void chatVisible: boolean } export interface ToolbarState { readMessages: number camDisabled: boolean micMuted: boolean fullScreenEnabled: boolean } export interface ToolbarButtonProps { className?: string badge?: string | number blink?: boolean onClick: () => void icon: string offIcon?: string on?: boolean title: string } function ToolbarButton(props: ToolbarButtonProps) { const { blink, on } = props const icon = !on && props.offIcon ? props.offIcon : props.icon return ( {!!props.badge && {props.badge}} {props.title} ) } export default class Toolbar extends React.PureComponent { file = React.createRef() desktopStream: MediaStream | undefined constructor(props: ToolbarProps) { super(props) this.state = { readMessages: props.messagesCount, camDisabled: false, micMuted: false, fullScreenEnabled: false, } } handleMicClick = () => { const { stream } = this.props stream.stream.getAudioTracks().forEach(track => { track.enabled = !track.enabled }) this.setState({ ...this.state, micMuted: !this.state.micMuted, }) } handleCamClick = () => { const { stream } = this.props stream.stream.getVideoTracks().forEach(track => { track.enabled = !track.enabled }) this.setState({ ...this.state, camDisabled: !this.state.camDisabled, }) } handleFullscreenClick = () => { if (screenfull.isEnabled) { screenfull.toggle() this.setState({ ...this.state, fullScreenEnabled: !screenfull.isFullscreen, }) } } handleHangoutClick = () => { window.location.href = '/' } handleSendFile = () => { this.file.current!.click() } handleSelectFiles = (event: React.ChangeEvent) => { Array .from(event.target!.files!) .forEach(file => this.props.onSendFile(file)) } handleToggleChat = () => { this.setState({ readMessages: this.props.messagesCount, }) this.props.onToggleChat() } handleToggleShareDesktop = () => { if (this.props.desktopStream) { this.props.onRemoveStream(ME_DESKTOP) } else { this.props.onGetDesktopStream().catch(() => {}) } } render () { const { messagesCount, stream } = this.props const unreadCount = messagesCount - this.state.readMessages const hasUnread = unreadCount > 0 return (
{stream && ( )} {this.props.stream && this.props.stream.stream && ( )}
) } }