diff --git a/ui/src/CurrentUser.ts b/ui/src/CurrentUser.ts index 6eeea03..59d5a55 100644 --- a/ui/src/CurrentUser.ts +++ b/ui/src/CurrentUser.ts @@ -11,7 +11,7 @@ const tokenKey = 'gotify-login-key'; export class CurrentUser { private tokenCache: string | null = null; private reconnectTimeoutId: number | null = null; - private readonly reconnectTime: number = 3000; + private reconnectTime = 7500; @observable public loggedIn = false; @observable @@ -19,7 +19,7 @@ export class CurrentUser { @observable public user: IUser = {name: 'unknown', admin: false, id: -1}; @observable - public hasNetwork = true; + public connectionErrorMessage: string | null = null; public constructor(private readonly snack: SnackReporter) {} @@ -87,16 +87,24 @@ export class CurrentUser { .then((passThrough) => { this.user = passThrough.data; this.loggedIn = true; - this.hasNetwork = true; + this.connectionErrorMessage = null; + this.reconnectTime = 7500; return passThrough; }) .catch((error: AxiosError) => { if (!error || !error.response) { - this.lostNetwork(); + this.connectionError('No network connection or server unavailable.'); return Promise.reject(error); } - this.hasNetwork = true; + if (error.response.status >= 500) { + this.connectionError( + `${error.response.statusText} (code: ${error.response.status}).` + ); + return Promise.reject(error); + } + + this.connectionErrorMessage = null; if (error.response.status >= 400 && error.response.status < 500) { this.logout(); @@ -135,8 +143,8 @@ export class CurrentUser { }); }; - private lostNetwork = () => { - this.hasNetwork = false; + private connectionError = (message: string) => { + this.connectionErrorMessage = message; if (this.reconnectTimeoutId !== null) { window.clearTimeout(this.reconnectTimeoutId); } @@ -144,5 +152,6 @@ export class CurrentUser { () => this.tryReconnect(true), this.reconnectTime ); + this.reconnectTime = Math.min(this.reconnectTime * 2, 120000); }; } diff --git a/ui/src/common/NetworkLostBanner.tsx b/ui/src/common/ConnectionErrorBanner.tsx similarity index 77% rename from ui/src/common/NetworkLostBanner.tsx rename to ui/src/common/ConnectionErrorBanner.tsx index 11a73d8..71b21e5 100644 --- a/ui/src/common/NetworkLostBanner.tsx +++ b/ui/src/common/ConnectionErrorBanner.tsx @@ -2,12 +2,13 @@ import React from 'react'; import Button from '@material-ui/core/Button'; import Typography from '@material-ui/core/Typography'; -interface NetworkLostBannerProps { +interface ConnectionErrorBannerProps { height: number; retry: () => void; + message: string; } -export const NetworkLostBanner = ({height, retry}: NetworkLostBannerProps) => { +export const ConnectionErrorBanner = ({height, retry, message}: ConnectionErrorBannerProps) => { return (