Skip to content

Commit 9350c50

Browse files
committed
Step 6: Refactor event rendering to stop using getComponent
We move all of the event tile rendering into a factory manager for a couple reasons: 1. `EventTile` is uncomfortably large for a file 2. A simple map isn't possible anymore (can't convert the existing maps like `eventTileTypes` to `Record<string, typeof React.Component>` because the types are actually incompatible) So, by having a factory manager place we can more easily render components without having to use `getComponent()` all over the place, and without lying to ourselves about how simple the event rendering path is. This change also moves quite a bit of the rendering path into the new `EventTileFactory` file so it can be easily seen by future developers.
1 parent 115ae19 commit 9350c50

14 files changed

+523
-283
lines changed

src/Unread.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { EventType } from "matrix-js-sdk/src/@types/event";
2020

2121
import { MatrixClientPeg } from "./MatrixClientPeg";
2222
import shouldHideEvent from './shouldHideEvent';
23-
import { haveTileForEvent } from "./components/views/rooms/EventTile";
23+
import { haveRendererForEvent } from "./events/EventTileFactory";
2424

2525
/**
2626
* Returns true if this event arriving in a room should affect the room's
@@ -46,7 +46,7 @@ export function eventTriggersUnreadCount(ev: MatrixEvent): boolean {
4646
}
4747

4848
if (ev.isRedacted()) return false;
49-
return haveTileForEvent(ev);
49+
return haveRendererForEvent(ev);
5050
}
5151

5252
export function doesRoomHaveUnreadMessages(room: Room): boolean {

src/components/structures/MessagePanel.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import SettingsStore from '../../settings/SettingsStore';
3131
import RoomContext, { TimelineRenderingType } from "../../contexts/RoomContext";
3232
import { Layout } from "../../settings/enums/Layout";
3333
import { _t } from "../../languageHandler";
34-
import EventTile, { UnwrappedEventTile, haveTileForEvent, IReadReceiptProps } from "../views/rooms/EventTile";
34+
import EventTile, { UnwrappedEventTile, IReadReceiptProps } from "../views/rooms/EventTile";
3535
import { hasText } from "../../TextForEvent";
3636
import IRCTimelineProfileResizer from "../views/elements/IRCTimelineProfileResizer";
3737
import DMRoomMap from "../../utils/DMRoomMap";
@@ -52,6 +52,7 @@ import EditorStateTransfer from "../../utils/EditorStateTransfer";
5252
import { Action } from '../../dispatcher/actions';
5353
import { getEventDisplayInfo } from "../../utils/EventUtils";
5454
import { IReadReceiptInfo } from "../views/rooms/ReadReceiptMarker";
55+
import { haveRendererForEvent } from "../../events/EventTileFactory";
5556

5657
const CONTINUATION_MAX_INTERVAL = 5 * 60 * 1000; // 5 minutes
5758
const continuedTypes = [EventType.Sticker, EventType.RoomMessage];
@@ -95,7 +96,7 @@ export function shouldFormContinuation(
9596
timelineRenderingType !== TimelineRenderingType.Thread) return false;
9697

9798
// if we don't have tile for previous event then it was shown by showHiddenEvents and has no SenderProfile
98-
if (!haveTileForEvent(prevEvent, showHiddenEvents)) return false;
99+
if (!haveRendererForEvent(prevEvent, showHiddenEvents)) return false;
99100

100101
return true;
101102
}
@@ -488,7 +489,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
488489
return true;
489490
}
490491

491-
if (!haveTileForEvent(mxEv, this.showHiddenEvents)) {
492+
if (!haveRendererForEvent(mxEv, this.showHiddenEvents)) {
492493
return false; // no tile = no show
493494
}
494495

src/components/structures/RoomView.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import SettingsStore from "../../settings/SettingsStore";
5555
import { Layout } from "../../settings/enums/Layout";
5656
import AccessibleButton from "../views/elements/AccessibleButton";
5757
import RightPanelStore from "../../stores/right-panel/RightPanelStore";
58-
import { haveTileForEvent } from "../views/rooms/EventTile";
5958
import RoomContext, { TimelineRenderingType } from "../../contexts/RoomContext";
6059
import MatrixClientContext, { MatrixClientProps, withMatrixClientHOC } from "../../contexts/MatrixClientContext";
6160
import { E2EStatus, shieldStatusForRoom } from '../../utils/ShieldUtils';
@@ -108,6 +107,7 @@ import { DoAfterSyncPreparedPayload } from '../../dispatcher/payloads/DoAfterSyn
108107
import FileDropTarget from './FileDropTarget';
109108
import Measured from '../views/elements/Measured';
110109
import { FocusComposerPayload } from '../../dispatcher/payloads/FocusComposerPayload';
110+
import { haveRendererForEvent } from "../../events/EventTileFactory";
111111

112112
const DEBUG = false;
113113
let debuglog = function(msg: string) {};
@@ -1451,7 +1451,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
14511451
continue;
14521452
}
14531453

1454-
if (!haveTileForEvent(mxEv, this.state.showHiddenEventsInTimeline)) {
1454+
if (!haveRendererForEvent(mxEv, this.state.showHiddenEventsInTimeline)) {
14551455
// XXX: can this ever happen? It will make the result count
14561456
// not match the displayed count.
14571457
continue;

src/components/structures/TimelinePanel.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import dis from "../../dispatcher/dispatcher";
4040
import { Action } from '../../dispatcher/actions';
4141
import Timer from '../../utils/Timer';
4242
import shouldHideEvent from '../../shouldHideEvent';
43-
import { haveTileForEvent } from "../views/rooms/EventTile";
4443
import { arrayFastClone } from "../../utils/arrays";
4544
import MessagePanel from "./MessagePanel";
4645
import { IScrollState } from "./ScrollPanel";
@@ -54,6 +53,7 @@ import CallEventGrouper, { buildCallEventGroupers } from "./CallEventGrouper";
5453
import { ViewRoomPayload } from "../../dispatcher/payloads/ViewRoomPayload";
5554
import { getKeyBindingsManager } from "../../KeyBindingsManager";
5655
import { KeyBindingAction } from "../../accessibility/KeyboardShortcuts";
56+
import { haveRendererForEvent } from "../../events/EventTileFactory";
5757

5858
const PAGINATE_SIZE = 20;
5959
const INITIAL_SIZE = 20;
@@ -1473,7 +1473,7 @@ class TimelinePanel extends React.Component<IProps, IState> {
14731473

14741474
const shouldIgnore = !!ev.status || // local echo
14751475
(ignoreOwn && ev.getSender() === myUserId); // own message
1476-
const isWithoutTile = !haveTileForEvent(ev, this.context?.showHiddenEventsInTimeline) ||
1476+
const isWithoutTile = !haveRendererForEvent(ev, this.context?.showHiddenEventsInTimeline) ||
14771477
shouldHideEvent(ev, this.context);
14781478

14791479
if (isWithoutTile || !node) {

src/components/views/messages/MessageEvent.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ import MLocationBody from "./MLocationBody";
4141
import MjolnirBody from "./MjolnirBody";
4242

4343
// onMessageAllowed is handled internally
44-
interface IProps extends Omit<IBodyProps, "onMessageAllowed"> {
44+
interface IProps extends Omit<IBodyProps, "onMessageAllowed" | "mediaEventHelper"> {
4545
/* overrides for the msgtype-specific components, used by ReplyTile to override file rendering */
46-
overrideBodyTypes?: Record<string, React.Component>;
47-
overrideEventTypes?: Record<string, React.Component>;
46+
overrideBodyTypes?: Record<string, typeof React.Component>;
47+
overrideEventTypes?: Record<string, typeof React.Component>;
4848

4949
// helper function to access relations for this event
5050
getRelationsForEvent?: (eventId: string, relationType: string, eventType: string) => Relations;

0 commit comments

Comments
 (0)