Merge pull request #1293 from vector-im/connectionlostbanner_lk
Connection Lost Banner
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
"Change layout": "Change layout",
|
"Change layout": "Change layout",
|
||||||
"Close": "Close",
|
"Close": "Close",
|
||||||
"Confirm password": "Confirm password",
|
"Confirm password": "Confirm password",
|
||||||
|
"Connectivity to the server has been lost.": "Connectivity to the server has been lost.",
|
||||||
"Copied!": "Copied!",
|
"Copied!": "Copied!",
|
||||||
"Copy": "Copy",
|
"Copy": "Copy",
|
||||||
"Copy and share this call link": "Copy and share this call link",
|
"Copy and share this call link": "Copy and share this call link",
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import { usePageFocusStyle } from "./usePageFocusStyle";
|
|||||||
import { SequenceDiagramViewerPage } from "./SequenceDiagramViewerPage";
|
import { SequenceDiagramViewerPage } from "./SequenceDiagramViewerPage";
|
||||||
import { InspectorContextProvider } from "./room/GroupCallInspector";
|
import { InspectorContextProvider } from "./room/GroupCallInspector";
|
||||||
import { CrashView, LoadingView } from "./FullScreenView";
|
import { CrashView, LoadingView } from "./FullScreenView";
|
||||||
|
import { DisconnectedBanner } from "./DisconnectedBanner";
|
||||||
import { Initializer } from "./initializer";
|
import { Initializer } from "./initializer";
|
||||||
|
|
||||||
const SentryRoute = Sentry.withSentryRouting(Route);
|
const SentryRoute = Sentry.withSentryRouting(Route);
|
||||||
@@ -60,6 +61,7 @@ export default function App({ history }: AppProps) {
|
|||||||
<InspectorContextProvider>
|
<InspectorContextProvider>
|
||||||
<Sentry.ErrorBoundary fallback={errorPage}>
|
<Sentry.ErrorBoundary fallback={errorPage}>
|
||||||
<OverlayProvider>
|
<OverlayProvider>
|
||||||
|
<DisconnectedBanner />
|
||||||
<Switch>
|
<Switch>
|
||||||
<SentryRoute exact path="/">
|
<SentryRoute exact path="/">
|
||||||
<HomePage />
|
<HomePage />
|
||||||
|
|||||||
@@ -25,9 +25,10 @@ import {
|
|||||||
useMemo,
|
useMemo,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
import { MatrixClient } from "matrix-js-sdk/src/client";
|
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
import { logger } from "matrix-js-sdk/src/logger";
|
import { logger } from "matrix-js-sdk/src/logger";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync";
|
||||||
|
|
||||||
import { ErrorView } from "./FullScreenView";
|
import { ErrorView } from "./FullScreenView";
|
||||||
import {
|
import {
|
||||||
@@ -56,6 +57,9 @@ export type ClientState = ValidClientState | ErrorState;
|
|||||||
export type ValidClientState = {
|
export type ValidClientState = {
|
||||||
state: "valid";
|
state: "valid";
|
||||||
authenticated?: AuthenticatedClient;
|
authenticated?: AuthenticatedClient;
|
||||||
|
// 'Disconnected' rather than 'connected' because it tracks specifically
|
||||||
|
// whether the client is supposed to be connected but is not
|
||||||
|
disconnected: boolean;
|
||||||
setClient: (params?: SetClientParams) => void;
|
setClient: (params?: SetClientParams) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -264,6 +268,8 @@ export const ClientProvider: FC<Props> = ({ children }) => {
|
|||||||
}, [initClientState?.client, setAlreadyOpenedErr, t])
|
}, [initClientState?.client, setAlreadyOpenedErr, t])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const [isDisconnected, setIsDisconnected] = useState(false);
|
||||||
|
|
||||||
const state: ClientState = useMemo(() => {
|
const state: ClientState = useMemo(() => {
|
||||||
if (alreadyOpenedErr) {
|
if (alreadyOpenedErr) {
|
||||||
return { state: "error", error: alreadyOpenedErr };
|
return { state: "error", error: alreadyOpenedErr };
|
||||||
@@ -279,8 +285,27 @@ export const ClientProvider: FC<Props> = ({ children }) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return { state: "valid", authenticated, setClient };
|
return {
|
||||||
}, [alreadyOpenedErr, changePassword, initClientState, logout, setClient]);
|
state: "valid",
|
||||||
|
authenticated,
|
||||||
|
setClient,
|
||||||
|
disconnected: isDisconnected,
|
||||||
|
};
|
||||||
|
}, [
|
||||||
|
alreadyOpenedErr,
|
||||||
|
changePassword,
|
||||||
|
initClientState,
|
||||||
|
logout,
|
||||||
|
setClient,
|
||||||
|
isDisconnected,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const onSync = useCallback(
|
||||||
|
(state: SyncState, _old: SyncState | null, data?: ISyncStateData) => {
|
||||||
|
setIsDisconnected(clientIsDisconnected(state, data));
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!initClientState) {
|
if (!initClientState) {
|
||||||
@@ -292,7 +317,17 @@ export const ClientProvider: FC<Props> = ({ children }) => {
|
|||||||
|
|
||||||
if (PosthogAnalytics.hasInstance())
|
if (PosthogAnalytics.hasInstance())
|
||||||
PosthogAnalytics.instance.onLoginStatusChanged();
|
PosthogAnalytics.instance.onLoginStatusChanged();
|
||||||
}, [initClientState]);
|
|
||||||
|
if (initClientState.client) {
|
||||||
|
initClientState.client.on(ClientEvent.Sync, onSync);
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (initClientState.client) {
|
||||||
|
initClientState.client.removeListener(ClientEvent.Sync, onSync);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [initClientState, onSync]);
|
||||||
|
|
||||||
if (alreadyOpenedErr) {
|
if (alreadyOpenedErr) {
|
||||||
return <ErrorView error={alreadyOpenedErr} />;
|
return <ErrorView error={alreadyOpenedErr} />;
|
||||||
@@ -387,3 +422,8 @@ const loadSession = (): Session | undefined => {
|
|||||||
|
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const clientIsDisconnected = (
|
||||||
|
syncState: SyncState,
|
||||||
|
syncData?: ISyncStateData
|
||||||
|
) => syncState === "ERROR" && syncData?.error?.name === "ConnectionError";
|
||||||
|
|||||||
27
src/DisconnectedBanner.module.css
Normal file
27
src/DisconnectedBanner.module.css
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2023 New Vector Ltd
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
.banner {
|
||||||
|
position: absolute;
|
||||||
|
padding: 29px;
|
||||||
|
background-color: var(--quaternary-content);
|
||||||
|
vertical-align: middle;
|
||||||
|
font-size: var(--font-size-body);
|
||||||
|
text-align: center;
|
||||||
|
z-index: 1;
|
||||||
|
top: 76px;
|
||||||
|
width: calc(100% - 58px);
|
||||||
|
}
|
||||||
53
src/DisconnectedBanner.tsx
Normal file
53
src/DisconnectedBanner.tsx
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2023 New Vector Ltd
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import classNames from "classnames";
|
||||||
|
import { HTMLAttributes, ReactNode } from "react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
import styles from "./DisconnectedBanner.module.css";
|
||||||
|
import { ValidClientState, useClientState } from "./ClientContext";
|
||||||
|
|
||||||
|
interface DisconnectedBannerProps extends HTMLAttributes<HTMLElement> {
|
||||||
|
children?: ReactNode;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DisconnectedBanner({
|
||||||
|
children,
|
||||||
|
className,
|
||||||
|
...rest
|
||||||
|
}: DisconnectedBannerProps) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const clientState = useClientState();
|
||||||
|
let shouldShowBanner = false;
|
||||||
|
|
||||||
|
if (clientState?.state === "valid") {
|
||||||
|
const validClientState = clientState as ValidClientState;
|
||||||
|
shouldShowBanner = validClientState.disconnected;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{shouldShowBanner && (
|
||||||
|
<div className={classNames(styles.banner, className)} {...rest}>
|
||||||
|
{children}
|
||||||
|
{t("Connectivity to the server has been lost.")}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -45,6 +45,12 @@ class DependencyLoadStates {
|
|||||||
|
|
||||||
export class Initializer {
|
export class Initializer {
|
||||||
private static internalInstance: Initializer;
|
private static internalInstance: Initializer;
|
||||||
|
private isInitialized = false;
|
||||||
|
|
||||||
|
public static isInitialized(): boolean {
|
||||||
|
return Initializer.internalInstance?.isInitialized;
|
||||||
|
}
|
||||||
|
|
||||||
public static initBeforeReact() {
|
public static initBeforeReact() {
|
||||||
// this maybe also needs to return a promise in the future,
|
// this maybe also needs to return a promise in the future,
|
||||||
// if we have to do async inits before showing the loading screen
|
// if we have to do async inits before showing the loading screen
|
||||||
@@ -227,6 +233,7 @@ export class Initializer {
|
|||||||
if (this.loadStates.allDepsAreLoaded()) {
|
if (this.loadStates.allDepsAreLoaded()) {
|
||||||
// resolve if there is no dependency that is not loaded
|
// resolve if there is no dependency that is not loaded
|
||||||
resolve();
|
resolve();
|
||||||
|
this.isInitialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,11 +60,11 @@ function waitForSync(client: MatrixClient) {
|
|||||||
data?: ISyncStateData
|
data?: ISyncStateData
|
||||||
) => {
|
) => {
|
||||||
if (state === "PREPARED") {
|
if (state === "PREPARED") {
|
||||||
|
client.removeListener(ClientEvent.Sync, onSync);
|
||||||
resolve();
|
resolve();
|
||||||
client.removeListener(ClientEvent.Sync, onSync);
|
|
||||||
} else if (state === "ERROR") {
|
} else if (state === "ERROR") {
|
||||||
reject(data?.error);
|
|
||||||
client.removeListener(ClientEvent.Sync, onSync);
|
client.removeListener(ClientEvent.Sync, onSync);
|
||||||
|
reject(data?.error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
client.on(ClientEvent.Sync, onSync);
|
client.on(ClientEvent.Sync, onSync);
|
||||||
|
|||||||
Reference in New Issue
Block a user