87c3dcc057
* SyncFatalError: show nag instead of hard-crashing. ## Issue When sync fails, we crash the app. ## Ticket Maybe closes 39 "Better handle both internal and web backend interruptions / downtime" ## Approach I'm tackling this from the standpoint that (1) sync errors are not that fatal -- we'll just lost a few recent changes (2) network disconnection is the common cause. ## Changes - If we are offline: - Inform user through a nag. All other status is meaningless if we are offline. - If we are online: - If api is STATUS_DOWN, show the existing crash page. - If there is a sync error, show a nag saying settings are now potentially unsynchronized, and add a button to retry sync. - If there is a chunk error, nag to reload. * Attempt to detect `status=DOWN` Previous code resolves the status to either "ok" or "not", which makes the app unable to differentiate between the "degraded" (nag) and "down" (crash) states.
55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
import { SDK_API_PATH } from 'ui';
|
|
import { useEffect } from 'react';
|
|
import { getAuthToken } from 'util/saved-passwords';
|
|
import { X_LBRY_AUTH_TOKEN } from 'constants/token';
|
|
|
|
import fetchWithTimeout from 'util/fetch';
|
|
|
|
const STATUS_GENERAL_STATE = {
|
|
// internal/status/status.go#L44
|
|
OK: 'ok',
|
|
NOT_READY: 'not_ready',
|
|
OFFLINE: 'offline',
|
|
FAILING: 'failing',
|
|
};
|
|
|
|
const STATUS_TIMEOUT_LIMIT = 10000;
|
|
export const STATUS_OK = 'ok';
|
|
export const STATUS_DEGRADED = 'degraded';
|
|
export const STATUS_FAILING = 'failing';
|
|
export const STATUS_DOWN = 'down';
|
|
|
|
const getParams = (user) => {
|
|
const headers = {};
|
|
const token = getAuthToken();
|
|
if (token && user && user.has_verified_email) {
|
|
headers[X_LBRY_AUTH_TOKEN] = token;
|
|
}
|
|
const params = { headers };
|
|
return params;
|
|
};
|
|
|
|
export function useDegradedPerformance(onDegradedPerformanceCallback, user) {
|
|
const hasUser = user !== undefined && user !== null;
|
|
|
|
useEffect(() => {
|
|
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');
|
|
|
|
fetchWithTimeout(STATUS_TIMEOUT_LIMIT, fetch(STATUS_ENDPOINT, getParams(user)))
|
|
.then((response) => response.json())
|
|
.then((status) => {
|
|
if (status.general_state === STATUS_GENERAL_STATE.OFFLINE) {
|
|
onDegradedPerformanceCallback(STATUS_DOWN);
|
|
} else if (status.general_state !== STATUS_GENERAL_STATE.OK) {
|
|
onDegradedPerformanceCallback(STATUS_FAILING);
|
|
}
|
|
})
|
|
.catch(() => {
|
|
onDegradedPerformanceCallback(STATUS_FAILING);
|
|
});
|
|
}
|
|
}, [hasUser]);
|
|
}
|