2021-12-01 16:36:52 +01:00
|
|
|
import { SDK_API_PATH } from 'config';
|
2020-03-17 16:21:26 +01:00
|
|
|
import { useEffect } from 'react';
|
2020-12-10 23:31:20 +01:00
|
|
|
import { getAuthToken } from 'util/saved-passwords';
|
|
|
|
import { X_LBRY_AUTH_TOKEN } from 'constants/token';
|
2020-04-23 01:54:27 +02:00
|
|
|
|
2020-03-17 16:21:26 +01:00
|
|
|
import fetchWithTimeout from 'util/fetch';
|
|
|
|
|
2021-11-22 15:30:43 +01:00
|
|
|
const STATUS_GENERAL_STATE = {
|
|
|
|
// internal/status/status.go#L44
|
|
|
|
OK: 'ok',
|
|
|
|
NOT_READY: 'not_ready',
|
|
|
|
OFFLINE: 'offline',
|
|
|
|
FAILING: 'failing',
|
|
|
|
};
|
|
|
|
|
2020-03-17 16:21:26 +01:00
|
|
|
const STATUS_TIMEOUT_LIMIT = 10000;
|
|
|
|
export const STATUS_OK = 'ok';
|
|
|
|
export const STATUS_DEGRADED = 'degraded';
|
2020-03-26 21:17:41 +01:00
|
|
|
export const STATUS_FAILING = 'failing';
|
2020-03-17 16:21:26 +01:00
|
|
|
export const STATUS_DOWN = 'down';
|
|
|
|
|
2021-03-15 20:47:48 +01:00
|
|
|
const getParams = (user) => {
|
2020-10-28 02:29:45 +01:00
|
|
|
const headers = {};
|
2020-12-10 23:31:20 +01:00
|
|
|
const token = getAuthToken();
|
2021-06-18 00:52:21 +02:00
|
|
|
if (token && user && user.has_verified_email) {
|
2020-12-10 23:31:20 +01:00
|
|
|
headers[X_LBRY_AUTH_TOKEN] = token;
|
|
|
|
}
|
2020-10-28 02:29:45 +01:00
|
|
|
const params = { headers };
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
2020-12-10 23:31:20 +01:00
|
|
|
export function useDegradedPerformance(onDegradedPerformanceCallback, user) {
|
2021-06-18 00:52:21 +02:00
|
|
|
const hasUser = user !== undefined && user !== null;
|
2020-12-10 23:31:20 +01:00
|
|
|
|
2020-03-17 16:21:26 +01:00
|
|
|
useEffect(() => {
|
2020-12-10 23:31:20 +01:00
|
|
|
if (hasUser) {
|
|
|
|
// The status endpoint is the only endpoint at "v2" currently
|
|
|
|
// This should be moved into the config once more endpoints are using it
|
|
|
|
const STATUS_ENDPOINT = `${SDK_API_PATH}/status`.replace('v1', 'v2');
|
2020-06-22 18:20:13 +02:00
|
|
|
|
2020-12-10 23:31:20 +01:00
|
|
|
fetchWithTimeout(STATUS_TIMEOUT_LIMIT, fetch(STATUS_ENDPOINT, getParams(user)))
|
2021-03-15 20:47:48 +01:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((status) => {
|
2021-11-22 15:30:43 +01:00
|
|
|
if (status.general_state === STATUS_GENERAL_STATE.OFFLINE) {
|
|
|
|
onDegradedPerformanceCallback(STATUS_DOWN);
|
|
|
|
} else if (status.general_state !== STATUS_GENERAL_STATE.OK) {
|
2020-12-10 23:31:20 +01:00
|
|
|
onDegradedPerformanceCallback(STATUS_FAILING);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2020-03-26 21:17:41 +01:00
|
|
|
onDegradedPerformanceCallback(STATUS_FAILING);
|
2020-12-10 23:31:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [hasUser]);
|
2020-03-17 16:21:26 +01:00
|
|
|
}
|