import { useState } from 'react' import fetch from 'isomorphic-unfetch' import {useSocket, socketEventEmitter, socket} from '../hooks/useSocket' import Layout from '../components/Layout' import Link from 'next/link' import {BroadcastIcon, ServerIcon, DeviceDesktopIcon} from '@primer/octicons-react' const Tally = require('../domain/Tally') const countConnectedTallies = tallies => tallies.reduce((count, tally) => count + (Tally.fromValueObject(tally).isConnected() ? 1 : 0), 0) const createTallyList = (tallies, showDisconnected, showUnpatched) => { return tallies.map( tally => Tally.fromValueObject(tally) ).filter( tally => (tally.isActive() || showDisconnected) && (tally.isPatched() || showUnpatched) ).sort( (one, two) => { if (one.isActive() !== two.isActive()) { return one.isActive() ? -1 : 1 } else { return one.name.localeCompare(two.name) } } ) } const ChatOne = props => { const [talliesData, setTallies] = useState(props.tallies || new Array()) const [programs, setPrograms] = useState(props.programs || []) const [previews, setPreviews] = useState(props.previews || []) const [isMixerConnected, setIsMixerConnected] = useState(props.isMixerConnected || false) const [showDisconnected, setShowDisconnected] = useState(props.showDisconnected !== undefined ? props.showDisconnected : true) const [showUnpatched, setShowUnpatched] = useState(props.showUnpatched !== undefined ? props.showUnpatched : true) const [channels, setChannels] = useState(props.channels !== undefined ? props.channels : {}) const [isHubConnected, setIsHubConnected] = useState(socket.connected) const tallies = createTallyList(talliesData, showDisconnected, showUnpatched) useSocket('program.changed', data => { setPrograms(data.programs) setPreviews(data.previews) }) useSocket('tallies', tallies => { setTallies(tallies) }) useSocket('mixer', mixer => { setIsMixerConnected(mixer.isConnected) }) useSocket('config', config => { setChannels(config.channels) }) socketEventEmitter.on("connected", function() { setIsHubConnected(true) }) socketEventEmitter.on("disconnected", function() { setIsHubConnected(false) }) const patchTally = function(tally, channel) { socket.emit('tally.patch', tally.name, channel) } const toggleDisconnected = e => { setShowDisconnected(!showDisconnected) } const toggleUnpatched = e => { setShowUnpatched(!showUnpatched) } const handleHighlightTally = (e, tally) => { socket.emit('tally.highlight', tally.name) e.preventDefault() } const handleRemoveTally = (e, tally) => { socket.emit('tally.remove', tally.name) e.preventDefault() } const formatChannelOption = (idx, invalid) => { let label = `Channel ${idx}` if(channels.names[idx]) { label = channels.names[idx] } if(invalid) { label = `(${label})` } return () } const format = tally => { let classPatched = "card " if(tally.state === Tally.DISCONNECTED) { classPatched += "bg-dark " if(!tally.isPatched()) { classPatched += "border-light " } else if(tally.isIn(programs)) { classPatched += "border-danger " } else if(tally.isIn(previews)) { classPatched += "border-success " } else { classPatched += "border-secondary " } } else { if(!tally.isPatched()) { classPatched += "bg-light " } else if(tally.isIn(programs)) { classPatched += "bg-danger " } else if(tally.isIn(previews)) { classPatched += "bg-success " } else { classPatched += "bg-secondary " } } return (
{tally.name}
{tally.isPatched() ? (
{tally.channelId}
) : "" }
{tally.state !== Tally.DISCONNECTED ? ( handleHighlightTally(e, tally)}>Highlight ) : ""} {tally.state !== Tally.CONNECTED ? ( handleRemoveTally(e, tally)}>Remove ) : ""} Logs
{tally.state === Tally.DISCONNECTED ? (
disconnected
) : (
{tally.state === Tally.CONNECTED ? "connected" : "missing"}
{tally.address}:{tally.port}
)}
) } const nrConnectedTallies = countConnectedTallies(tallies) function refreshPage() { window.location.reload(false) return false } return (
{ isHubConnected ? "" : (

Hub disconnected

The displayed information might be outdated.

We will try to reconnect automatically, but you might also try to reload the page.

)}
{tallies.map(format)}
) } ChatOne.getInitialProps = async (context) => { const baseUrl = context && context.req ? `${context.req.protocol}://${context.req.get('Host')}` : ''; const response = await fetch(baseUrl + '/tallies') const info = await response.json() // @TODO: use asynchronous calls const config = await fetch(baseUrl + '/atem') const configJson = await config.json() info.channels = configJson.channels return info } export default ChatOne;