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

329 lines
12 KiB
TypeScript
Raw Normal View History

2015-11-27 10:42:03 +00:00
/*
2016-01-07 04:06:39 +00:00
Copyright 2015, 2016 OpenMarket Ltd
2021-07-01 19:54:05 +01:00
Copyright 2019, 2021 The Matrix.org Foundation C.I.C.
2015-11-27 10:42:03 +00:00
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 React from 'react';
import classNames from 'classnames';
2021-12-09 09:10:23 +00:00
import { throttle } from 'lodash';
import { MatrixEvent, Room, RoomStateEvent } from 'matrix-js-sdk/src/matrix';
2021-12-09 09:10:23 +00:00
import { CallType } from "matrix-js-sdk/src/webrtc/call";
2017-05-25 11:39:08 +01:00
import { _t } from '../../../languageHandler';
2021-06-03 08:41:22 +01:00
import { MatrixClientPeg } from '../../../MatrixClientPeg';
import defaultDispatcher from "../../../dispatcher/dispatcher";
import { Action } from "../../../dispatcher/actions";
import { UserTab } from "../dialogs/UserTab";
2017-10-28 20:21:34 -06:00
import SettingsStore from "../../../settings/SettingsStore";
import RoomHeaderButtons from '../right_panel/RoomHeaderButtons';
2019-02-01 13:40:19 +01:00
import E2EIcon from './E2EIcon';
import DecoratedRoomAvatar from "../avatars/DecoratedRoomAvatar";
2020-07-17 18:16:21 +01:00
import AccessibleTooltipButton from "../elements/AccessibleTooltipButton";
import RoomTopic from "../elements/RoomTopic";
import RoomName from "../elements/RoomName";
2021-07-01 19:54:05 +01:00
import { E2EStatus } from '../../../utils/ShieldUtils';
import { IOOBData } from '../../../stores/ThreepidInviteStore';
import { SearchScope } from './SearchBar';
2021-11-15 11:39:25 +00:00
import { ContextMenuTooltipButton } from '../../structures/ContextMenu';
import RoomContextMenu from "../context_menus/RoomContextMenu";
import { contextMenuBelow } from './RoomTile';
import { RoomNotificationStateStore } from '../../../stores/notifications/RoomNotificationStateStore';
2022-01-05 16:14:44 +01:00
import { RightPanelPhases } from '../../../stores/right-panel/RightPanelStorePhases';
import { NotificationStateEvents } from '../../../stores/notifications/NotificationState';
import RoomContext from "../../../contexts/RoomContext";
import RoomLiveShareWarning from '../beacon/RoomLiveShareWarning';
import { BetaPill } from "../beta/BetaCard";
2021-07-01 19:54:05 +01:00
export interface ISearchInfo {
searchTerm: string;
searchScope: SearchScope;
searchCount: number;
}
interface IProps {
2021-07-01 20:57:56 +01:00
room: Room;
oobData?: IOOBData;
inRoom: boolean;
2021-07-01 19:54:05 +01:00
onSearchClick: () => void;
2022-04-20 11:03:33 -04:00
onInviteClick: () => void;
2021-07-01 19:54:05 +01:00
onForgetClick: () => void;
2021-11-30 19:09:13 +01:00
onCallPlaced: (type: CallType) => void;
2021-07-01 19:54:05 +01:00
onAppsClick: () => void;
e2eStatus: E2EStatus;
appsShown: boolean;
searchInfo: ISearchInfo;
excludedRightPanelPhaseButtons?: Array<RightPanelPhases>;
showButtons?: boolean;
enableRoomOptionsMenu?: boolean;
2021-07-01 19:54:05 +01:00
}
2016-01-11 12:46:12 +00:00
2021-11-15 11:39:25 +00:00
interface IState {
contextMenuPosition?: DOMRect;
}
export default class RoomHeader extends React.Component<IProps, IState> {
2020-08-29 12:14:16 +01:00
static defaultProps = {
editing: false,
inRoom: false,
excludedRightPanelPhaseButtons: [],
showButtons: true,
enableRoomOptionsMenu: true,
2020-08-29 12:14:16 +01:00
};
static contextType = RoomContext;
public context!: React.ContextType<typeof RoomContext>;
2021-11-15 11:39:25 +00:00
constructor(props, context) {
super(props, context);
const notiStore = RoomNotificationStateStore.instance.getRoomState(props.room);
notiStore.on(NotificationStateEvents.Update, this.onNotificationUpdate);
2021-11-15 11:39:25 +00:00
this.state = {};
}
2021-07-01 19:54:05 +01:00
public componentDidMount() {
const cli = MatrixClientPeg.get();
cli.on(RoomStateEvent.Events, this.onRoomStateEvents);
2020-08-29 12:14:16 +01:00
}
2016-01-11 12:46:12 +00:00
2021-07-01 19:54:05 +01:00
public componentWillUnmount() {
const cli = MatrixClientPeg.get();
2016-03-29 16:31:13 +01:00
if (cli) {
cli.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
2016-03-29 16:31:13 +01:00
}
const notiStore = RoomNotificationStateStore.instance.getRoomState(this.props.room);
notiStore.removeListener(NotificationStateEvents.Update, this.onNotificationUpdate);
2020-08-29 12:14:16 +01:00
}
2016-03-29 16:31:13 +01:00
private onRoomStateEvents = (event: MatrixEvent) => {
if (!this.props.room || event.getRoomId() !== this.props.room.roomId) {
2016-03-29 16:31:13 +01:00
return;
}
// redisplay the room name, topic, etc.
2021-07-01 19:54:05 +01:00
this.rateLimitedUpdate();
2020-08-29 12:14:16 +01:00
};
2016-03-29 16:31:13 +01:00
private onNotificationUpdate = () => {
this.forceUpdate();
};
2021-07-01 19:54:05 +01:00
private rateLimitedUpdate = throttle(() => {
2016-10-26 13:09:53 +01:00
this.forceUpdate();
2021-07-01 18:35:38 +01:00
}, 500, { leading: true, trailing: true });
2016-10-26 13:09:53 +01:00
2021-11-15 11:39:25 +00:00
private onContextMenuOpenClick = (ev: React.MouseEvent) => {
ev.preventDefault();
ev.stopPropagation();
const target = ev.target as HTMLButtonElement;
this.setState({ contextMenuPosition: target.getBoundingClientRect() });
};
private onContextMenuCloseClick = () => {
this.setState({ contextMenuPosition: null });
};
private renderButtons(): JSX.Element[] {
2021-10-14 15:27:35 +02:00
const buttons: JSX.Element[] = [];
2016-03-29 12:51:46 +01:00
if (this.props.inRoom &&
this.props.onCallPlaced &&
!this.context.tombstone &&
SettingsStore.getValue("showCallButtonsInComposer")
) {
2021-10-14 15:27:35 +02:00
const voiceCallButton = <AccessibleTooltipButton
className="mx_RoomHeader_button mx_RoomHeader_voiceCallButton"
2021-11-30 19:09:13 +01:00
onClick={() => this.props.onCallPlaced(CallType.Voice)}
2021-10-14 15:27:35 +02:00
title={_t("Voice call")}
key="voice"
2021-10-14 15:27:35 +02:00
/>;
const videoCallButton = <AccessibleTooltipButton
className="mx_RoomHeader_button mx_RoomHeader_videoCallButton"
onClick={() => this.props.onCallPlaced(CallType.Video)}
2021-10-14 15:27:35 +02:00
title={_t("Video call")}
key="video"
2021-10-14 15:27:35 +02:00
/>;
buttons.push(voiceCallButton, videoCallButton);
}
if (this.props.onForgetClick) {
const forgetButton = <AccessibleTooltipButton
className="mx_RoomHeader_button mx_RoomHeader_forgetButton"
onClick={this.props.onForgetClick}
title={_t("Forget room")}
key="forget"
2021-10-14 15:27:35 +02:00
/>;
buttons.push(forgetButton);
}
if (this.props.onAppsClick) {
const appsButton = <AccessibleTooltipButton
className={classNames("mx_RoomHeader_button mx_RoomHeader_appsButton", {
mx_RoomHeader_appsButton_highlight: this.props.appsShown,
})}
onClick={this.props.onAppsClick}
title={this.props.appsShown ? _t("Hide Widgets") : _t("Show Widgets")}
key="apps"
2021-10-14 15:27:35 +02:00
/>;
buttons.push(appsButton);
}
if (this.props.onSearchClick && this.props.inRoom) {
const searchButton = <AccessibleTooltipButton
className="mx_RoomHeader_button mx_RoomHeader_searchButton"
onClick={this.props.onSearchClick}
title={_t("Search")}
key="search"
2021-10-14 15:27:35 +02:00
/>;
buttons.push(searchButton);
2021-02-26 13:46:39 -07:00
}
2022-04-20 11:03:33 -04:00
if (this.props.onInviteClick && this.props.inRoom) {
const inviteButton = <AccessibleTooltipButton
className="mx_RoomHeader_button mx_RoomHeader_inviteButton"
onClick={this.props.onInviteClick}
title={_t("Invite")}
key="invite"
/>;
buttons.push(inviteButton);
}
return buttons;
}
private renderName(oobName) {
let contextMenu: JSX.Element;
if (this.state.contextMenuPosition && this.props.room) {
contextMenu = (
<RoomContextMenu
{...contextMenuBelow(this.state.contextMenuPosition)}
room={this.props.room}
onFinished={this.onContextMenuCloseClick}
/>
);
}
// XXX: this is a bit inefficient - we could just compare room.name for 'Empty room'...
let settingsHint = false;
const members = this.props.room ? this.props.room.getJoinedMembers() : undefined;
if (members) {
if (members.length === 1 && members[0].userId === MatrixClientPeg.get().credentials.userId) {
const nameEvent = this.props.room.currentState.getStateEvents('m.room.name', '');
if (!nameEvent || !nameEvent.getContent().name) {
settingsHint = true;
}
}
}
const textClasses = classNames('mx_RoomHeader_nametext', { mx_RoomHeader_settingsHint: settingsHint });
const roomName = <RoomName room={this.props.room}>
{ (name) => {
const roomName = name || oobName;
return <div dir="auto" className={textClasses} title={roomName}>{ roomName }</div>;
} }
</RoomName>;
if (this.props.enableRoomOptionsMenu) {
return (
<ContextMenuTooltipButton
className="mx_RoomHeader_name"
onClick={this.onContextMenuOpenClick}
isExpanded={!!this.state.contextMenuPosition}
title={_t("Room options")}
>
{ roomName }
{ this.props.room && <div className="mx_RoomHeader_chevron" /> }
{ contextMenu }
</ContextMenuTooltipButton>
);
}
return <div className="mx_RoomHeader_name mx_RoomHeader_name--textonly">
{ roomName }
</div>;
}
public render() {
let searchStatus = null;
// don't display the search count until the search completes and
// gives us a valid (possibly zero) searchCount.
if (this.props.searchInfo &&
this.props.searchInfo.searchCount !== undefined &&
this.props.searchInfo.searchCount !== null) {
searchStatus = <div className="mx_RoomHeader_searchStatus">&nbsp;
{ _t("(~%(count)s results)", { count: this.props.searchInfo.searchCount }) }
2019-02-04 13:25:26 -07:00
</div>;
}
let oobName = _t("Join Room");
if (this.props.oobData && this.props.oobData.name) {
oobName = this.props.oobData.name;
}
const name = this.renderName(oobName);
const topicElement = <RoomTopic
room={this.props.room}
className="mx_RoomHeader_topic"
/>;
let roomAvatar;
if (this.props.room) {
roomAvatar = <DecoratedRoomAvatar
room={this.props.room}
avatarSize={24}
oobData={this.props.oobData}
viewAvatarOnClick={true}
/>;
}
let buttons;
if (this.props.showButtons) {
buttons = <React.Fragment>
<div className="mx_RoomHeader_buttons">
{ this.renderButtons() }
</div>
<RoomHeaderButtons room={this.props.room} excludedRightPanelPhaseButtons={this.props.excludedRightPanelPhaseButtons} />
</React.Fragment>;
}
2016-03-29 12:51:46 +01:00
const e2eIcon = this.props.e2eStatus ? <E2EIcon status={this.props.e2eStatus} /> : undefined;
const isVideoRoom = SettingsStore.getValue("feature_video_rooms") && this.props.room.isElementVideoRoom();
const viewLabs = () => defaultDispatcher.dispatch({
action: Action.ViewUserSettings,
initialTabId: UserTab.Labs,
});
const betaPill = isVideoRoom ? (
<BetaPill onClick={viewLabs} tooltipTitle={_t("Video rooms are a beta feature")} />
) : null;
2015-11-27 10:42:03 +00:00
return (
2019-02-04 13:25:26 -07:00
<div className="mx_RoomHeader light-panel">
2020-02-13 19:18:21 +01:00
<div className="mx_RoomHeader_wrapper" aria-owns="mx_RightPanel">
<div className="mx_RoomHeader_avatar">{ roomAvatar }</div>
<div className="mx_RoomHeader_e2eIcon">{ e2eIcon }</div>
2018-10-23 16:57:56 +02:00
{ name }
2021-11-15 11:39:25 +00:00
{ searchStatus }
2018-10-23 16:57:56 +02:00
{ topicElement }
{ betaPill }
{ buttons }
</div>
<RoomLiveShareWarning roomId={this.props.room.roomId} />
2015-11-27 10:42:03 +00:00
</div>
);
2020-08-29 12:14:16 +01:00
}
}