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.
|
|
|
|
|
2023-02-22 20:21:00 +03:00
|
|
|
import React, { useState, useEffect, FC, ReactElement } from 'react';
|
2020-11-06 05:30:14 +03:00
|
|
|
|
2020-11-13 15:43:27 +03:00
|
|
|
import { STATUS, fetchData, FETCH_INTERVAL, SERVER_CONFIG } from './apis';
|
2021-04-12 10:07:08 +03:00
|
|
|
import { ConfigDetails, UpdateArgs } from '../types/config-section';
|
2021-02-07 06:38:58 +03:00
|
|
|
import { DEFAULT_VARIANT_STATE } from './config-constants';
|
2020-11-06 05:30:14 +03:00
|
|
|
|
2021-01-03 11:29:37 +03:00
|
|
|
export const initialServerConfigState: ConfigDetails = {
|
2022-11-29 07:22:26 +03:00
|
|
|
streamKeys: [],
|
2023-02-28 04:08:52 +03:00
|
|
|
streamKeyOverridden: false,
|
2022-11-29 07:22:26 +03:00
|
|
|
adminPassword: '',
|
2020-12-31 05:07:15 +03:00
|
|
|
instanceDetails: {
|
2021-04-12 10:07:08 +03:00
|
|
|
customStyles: '',
|
2023-01-19 09:38:24 +03:00
|
|
|
customJavascript: '',
|
2021-01-03 11:29:37 +03:00
|
|
|
extraPageContent: '',
|
|
|
|
logo: '',
|
|
|
|
name: '',
|
|
|
|
nsfw: false,
|
2021-01-19 21:34:06 +03:00
|
|
|
socialHandles: [],
|
2021-01-03 11:29:37 +03:00
|
|
|
streamTitle: '',
|
|
|
|
summary: '',
|
2020-12-31 05:07:15 +03:00
|
|
|
tags: [],
|
2021-01-03 11:29:37 +03:00
|
|
|
title: '',
|
2021-04-12 10:07:08 +03:00
|
|
|
welcomeMessage: '',
|
2022-08-17 07:44:37 +03:00
|
|
|
offlineMessage: '',
|
2022-11-13 07:26:55 +03:00
|
|
|
appearanceVariables: {},
|
2020-12-31 05:07:15 +03:00
|
|
|
},
|
2021-01-03 11:29:37 +03:00
|
|
|
ffmpegPath: '',
|
|
|
|
rtmpServerPort: '',
|
|
|
|
webServerPort: '',
|
2022-03-07 04:12:37 +03:00
|
|
|
socketHostOverride: null,
|
2021-02-01 10:40:39 +03:00
|
|
|
s3: {
|
|
|
|
accessKey: '',
|
|
|
|
acl: '',
|
|
|
|
bucket: '',
|
|
|
|
enabled: false,
|
|
|
|
endpoint: '',
|
|
|
|
region: '',
|
|
|
|
secret: '',
|
|
|
|
servingEndpoint: '',
|
2021-10-29 03:33:32 +03:00
|
|
|
forcePathStyle: false,
|
2021-02-01 10:40:39 +03:00
|
|
|
},
|
2020-11-13 15:43:27 +03:00
|
|
|
yp: {
|
|
|
|
enabled: false,
|
2021-01-03 11:29:37 +03:00
|
|
|
instanceUrl: '',
|
2020-11-13 15:43:27 +03:00
|
|
|
},
|
|
|
|
videoSettings: {
|
2021-01-18 23:11:48 +03:00
|
|
|
latencyLevel: 4,
|
2021-01-31 09:53:00 +03:00
|
|
|
cpuUsageLevel: 3,
|
2021-01-10 13:37:22 +03:00
|
|
|
videoQualityVariants: [DEFAULT_VARIANT_STATE],
|
2021-02-01 10:40:39 +03:00
|
|
|
},
|
2022-01-13 00:52:37 +03:00
|
|
|
federation: {
|
|
|
|
enabled: false,
|
|
|
|
isPrivate: false,
|
|
|
|
username: '',
|
|
|
|
goLiveMessage: '',
|
|
|
|
showEngagement: true,
|
|
|
|
blockedDomains: [],
|
|
|
|
},
|
2022-03-23 18:57:09 +03:00
|
|
|
notifications: {
|
|
|
|
browser: { enabled: false, goLiveMessage: '' },
|
|
|
|
discord: { enabled: false, webhook: '', goLiveMessage: '' },
|
|
|
|
},
|
2021-03-16 01:27:19 +03:00
|
|
|
externalActions: [],
|
2021-03-23 06:34:52 +03:00
|
|
|
supportedCodecs: [],
|
|
|
|
videoCodec: '',
|
2021-07-20 08:02:02 +03:00
|
|
|
forbiddenUsernames: [],
|
2022-01-12 21:17:14 +03:00
|
|
|
suggestedUsernames: [],
|
2021-07-20 08:02:02 +03:00
|
|
|
chatDisabled: false,
|
2022-03-06 09:36:38 +03:00
|
|
|
chatJoinMessagesEnabled: true,
|
2022-03-07 11:06:07 +03:00
|
|
|
chatEstablishedUserMode: false,
|
2022-06-26 10:46:55 +03:00
|
|
|
hideViewerCount: false,
|
2020-11-13 15:43:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const initialServerStatusState = {
|
2020-11-06 05:30:14 +03:00
|
|
|
broadcastActive: false,
|
|
|
|
broadcaster: null,
|
2021-02-07 06:38:58 +03:00
|
|
|
currentBroadcast: null,
|
2020-11-06 05:30:14 +03:00
|
|
|
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,
|
|
|
|
versionNumber: '0.0.0',
|
2021-02-02 09:20:59 +03:00
|
|
|
streamTitle: '',
|
2021-07-20 08:02:02 +03:00
|
|
|
chatDisabled: false,
|
2022-03-25 09:04:20 +03:00
|
|
|
health: {
|
|
|
|
healthy: true,
|
|
|
|
healthPercentage: 100,
|
|
|
|
message: '',
|
2022-03-28 02:28:14 +03:00
|
|
|
representation: 0,
|
2022-03-25 09:04:20 +03:00
|
|
|
},
|
2023-03-11 22:38:29 +03:00
|
|
|
error: {
|
|
|
|
type: null,
|
|
|
|
msg: null,
|
|
|
|
},
|
2020-11-06 05:30:14 +03:00
|
|
|
};
|
|
|
|
|
2020-11-13 15:43:27 +03:00
|
|
|
export const ServerStatusContext = React.createContext({
|
|
|
|
...initialServerStatusState,
|
|
|
|
serverConfig: initialServerConfigState,
|
2020-12-27 05:04:23 +03:00
|
|
|
|
2021-02-07 06:38:58 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
setFieldInConfigState: (args: UpdateArgs) => null,
|
2020-11-13 15:43:27 +03:00
|
|
|
});
|
2020-11-06 05:30:14 +03:00
|
|
|
|
2023-02-22 20:21:00 +03:00
|
|
|
export type ServerStatusProviderProps = {
|
|
|
|
children: ReactElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ServerStatusProvider: FC<ServerStatusProviderProps> = ({ 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);
|
2023-03-11 22:38:29 +03:00
|
|
|
|
|
|
|
if (result instanceof Error) {
|
|
|
|
throw result;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus({ ...result, error: { type: null, msg: null } });
|
2020-11-06 05:30:14 +03:00
|
|
|
} catch (error) {
|
2023-03-11 22:38:29 +03:00
|
|
|
setStatus(initialStatus => ({
|
|
|
|
...initialStatus,
|
|
|
|
error: {
|
|
|
|
type: 'OWNCAST_SERVICE_UNREACHABLE',
|
|
|
|
msg: 'Cannot connect to the Owncast service. Please check you are connected to the internet and the Owncast server is running.',
|
|
|
|
},
|
|
|
|
}));
|
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);
|
2023-03-11 22:38:29 +03:00
|
|
|
|
|
|
|
if (result instanceof Error) {
|
|
|
|
throw result;
|
|
|
|
}
|
|
|
|
|
2020-11-13 15:43:27 +03:00
|
|
|
setConfig(result);
|
|
|
|
} catch (error) {
|
2023-03-11 22:38:29 +03:00
|
|
|
console.error(error);
|
2020-11-13 15:43:27 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-03 12:54:04 +03:00
|
|
|
const setFieldInConfigState = ({ fieldName, value, path }: UpdateArgs) => {
|
2021-02-01 10:40:39 +03:00
|
|
|
const updatedConfig = path
|
|
|
|
? {
|
|
|
|
...config,
|
|
|
|
[path]: {
|
|
|
|
...config[path],
|
|
|
|
[fieldName]: value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
...config,
|
|
|
|
[fieldName]: value,
|
|
|
|
};
|
2020-12-27 05:04:23 +03:00
|
|
|
setConfig(updatedConfig);
|
2021-01-04 10:32:47 +03:00
|
|
|
};
|
2020-12-27 05:04:23 +03:00
|
|
|
|
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();
|
|
|
|
|
2021-02-01 10:40:39 +03:00
|
|
|
// returned function will be called on component unmount
|
2020-11-06 05:30:14 +03:00
|
|
|
return () => {
|
|
|
|
clearInterval(getStatusIntervalId);
|
2021-02-01 10:40:39 +03:00
|
|
|
};
|
2021-01-04 10:32:47 +03:00
|
|
|
}, []);
|
2020-11-06 05:30:14 +03:00
|
|
|
|
2022-06-26 10:46:55 +03:00
|
|
|
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
2020-11-13 15:43:27 +03:00
|
|
|
const providerValue = {
|
2021-02-01 10:40:39 +03:00
|
|
|
...status,
|
|
|
|
serverConfig: config,
|
2020-12-27 05:04:23 +03:00
|
|
|
|
2021-02-01 10:40:39 +03:00
|
|
|
setFieldInConfigState,
|
2020-11-13 15:43:27 +03:00
|
|
|
};
|
2020-11-06 05:30:14 +03:00
|
|
|
return (
|
2021-02-01 10:40:39 +03:00
|
|
|
<ServerStatusContext.Provider value={providerValue}>{children}</ServerStatusContext.Provider>
|
2020-11-06 05:30:14 +03:00
|
|
|
);
|
2021-02-01 10:40:39 +03:00
|
|
|
};
|
2020-11-06 05:30:14 +03:00
|
|
|
|
2021-02-01 10:40:39 +03:00
|
|
|
export default ServerStatusProvider;
|