owncast/web/utils/server-status-context.tsx

141 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-01-17 11:40:46 +03: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.
import React, { useState, useEffect } from 'react';
2021-01-29 21:26:55 +03:00
import PropTypes from 'prop-types';
import { STATUS, fetchData, FETCH_INTERVAL, SERVER_CONFIG } from './apis';
import { ConfigDetails, UpdateArgs } from '../types/config-section';
import { DEFAULT_VARIANT_STATE } from './config-constants';
export const initialServerConfigState: ConfigDetails = {
streamKey: '',
instanceDetails: {
customStyles: '',
extraPageContent: '',
logo: '',
name: '',
nsfw: false,
2021-01-19 21:34:06 +03:00
socialHandles: [],
streamTitle: '',
summary: '',
tags: [],
title: '',
welcomeMessage: '',
},
ffmpegPath: '',
rtmpServerPort: '',
webServerPort: '',
chatDisabled: false,
s3: {
accessKey: '',
acl: '',
bucket: '',
enabled: false,
endpoint: '',
region: '',
secret: '',
servingEndpoint: '',
},
yp: {
enabled: false,
instanceUrl: '',
},
videoSettings: {
latencyLevel: 4,
2021-01-31 09:53:00 +03:00
cpuUsageLevel: 3,
2021-01-10 13:37:22 +03:00
videoQualityVariants: [DEFAULT_VARIANT_STATE],
},
externalActions: [],
2021-03-23 06:34:52 +03:00
supportedCodecs: [],
videoCodec: '',
};
const initialServerStatusState = {
broadcastActive: false,
broadcaster: null,
currentBroadcast: null,
online: false,
viewerCount: 0,
sessionMaxViewerCount: 0,
sessionPeakViewerCount: 0,
overallPeakViewerCount: 0,
versionNumber: '0.0.0',
streamTitle: '',
};
export const ServerStatusContext = React.createContext({
...initialServerStatusState,
serverConfig: initialServerConfigState,
2020-12-27 05:04:23 +03:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
setFieldInConfigState: (args: UpdateArgs) => null,
});
const ServerStatusProvider = ({ children }) => {
const [status, setStatus] = useState(initialServerStatusState);
const [config, setConfig] = useState(initialServerConfigState);
const getStatus = async () => {
try {
const result = await fetchData(STATUS);
setStatus({ ...result });
} catch (error) {
// todo
}
};
const getConfig = async () => {
try {
const result = await fetchData(SERVER_CONFIG);
setConfig(result);
} catch (error) {
// todo
}
};
2021-01-03 12:54:04 +03:00
const setFieldInConfigState = ({ fieldName, value, path }: UpdateArgs) => {
const updatedConfig = path
? {
...config,
[path]: {
...config[path],
[fieldName]: value,
},
}
: {
...config,
[fieldName]: value,
};
2020-12-27 05:04:23 +03:00
setConfig(updatedConfig);
};
2020-12-27 05:04:23 +03:00
useEffect(() => {
let getStatusIntervalId = null;
getStatus();
getStatusIntervalId = setInterval(getStatus, FETCH_INTERVAL);
getConfig();
// returned function will be called on component unmount
return () => {
clearInterval(getStatusIntervalId);
};
}, []);
const providerValue = {
...status,
serverConfig: config,
2020-12-27 05:04:23 +03:00
setFieldInConfigState,
};
return (
<ServerStatusContext.Provider value={providerValue}>{children}</ServerStatusContext.Provider>
);
};
ServerStatusProvider.propTypes = {
children: PropTypes.element.isRequired,
};
export default ServerStatusProvider;