Files
ThreadNet-Web/src/components/structures/RightPanel.tsx
T

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

307 lines
12 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2015-2022 The Matrix.org Foundation C.I.C.
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
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.
*/
import React from "react";
2025-02-05 13:25:06 +00:00
import { type Room, type RoomState, RoomStateEvent, RoomMember, type MatrixEvent } from "matrix-js-sdk/src/matrix";
2021-12-09 09:10:23 +00:00
import { throttle } from "lodash";
2020-09-03 16:08:10 +01:00
2020-05-13 20:41:41 -06:00
import dis from "../../dispatcher/dispatcher";
2022-01-05 16:14:44 +01:00
import { RightPanelPhases } from "../../stores/right-panel/RightPanelStorePhases";
import RightPanelStore from "../../stores/right-panel/RightPanelStore";
2019-12-17 17:26:12 +00:00
import MatrixClientContext from "../../contexts/MatrixClientContext";
2025-05-15 16:17:21 +02:00
import RoomSummaryCardView from "../views/right_panel/RoomSummaryCardView";
2020-09-08 08:48:03 +01:00
import WidgetCard from "../views/right_panel/WidgetCard";
2021-05-25 12:13:16 +01:00
import UserInfo from "../views/right_panel/UserInfo";
import ThirdPartyMemberInfo from "../views/rooms/ThirdPartyMemberInfo";
import FilePanel from "./FilePanel";
2021-08-10 14:30:12 +02:00
import ThreadView from "./ThreadView";
2021-08-17 10:38:09 +01:00
import ThreadPanel from "./ThreadPanel";
2021-05-25 12:13:16 +01:00
import NotificationPanel from "./NotificationPanel";
2025-02-05 13:25:06 +00:00
import type ResizeNotifier from "../../utils/ResizeNotifier";
import { PinnedMessagesCard } from "../views/right_panel/PinnedMessagesCard";
2025-02-05 13:25:06 +00:00
import { type RoomPermalinkCreator } from "../../utils/permalinks/Permalinks";
import { type E2EStatus } from "../../utils/ShieldUtils";
2021-11-29 17:06:15 +01:00
import TimelineCard from "../views/right_panel/TimelineCard";
2022-01-05 16:14:44 +01:00
import { UPDATE_EVENT } from "../../stores/AsyncStore";
2025-02-05 13:25:06 +00:00
import { type IRightPanelCard, type IRightPanelCardState } from "../../stores/right-panel/RightPanelStoreIPanelState";
2022-02-22 11:04:27 +01:00
import { Action } from "../../dispatcher/actions";
2025-02-05 13:25:06 +00:00
import { type XOR } from "../../@types/common";
import ExtensionsCard from "../views/right_panel/ExtensionsCard";
import MemberListView from "../views/rooms/MemberList/MemberListView";
import { _t } from "../../languageHandler";
2021-05-25 12:13:16 +01:00
interface BaseProps {
2022-01-05 16:14:44 +01:00
overwriteCard?: IRightPanelCard; // used to display a custom card and ignoring the RightPanelStore (used for UserView)
2021-05-25 12:13:16 +01:00
resizeNotifier: ResizeNotifier;
2021-09-07 16:02:26 +01:00
e2eStatus?: E2EStatus;
2021-05-25 12:13:16 +01:00
}
interface RoomlessProps extends BaseProps {
room?: undefined;
permalinkCreator?: undefined;
}
interface RoomProps extends BaseProps {
room: Room;
permalinkCreator: RoomPermalinkCreator;
onSearchChange?: (term: string) => void;
2024-07-08 10:57:41 +01:00
onSearchCancel?: () => void;
searchTerm?: string;
}
type Props = XOR<RoomlessProps, RoomProps>;
2021-05-25 12:13:16 +01:00
interface IState {
2022-02-16 11:19:28 +00:00
phase?: RightPanelPhases;
2022-01-05 16:14:44 +01:00
cardState?: IRightPanelCardState;
2021-05-25 12:13:16 +01:00
}
export default class RightPanel extends React.Component<Props, IState> {
public static contextType = MatrixClientContext;
2024-12-02 09:39:36 +00:00
declare public context: React.ContextType<typeof MatrixClientContext>;
private ref = React.createRef<HTMLDivElement>();
public constructor(props: Props) {
super(props);
this.state = RightPanel.getDerivedStateFromProps(props);
}
2021-07-01 18:54:51 +01:00
private readonly delayedUpdate = throttle(
(): void => {
this.forceUpdate();
},
500,
{ leading: true, trailing: true },
);
public componentDidMount(): void {
this.context.on(RoomStateEvent.Members, this.onRoomStateMember);
2022-01-05 16:14:44 +01:00
RightPanelStore.instance.on(UPDATE_EVENT, this.onRightPanelStoreUpdate);
this.ref.current?.focus();
}
public componentWillUnmount(): void {
this.context?.removeListener(RoomStateEvent.Members, this.onRoomStateMember);
2022-01-05 16:14:44 +01:00
RightPanelStore.instance.off(UPDATE_EVENT, this.onRightPanelStoreUpdate);
}
public static getDerivedStateFromProps(props: Props): Partial<IState> {
let currentCard: IRightPanelCard | undefined;
if (props.room) {
currentCard = RightPanelStore.instance.currentCardForRoom(props.room.roomId);
}
2022-02-16 11:19:28 +00:00
return {
cardState: currentCard?.state,
phase: currentCard?.phase ?? undefined,
2022-02-16 11:19:28 +00:00
};
}
private onRoomStateMember = (ev: MatrixEvent, state: RoomState, member: RoomMember): void => {
2020-10-06 18:11:28 +01:00
if (!this.props.room || member.roomId !== this.props.room.roomId) {
2018-09-07 14:16:26 +02:00
return;
}
2022-11-07 13:45:34 +00:00
// redraw the badge on the membership list
if (this.state.phase === RightPanelPhases.MemberList) {
2021-05-25 12:13:16 +01:00
this.delayedUpdate();
2022-01-05 16:14:44 +01:00
} else if (
this.state.phase === RightPanelPhases.MemberInfo &&
member.userId === this.state.cardState?.member?.userId
2022-01-05 16:14:44 +01:00
) {
// refresh the member info (e.g. new power level)
2021-05-25 12:13:16 +01:00
this.delayedUpdate();
}
2021-05-25 12:13:16 +01:00
};
private onRightPanelStoreUpdate = (): void => {
const oldPhase = this.state.phase;
const newState = RightPanel.getDerivedStateFromProps(this.props) as IState;
this.setState({ ...newState });
if (oldPhase !== newState.phase) {
this.ref.current?.focus();
}
2022-01-05 16:14:44 +01:00
};
private onClose = (): void => {
2020-05-27 10:28:25 +01:00
// XXX: There are three different ways of 'closing' this panel depending on what state
// things are in... this knows far more than it should do about the state of the rest
// of the app and is generally a bit silly.
2022-01-05 16:14:44 +01:00
if (this.props.overwriteCard?.state?.member) {
2020-05-27 10:28:25 +01:00
// If we have a user prop then we're displaying a user from the 'user' page type
// in LoggedInView, so need to change the page type to close the panel (we switch
// to the home page which is not obviously the correct thing to do, but I'm not sure
// anything else is - we could hide the close button altogether?)
dis.dispatch({
2022-02-22 11:04:27 +01:00
action: Action.ViewHomePage,
2020-05-27 10:28:25 +01:00
});
} else if (
this.state.phase === RightPanelPhases.EncryptionPanel &&
this.state.cardState?.verificationRequest?.pending
) {
// When the user clicks close on the encryption panel cancel the pending request first if any
2022-01-05 16:14:44 +01:00
this.state.cardState.verificationRequest.cancel();
2020-05-27 10:28:25 +01:00
} else {
RightPanelStore.instance.togglePanel(this.props.room?.roomId ?? null);
2020-05-27 10:28:25 +01:00
}
};
public render(): React.ReactNode {
2022-01-05 16:14:44 +01:00
let card = <div />;
const roomId = this.props.room?.roomId;
2022-01-05 16:14:44 +01:00
const phase = this.props.overwriteCard?.phase ?? this.state.phase;
const cardState = this.props.overwriteCard?.state ?? this.state.cardState;
switch (phase) {
case RightPanelPhases.MemberList:
if (!!roomId) {
card = <MemberListView roomId={roomId} onClose={this.onClose} />;
2020-01-28 23:43:09 +00:00
}
break;
2020-09-08 08:48:03 +01:00
case RightPanelPhases.MemberInfo:
2022-01-05 16:14:44 +01:00
case RightPanelPhases.EncryptionPanel: {
if (!!cardState?.member) {
const roomMember = cardState.member instanceof RoomMember ? cardState.member : undefined;
card = (
<UserInfo
user={cardState.member}
room={this.context.getRoom(roomMember?.roomId) ?? this.props.room}
key={roomId ?? cardState.member.userId}
onClose={this.onClose}
phase={phase}
verificationRequest={cardState.verificationRequest}
verificationRequestPromise={cardState.verificationRequestPromise}
/>
);
}
2020-01-28 23:43:09 +00:00
break;
2022-01-05 16:14:44 +01:00
}
case RightPanelPhases.ThreePidMemberInfo:
if (!!cardState?.memberInfoEvent) {
2023-11-29 21:25:34 +13:00
card = (
<ThirdPartyMemberInfo event={cardState.memberInfoEvent} key={roomId} onClose={this.onClose} />
);
}
2020-01-28 23:43:09 +00:00
break;
2020-09-08 08:48:03 +01:00
case RightPanelPhases.NotificationPanel:
2022-01-05 16:14:44 +01:00
card = <NotificationPanel onClose={this.onClose} />;
break;
case RightPanelPhases.PinnedMessages:
2024-09-16 16:51:59 +02:00
if (!!this.props.room) {
card = (
<PinnedMessagesCard
room={this.props.room}
onClose={this.onClose}
permalinkCreator={this.props.permalinkCreator}
/>
);
2021-06-03 08:35:12 +01:00
}
2020-01-28 23:43:09 +00:00
break;
2021-11-29 17:06:15 +01:00
case RightPanelPhases.Timeline:
if (!!this.props.room) {
card = (
<TimelineCard
classNames="mx_ThreadPanel mx_TimelineCard"
room={this.props.room}
timelineSet={this.props.room.getUnfilteredTimelineSet()}
resizeNotifier={this.props.resizeNotifier}
onClose={this.onClose}
permalinkCreator={this.props.permalinkCreator}
e2eStatus={this.props.e2eStatus}
/>
);
}
2021-11-29 17:06:15 +01:00
break;
case RightPanelPhases.FilePanel:
if (!!roomId) {
card = (
<FilePanel roomId={roomId} resizeNotifier={this.props.resizeNotifier} onClose={this.onClose} />
);
}
2020-09-08 08:48:03 +01:00
break;
2021-08-10 14:30:12 +02:00
case RightPanelPhases.ThreadView:
if (!!this.props.room && !!cardState?.threadHeadEvent) {
card = (
<ThreadView
room={this.props.room}
resizeNotifier={this.props.resizeNotifier}
onClose={this.onClose}
mxEvent={cardState.threadHeadEvent}
initialEvent={cardState.initialEvent}
isInitialEventHighlighted={cardState.isInitialEventHighlighted}
initialEventScrollIntoView={cardState.initialEventScrollIntoView}
permalinkCreator={this.props.permalinkCreator}
e2eStatus={this.props.e2eStatus}
/>
);
}
2021-08-17 10:38:09 +01:00
break;
case RightPanelPhases.ThreadPanel:
if (!!this.props.room) {
card = (
<ThreadPanel
roomId={this.props.room.roomId}
resizeNotifier={this.props.resizeNotifier}
onClose={this.onClose}
permalinkCreator={this.props.permalinkCreator}
/>
);
}
2021-08-10 14:30:12 +02:00
break;
2020-09-08 08:48:03 +01:00
case RightPanelPhases.RoomSummary:
if (!!this.props.room) {
card = (
2025-05-15 16:17:21 +02:00
<RoomSummaryCardView
room={this.props.room}
// whenever RightPanel is passed a room it is passed a permalinkcreator
permalinkCreator={this.props.permalinkCreator!}
2024-07-08 10:57:41 +01:00
onSearchChange={this.props.onSearchChange}
onSearchCancel={this.props.onSearchCancel}
searchTerm={this.props.searchTerm}
2024-07-08 10:57:41 +01:00
focusRoomSearch={cardState?.focusRoomSearch}
/>
);
}
2020-09-08 08:48:03 +01:00
break;
case RightPanelPhases.Extensions:
if (!!this.props.room) {
card = <ExtensionsCard room={this.props.room} onClose={this.onClose} />;
}
break;
2020-09-08 08:48:03 +01:00
case RightPanelPhases.Widget:
if (!!this.props.room && !!cardState?.widgetId) {
card = <WidgetCard room={this.props.room} widgetId={cardState.widgetId} onClose={this.onClose} />;
}
2020-01-28 23:43:09 +00:00
break;
}
return (
<aside
aria-label={_t("right_panel|title")}
ref={this.ref}
className="mx_RightPanel"
id="mx_RightPanel"
data-testid="right-panel"
tabIndex={-1}
>
2022-01-05 16:14:44 +01:00
{card}
</aside>
);
}
}