Refactor view source event to MVVM (#33428)
* Refactor view source event to MVVM * remove unused variable since movement * Update view source event screenshots * Update packages/shared-components/src/room/timeline/event-tile/body/ViewSourceEventView/ViewSourceEventView.tsx Co-authored-by: Florian Duros <florian.duros@ormaz.fr> * Use view model disposables for source event decryption * Consolidate source event view model updates * Fix prettier * Fix view source expanded class name * Remove void from source event decryption --------- Co-authored-by: Florian Duros <florian.duros@ormaz.fr>
This commit is contained in:
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
Copyright 2024 New Vector Ltd.
|
||||
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
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import React from "react";
|
||||
import { type MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/matrix";
|
||||
import classNames from "classnames";
|
||||
import { CollapseIcon, ExpandIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
|
||||
|
||||
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
||||
import { _t } from "../../../languageHandler";
|
||||
import AccessibleButton, { type ButtonEvent } from "../elements/AccessibleButton";
|
||||
|
||||
interface IProps {
|
||||
mxEvent: MatrixEvent;
|
||||
}
|
||||
|
||||
interface IState {
|
||||
expanded: boolean;
|
||||
}
|
||||
|
||||
export default class ViewSourceEvent extends React.PureComponent<IProps, IState> {
|
||||
public constructor(props: IProps) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
expanded: false,
|
||||
};
|
||||
}
|
||||
|
||||
public componentDidMount(): void {
|
||||
const { mxEvent } = this.props;
|
||||
|
||||
const client = MatrixClientPeg.safeGet();
|
||||
client.decryptEventIfNeeded(mxEvent);
|
||||
|
||||
if (mxEvent.isBeingDecrypted()) {
|
||||
mxEvent.once(MatrixEventEvent.Decrypted, () => this.forceUpdate());
|
||||
}
|
||||
}
|
||||
|
||||
private onToggle = (ev: ButtonEvent): void => {
|
||||
ev.preventDefault();
|
||||
const { expanded } = this.state;
|
||||
this.setState({
|
||||
expanded: !expanded,
|
||||
});
|
||||
};
|
||||
|
||||
public render(): React.ReactNode {
|
||||
const { mxEvent } = this.props;
|
||||
const { expanded } = this.state;
|
||||
|
||||
let content;
|
||||
if (expanded) {
|
||||
content = <pre>{JSON.stringify(mxEvent, null, 4)}</pre>;
|
||||
} else {
|
||||
content = <code>{`{ "type": ${mxEvent.getType()} }`}</code>;
|
||||
}
|
||||
|
||||
const classes = classNames("mx_ViewSourceEvent mx_EventTile_content", {
|
||||
mx_ViewSourceEvent_expanded: expanded,
|
||||
});
|
||||
|
||||
return (
|
||||
<span className={classes}>
|
||||
{content}
|
||||
<AccessibleButton
|
||||
kind="link"
|
||||
title={_t("devtools|toggle_event")}
|
||||
className="mx_ViewSourceEvent_toggle"
|
||||
onClick={this.onToggle}
|
||||
>
|
||||
{expanded ? <CollapseIcon /> : <ExpandIcon />}
|
||||
</AccessibleButton>
|
||||
</span>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
MKeyVerificationRequestView,
|
||||
RoomAvatarEventView,
|
||||
TextualEventView,
|
||||
ViewSourceEventView,
|
||||
useCreateAutoDisposedViewModel,
|
||||
} from "@element-hq/web-shared-components";
|
||||
|
||||
@@ -44,7 +45,6 @@ import { useMatrixClientContext } from "../contexts/MatrixClientContext";
|
||||
import { WidgetType } from "../widgets/WidgetType";
|
||||
import { hasText } from "../TextForEvent";
|
||||
import { getMessageModerationState, MessageModerationState } from "../utils/EventUtils";
|
||||
import ViewSourceEvent from "../components/views/messages/ViewSourceEvent";
|
||||
import { shouldDisplayAsBeaconTile } from "../utils/beacon/timeline";
|
||||
import { type IBodyProps } from "../components/views/messages/IBodyProps";
|
||||
import { ModuleApi } from "../modules/Api";
|
||||
@@ -54,6 +54,7 @@ import { MKeyVerificationRequestViewModel } from "../viewmodels/room/timeline/ev
|
||||
import { RoomAvatarEventViewModel } from "../viewmodels/room/timeline/event-tile/RoomAvatarEventViewModel";
|
||||
import { TextualEventViewModel } from "../viewmodels/room/timeline/event-tile/TextualEventViewModel";
|
||||
import { HiddenBodyViewModel } from "../viewmodels/room/timeline/event-tile/body/HiddenBodyViewModel";
|
||||
import { ViewSourceEventViewModel } from "../viewmodels/room/timeline/event-tile/body/ViewSourceEventViewModel";
|
||||
import { ElementCallEventType } from "../call-types";
|
||||
import { CallStartedTileViewModel } from "../viewmodels/room/timeline/event-tile/call/CallStartedTileViewModel";
|
||||
|
||||
@@ -127,6 +128,24 @@ function HiddenBodyWrappedView({ mxEvent, ref }: IBodyProps): JSX.Element {
|
||||
}
|
||||
const HiddenEventFactory: Factory = (ref, props) => <HiddenBodyWrappedView ref={ref} {...props} />;
|
||||
|
||||
function ViewSourceEventWrappedView({ mxEvent, ref }: IBodyProps): JSX.Element {
|
||||
const cli = useMatrixClientContext();
|
||||
const vm = useCreateAutoDisposedViewModel(() => new ViewSourceEventViewModel({ mxEvent, cli }));
|
||||
|
||||
useEffect(() => {
|
||||
vm.setProps({ cli, mxEvent });
|
||||
}, [cli, mxEvent, vm]);
|
||||
|
||||
return (
|
||||
<ViewSourceEventView
|
||||
vm={vm}
|
||||
ref={ref}
|
||||
className="mx_ViewSourceEvent mx_EventTile_content"
|
||||
expandedClassName="mx_ViewSourceEvent_expanded"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function MJitsiWidgetEventWrappedView({ mxEvent, ref }: IBodyProps): JSX.Element {
|
||||
const cli = useMatrixClientContext();
|
||||
const vm = useCreateAutoDisposedViewModel(() => new MJitsiWidgetEventViewModel({ mxEvent, cli }));
|
||||
@@ -178,8 +197,8 @@ export const CallStartedEventFactory: Factory = (ref, props) => {
|
||||
};
|
||||
|
||||
// These factories are exported for reference comparison against pickFactory()
|
||||
export const JSONEventFactory: Factory = (ref, props) => <ViewSourceEventWrappedView ref={ref} {...props} />;
|
||||
export const JitsiEventFactory: Factory = (ref, props) => <MJitsiWidgetEventWrappedView ref={ref} {...props} />;
|
||||
export const JSONEventFactory: Factory = (ref, props) => <ViewSourceEvent ref={ref} {...props} />;
|
||||
export const RoomCreateEventFactory: Factory = (_ref, props) => <RoomPredecessorTile {...props} />;
|
||||
|
||||
const EVENT_TILE_TYPES = new Map<string, Factory>([
|
||||
|
||||
@@ -892,7 +892,6 @@
|
||||
"thread_root_id": "Thread Root ID: %(threadRootId)s",
|
||||
"threads_timeline": "Threads timeline",
|
||||
"title": "Developer tools",
|
||||
"toggle_event": "toggle event",
|
||||
"toolbox": "Toolbox",
|
||||
"use_at_own_risk": "This UI does NOT check the types of the values. Use at your own risk.",
|
||||
"user_avatar": "Avatar: %(avatar)s",
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
Copyright 2024 New Vector Ltd.
|
||||
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
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { type MouseEvent } from "react";
|
||||
import { type MatrixClient, type MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/matrix";
|
||||
import {
|
||||
BaseViewModel,
|
||||
Disposables,
|
||||
type ViewSourceEventViewModel as ViewSourceEventViewModelInterface,
|
||||
type ViewSourceEventViewSnapshot,
|
||||
} from "@element-hq/web-shared-components";
|
||||
|
||||
export interface ViewSourceEventViewModelProps {
|
||||
/**
|
||||
* The hidden event whose source is being rendered.
|
||||
*/
|
||||
mxEvent: MatrixEvent;
|
||||
/**
|
||||
* Matrix client used to request decryption before rendering event source.
|
||||
*/
|
||||
cli: MatrixClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* ViewModel for hidden event source rendering.
|
||||
*/
|
||||
export class ViewSourceEventViewModel
|
||||
extends BaseViewModel<ViewSourceEventViewSnapshot, ViewSourceEventViewModelProps>
|
||||
implements ViewSourceEventViewModelInterface
|
||||
{
|
||||
private decryptionListenerDisposables?: Disposables;
|
||||
|
||||
private static computeSnapshot(
|
||||
{ mxEvent }: ViewSourceEventViewModelProps,
|
||||
expanded: boolean,
|
||||
): ViewSourceEventViewSnapshot {
|
||||
return {
|
||||
expanded,
|
||||
preview: `{ "type": ${mxEvent.getType()} }`,
|
||||
source: expanded ? ViewSourceEventViewModel.computeSource(mxEvent) : "",
|
||||
};
|
||||
}
|
||||
|
||||
private static computeSource(mxEvent: MatrixEvent): string {
|
||||
return JSON.stringify(mxEvent, null, 4) ?? "";
|
||||
}
|
||||
|
||||
public constructor(props: ViewSourceEventViewModelProps) {
|
||||
super(props, ViewSourceEventViewModel.computeSnapshot(props, false));
|
||||
this.disposables.track(() => this.removeDecryptionListener());
|
||||
this.setupDecryptionListener();
|
||||
}
|
||||
|
||||
public setProps(newProps: Partial<ViewSourceEventViewModelProps>): void {
|
||||
const nextProps = { ...this.props, ...newProps };
|
||||
const eventChanged = this.props.mxEvent !== nextProps.mxEvent;
|
||||
const clientChanged = this.props.cli !== nextProps.cli;
|
||||
|
||||
if (!eventChanged && !clientChanged) return;
|
||||
|
||||
this.props = nextProps;
|
||||
|
||||
this.setupDecryptionListener();
|
||||
|
||||
if (eventChanged) {
|
||||
this.updateSnapshotFromProps();
|
||||
}
|
||||
}
|
||||
|
||||
public onToggle = (event: MouseEvent<HTMLButtonElement>): void => {
|
||||
event.preventDefault();
|
||||
|
||||
const expanded = !this.snapshot.current.expanded;
|
||||
this.snapshot.merge({
|
||||
expanded,
|
||||
source: expanded ? ViewSourceEventViewModel.computeSource(this.props.mxEvent) : "",
|
||||
});
|
||||
};
|
||||
|
||||
private updateSnapshotFromProps(): void {
|
||||
this.snapshot.merge(ViewSourceEventViewModel.computeSnapshot(this.props, this.snapshot.current.expanded));
|
||||
}
|
||||
|
||||
private setupDecryptionListener(): void {
|
||||
this.removeDecryptionListener();
|
||||
|
||||
const { cli, mxEvent } = this.props;
|
||||
cli.decryptEventIfNeeded(mxEvent);
|
||||
|
||||
if (!mxEvent.isBeingDecrypted()) return;
|
||||
|
||||
const onDecrypted = (): void => {
|
||||
this.removeDecryptionListener();
|
||||
if (this.props.mxEvent !== mxEvent) return;
|
||||
|
||||
this.updateSnapshotFromProps();
|
||||
};
|
||||
|
||||
this.decryptionListenerDisposables = new Disposables();
|
||||
this.decryptionListenerDisposables.trackListener(mxEvent, MatrixEventEvent.Decrypted, onDecrypted);
|
||||
}
|
||||
|
||||
private removeDecryptionListener(): void {
|
||||
this.decryptionListenerDisposables?.dispose();
|
||||
this.decryptionListenerDisposables = undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user