From 48e928e87a24eaf5e72003f6362071a52c37b644 Mon Sep 17 00:00:00 2001 From: Max Bischof <106820326+bischofmax@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:52:59 +0200 Subject: [PATCH] BC-7851 - Listen for delete message (#92) --- src/stores/setup.ts | 20 +++++++++++++++++++- src/utils/redirectUtils.ts | 12 +++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/stores/setup.ts b/src/stores/setup.ts index 7b34ef2f..ff5a1a1b 100644 --- a/src/stores/setup.ts +++ b/src/stores/setup.ts @@ -6,7 +6,10 @@ import { UserPresence } from "../types/UserPresence"; import { getConnectionOptions, getRoomId } from "../utils/connectionOptions"; import { getEnvs } from "../utils/envConfig"; import { getUserData } from "../utils/userData"; -import { handleRedirectIfNotValid } from "../utils/redirectUtils"; +import { + handleRedirectIfNotValid, + redirectToNotFoundErrorPage, +} from "../utils/redirectUtils"; import { clearErrorData } from "../utils/errorData"; import { setDefaultState } from "../utils/userSettings"; @@ -34,6 +37,21 @@ const provider = new WebsocketProvider( }, ); +provider.on("status", (event: { status: string }) => { + if (!provider.ws?.onmessage || event.status !== "connected") return; + + const originalOnMessage = provider.ws.onmessage.bind(provider.ws); + + provider.ws.onmessage = (messageEvent) => { + if (messageEvent.data === "action:delete") { + provider.disconnect(); + redirectToNotFoundErrorPage(); + } else { + originalOnMessage(messageEvent); + } + }; +}); + const room = new Room(provider.awareness, {}); const yShapes: Map = doc.getMap("shapes"); const yBindings: Map = doc.getMap("bindings"); diff --git a/src/utils/redirectUtils.ts b/src/utils/redirectUtils.ts index c5980f24..0b339639 100644 --- a/src/utils/redirectUtils.ts +++ b/src/utils/redirectUtils.ts @@ -25,6 +25,11 @@ const redirectToErrorPage = () => { } }; +const redirectToNotFoundErrorPage = () => { + setErrorData(HttpStatusCode.NotFound, "error.404"); + redirectToErrorPage(); +}; + const handleRedirectIfNotValid = (userResult: UserResult, envs?: Envs) => { if (userResult.statusCode === HttpStatusCode.Unauthorized) { redirectToLoginPage(); @@ -44,4 +49,9 @@ const handleRedirectIfNotValid = (userResult: UserResult, envs?: Envs) => { } }; -export { redirectToLoginPage, redirectToErrorPage, handleRedirectIfNotValid }; +export { + redirectToLoginPage, + redirectToErrorPage, + redirectToNotFoundErrorPage, + handleRedirectIfNotValid, +};