diff --git a/src/Rooms.ts b/src/Rooms.ts index 9bb43c3d8c..32419080ac 100644 --- a/src/Rooms.ts +++ b/src/Rooms.ts @@ -19,9 +19,6 @@ import { EventType } from "matrix-js-sdk/src/@types/event"; import { MatrixClientPeg } from './MatrixClientPeg'; import AliasCustomisations from './customisations/Alias'; -import DMRoomMap from "./utils/DMRoomMap"; -import SpaceStore from "./stores/spaces/SpaceStore"; -import { _t } from "./languageHandler"; /** * Given a room object, return the alias we should use for it, @@ -157,48 +154,3 @@ function guessDMRoomTargetId(room: Room, myUserId: string): string { if (oldestUser === undefined) return myUserId; return oldestUser.userId; } - -export function spaceContextDetailsText(space: Room): string { - if (!space.isSpaceRoom()) return undefined; - - const [parent, secondParent, ...otherParents] = SpaceStore.instance.getKnownParents(space.roomId); - if (secondParent && !otherParents?.length) { - // exactly 2 edge case for improved i18n - return _t("%(space1Name)s and %(space2Name)s", { - space1Name: space.client.getRoom(parent)?.name, - space2Name: space.client.getRoom(secondParent)?.name, - }); - } else if (parent) { - return _t("%(spaceName)s and %(count)s others", { - spaceName: space.client.getRoom(parent)?.name, - count: otherParents.length, - }); - } - - return space.getCanonicalAlias(); -} - -export function roomContextDetailsText(room: Room): string { - if (room.isSpaceRoom()) return undefined; - - const dmPartner = DMRoomMap.shared().getUserIdForRoomId(room.roomId); - if (dmPartner) { - return dmPartner; - } - - const [parent, secondParent, ...otherParents] = SpaceStore.instance.getKnownParents(room.roomId); - if (secondParent && !otherParents?.length) { - // exactly 2 edge case for improved i18n - return _t("%(space1Name)s and %(space2Name)s", { - space1Name: room.client.getRoom(parent)?.name, - space2Name: room.client.getRoom(secondParent)?.name, - }); - } else if (parent) { - return _t("%(spaceName)s and %(count)s others", { - spaceName: room.client.getRoom(parent)?.name, - count: otherParents.length, - }); - } - - return room.getCanonicalAlias(); -} diff --git a/src/components/views/dialogs/ForwardDialog.tsx b/src/components/views/dialogs/ForwardDialog.tsx index d3f38fa9d1..0a063df9c7 100644 --- a/src/components/views/dialogs/ForwardDialog.tsx +++ b/src/components/views/dialogs/ForwardDialog.tsx @@ -43,10 +43,10 @@ import QueryMatcher from "../../../autocomplete/QueryMatcher"; import TruncatedList from "../elements/TruncatedList"; import EntityTile from "../rooms/EntityTile"; import BaseAvatar from "../avatars/BaseAvatar"; -import { roomContextDetailsText } from "../../../Rooms"; import { Action } from "../../../dispatcher/actions"; import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload"; import { ButtonEvent } from "../elements/AccessibleButton"; +import { roomContextDetailsText } from "../../../utils/i18n-helpers"; const AVATAR_SIZE = 30; diff --git a/src/components/views/dialogs/SpotlightDialog.tsx b/src/components/views/dialogs/SpotlightDialog.tsx index da13b85919..8b4e8ed701 100644 --- a/src/components/views/dialogs/SpotlightDialog.tsx +++ b/src/components/views/dialogs/SpotlightDialog.tsx @@ -53,7 +53,6 @@ import DMRoomMap from "../../../utils/DMRoomMap"; import { mediaFromMxc } from "../../../customisations/Media"; import BaseAvatar from "../avatars/BaseAvatar"; import Spinner from "../elements/Spinner"; -import { roomContextDetailsText, spaceContextDetailsText } from "../../../Rooms"; import DecoratedRoomAvatar from "../avatars/DecoratedRoomAvatar"; import { Action } from "../../../dispatcher/actions"; import Modal from "../../../Modal"; @@ -74,6 +73,7 @@ import { getKeyBindingsManager } from "../../../KeyBindingsManager"; import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts"; import { PosthogAnalytics } from "../../../PosthogAnalytics"; import { getCachedRoomIDForAlias } from "../../../RoomAliasCache"; +import { roomContextDetailsText, spaceContextDetailsText } from "../../../utils/i18n-helpers"; const MAX_RECENT_SEARCHES = 10; const SECTION_LIMIT = 50; // only show 50 results per section for performance reasons diff --git a/src/components/views/rooms/RecentlyViewedButton.tsx b/src/components/views/rooms/RecentlyViewedButton.tsx index 5a5ec1520e..9fd71d50e2 100644 --- a/src/components/views/rooms/RecentlyViewedButton.tsx +++ b/src/components/views/rooms/RecentlyViewedButton.tsx @@ -23,10 +23,10 @@ import { useEventEmitterState } from "../../../hooks/useEventEmitter"; import { _t } from "../../../languageHandler"; import dis from "../../../dispatcher/dispatcher"; import InteractiveTooltip, { Direction } from "../elements/InteractiveTooltip"; -import { roomContextDetailsText } from "../../../Rooms"; import { Action } from "../../../dispatcher/actions"; import DecoratedRoomAvatar from "../avatars/DecoratedRoomAvatar"; import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload"; +import { roomContextDetailsText } from "../../../utils/i18n-helpers"; const RecentlyViewedButton = () => { const tooltipRef = useRef(); diff --git a/src/utils/i18n-helpers.ts b/src/utils/i18n-helpers.ts new file mode 100644 index 0000000000..ce1d04f9f4 --- /dev/null +++ b/src/utils/i18n-helpers.ts @@ -0,0 +1,66 @@ +/* +Copyright 2022 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. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { Room } from "matrix-js-sdk/src/models/room"; + +import SpaceStore from "../stores/spaces/SpaceStore"; +import { _t } from "../languageHandler"; +import DMRoomMap from "./DMRoomMap"; + +export function spaceContextDetailsText(space: Room): string { + if (!space.isSpaceRoom()) return undefined; + + const [parent, secondParent, ...otherParents] = SpaceStore.instance.getKnownParents(space.roomId); + if (secondParent && !otherParents?.length) { + // exactly 2 edge case for improved i18n + return _t("%(space1Name)s and %(space2Name)s", { + space1Name: space.client.getRoom(parent)?.name, + space2Name: space.client.getRoom(secondParent)?.name, + }); + } else if (parent) { + return _t("%(spaceName)s and %(count)s others", { + spaceName: space.client.getRoom(parent)?.name, + count: otherParents.length, + }); + } + + return space.getCanonicalAlias(); +} + +export function roomContextDetailsText(room: Room): string { + if (room.isSpaceRoom()) return undefined; + + const dmPartner = DMRoomMap.shared().getUserIdForRoomId(room.roomId); + if (dmPartner) { + return dmPartner; + } + + const [parent, secondParent, ...otherParents] = SpaceStore.instance.getKnownParents(room.roomId); + if (secondParent && !otherParents?.length) { + // exactly 2 edge case for improved i18n + return _t("%(space1Name)s and %(space2Name)s", { + space1Name: room.client.getRoom(parent)?.name, + space2Name: room.client.getRoom(secondParent)?.name, + }); + } else if (parent) { + return _t("%(spaceName)s and %(count)s others", { + spaceName: room.client.getRoom(parent)?.name, + count: otherParents.length, + }); + } + + return room.getCanonicalAlias(); +}