2021-01-17 09:40:46 +01:00
|
|
|
// TODO: add a notication after updating info that changes will take place either on a new stream or server restart. may be different for each field.
|
|
|
|
|
2020-11-06 03:30:14 +01:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-01-29 19:26:55 +01:00
|
|
|
import PropTypes 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-02-07 04:38:58 +01:00
|
|
|
import { DEFAULT_VARIANT_STATE } from './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,
|
2021-01-19 19:34:06 +01:00
|
|
|
socialHandles: [],
|
2021-01-03 09:29:37 +01:00
|
|
|
streamTitle: '',
|
|
|
|
summary: '',
|
2021-03-15 05:55:01 +01:00
|
|
|
welcomeMessage: '',
|
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: '',
|
2021-03-14 19:39:07 +01:00
|
|
|
chatDisabled: false,
|
2021-02-01 08:40:39 +01:00
|
|
|
s3: {
|
|
|
|
accessKey: '',
|
|
|
|
acl: '',
|
|
|
|
bucket: '',
|
|
|
|
enabled: false,
|
|
|
|
endpoint: '',
|
|
|
|
region: '',
|
|
|
|
secret: '',
|
|
|
|
servingEndpoint: '',
|
|
|
|
},
|
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-18 21:11:48 +01:00
|
|
|
latencyLevel: 4,
|
2021-01-31 07:53:00 +01:00
|
|
|
cpuUsageLevel: 3,
|
2021-01-10 11:37:22 +01:00
|
|
|
videoQualityVariants: [DEFAULT_VARIANT_STATE],
|
2021-02-01 08:40:39 +01:00
|
|
|
},
|
2020-11-13 13:43:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const initialServerStatusState = {
|
2020-11-06 03:30:14 +01:00
|
|
|
broadcastActive: false,
|
|
|
|
broadcaster: null,
|
2021-02-07 04:38:58 +01:00
|
|
|
currentBroadcast: null,
|
2020-11-06 03:30:14 +01:00
|
|
|
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,
|
|
|
|
versionNumber: '0.0.0',
|
2021-02-02 07:20:59 +01:00
|
|
|
streamTitle: '',
|
2020-11-06 03:30:14 +01:00
|
|
|
};
|
|
|
|
|
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-02-07 04:38:58 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
setFieldInConfigState: (args: UpdateArgs) => null,
|
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) => {
|
2021-02-01 08:40:39 +01:00
|
|
|
const updatedConfig = path
|
|
|
|
? {
|
|
|
|
...config,
|
|
|
|
[path]: {
|
|
|
|
...config[path],
|
|
|
|
[fieldName]: value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
...config,
|
|
|
|
[fieldName]: value,
|
|
|
|
};
|
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();
|
|
|
|
|
2021-02-01 08:40:39 +01:00
|
|
|
// returned function will be called on component unmount
|
2020-11-06 03:30:14 +01:00
|
|
|
return () => {
|
|
|
|
clearInterval(getStatusIntervalId);
|
2021-02-01 08:40:39 +01:00
|
|
|
};
|
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 = {
|
2021-02-01 08:40:39 +01:00
|
|
|
...status,
|
|
|
|
serverConfig: config,
|
2020-12-27 03:04:23 +01:00
|
|
|
|
2021-02-01 08:40:39 +01:00
|
|
|
setFieldInConfigState,
|
2020-11-13 13:43:27 +01:00
|
|
|
};
|
2020-11-06 03:30:14 +01:00
|
|
|
return (
|
2021-02-01 08:40:39 +01:00
|
|
|
<ServerStatusContext.Provider value={providerValue}>{children}</ServerStatusContext.Provider>
|
2020-11-06 03:30:14 +01:00
|
|
|
);
|
2021-02-01 08:40:39 +01:00
|
|
|
};
|
2020-11-06 03:30:14 +01:00
|
|
|
|
|
|
|
ServerStatusProvider.propTypes = {
|
|
|
|
children: PropTypes.element.isRequired,
|
|
|
|
};
|
|
|
|
|
2021-02-01 08:40:39 +01:00
|
|
|
export default ServerStatusProvider;
|