lbry-desktop/web/effects/use-degraded-performance.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-04-23 01:54:27 +02:00
import { SDK_API_PATH } from 'ui';
import { useEffect } from 'react';
2020-10-28 02:29:45 +01:00
import { getAuthToken } from 'util/saved-passwords';
import { X_LBRY_AUTH_TOKEN } from 'constants/token';
2020-04-23 01:54:27 +02:00
import fetchWithTimeout from 'util/fetch';
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';
export const STATUS_DOWN = 'down';
2020-10-28 02:29:45 +01:00
const getParams = () => {
const headers = {};
const token = getAuthToken();
if (token) {
headers[X_LBRY_AUTH_TOKEN] = token;
}
const params = { headers };
return params;
};
export function useDegradedPerformance(onDegradedPerformanceCallback) {
useEffect(() => {
2020-06-22 18:20:13 +02:00
// 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-10-28 02:29:45 +01:00
fetchWithTimeout(STATUS_TIMEOUT_LIMIT, fetch(STATUS_ENDPOINT, getParams()))
.then(response => response.json())
.then(status => {
2020-03-26 21:17:41 +01:00
if (status.general_state !== STATUS_OK) {
onDegradedPerformanceCallback(STATUS_FAILING);
}
})
.catch(() => {
2020-03-26 21:17:41 +01:00
onDegradedPerformanceCallback(STATUS_FAILING);
});
}, []);
}