2020-11-06 03:30:14 +01:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-12-31 03:07:15 +01:00
|
|
|
import PropTypes, { any } from 'prop-types';
|
2020-11-06 03:30:14 +01:00
|
|
|
|
2020-11-13 13:43:27 +01:00
|
|
|
import { STATUS, fetchData, FETCH_INTERVAL, SERVER_CONFIG } from './apis';
|
2021-01-03 09:29:37 +01:00
|
|
|
import { ConfigDetails, UpdateArgs } from '../types/config-section';
|
2021-01-10 11:37:22 +01:00
|
|
|
import { DEFAULT_VARIANT_STATE } from '../pages/components/config/constants';
|
2020-11-06 03:30:14 +01:00
|
|
|
|
2021-01-03 09:29:37 +01:00
|
|
|
export const initialServerConfigState: ConfigDetails = {
|
2020-11-13 13:43:27 +01:00
|
|
|
streamKey: '',
|
2020-12-31 03:07:15 +01:00
|
|
|
instanceDetails: {
|
2021-01-03 09:29:37 +01:00
|
|
|
extraPageContent: '',
|
|
|
|
logo: '',
|
|
|
|
name: '',
|
|
|
|
nsfw: false,
|
|
|
|
streamTitle: '',
|
|
|
|
summary: '',
|
2020-12-31 03:07:15 +01:00
|
|
|
tags: [],
|
2021-01-03 09:29:37 +01:00
|
|
|
title: '',
|
2020-12-31 03:07:15 +01:00
|
|
|
},
|
2021-01-03 09:29:37 +01:00
|
|
|
ffmpegPath: '',
|
|
|
|
rtmpServerPort: '',
|
|
|
|
webServerPort: '',
|
|
|
|
s3: {},
|
2020-11-13 13:43:27 +01:00
|
|
|
yp: {
|
|
|
|
enabled: false,
|
2021-01-03 09:29:37 +01:00
|
|
|
instanceUrl: '',
|
2020-11-13 13:43:27 +01:00
|
|
|
},
|
|
|
|
videoSettings: {
|
2021-01-04 08:32:47 +01:00
|
|
|
numberOfPlaylistItems: 5,
|
|
|
|
segmentLengthSeconds: 4,
|
2021-01-10 11:37:22 +01:00
|
|
|
videoQualityVariants: [DEFAULT_VARIANT_STATE],
|
2020-11-13 13:43:27 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const initialServerStatusState = {
|
2020-11-06 03:30:14 +01:00
|
|
|
broadcastActive: false,
|
|
|
|
broadcaster: null,
|
|
|
|
online: false,
|
|
|
|
viewerCount: 0,
|
2020-11-13 12:43:28 +01:00
|
|
|
sessionMaxViewerCount: 0,
|
2020-11-06 03:30:14 +01:00
|
|
|
sessionPeakViewerCount: 0,
|
|
|
|
overallPeakViewerCount: 0,
|
|
|
|
disableUpgradeChecks: true,
|
|
|
|
versionNumber: '0.0.0',
|
|
|
|
};
|
|
|
|
|
2020-11-13 13:43:27 +01:00
|
|
|
export const ServerStatusContext = React.createContext({
|
|
|
|
...initialServerStatusState,
|
|
|
|
serverConfig: initialServerConfigState,
|
2020-12-27 03:04:23 +01:00
|
|
|
|
2021-01-03 10:54:04 +01:00
|
|
|
setFieldInConfigState: any,
|
2020-11-13 13:43:27 +01:00
|
|
|
});
|
2020-11-06 03:30:14 +01:00
|
|
|
|
|
|
|
const ServerStatusProvider = ({ children }) => {
|
2020-11-13 13:43:27 +01:00
|
|
|
const [status, setStatus] = useState(initialServerStatusState);
|
|
|
|
const [config, setConfig] = useState(initialServerConfigState);
|
2020-11-06 03:30:14 +01:00
|
|
|
|
|
|
|
const getStatus = async () => {
|
|
|
|
try {
|
|
|
|
const result = await fetchData(STATUS);
|
|
|
|
setStatus({ ...result });
|
|
|
|
|
|
|
|
} catch (error) {
|
2020-11-13 12:43:28 +01:00
|
|
|
// todo
|
2020-11-06 03:30:14 +01:00
|
|
|
}
|
|
|
|
};
|
2020-11-13 13:43:27 +01:00
|
|
|
const getConfig = async () => {
|
|
|
|
try {
|
|
|
|
const result = await fetchData(SERVER_CONFIG);
|
|
|
|
setConfig(result);
|
|
|
|
} catch (error) {
|
|
|
|
// todo
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-03 10:54:04 +01:00
|
|
|
const setFieldInConfigState = ({ fieldName, value, path }: UpdateArgs) => {
|
2020-12-28 10:11:26 +01:00
|
|
|
const updatedConfig = path ?
|
|
|
|
{
|
|
|
|
...config,
|
|
|
|
[path]: {
|
|
|
|
...config[path],
|
|
|
|
[fieldName]: value,
|
|
|
|
},
|
|
|
|
} :
|
|
|
|
{
|
2020-12-27 03:04:23 +01:00
|
|
|
...config,
|
|
|
|
[fieldName]: value,
|
|
|
|
};
|
2020-12-28 10:11:26 +01:00
|
|
|
|
2020-12-27 03:04:23 +01:00
|
|
|
setConfig(updatedConfig);
|
2021-01-04 08:32:47 +01:00
|
|
|
};
|
2020-12-27 03:04:23 +01:00
|
|
|
|
2020-11-06 03:30:14 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let getStatusIntervalId = null;
|
|
|
|
|
|
|
|
getStatus();
|
|
|
|
getStatusIntervalId = setInterval(getStatus, FETCH_INTERVAL);
|
2020-11-13 13:43:27 +01:00
|
|
|
|
|
|
|
getConfig();
|
|
|
|
|
2020-11-06 03:30:14 +01:00
|
|
|
// returned function will be called on component unmount
|
|
|
|
return () => {
|
|
|
|
clearInterval(getStatusIntervalId);
|
|
|
|
}
|
2021-01-04 08:32:47 +01:00
|
|
|
}, []);
|
2020-11-06 03:30:14 +01:00
|
|
|
|
2020-11-13 13:43:27 +01:00
|
|
|
const providerValue = {
|
|
|
|
...status,
|
|
|
|
serverConfig: config,
|
2020-12-27 03:04:23 +01:00
|
|
|
|
2021-01-03 10:54:04 +01:00
|
|
|
setFieldInConfigState,
|
2020-11-13 13:43:27 +01:00
|
|
|
};
|
2020-11-06 03:30:14 +01:00
|
|
|
return (
|
2020-11-13 13:43:27 +01:00
|
|
|
<ServerStatusContext.Provider value={providerValue}>
|
2020-11-06 03:30:14 +01:00
|
|
|
{children}
|
|
|
|
</ServerStatusContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerStatusProvider.propTypes = {
|
|
|
|
children: PropTypes.element.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ServerStatusProvider;
|