2022-03-24 21:33:22 +00:00
|
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-03-24 21:33:22 +00:00
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-03-24 21:33:22 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2023-08-04 07:36:16 +00:00
|
|
|
|
import { Room } from "matrix-js-sdk/src/matrix";
|
2022-03-24 21:33:22 +00:00
|
|
|
|
|
|
|
|
|
import SpaceStore from "../stores/spaces/SpaceStore";
|
|
|
|
|
import { _t } from "../languageHandler";
|
|
|
|
|
import DMRoomMap from "./DMRoomMap";
|
2023-09-25 11:18:15 +00:00
|
|
|
|
import { formatList } from "./FormattingUtils";
|
2022-03-24 21:33:22 +00:00
|
|
|
|
|
2022-07-11 11:34:23 +00:00
|
|
|
|
export interface RoomContextDetails {
|
2023-02-13 17:01:43 +00:00
|
|
|
|
details: string | null;
|
2022-07-11 11:34:23 +00:00
|
|
|
|
ariaLabel?: string;
|
2022-03-24 21:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:34:23 +00:00
|
|
|
|
export function roomContextDetails(room: Room): RoomContextDetails | null {
|
2022-03-24 21:33:22 +00:00
|
|
|
|
const dmPartner = DMRoomMap.shared().getUserIdForRoomId(room.roomId);
|
2022-06-30 09:44:56 +00:00
|
|
|
|
// if we’ve got more than 2 users, don’t treat it like a regular DM
|
|
|
|
|
const isGroupDm = room.getMembers().length > 2;
|
2022-07-11 11:34:23 +00:00
|
|
|
|
if (!room.isSpaceRoom() && dmPartner && !isGroupDm) {
|
|
|
|
|
return { details: dmPartner };
|
2022-03-24 21:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [parent, secondParent, ...otherParents] = SpaceStore.instance.getKnownParents(room.roomId);
|
|
|
|
|
if (secondParent && !otherParents?.length) {
|
|
|
|
|
// exactly 2 edge case for improved i18n
|
2022-07-11 11:34:23 +00:00
|
|
|
|
const space1Name = room.client.getRoom(parent)?.name;
|
|
|
|
|
const space2Name = room.client.getRoom(secondParent)?.name;
|
|
|
|
|
return {
|
2023-09-25 11:18:15 +00:00
|
|
|
|
details: formatList([space1Name ?? "", space2Name ?? ""]),
|
2023-09-22 15:39:40 +00:00
|
|
|
|
ariaLabel: _t("in_space1_and_space2", { space1Name, space2Name }),
|
2022-07-11 11:34:23 +00:00
|
|
|
|
};
|
2022-03-24 21:33:22 +00:00
|
|
|
|
} else if (parent) {
|
2023-08-16 15:26:21 +00:00
|
|
|
|
const spaceName = room.client.getRoom(parent)?.name ?? "";
|
2022-07-11 11:34:23 +00:00
|
|
|
|
const count = otherParents.length;
|
2023-08-16 15:26:21 +00:00
|
|
|
|
if (count > 0) {
|
|
|
|
|
return {
|
2023-09-25 11:18:15 +00:00
|
|
|
|
details: formatList([spaceName, ...otherParents], 1),
|
2023-09-22 15:39:40 +00:00
|
|
|
|
ariaLabel: _t("in_space_and_n_other_spaces", { spaceName, count }),
|
2023-08-16 15:26:21 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2022-07-11 11:34:23 +00:00
|
|
|
|
return {
|
2023-08-16 15:26:21 +00:00
|
|
|
|
details: spaceName,
|
2023-09-22 15:39:40 +00:00
|
|
|
|
ariaLabel: _t("in_space", { spaceName }),
|
2022-07-11 11:34:23 +00:00
|
|
|
|
};
|
2022-03-24 21:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:34:23 +00:00
|
|
|
|
return { details: room.getCanonicalAlias() };
|
2022-03-24 21:33:22 +00:00
|
|
|
|
}
|