Files
ThreadNet-Web/apps/web/src/stores/LifecycleStore.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-05-24 16:56:13 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2021-06-07 10:57:39 +01:00
Copyright 2017-2021 The Matrix.org Foundation C.I.C.
2017-05-24 16:56:13 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2017-05-24 16:56:13 +01:00
*/
2021-06-07 10:57:39 +01:00
import { SyncState } from "matrix-js-sdk/src/matrix";
import { MINIMUM_MATRIX_VERSION, SUPPORTED_MATRIX_VERSIONS } from "matrix-js-sdk/src/version-support";
import { logger } from "matrix-js-sdk/src/logger";
2022-02-22 11:04:27 +01:00
import { Action } from "../dispatcher/actions";
2020-05-13 20:41:41 -06:00
import dis from "../dispatcher/dispatcher";
2025-02-05 13:25:06 +00:00
import { type ActionPayload } from "../dispatcher/payloads";
import { type DoAfterSyncPreparedPayload } from "../dispatcher/payloads/DoAfterSyncPreparedPayload";
2023-03-08 14:19:05 +00:00
import { AsyncStore } from "./AsyncStore";
import { MatrixClientPeg } from "../MatrixClientPeg";
import ToastStore from "./ToastStore";
import { _t } from "../languageHandler";
import SdkConfig from "../SdkConfig";
import GenericToast from "../components/views/toasts/GenericToast";
2021-06-07 10:57:39 +01:00
interface IState {
deferredAction: ActionPayload | null;
2021-06-07 10:57:39 +01:00
}
2017-05-24 16:56:13 +01:00
2023-02-13 11:39:16 +00:00
const INITIAL_STATE: IState = {
2021-06-07 10:57:39 +01:00
deferredAction: null,
2017-05-26 17:23:02 +01:00
};
2017-05-24 16:56:13 +01:00
/**
2023-03-08 14:19:05 +00:00
* A class for storing application state to do with authentication. This is a simple
2019-01-21 16:11:10 -06:00
* store that listens for actions and updates its state accordingly, informing any
2017-05-24 16:56:13 +01:00
* listeners (views) of state changes.
*/
2023-03-08 14:19:05 +00:00
class LifecycleStore extends AsyncStore<IState> {
public constructor() {
2023-03-08 14:19:05 +00:00
super(dis, INITIAL_STATE);
2017-05-24 16:56:13 +01:00
}
2023-03-08 14:19:05 +00:00
protected onDispatch(payload: ActionPayload | DoAfterSyncPreparedPayload<ActionPayload>): void {
2017-05-24 16:56:13 +01:00
switch (payload.action) {
2022-02-22 11:04:27 +01:00
case Action.DoAfterSyncPrepared:
2023-03-08 14:19:05 +00:00
this.updateState({
2021-06-07 10:57:39 +01:00
deferredAction: payload.deferred_action,
2017-05-24 16:56:13 +01:00
});
break;
2017-06-05 18:37:38 +01:00
case "cancel_after_sync_prepared":
2023-03-08 14:19:05 +00:00
this.updateState({
2021-06-07 10:57:39 +01:00
deferredAction: null,
2017-06-05 18:37:38 +01:00
});
break;
case "MatrixActions.sync": {
if (payload.state === SyncState.Syncing && payload.prevState !== SyncState.Syncing) {
// We've reconnected to the server: update server version support
// This is async but we don't care about the result, so just fire & forget.
checkServerVersions();
}
2017-05-24 16:56:13 +01:00
if (payload.state !== "PREPARED") {
break;
}
2021-06-07 10:57:39 +01:00
if (!this.state.deferredAction) break;
const deferredAction = Object.assign({}, this.state.deferredAction);
2023-03-08 14:19:05 +00:00
this.updateState({
2021-06-07 10:57:39 +01:00
deferredAction: null,
2017-05-24 16:56:13 +01:00
});
dis.dispatch(deferredAction);
break;
2017-07-01 14:35:40 +01:00
}
case Action.ClientNotViable:
case Action.OnLoggedOut:
2017-05-26 17:23:02 +01:00
this.reset();
2017-05-25 17:16:16 +01:00
break;
2017-05-24 16:56:13 +01:00
}
}
}
async function checkServerVersions(): Promise<void> {
try {
const client = MatrixClientPeg.get();
if (!client) return;
for (const version of SUPPORTED_MATRIX_VERSIONS) {
// Check if the server supports this spec version. (`isVersionSupported` caches the response, so this loop will
// only make a single HTTP request).
// Note that although we do this on a reconnect, we cache the server's versions in memory
// indefinitely, so it will only ever trigger the toast on the first connection after a fresh
// restart of the client.
if (await client.isVersionSupported(version)) {
// we found a compatible spec version
return;
}
}
// This is retrospective doc having debated about the exactly what this toast is for, but
// our guess is that it's a nudge to update, or ask your HS admin to update your Homeserver
// after a new version of Element has come out, in a way that doesn't lock you out of all
// your messages.
const toastKey = "LEGACY_SERVER";
ToastStore.sharedInstance().addOrReplaceToast({
key: toastKey,
title: _t("unsupported_server_title"),
props: {
description: _t("unsupported_server_description", {
version: MINIMUM_MATRIX_VERSION,
brand: SdkConfig.get().brand,
}),
2024-07-30 13:57:15 +01:00
primaryLabel: _t("action|ok"),
onPrimaryClick: () => {
ToastStore.sharedInstance().dismissToast(toastKey);
},
},
component: GenericToast,
priority: 98,
});
} catch (e) {
logger.warn("Failed to check server versions", e);
}
}
let singletonLifecycleStore: LifecycleStore | null = null;
2017-05-24 16:56:13 +01:00
if (!singletonLifecycleStore) {
singletonLifecycleStore = new LifecycleStore();
}
export default singletonLifecycleStore!;