2020-10-23 02:16:28 +02:00
|
|
|
import React, { useState, useEffect, useContext } from 'react';
|
2020-10-12 06:46:07 +02:00
|
|
|
import {timeFormat} from 'd3-time-format';
|
|
|
|
import { LineChart, XAxis, YAxis, Line, Tooltip } from 'recharts';
|
2020-10-23 02:16:28 +02:00
|
|
|
import { BroadcastStatusContext } from './utils/broadcast-status-context';
|
2020-10-23 01:18:18 +02:00
|
|
|
|
|
|
|
import { VIEWERS_OVER_TIME, fetchData } from './utils/apis';
|
2020-10-08 09:17:40 +02:00
|
|
|
|
2020-10-12 04:46:48 +02:00
|
|
|
const FETCH_INTERVAL = 5 * 60 * 1000; // 5 mins
|
|
|
|
|
|
|
|
export default function ViewersOverTime() {
|
2020-10-23 02:16:28 +02:00
|
|
|
const context = useContext(BroadcastStatusContext);
|
|
|
|
const { broadcastActive } = context || {};
|
|
|
|
|
2020-10-12 04:46:48 +02:00
|
|
|
const [viewerInfo, setViewerInfo] = useState([]);
|
2020-10-08 09:17:40 +02:00
|
|
|
|
|
|
|
const getInfo = async () => {
|
|
|
|
try {
|
|
|
|
const result = await fetchData(VIEWERS_OVER_TIME);
|
2020-10-12 04:46:48 +02:00
|
|
|
setViewerInfo(result);
|
2020-10-08 09:17:40 +02:00
|
|
|
} catch (error) {
|
2020-10-12 04:46:48 +02:00
|
|
|
console.log("==== error", error)
|
2020-10-08 09:17:40 +02:00
|
|
|
}
|
|
|
|
};
|
2020-10-23 02:16:28 +02:00
|
|
|
|
2020-10-08 09:17:40 +02:00
|
|
|
useEffect(() => {
|
|
|
|
let getStatusIntervalId = null;
|
|
|
|
|
|
|
|
getInfo();
|
2020-10-23 02:16:28 +02:00
|
|
|
if (broadcastActive) {
|
|
|
|
getStatusIntervalId = setInterval(getInfo, FETCH_INTERVAL);
|
|
|
|
// returned function will be called on component unmount
|
|
|
|
return () => {
|
|
|
|
clearInterval(getStatusIntervalId);
|
|
|
|
}
|
2020-10-08 09:17:40 +02:00
|
|
|
}
|
2020-10-23 02:16:28 +02:00
|
|
|
return () => [];
|
2020-10-08 09:17:40 +02:00
|
|
|
}, []);
|
2020-10-23 02:16:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
// todo - check to see if broadcast active has changed. if so, start polling.
|
|
|
|
|
|
|
|
|
|
|
|
if (!viewerInfo.length) {
|
|
|
|
return "no info";
|
|
|
|
}
|
2020-10-08 09:17:40 +02:00
|
|
|
|
2020-10-12 06:46:07 +02:00
|
|
|
const timeFormatter = (tick) => {return timeFormat('%H:%M:%S')(new Date(tick));};
|
|
|
|
|
2020-10-21 10:19:29 +02:00
|
|
|
const CustomizedTooltip = (props) => {
|
|
|
|
const { active, payload, label } = props;
|
|
|
|
if (active) {
|
|
|
|
const numViewers = payload && payload[0] && payload[0].value;
|
|
|
|
const time = timeFormatter(label);
|
|
|
|
const message = `${numViewers} viewer(s) at ${time}`;
|
|
|
|
return (
|
|
|
|
<div className="custom-tooltip">
|
|
|
|
<p className="label">{message}</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2020-10-12 04:46:48 +02:00
|
|
|
|
|
|
|
|
2020-10-08 09:17:40 +02:00
|
|
|
return (
|
2020-10-21 10:19:29 +02:00
|
|
|
<div>
|
|
|
|
<h2>Current Viewers</h2>
|
|
|
|
<div className="chart-container">
|
|
|
|
<LineChart width={800} height={400} data={viewerInfo}>
|
|
|
|
<XAxis dataKey="time" tickFormatter={timeFormatter}/>
|
|
|
|
<YAxis dataKey="value"/>
|
|
|
|
<Tooltip
|
|
|
|
content={<CustomizedTooltip />}
|
|
|
|
/>
|
|
|
|
<Line
|
|
|
|
type="monotone"
|
|
|
|
dataKey="value"
|
|
|
|
stroke="#ff84d8"
|
|
|
|
dot={{ stroke: 'red', strokeWidth: 2 }}
|
|
|
|
/>
|
|
|
|
</LineChart>
|
2020-10-08 09:17:40 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|