From fb75adebb33b9d99051134004959034ecc8148aa Mon Sep 17 00:00:00 2001
From: Ajay Bura <32841439+ajbura@users.noreply.github.com>
Date: Thu, 18 Jul 2024 09:57:05 +0530
Subject: [PATCH] fix selecting tombstone room opens replacement room

---
 src/app/utils/matrix.ts | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/app/utils/matrix.ts b/src/app/utils/matrix.ts
index 55e16a95..278bb46b 100644
--- a/src/app/utils/matrix.ts
+++ b/src/app/utils/matrix.ts
@@ -14,6 +14,8 @@ import {
 } from 'matrix-js-sdk';
 import { IImageInfo, IThumbnailContent, IVideoInfo } from '../../types/matrix/common';
 import { AccountDataEvent } from '../../types/matrix/accountData';
+import { getStateEvent } from './room';
+import { StateEvent } from '../../types/matrix/room';
 
 export const matchMxId = (id: string): RegExpMatchArray | null =>
   id.match(/^([@!$+#])(\S+):(\S+)$/);
@@ -42,8 +44,12 @@ export const parseMatrixToUrl = (url: string): [string | undefined, string | und
 export const getCanonicalAliasRoomId = (mx: MatrixClient, alias: string): string | undefined =>
   mx.getRooms()?.find((room) => room.getCanonicalAlias() === alias)?.roomId;
 
-export const getCanonicalAliasOrRoomId = (mx: MatrixClient, roomId: string): string =>
-  mx.getRoom(roomId)?.getCanonicalAlias() || roomId;
+export const getCanonicalAliasOrRoomId = (mx: MatrixClient, roomId: string): string => {
+  const room = mx.getRoom(roomId);
+  if (!room) return roomId;
+  if (getStateEvent(room, StateEvent.RoomTombstone) !== undefined) return roomId;
+  return room.getCanonicalAlias() || roomId;
+};
 
 export const getImageInfo = (img: HTMLImageElement, fileOrBlob: File | Blob): IImageInfo => {
   const info: IImageInfo = {};