Files
ThreadNet-Web/apps/web/src/utils/i18n-helpers.ts
T

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

55 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-03-24 15:33:22 -06:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2022-03-24 15:33:22 -06:00
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2022-03-24 15:33:22 -06:00
*/
2025-02-05 13:25:06 +00:00
import { type Room } from "matrix-js-sdk/src/matrix";
2022-03-24 15:33:22 -06:00
import { _t } from "../languageHandler";
import DMRoomMap from "./DMRoomMap";
import { formatList } from "./FormattingUtils";
2026-07-13 15:31:39 +01:00
import { SDKContextClass } from "../contexts/SDKContextClass.ts";
2022-03-24 15:33:22 -06:00
export interface RoomContextDetails {
details: string | null;
ariaLabel?: string;
2022-03-24 15:33:22 -06:00
}
export function roomContextDetails(room: Room): RoomContextDetails | null {
2022-03-24 15:33:22 -06: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 15:33:22 -06:00
}
2026-07-13 15:31:39 +01:00
const [parent, secondParent, ...otherParents] = SDKContextClass.instance.spaceStore.getKnownParents(room.roomId);
2022-03-24 15:33:22 -06:00
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 15:33:22 -06: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 15:33:22 -06:00
}
return { details: room.getCanonicalAlias() };
2022-03-24 15:33:22 -06:00
}