element-web/src/utils/i18n-helpers.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-03-24 21:33:22 +00:00
/*
Copyright 2024 New Vector Ltd.
2022-03-24 21:33:22 +00:00
Copyright 2022 The Matrix.org Foundation C.I.C.
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
*/
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";
import { formatList } from "./FormattingUtils";
2022-03-24 21:33:22 +00:00
export interface RoomContextDetails {
details: string | null;
ariaLabel?: string;
2022-03-24 21:33:22 +00:00
}
export function roomContextDetails(room: Room): RoomContextDetails | null {
2022-03-24 21:33:22 +00:00
const dmPartner = DMRoomMap.shared().getUserIdForRoomId(room.roomId);
// if weve got more than 2 users, dont treat it like a regular DM
const isGroupDm = room.getMembers().length > 2;
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
const space1Name = room.client.getRoom(parent)?.name;
const space2Name = room.client.getRoom(secondParent)?.name;
return {
details: formatList([space1Name ?? "", space2Name ?? ""]),
ariaLabel: _t("in_space1_and_space2", { space1Name, space2Name }),
};
2022-03-24 21:33:22 +00:00
} else if (parent) {
const spaceName = room.client.getRoom(parent)?.name ?? "";
const count = otherParents.length;
if (count > 0) {
return {
details: formatList([spaceName, ...otherParents], 1),
ariaLabel: _t("in_space_and_n_other_spaces", { spaceName, count }),
};
}
return {
details: spaceName,
ariaLabel: _t("in_space", { spaceName }),
};
2022-03-24 21:33:22 +00:00
}
return { details: room.getCanonicalAlias() };
2022-03-24 21:33:22 +00:00
}