Files
ThreadNet-Web/src/components/views/rooms/RoomInfoLine.tsx
T

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

86 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-05-04 17:02:06 -04:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2022-05-04 17:02:06 -04:00
Copyright 2022 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01: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-05-04 17:02:06 -04:00
*/
import React, { FC } from "react";
import { Room, JoinRule, MatrixClient } from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types";
2022-05-04 17:02:06 -04:00
import { _t } from "../../../languageHandler";
import RightPanelStore from "../../../stores/right-panel/RightPanelStore";
import { RightPanelPhases } from "../../../stores/right-panel/RightPanelStorePhases";
import { useAsyncMemo } from "../../../hooks/useAsyncMemo";
import { useRoomState } from "../../../hooks/useRoomState";
import { useRoomMemberCount, useMyRoomMembership } from "../../../hooks/useRoomMembers";
import AccessibleButton from "../elements/AccessibleButton";
import { isVideoRoom as calcIsVideoRoom } from "../../../utils/video-rooms";
2022-05-04 17:02:06 -04:00
interface IProps {
room: Room;
}
const RoomInfoLine: FC<IProps> = ({ room }) => {
// summary will begin as undefined whilst loading and go null if it fails to load or we are not invited.
const summary = useAsyncMemo(async (): Promise<Awaited<ReturnType<MatrixClient["getRoomSummary"]>> | null> => {
if (room.getMyMembership() !== KnownMembership.Invite) return null;
2022-05-04 17:02:06 -04:00
try {
return await room.client.getRoomSummary(room.roomId);
2022-05-04 17:02:06 -04:00
} catch (e) {
return null;
}
}, [room]);
const joinRule = useRoomState(room, (state) => state.getJoinRule());
const membership = useMyRoomMembership(room);
const memberCount = useRoomMemberCount(room);
const isVideoRoom = calcIsVideoRoom(room);
2022-09-16 11:12:27 -04:00
2022-05-04 17:02:06 -04:00
let iconClass: string;
let roomType: string;
2022-09-16 11:12:27 -04:00
if (isVideoRoom) {
2022-05-04 17:02:06 -04:00
iconClass = "mx_RoomInfoLine_video";
roomType = _t("common|video_room");
2022-05-04 17:02:06 -04:00
} else if (joinRule === JoinRule.Public) {
iconClass = "mx_RoomInfoLine_public";
roomType = room.isSpaceRoom() ? _t("common|public_space") : _t("common|public_room");
2022-05-04 17:02:06 -04:00
} else {
iconClass = "mx_RoomInfoLine_private";
roomType = room.isSpaceRoom() ? _t("common|private_space") : _t("common|private_room");
2022-05-04 17:02:06 -04:00
}
let members: JSX.Element | undefined;
if (membership === KnownMembership.Invite && summary) {
2022-05-04 17:02:06 -04:00
// Don't trust local state and instead use the summary API
members = (
<span className="mx_RoomInfoLine_members">
{_t("common|n_members", { count: summary.num_joined_members })}
2022-05-04 17:02:06 -04:00
</span>
);
} else if (memberCount && summary !== undefined) {
// summary is not still loading
const viewMembers = (): void =>
2022-05-04 17:02:06 -04:00
RightPanelStore.instance.setCard({
phase: room.isSpaceRoom() ? RightPanelPhases.SpaceMemberList : RightPanelPhases.RoomMemberList,
});
members = (
<AccessibleButton kind="link" className="mx_RoomInfoLine_members" onClick={viewMembers}>
{_t("common|n_members", { count: memberCount })}
2022-05-04 17:02:06 -04:00
</AccessibleButton>
);
}
return (
<div className={`mx_RoomInfoLine ${iconClass}`}>
{roomType}
{members}
</div>
);
};
export default RoomInfoLine;