Skip to content

Commit 8d076c8

Browse files
ZacksBotMidhunSureshRflorianduros
authored
Refactor EventContentBody to shared-components (#31914)
* Init of refactoring of eventcontentbody * update stories css by copying css from element x to shared components * Replaced old component EventContentBody with newly created mmvm component EventContentBodyViewModel * Refactor TextualBody and EditHistoryMessage to properly manage EventContentBodyViewModel * generated snapshot after vitest * Update import placement for eslint to pass CI * Fixed lint warnings * Update css for codeblock to represent js highlight * test: add EventContentBodyViewModel snapshot coverage * fix: pass content ref to EventContentBodyView for link previews * Fix: return to old code that passed tests * Added storybook snapshots * Removal of old component that is being unused * Update snapshot * Fix missing enableBigEmoji and shouldShowPillAvatar settings in EventContentBodyViewModel * update snapshot * narrow setProps to mutable fields and skip no-op snapshot recomputes * Update Snapshots * replace EventContentBodyViewModel setProps with explicit setters and update call sites * render body in view and keep parser/replacer in snapshot * Eslint Restruct * Eslint Restructure * Removed unused function, moved to shared component * Remove Unused Module (Moved To Shared Component) * Disable EventContent-body Test to check weather it fixes CI * Enable EventContentBody Tests * Remove EventTest * Update Include in Vitest * Added EventContentBody test * Update Package.json * Update Lockfile * Update dependencies * update lockfile * ptimize EventContentBodyViewModel to recompute/merge only changed snapshot fields * Update snapshots * setEventContent and setStripReply run whenever the existing update block runs * defined arrow functions for undefined runtime issues that might occur. * Update test cases * Update packages/shared-components/src/message-body/EventContentBody/EventContentBodyView.tsx Co-authored-by: R Midhun Suresh <rmidhunsuresh@gmail.com> * Update packages/shared-components/src/message-body/EventContentBody/EventContentBodyView.tsx Co-authored-by: R Midhun Suresh <rmidhunsuresh@gmail.com> * move big-emoji and pill-avatar setting watchers into EventContentBodyViewModel * Update packages/shared-components/src/message-body/EventContentBody/index.tsx Co-authored-by: Florian Duros <florian.duros@ormaz.fr> * Update packages/shared-components/src/message-body/EventContentBody/EventContentBodyView.tsx Co-authored-by: Florian Duros <florian.duros@ormaz.fr> * Update packages/shared-components/src/message-body/EventContentBody/EventContentBody.test.tsx Co-authored-by: Florian Duros <florian.duros@ormaz.fr> * Update packages/shared-components/src/message-body/EventContentBody/EventContentBody.stories.tsx Co-authored-by: Florian Duros <florian.duros@ormaz.fr> * Update packages/shared-components/src/message-body/EventContentBody/EventContentBodyView.tsx Co-authored-by: Florian Duros <florian.duros@ormaz.fr> * Update packages/shared-components/src/message-body/EventContentBody/EventContentBodyView.tsx Co-authored-by: Florian Duros <florian.duros@ormaz.fr> * Fix dubblicate variables * clarify applyReplacerOnString input/replacer params * Added memo to the view * Prettier Fix * Update apps/web/src/viewmodels/message-body/EventContentBodyViewModel.ts Co-authored-by: Florian Duros <florian.duros@ormaz.fr> * Added compund variables instead of reguler values * Added boolean default values * remove redundant setting props from TextualBody and EditHistoryMessage * Prettier FIx * replace MatrixClientPeg usage with `client: MatrixClient | null` passed from context * TextualBody now passes EventContentBodyViewModel `client` from RoomContext. * Remove redundant as prop from EventContentBody VM usage * Normalize EventContentBodyViewModel renderer flags to booleans --------- Co-authored-by: R Midhun Suresh <rmidhunsuresh@gmail.com> Co-authored-by: Florian Duros <florian.duros@ormaz.fr>
1 parent 3e77974 commit 8d076c8

24 files changed

Lines changed: 1281 additions & 252 deletions

File tree

apps/web/src/components/views/messages/EditHistoryMessage.tsx

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ Please see LICENSE files in the repository root for full details.
99
import React, { type JSX, createRef } from "react";
1010
import { type EventStatus, type IContent, type MatrixEvent, MatrixEventEvent, MsgType } from "matrix-js-sdk/src/matrix";
1111
import classNames from "classnames";
12+
import { EventContentBodyView } from "@element-hq/web-shared-components";
1213

13-
import EventContentBody from "./EventContentBody.tsx";
14+
import { EventContentBodyViewModel } from "../../../viewmodels/message-body/EventContentBodyViewModel";
1415
import { editBodyDiffToHtml } from "../../../utils/MessageDiffUtils";
1516
import { formatTime } from "../../../DateUtils";
1617
import { _t } from "../../../languageHandler";
@@ -45,17 +46,39 @@ export default class EditHistoryMessage extends React.PureComponent<IProps, ISta
4546
declare public context: React.ContextType<typeof MatrixClientContext>;
4647

4748
private content = createRef<HTMLDivElement>();
49+
private EventContentBodyViewModel: EventContentBodyViewModel;
4850

4951
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
5052
super(props, context);
5153

5254
const cli = this.context;
5355
const userId = cli.getSafeUserId();
54-
const event = this.props.mxEvent;
56+
const event = props.mxEvent;
5557
const room = cli.getRoom(event.getRoomId());
5658
event.localRedactionEvent()?.on(MatrixEventEvent.Status, this.onAssociatedStatusChanged);
5759
const canRedact = room?.currentState.maySendRedactionForEvent(event, userId) ?? false;
5860
this.state = { canRedact, sendStatus: event.getAssociatedStatus() };
61+
62+
const mxEventContent = getReplacedContent(event);
63+
this.EventContentBodyViewModel = new EventContentBodyViewModel({
64+
mxEvent: event,
65+
content: mxEventContent,
66+
highlights: [],
67+
stripReply: true,
68+
renderTooltipsForAmbiguousLinks: true,
69+
renderMentionPills: true,
70+
renderCodeBlocks: true,
71+
renderSpoilers: true,
72+
linkify: true,
73+
client: cli,
74+
});
75+
}
76+
77+
public componentDidUpdate(prevProps: IProps): void {
78+
if (prevProps.mxEvent !== this.props.mxEvent) {
79+
const mxEventContent = getReplacedContent(this.props.mxEvent);
80+
this.EventContentBodyViewModel.setEventContent(this.props.mxEvent, mxEventContent);
81+
}
5982
}
6083

6184
private onAssociatedStatusChanged = (): void => {
@@ -92,6 +115,7 @@ export default class EditHistoryMessage extends React.PureComponent<IProps, ISta
92115
public componentWillUnmount(): void {
93116
const event = this.props.mxEvent;
94117
event.localRedactionEvent()?.off(MatrixEventEvent.Status, this.onAssociatedStatusChanged);
118+
this.EventContentBodyViewModel.dispose();
95119
}
96120

97121
private renderActionBar(): React.ReactNode {
@@ -133,20 +157,7 @@ export default class EditHistoryMessage extends React.PureComponent<IProps, ISta
133157
if (this.props.previousEdit) {
134158
contentElements = editBodyDiffToHtml(getReplacedContent(this.props.previousEdit), content);
135159
} else {
136-
contentElements = (
137-
<EventContentBody
138-
as="span"
139-
mxEvent={mxEvent}
140-
content={content}
141-
highlights={[]}
142-
stripReply
143-
renderTooltipsForAmbiguousLinks
144-
renderMentionPills
145-
renderCodeBlocks
146-
renderSpoilers
147-
linkify
148-
/>
149-
);
160+
contentElements = <EventContentBodyView vm={this.EventContentBodyViewModel} as="span" />;
150161
}
151162
if (mxEvent.getContent().msgtype === MsgType.Emote) {
152163
const name = mxEvent.sender ? mxEvent.sender.name : mxEvent.getSender();

apps/web/src/components/views/messages/EventContentBody.tsx

Lines changed: 0 additions & 189 deletions
This file was deleted.

0 commit comments

Comments
 (0)