owncast/web/pages/connected-clients.tsx

93 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-10-23 02:16:28 +02:00
import React, { useState, useEffect, useContext } from 'react';
2020-10-21 10:19:29 +02:00
import { Table } from 'antd';
2020-10-23 02:16:28 +02:00
import { BroadcastStatusContext } from './utils/broadcast-status-context';
2020-10-21 10:19:29 +02:00
import { CONNECTED_CLIENTS, fetchData, FETCH_INTERVAL } from './utils/apis';
2020-10-08 09:26:24 +02:00
/*
geo data looks like this
"geo": {
"countryCode": "US",
"regionName": "California",
"timeZone": "America/Los_Angeles"
}
*/
2020-10-23 02:16:28 +02:00
export default function ConnectedClients() {
const context = useContext(BroadcastStatusContext);
const { broadcastActive } = context || {};
const [clients, setClients] = useState([]);
2020-10-08 09:26:24 +02:00
const getInfo = async () => {
try {
const result = await fetchData(CONNECTED_CLIENTS);
2020-10-23 02:16:28 +02:00
console.log("result",result)
2020-10-21 10:19:29 +02:00
setClients(result);
2020-10-08 09:26:24 +02:00
} catch (error) {
2020-10-21 10:19:29 +02:00
console.log("==== error", error)
2020-10-08 09:26:24 +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:26:24 +02:00
}
2020-10-23 02:16:28 +02:00
return () => [];
2020-10-08 09:26:24 +02:00
}, []);
2020-10-21 10:19:29 +02:00
2020-10-23 02:16:28 +02:00
if (!clients.length) {
return "no clients";
}
// todo - check to see if broadcast active has changed. if so, start polling.
2020-10-21 10:19:29 +02:00
const columns = [
{
title: 'User name',
dataIndex: 'username',
key: 'username',
render: username => username || '-',
sorter: (a, b) => a.username - b.username,
sortDirections: ['descend', 'ascend'],
},
{
title: 'Messages sent',
dataIndex: 'messageCount',
key: 'messageCount',
sorter: (a, b) => a.messageCount - b.messageCount,
sortDirections: ['descend', 'ascend'],
},
{
title: 'Connected Time',
dataIndex: 'connectedAt',
key: 'connectedAt',
render: time => (Date.now() - (new Date(time).getTime())) / 1000 / 60,
},
{
title: 'User Agent',
dataIndex: 'userAgent',
key: 'userAgent',
},
{
title: 'Location',
dataIndex: 'geo',
key: 'geo',
render: geo => geo && `${geo.regionName}, ${geo.countryCode}`,
},
];
2020-10-08 09:26:24 +02:00
return (
<div>
<h2>Connected Clients</h2>
2020-10-21 10:19:29 +02:00
<Table dataSource={clients} columns={columns} />;
2020-10-08 09:26:24 +02:00
</div>
);
}