Skip to content

Commit

Permalink
BC-7851 - Listen for delete message (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax authored Sep 23, 2024
1 parent ee1fcf5 commit 48e928e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/stores/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<UserPresence>(provider.awareness, {});
const yShapes: Map<TDShape> = doc.getMap("shapes");
const yBindings: Map<TDBinding> = doc.getMap("bindings");
Expand Down
12 changes: 11 additions & 1 deletion src/utils/redirectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -44,4 +49,9 @@ const handleRedirectIfNotValid = (userResult: UserResult, envs?: Envs) => {
}
};

export { redirectToLoginPage, redirectToErrorPage, handleRedirectIfNotValid };
export {
redirectToLoginPage,
redirectToErrorPage,
redirectToNotFoundErrorPage,
handleRedirectIfNotValid,
};

0 comments on commit 48e928e

Please sign in to comment.