2020-11-06 05:30:14 +03:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2020-11-13 15:43:27 +03:00
|
|
|
import { STATUS, fetchData, FETCH_INTERVAL, SERVER_CONFIG } from './apis';
|
2020-11-06 05:30:14 +03:00
|
|
|
|
2020-11-13 15:43:27 +03:00
|
|
|
export const initialServerConfigState = {
|
|
|
|
streamKey: '',
|
|
|
|
yp: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
videoSettings: {
|
|
|
|
videoQualityVariants: [
|
|
|
|
{
|
|
|
|
audioPassthrough: false,
|
2020-11-20 08:56:29 +03:00
|
|
|
videoPassthrough: false,
|
2020-11-13 15:43:27 +03:00
|
|
|
videoBitrate: 0,
|
|
|
|
audioBitrate: 0,
|
|
|
|
framerate: 0,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const initialServerStatusState = {
|
2020-11-06 05:30:14 +03:00
|
|
|
broadcastActive: false,
|
|
|
|
broadcaster: null,
|
|
|
|
online: false,
|
|
|
|
viewerCount: 0,
|
2020-11-13 14:43:28 +03:00
|
|
|
sessionMaxViewerCount: 0,
|
2020-11-06 05:30:14 +03:00
|
|
|
sessionPeakViewerCount: 0,
|
|
|
|
overallPeakViewerCount: 0,
|
|
|
|
disableUpgradeChecks: true,
|
|
|
|
versionNumber: '0.0.0',
|
|
|
|
};
|
|
|
|
|
2020-11-13 15:43:27 +03:00
|
|
|
export const ServerStatusContext = React.createContext({
|
|
|
|
...initialServerStatusState,
|
|
|
|
serverConfig: initialServerConfigState,
|
|
|
|
});
|
2020-11-06 05:30:14 +03:00
|
|
|
|
|
|
|
const ServerStatusProvider = ({ children }) => {
|
2020-11-13 15:43:27 +03:00
|
|
|
const [status, setStatus] = useState(initialServerStatusState);
|
|
|
|
const [config, setConfig] = useState(initialServerConfigState);
|
2020-11-06 05:30:14 +03:00
|
|
|
|
|
|
|
const getStatus = async () => {
|
|
|
|
try {
|
|
|
|
const result = await fetchData(STATUS);
|
|
|
|
setStatus({ ...result });
|
|
|
|
|
|
|
|
} catch (error) {
|
2020-11-13 14:43:28 +03:00
|
|
|
// todo
|
2020-11-06 05:30:14 +03:00
|
|
|
}
|
|
|
|
};
|
2020-11-13 15:43:27 +03:00
|
|
|
const getConfig = async () => {
|
|
|
|
try {
|
|
|
|
const result = await fetchData(SERVER_CONFIG);
|
|
|
|
setConfig(result);
|
|
|
|
} catch (error) {
|
|
|
|
// todo
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-06 05:30:14 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let getStatusIntervalId = null;
|
|
|
|
|
|
|
|
getStatus();
|
|
|
|
getStatusIntervalId = setInterval(getStatus, FETCH_INTERVAL);
|
2020-11-13 15:43:27 +03:00
|
|
|
|
|
|
|
getConfig();
|
|
|
|
|
2020-11-06 05:30:14 +03:00
|
|
|
// returned function will be called on component unmount
|
|
|
|
return () => {
|
|
|
|
clearInterval(getStatusIntervalId);
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2020-11-13 15:43:27 +03:00
|
|
|
const providerValue = {
|
|
|
|
...status,
|
|
|
|
serverConfig: config,
|
|
|
|
};
|
2020-11-06 05:30:14 +03:00
|
|
|
return (
|
2020-11-13 15:43:27 +03:00
|
|
|
<ServerStatusContext.Provider value={providerValue}>
|
2020-11-06 05:30:14 +03:00
|
|
|
{children}
|
|
|
|
</ServerStatusContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerStatusProvider.propTypes = {
|
|
|
|
children: PropTypes.element.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ServerStatusProvider;
|