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

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

180 lines
7.4 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2019-02-24 03:43:44 +00:00
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
2024-09-09 14:57:16 +01:00
Copyright 2015, 2016 , 2019, 2023 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.
*/
2021-03-04 23:17:29 +02:00
import React from "react";
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
2021-03-04 23:17:29 +02:00
import SyntaxHighlight from "../views/elements/SyntaxHighlight";
import { _t } from "../../languageHandler";
import MatrixClientContext from "../../contexts/MatrixClientContext";
import { canEditContent } from "../../utils/EventUtils";
import { MatrixClientPeg } from "../../MatrixClientPeg";
2021-09-12 09:33:33 +02:00
import BaseDialog from "../views/dialogs/BaseDialog";
import { DevtoolsContext } from "../views/dialogs/devtools/BaseTool";
import { StateEventEditor } from "../views/dialogs/devtools/RoomState";
import { stringify, TimelineEventEditor } from "../views/dialogs/devtools/Event";
import CopyableText from "../views/elements/CopyableText";
2021-09-12 09:33:33 +02:00
interface IProps {
2021-09-12 09:33:33 +02:00
mxEvent: MatrixEvent; // the MatrixEvent associated with the context menu
ignoreEdits?: boolean;
onFinished(): void;
2021-09-12 09:33:33 +02:00
}
interface IState {
isEditing: boolean;
}
2021-09-12 09:33:33 +02:00
export default class ViewSource extends React.Component<IProps, IState> {
public constructor(props: IProps) {
2021-03-04 23:17:29 +02:00
super(props);
this.state = {
2021-03-06 11:30:31 +02:00
isEditing: false,
2021-03-04 23:17:29 +02:00
};
}
private onBack = (): void => {
// TODO: refresh the "Event ID:" modal header
2021-03-06 11:30:31 +02:00
this.setState({ isEditing: false });
};
2021-03-04 23:17:29 +02:00
2021-09-12 09:33:33 +02:00
private onEdit(): void {
2021-03-06 11:30:31 +02:00
this.setState({ isEditing: true });
2021-03-04 23:17:29 +02:00
}
2021-03-06 11:30:31 +02:00
// returns the dialog body for viewing the event source
2021-09-12 09:33:33 +02:00
private viewSourceContent(): JSX.Element {
let mxEvent = this.props.mxEvent.replacingEvent() || this.props.mxEvent; // show the replacing event, not the original, if it is an edit
if (this.props.ignoreEdits) {
mxEvent = this.props.mxEvent;
}
2021-03-08 22:15:34 +02:00
const isEncrypted = mxEvent.isEncrypted();
2021-09-12 09:33:33 +02:00
// @ts-ignore
const decryptedEventSource = mxEvent.clearEvent; // FIXME: clearEvent is private
2021-03-06 11:30:31 +02:00
const originalEventSource = mxEvent.event;
const copyOriginalFunc = (): string => {
return stringify(originalEventSource);
};
2021-03-06 11:30:31 +02:00
if (isEncrypted) {
const copyDecryptedFunc = (): string => {
return stringify(decryptedEventSource || {});
};
2021-03-06 11:30:31 +02:00
return (
2021-03-04 23:17:29 +02:00
<>
<details open className="mx_ViewSource_details">
<summary>
<span className="mx_ViewSource_heading">
{_t("devtools|view_source_decrypted_event_source")}
</span>
2021-03-04 23:17:29 +02:00
</summary>
{decryptedEventSource ? (
<CopyableText getTextToCopy={copyDecryptedFunc}>
<SyntaxHighlight language="json">{stringify(decryptedEventSource)}</SyntaxHighlight>
</CopyableText>
) : (
<div>{_t("devtools|view_source_decrypted_event_source_unavailable")}</div>
)}
2021-03-04 23:17:29 +02:00
</details>
<details className="mx_ViewSource_details">
<summary>
<span className="mx_ViewSource_heading">{_t("devtools|original_event_source")}</span>
2021-03-04 23:17:29 +02:00
</summary>
2022-06-25 12:50:58 +00:00
<CopyableText getTextToCopy={copyOriginalFunc}>
<SyntaxHighlight language="json">{stringify(originalEventSource)}</SyntaxHighlight>
</CopyableText>
2021-03-04 23:17:29 +02:00
</details>
</>
);
2021-03-03 23:48:39 +02:00
} else {
2021-03-06 11:30:31 +02:00
return (
2021-03-04 23:17:29 +02:00
<>
<div className="mx_ViewSource_heading">{_t("devtools|original_event_source")}</div>
2022-06-25 12:50:58 +00:00
<CopyableText getTextToCopy={copyOriginalFunc}>
<SyntaxHighlight language="json">{stringify(originalEventSource)}</SyntaxHighlight>
</CopyableText>
2021-03-04 23:17:29 +02:00
</>
);
2021-03-03 23:48:39 +02:00
}
2021-03-06 11:30:31 +02:00
}
2021-03-03 22:38:30 +02:00
2021-03-06 11:30:31 +02:00
// returns the SendCustomEvent component prefilled with the correct details
2021-09-12 09:33:33 +02:00
private editSourceContent(): JSX.Element {
2021-03-06 11:30:31 +02:00
const mxEvent = this.props.mxEvent.replacingEvent() || this.props.mxEvent; // show the replacing event, not the original, if it is an edit
2021-03-04 23:17:29 +02:00
2021-03-06 11:30:31 +02:00
const isStateEvent = mxEvent.isState();
const roomId = mxEvent.getRoomId();
2021-03-06 11:30:31 +02:00
if (isStateEvent) {
return (
<MatrixClientContext.Consumer>
{(cli) => (
<DevtoolsContext.Provider value={{ room: cli.getRoom(roomId)! }}>
<StateEventEditor onBack={this.onBack} mxEvent={mxEvent} />
</DevtoolsContext.Provider>
)}
2021-03-06 11:30:31 +02:00
</MatrixClientContext.Consumer>
);
}
return (
<MatrixClientContext.Consumer>
{(cli) => (
<DevtoolsContext.Provider value={{ room: cli.getRoom(roomId)! }}>
<TimelineEventEditor onBack={this.onBack} mxEvent={mxEvent} />
</DevtoolsContext.Provider>
)}
</MatrixClientContext.Consumer>
);
2021-03-06 11:30:31 +02:00
}
2021-09-12 09:33:33 +02:00
private canSendStateEvent(mxEvent: MatrixEvent): boolean {
const cli = MatrixClientPeg.safeGet();
const room = cli.getRoom(mxEvent.getRoomId());
return !!room?.currentState.mayClientSendStateEvent(mxEvent.getType(), cli);
}
public render(): React.ReactNode {
2021-03-06 11:30:31 +02:00
const mxEvent = this.props.mxEvent.replacingEvent() || this.props.mxEvent; // show the replacing event, not the original, if it is an edit
const isEditing = this.state.isEditing;
const roomId = mxEvent.getRoomId()!;
const eventId = mxEvent.getId()!;
const canEdit = mxEvent.isState()
? this.canSendStateEvent(mxEvent)
: canEditContent(MatrixClientPeg.safeGet(), this.props.mxEvent);
return (
<BaseDialog className="mx_ViewSource" onFinished={this.props.onFinished} title={_t("action|view_source")}>
2022-06-25 12:50:58 +00:00
<div className="mx_ViewSource_header">
<CopyableText getTextToCopy={() => roomId} border={false}>
{_t("devtools|room_id", { roomId })}
2022-06-25 12:50:58 +00:00
</CopyableText>
<CopyableText getTextToCopy={() => eventId} border={false}>
{_t("devtools|event_id", { eventId })}
2022-06-25 12:50:58 +00:00
</CopyableText>
{mxEvent.threadRootId && (
<CopyableText getTextToCopy={() => mxEvent.threadRootId!} border={false}>
{_t("devtools|thread_root_id", {
threadRootId: mxEvent.threadRootId,
})}
</CopyableText>
)}
2019-02-24 03:43:44 +00:00
</div>
2022-06-25 12:50:58 +00:00
{isEditing ? this.editSourceContent() : this.viewSourceContent()}
{!isEditing && canEdit && (
2021-03-04 23:17:29 +02:00
<div className="mx_Dialog_buttons">
<button onClick={() => this.onEdit()}>{_t("action|edit")}</button>
2021-03-04 23:17:29 +02:00
</div>
)}
2019-02-24 03:43:44 +00:00
</BaseDialog>
);
2020-08-29 12:14:16 +01:00
}
}