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

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

99 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-02-20 12:45:55 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2019-2024 New Vector Ltd.
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
2019-02-20 12:45:55 +01:00
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.
2019-02-20 12:45:55 +01:00
*/
import React from "react";
import { MatrixEvent, RoomMember, MatrixClient } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
import Modal from "../../Modal";
import { _t } from "../../languageHandler";
2021-09-12 09:09:16 +02:00
import ErrorDialog from "../views/dialogs/ErrorDialog";
import MainSplit from "./MainSplit";
import RightPanel from "./RightPanel";
import Spinner from "../views/elements/Spinner";
import ResizeNotifier from "../../utils/ResizeNotifier";
2022-01-05 16:14:44 +01:00
import { RightPanelPhases } from "../../stores/right-panel/RightPanelStorePhases";
2022-07-29 13:43:29 +02:00
import { UserOnboardingPage } from "../views/user-onboarding/UserOnboardingPage";
import MatrixClientContext from "../../contexts/MatrixClientContext";
2021-09-12 09:09:16 +02:00
interface IProps {
userId: string;
2021-09-12 09:09:16 +02:00
resizeNotifier: ResizeNotifier;
}
interface IState {
loading: boolean;
member?: RoomMember;
}
2019-02-20 12:45:55 +01:00
2021-09-12 09:09:16 +02:00
export default class UserView extends React.Component<IProps, IState> {
public static contextType = MatrixClientContext;
2024-08-01 17:14:28 +01:00
public declare context: React.ContextType<typeof MatrixClientContext>;
2024-08-01 13:01:05 +01:00
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
super(props, context);
2021-09-12 09:09:16 +02:00
this.state = {
loading: true,
2019-02-20 12:45:55 +01:00
};
}
2021-09-12 09:09:16 +02:00
public componentDidMount(): void {
2019-02-20 12:45:55 +01:00
if (this.props.userId) {
2021-09-12 09:09:16 +02:00
this.loadProfileInfo();
2019-02-20 12:45:55 +01:00
}
}
2021-09-12 09:09:16 +02:00
public componentDidUpdate(prevProps: IProps): void {
// XXX: We shouldn't need to null check the userId here, but we declare
// it as optional and MatrixChat sometimes fires in a way which results
// in an NPE when we try to update the profile info.
if (prevProps.userId !== this.props.userId && this.props.userId) {
2021-09-12 09:09:16 +02:00
this.loadProfileInfo();
2019-02-20 12:45:55 +01:00
}
}
2021-09-12 09:09:16 +02:00
private async loadProfileInfo(): Promise<void> {
2021-06-29 13:11:58 +01:00
this.setState({ loading: true });
let profileInfo: Awaited<ReturnType<MatrixClient["getProfileInfo"]>>;
2019-02-20 12:45:55 +01:00
try {
profileInfo = await this.context.getProfileInfo(this.props.userId);
2019-02-20 12:45:55 +01:00
} catch (err) {
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
title: _t("error_dialog|error_loading_user_profile"),
description: err instanceof Error ? err.message : _t("invite|failed_generic"),
});
2021-06-29 13:11:58 +01:00
this.setState({ loading: false });
2019-02-20 12:45:55 +01:00
return;
}
2021-06-29 13:11:58 +01:00
const fakeEvent = new MatrixEvent({ type: "m.room.member", content: profileInfo });
// We pass an empty string room ID here, this is slight abuse of the class to simplify code
const member = new RoomMember("", this.props.userId);
member.setMembershipEvent(fakeEvent);
2021-06-29 13:11:58 +01:00
this.setState({ member, loading: false });
2019-02-20 12:45:55 +01:00
}
public render(): React.ReactNode {
2019-02-20 12:45:55 +01:00
if (this.state.loading) {
return <Spinner />;
2021-09-24 14:35:00 +01:00
} else if (this.state.member) {
2022-01-05 16:14:44 +01:00
const panel = (
<RightPanel
overwriteCard={{ phase: RightPanelPhases.RoomMemberInfo, state: { member: this.state.member } }}
resizeNotifier={this.props.resizeNotifier}
/>
);
return (
<MainSplit panel={panel} resizeNotifier={this.props.resizeNotifier}>
2022-07-29 13:43:29 +02:00
<UserOnboardingPage />
</MainSplit>
);
2019-02-20 12:45:55 +01:00
} else {
return <div />;
}
}
}