Skip to content

Commit 60eeb8a

Browse files
committed
Support functional components for message body rendering.
1 parent 571a2e3 commit 60eeb8a

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

src/components/views/messages/MFileBody.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export function computedStyle(element: HTMLElement | null): string {
9393

9494
interface IProps extends IBodyProps {
9595
/* whether or not to show the default placeholder for the file. Defaults to true. */
96-
showGenericPlaceholder: boolean;
96+
showGenericPlaceholder?: boolean;
9797
}
9898

9999
interface IState {
@@ -105,11 +105,6 @@ export default class MFileBody extends React.Component<IProps, IState> {
105105
declare public context: React.ContextType<typeof RoomContext>;
106106

107107
public state: IState = {};
108-
109-
public static defaultProps = {
110-
showGenericPlaceholder: true,
111-
};
112-
113108
private iframe: React.RefObject<HTMLIFrameElement> = createRef();
114109
private dummyLink: React.RefObject<HTMLAnchorElement> = createRef();
115110
private userDidClick = false;
@@ -191,15 +186,16 @@ export default class MFileBody extends React.Component<IProps, IState> {
191186
const contentUrl = this.getContentUrl();
192187
const contentFileSize = this.content.info ? this.content.info.size : null;
193188
const fileType = this.content.info?.mimetype ?? "application/octet-stream";
189+
const showGenericPlaceholder = this.props.showGenericPlaceholder ?? true;
194190

195191
let showDownloadLink =
196-
!this.props.showGenericPlaceholder ||
192+
!showGenericPlaceholder ||
197193
(this.context.timelineRenderingType !== TimelineRenderingType.Room &&
198194
this.context.timelineRenderingType !== TimelineRenderingType.Search &&
199195
this.context.timelineRenderingType !== TimelineRenderingType.Pinned);
200196

201197
let placeholder: React.ReactNode = null;
202-
if (this.props.showGenericPlaceholder) {
198+
if (showGenericPlaceholder) {
203199
placeholder = (
204200
<AccessibleButton className="mx_MediaBody mx_MFileBody_info" onClick={this.onPlaceholderClick}>
205201
<span className="mx_MFileBody_info_icon" />

src/components/views/messages/MessageEvent.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ import { type GetRelationsForEvent, type IEventTileOps } from "../rooms/EventTil
4545
// onMessageAllowed is handled internally
4646
interface IProps extends Omit<IBodyProps, "onMessageAllowed" | "mediaEventHelper"> {
4747
/* overrides for the msgtype-specific components, used by ReplyTile to override file rendering */
48-
overrideBodyTypes?: Record<string, typeof React.Component>;
49-
overrideEventTypes?: Record<string, typeof React.Component>;
48+
overrideBodyTypes?: Record<string, React.ComponentType<IBodyProps>>;
49+
overrideEventTypes?: Record<string, React.ComponentType<IBodyProps>>;
5050

5151
// helper function to access relations for this event
5252
getRelationsForEvent?: GetRelationsForEvent;
@@ -58,7 +58,7 @@ export interface IOperableEventTile {
5858
getEventTileOps(): IEventTileOps | null;
5959
}
6060

61-
const baseBodyTypes = new Map<string, typeof React.Component>([
61+
const baseBodyTypes = new Map<string, React.ComponentType<IBodyProps>>([
6262
[MsgType.Text, TextualBody],
6363
[MsgType.Notice, TextualBody],
6464
[MsgType.Emote, TextualBody],
@@ -80,7 +80,7 @@ const baseEvTypes = new Map<string, React.ComponentType<IBodyProps>>([
8080
export default class MessageEvent extends React.Component<IProps> implements IMediaBody, IOperableEventTile {
8181
private body: React.RefObject<React.Component | IOperableEventTile> = createRef();
8282
private mediaHelper?: MediaEventHelper;
83-
private bodyTypes = new Map<string, typeof React.Component>(baseBodyTypes.entries());
83+
private bodyTypes = new Map<string, React.ComponentType<IBodyProps>>(baseBodyTypes.entries());
8484
private evTypes = new Map<string, React.ComponentType<IBodyProps>>(baseEvTypes.entries());
8585

8686
public static contextType = MatrixClientContext;
@@ -115,7 +115,7 @@ export default class MessageEvent extends React.Component<IProps> implements IMe
115115
}
116116

117117
private updateComponentMaps(): void {
118-
this.bodyTypes = new Map<string, typeof React.Component>(baseBodyTypes.entries());
118+
this.bodyTypes = new Map<string, React.ComponentType<IBodyProps>>(baseBodyTypes.entries());
119119
for (const [bodyType, bodyComponent] of Object.entries(this.props.overrideBodyTypes ?? {})) {
120120
this.bodyTypes.set(bodyType, bodyComponent);
121121
}

src/components/views/rooms/ReplyTile.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { type ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPaylo
2626
import { renderReplyTile } from "../../../events/EventTileFactory";
2727
import { type GetRelationsForEvent } from "../rooms/EventTile";
2828
import { MatrixClientPeg } from "../../../MatrixClientPeg";
29+
import { type IBodyProps } from "../messages/IBodyProps";
2930

3031
interface IProps {
3132
mxEvent: MatrixEvent;
@@ -139,13 +140,13 @@ export default class ReplyTile extends React.PureComponent<IProps> {
139140
);
140141
}
141142

142-
const msgtypeOverrides: Record<string, typeof React.Component> = {
143+
const msgtypeOverrides: Record<string, React.ComponentType<IBodyProps>> = {
143144
[MsgType.Image]: MImageReplyBody,
144145
// Override audio and video body with file body. We also hide the download/decrypt button using CSS
145146
[MsgType.Audio]: isVoiceMessage(mxEvent) ? MVoiceMessageBody : MFileBody,
146147
[MsgType.Video]: MFileBody,
147148
};
148-
const evOverrides: Record<string, typeof React.Component> = {
149+
const evOverrides: Record<string, React.ComponentType<IBodyProps>> = {
149150
// Use MImageReplyBody so that the sticker isn't taking up a lot of space
150151
[EventType.Sticker]: MImageReplyBody,
151152
};

src/events/EventTileFactory.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import HiddenBody from "../components/views/messages/HiddenBody";
4242
import ViewSourceEvent from "../components/views/messages/ViewSourceEvent";
4343
import { shouldDisplayAsBeaconTile } from "../utils/beacon/timeline";
4444
import { ElementCall } from "../models/Call";
45+
import { type IBodyProps } from "../components/views/messages/IBodyProps";
4546

4647
// Subset of EventTile's IProps plus some mixins
4748
export interface EventTileTypeProps
@@ -64,8 +65,8 @@ export interface EventTileTypeProps
6465
ref?: React.RefObject<any>; // `any` because it's effectively impossible to convince TS of a reasonable type
6566
timestamp?: JSX.Element;
6667
maxImageHeight?: number; // pixels
67-
overrideBodyTypes?: Record<string, typeof React.Component>;
68-
overrideEventTypes?: Record<string, typeof React.Component>;
68+
overrideBodyTypes?: Record<string, React.ComponentType<IBodyProps>>;
69+
overrideEventTypes?: Record<string, React.ComponentType<IBodyProps>>;
6970
}
7071

7172
type FactoryProps = Omit<EventTileTypeProps, "ref">;

0 commit comments

Comments
 (0)