diff --git a/src/Rooms.ts b/src/Rooms.ts index 4d1682660b..f2f10e756d 100644 --- a/src/Rooms.ts +++ b/src/Rooms.ts @@ -28,7 +28,15 @@ import { MatrixClientPeg } from './MatrixClientPeg'; * @returns {string} A display alias for the given room */ export function getDisplayAliasForRoom(room: Room): string { - return room.getCanonicalAlias() || room.getAltAliases()[0]; + return getDisplayAliasForAliasSet( + room.getCanonicalAlias(), room.getAltAliases(), + ); +} + +// The various display alias getters all feed through this one path so there's a +// single place to change the logic. +export function getDisplayAliasForAliasSet(canonicalAlias: string, altAliases: string[]): string { + return canonicalAlias || altAliases?.[0]; } export function looksLikeDirectMessageRoom(room: Room, myUserId: string): boolean { diff --git a/src/components/structures/RoomDirectory.tsx b/src/components/structures/RoomDirectory.tsx index bd25a764a0..b1974d6c0a 100644 --- a/src/components/structures/RoomDirectory.tsx +++ b/src/components/structures/RoomDirectory.tsx @@ -44,6 +44,7 @@ import NetworkDropdown from "../views/directory/NetworkDropdown"; import ScrollPanel from "./ScrollPanel"; import Spinner from "../views/elements/Spinner"; import { ActionPayload } from "../../dispatcher/payloads"; +import { getDisplayAliasForAliasSet } from "../../Rooms"; const MAX_NAME_LENGTH = 80; const MAX_TOPIC_LENGTH = 800; @@ -854,5 +855,5 @@ export default class RoomDirectory extends React.Component { // Similar to matrix-react-sdk's MatrixTools.getDisplayAliasForRoom // but works with the objects we get from the public room list function getDisplayAliasForRoom(room: IRoom) { - return room.canonical_alias || room.aliases?.[0] || ""; + return getDisplayAliasForAliasSet(room.canonical_alias, room.aliases); } diff --git a/src/components/structures/SpaceRoomDirectory.tsx b/src/components/structures/SpaceRoomDirectory.tsx index 2ee0327420..a08fbb5098 100644 --- a/src/components/structures/SpaceRoomDirectory.tsx +++ b/src/components/structures/SpaceRoomDirectory.tsx @@ -42,6 +42,7 @@ import { useStateToggle } from "../../hooks/useStateToggle"; import { getChildOrder } from "../../stores/SpaceStore"; import AccessibleTooltipButton from "../views/elements/AccessibleTooltipButton"; import { linkifyElement } from "../../HtmlUtils"; +import { getDisplayAliasForAliasSet } from "../../Rooms"; interface IHierarchyProps { space: Room; @@ -666,5 +667,5 @@ export default SpaceRoomDirectory; // Similar to matrix-react-sdk's MatrixTools.getDisplayAliasForRoom // but works with the objects we get from the public room list function getDisplayAliasForRoom(room: ISpaceSummaryRoom) { - return room.canonical_alias || (room.aliases ? room.aliases[0] : ""); + return getDisplayAliasForAliasSet(room.canonical_alias, room.aliases); } diff --git a/src/components/views/rooms/RoomDetailRow.js b/src/components/views/rooms/RoomDetailRow.js index 6cee691dfa..25fff09c10 100644 --- a/src/components/views/rooms/RoomDetailRow.js +++ b/src/components/views/rooms/RoomDetailRow.js @@ -1,5 +1,5 @@ /* -Copyright 2017 New Vector Ltd. +Copyright 2017-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -21,9 +21,10 @@ import { linkifyElement } from '../../../HtmlUtils'; import PropTypes from 'prop-types'; import { replaceableComponent } from "../../../utils/replaceableComponent"; import { mediaFromMxc } from "../../../customisations/Media"; +import { getDisplayAliasForAliasSet } from '../../../Rooms'; export function getDisplayAliasForRoom(room) { - return room.canonicalAlias || (room.aliases ? room.aliases[0] : ""); + return getDisplayAliasForAliasSet(room.canonicalAlias, room.aliases); } export const roomShape = PropTypes.shape({