Files
ThreadNet-Web/src/components/views/messages/ViewSourceEvent.tsx
T

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

84 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-05-02 15:11:06 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2019-05-02 15:11:06 +01:00
Copyright 2019 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.
2019-05-02 15:11:06 +01:00
*/
import React from "react";
2025-02-05 13:25:06 +00:00
import { type MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/matrix";
2019-05-02 15:11:06 +01:00
import classNames from "classnames";
2024-10-21 11:36:31 +01:00
import { CollapseIcon, ExpandIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
2021-10-22 17:23:32 -05:00
import { MatrixClientPeg } from "../../../MatrixClientPeg";
2022-01-03 18:51:58 +01:00
import { _t } from "../../../languageHandler";
2025-02-05 13:25:06 +00:00
import AccessibleButton, { type ButtonEvent } from "../elements/AccessibleButton";
2019-05-02 15:11:06 +01:00
2021-07-22 17:11:23 +02:00
interface IProps {
mxEvent: MatrixEvent;
}
2019-05-02 15:11:06 +01:00
2021-07-22 17:11:23 +02:00
interface IState {
expanded: boolean;
}
export default class ViewSourceEvent extends React.PureComponent<IProps, IState> {
2023-02-13 11:39:16 +00:00
public constructor(props: IProps) {
2019-05-02 15:11:06 +01:00
super(props);
this.state = {
expanded: false,
};
}
2021-07-22 17:11:23 +02:00
public componentDidMount(): void {
2021-06-29 13:11:58 +01:00
const { mxEvent } = this.props;
const client = MatrixClientPeg.safeGet();
2021-05-18 13:01:38 +01:00
client.decryptEventIfNeeded(mxEvent);
if (mxEvent.isBeingDecrypted()) {
mxEvent.once(MatrixEventEvent.Decrypted, () => this.forceUpdate());
}
}
private onToggle = (ev: ButtonEvent): void => {
2019-05-02 15:11:06 +01:00
ev.preventDefault();
const { expanded } = this.state;
this.setState({
expanded: !expanded,
});
2021-07-22 17:11:23 +02:00
};
2019-05-02 15:11:06 +01:00
2021-07-22 17:11:23 +02:00
public render(): React.ReactNode {
2019-05-02 15:11:06 +01:00
const { mxEvent } = this.props;
const { expanded } = this.state;
let content;
if (expanded) {
content = <pre>{JSON.stringify(mxEvent, null, 4)}</pre>;
2019-05-02 15:11:06 +01:00
} else {
content = <code>{`{ "type": ${mxEvent.getType()} }`}</code>;
2019-05-02 15:11:06 +01:00
}
const classes = classNames("mx_ViewSourceEvent mx_EventTile_content", {
mx_ViewSourceEvent_expanded: expanded,
});
return (
<span className={classes}>
{content}
2022-01-07 10:40:53 +01:00
<AccessibleButton
kind="link"
title={_t("devtools|toggle_event")}
2019-05-02 15:11:06 +01:00
className="mx_ViewSourceEvent_toggle"
onClick={this.onToggle}
2024-10-21 11:36:31 +01:00
>
{expanded ? <CollapseIcon /> : <ExpandIcon />}
</AccessibleButton>
2019-05-02 15:11:06 +01:00
</span>
);
}
2019-05-17 17:44:11 +01:00
}