Skip to content

Commit 09261c5

Browse files
committed
Update custom component logic to always return a hint.
1 parent 0fe275f commit 09261c5

File tree

4 files changed

+52
-45
lines changed

4 files changed

+52
-45
lines changed

src/components/views/elements/ImageView.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -607,20 +607,18 @@ function DownloadButton({
607607
return;
608608
}
609609
const hints = ModuleApi.customComponents.getHintsForMessage(mxEvent);
610-
if (hints?.allowDownloadingMedia) {
611-
// Disable downloading as soon as we know there is a hint.
612-
setCanDownload(false);
613-
hints
614-
.allowDownloadingMedia()
615-
.then((downloadable) => {
616-
setCanDownload(downloadable);
617-
})
618-
.catch((ex) => {
619-
logger.error(`Failed to check if media from ${mxEvent.getId()} could be downloaded`, ex);
620-
// Err on the side of safety.
621-
setCanDownload(false);
622-
});
623-
}
610+
// Disable downloading as soon as we know there is a hint.
611+
setCanDownload(false);
612+
hints
613+
.allowDownloadingMedia()
614+
.then((downloadable) => {
615+
setCanDownload(downloadable);
616+
})
617+
.catch((ex) => {
618+
logger.error(`Failed to check if media from ${mxEvent.getId()} could be downloaded`, ex);
619+
// Err on the side of safety.
620+
setCanDownload(false);
621+
});
624622
}, [mxEvent]);
625623

626624
function showError(e: unknown): void {

src/components/views/messages/DownloadActionButton.tsx

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,20 @@ export default class DownloadActionButton extends React.PureComponent<IProps, IS
4444
super(props);
4545

4646
const moduleHints = ModuleApi.customComponents.getHintsForMessage(props.mxEvent);
47-
const downloadState: Pick<IState, "canDownload"> = { canDownload: true };
48-
if (moduleHints?.allowDownloadingMedia) {
49-
downloadState.canDownload = null;
50-
moduleHints
51-
.allowDownloadingMedia()
52-
.then((canDownload) => {
53-
this.setState({
54-
canDownload: canDownload,
55-
});
56-
})
57-
.catch((ex) => {
58-
logger.error(`Failed to check if media from ${props.mxEvent.getId()} could be downloaded`, ex);
59-
this.setState({
60-
canDownload: false,
61-
});
47+
const downloadState: Pick<IState, "canDownload"> = { canDownload: null };
48+
moduleHints
49+
.allowDownloadingMedia()
50+
.then((canDownload) => {
51+
this.setState({
52+
canDownload,
6253
});
63-
}
54+
})
55+
.catch((ex) => {
56+
logger.error(`Failed to check if media from ${props.mxEvent.getId()} could be downloaded`, ex);
57+
this.setState({
58+
canDownload: false,
59+
});
60+
});
6461

6562
this.state = {
6663
loading: false,

src/events/EventTileFactory.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,7 @@ export function haveRendererForEvent(
427427
return false;
428428
}
429429

430-
// Check to see if we have any hints for this message, which indicates
431-
// there is a custom renderer for the event.
432-
if (ModuleApi.customComponents.getHintsForMessage(mxEvent)) {
430+
if (ModuleApi.customComponents.hasRendererForEvent(mxEvent)) {
433431
return true;
434432
}
435433

src/modules/customComponentApi.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ interface CustomMessageRenderHints extends Omit<ModuleCustomCustomMessageRenderH
3535
allowDownloadingMedia?: () => Promise<boolean>;
3636
}
3737

38+
const DEFAULT_HINTS: Required<CustomMessageRenderHints> = {
39+
allowEditingEvent: true,
40+
allowDownloadingMedia: () => Promise.resolve(true),
41+
};
42+
3843
export class CustomComponentsApi implements ICustomComponentsApi {
3944
/**
4045
* Convert a matrix-js-sdk event into a ModuleMatrixEvent.
@@ -115,23 +120,32 @@ export class CustomComponentsApi implements ICustomComponentsApi {
115120
return originalComponent?.() ?? null;
116121
}
117122

123+
/**
124+
* Has a custom component been registered for this event.
125+
* @param mxEvent
126+
* @returns `true` if a component has been registered and would be rendered, otherwise false.
127+
*/
128+
public hasRendererForEvent(mxEvent: MatrixEvent): boolean {
129+
const moduleEv = CustomComponentsApi.getModuleMatrixEvent(mxEvent);
130+
return !!(moduleEv && this.selectRenderer(moduleEv));
131+
}
132+
118133
/**
119134
* Get hints about an message before rendering it.
120135
* @param mxEvent The message event being rendered.
121-
* @returns A component if a custom renderer exists, or originalComponent returns a value. Otherwise null.
136+
* @returns A set of hints to use when rendering messages, provided by custom renderers. If a hint
137+
* is not provided by a renderer, or no renderers are present then `DEFAULT_HINTS` are used.
122138
*/
123-
public getHintsForMessage(mxEvent: MatrixEvent): CustomMessageRenderHints | null {
139+
public getHintsForMessage(mxEvent: MatrixEvent): Required<CustomMessageRenderHints> {
124140
const moduleEv = CustomComponentsApi.getModuleMatrixEvent(mxEvent);
125141
const renderer = moduleEv && this.selectRenderer(moduleEv);
126-
if (renderer) {
127-
return {
128-
...renderer.hints,
129-
// Convert from js-sdk style events to module events automatically.
130-
allowDownloadingMedia: renderer.hints.allowDownloadingMedia
131-
? () => renderer.hints.allowDownloadingMedia!(moduleEv)
132-
: undefined,
133-
};
134-
}
135-
return null;
142+
return {
143+
...DEFAULT_HINTS,
144+
...renderer?.hints,
145+
// Convert from js-sdk style events to module events automatically.
146+
allowDownloadingMedia: renderer?.hints.allowDownloadingMedia
147+
? () => renderer.hints.allowDownloadingMedia!(moduleEv!)
148+
: DEFAULT_HINTS.allowDownloadingMedia,
149+
};
136150
}
137151
}

0 commit comments

Comments
 (0)