mirror of
https://github.com/vector-im/element-web.git
synced 2026-05-05 12:16:53 +02:00
Update custom component logic to always return a hint.
This commit is contained in:
parent
0fe275fbd2
commit
09261c53b8
@ -607,20 +607,18 @@ function DownloadButton({
|
||||
return;
|
||||
}
|
||||
const hints = ModuleApi.customComponents.getHintsForMessage(mxEvent);
|
||||
if (hints?.allowDownloadingMedia) {
|
||||
// Disable downloading as soon as we know there is a hint.
|
||||
setCanDownload(false);
|
||||
hints
|
||||
.allowDownloadingMedia()
|
||||
.then((downloadable) => {
|
||||
setCanDownload(downloadable);
|
||||
})
|
||||
.catch((ex) => {
|
||||
logger.error(`Failed to check if media from ${mxEvent.getId()} could be downloaded`, ex);
|
||||
// Err on the side of safety.
|
||||
setCanDownload(false);
|
||||
});
|
||||
}
|
||||
// Disable downloading as soon as we know there is a hint.
|
||||
setCanDownload(false);
|
||||
hints
|
||||
.allowDownloadingMedia()
|
||||
.then((downloadable) => {
|
||||
setCanDownload(downloadable);
|
||||
})
|
||||
.catch((ex) => {
|
||||
logger.error(`Failed to check if media from ${mxEvent.getId()} could be downloaded`, ex);
|
||||
// Err on the side of safety.
|
||||
setCanDownload(false);
|
||||
});
|
||||
}, [mxEvent]);
|
||||
|
||||
function showError(e: unknown): void {
|
||||
|
||||
@ -44,23 +44,20 @@ export default class DownloadActionButton extends React.PureComponent<IProps, IS
|
||||
super(props);
|
||||
|
||||
const moduleHints = ModuleApi.customComponents.getHintsForMessage(props.mxEvent);
|
||||
const downloadState: Pick<IState, "canDownload"> = { canDownload: true };
|
||||
if (moduleHints?.allowDownloadingMedia) {
|
||||
downloadState.canDownload = null;
|
||||
moduleHints
|
||||
.allowDownloadingMedia()
|
||||
.then((canDownload) => {
|
||||
this.setState({
|
||||
canDownload: canDownload,
|
||||
});
|
||||
})
|
||||
.catch((ex) => {
|
||||
logger.error(`Failed to check if media from ${props.mxEvent.getId()} could be downloaded`, ex);
|
||||
this.setState({
|
||||
canDownload: false,
|
||||
});
|
||||
const downloadState: Pick<IState, "canDownload"> = { canDownload: null };
|
||||
moduleHints
|
||||
.allowDownloadingMedia()
|
||||
.then((canDownload) => {
|
||||
this.setState({
|
||||
canDownload,
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((ex) => {
|
||||
logger.error(`Failed to check if media from ${props.mxEvent.getId()} could be downloaded`, ex);
|
||||
this.setState({
|
||||
canDownload: false,
|
||||
});
|
||||
});
|
||||
|
||||
this.state = {
|
||||
loading: false,
|
||||
|
||||
@ -427,9 +427,7 @@ export function haveRendererForEvent(
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check to see if we have any hints for this message, which indicates
|
||||
// there is a custom renderer for the event.
|
||||
if (ModuleApi.customComponents.getHintsForMessage(mxEvent)) {
|
||||
if (ModuleApi.customComponents.hasRendererForEvent(mxEvent)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -35,6 +35,11 @@ interface CustomMessageRenderHints extends Omit<ModuleCustomCustomMessageRenderH
|
||||
allowDownloadingMedia?: () => Promise<boolean>;
|
||||
}
|
||||
|
||||
const DEFAULT_HINTS: Required<CustomMessageRenderHints> = {
|
||||
allowEditingEvent: true,
|
||||
allowDownloadingMedia: () => Promise.resolve(true),
|
||||
};
|
||||
|
||||
export class CustomComponentsApi implements ICustomComponentsApi {
|
||||
/**
|
||||
* Convert a matrix-js-sdk event into a ModuleMatrixEvent.
|
||||
@ -115,23 +120,32 @@ export class CustomComponentsApi implements ICustomComponentsApi {
|
||||
return originalComponent?.() ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has a custom component been registered for this event.
|
||||
* @param mxEvent
|
||||
* @returns `true` if a component has been registered and would be rendered, otherwise false.
|
||||
*/
|
||||
public hasRendererForEvent(mxEvent: MatrixEvent): boolean {
|
||||
const moduleEv = CustomComponentsApi.getModuleMatrixEvent(mxEvent);
|
||||
return !!(moduleEv && this.selectRenderer(moduleEv));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hints about an message before rendering it.
|
||||
* @param mxEvent The message event being rendered.
|
||||
* @returns A component if a custom renderer exists, or originalComponent returns a value. Otherwise null.
|
||||
* @returns A set of hints to use when rendering messages, provided by custom renderers. If a hint
|
||||
* is not provided by a renderer, or no renderers are present then `DEFAULT_HINTS` are used.
|
||||
*/
|
||||
public getHintsForMessage(mxEvent: MatrixEvent): CustomMessageRenderHints | null {
|
||||
public getHintsForMessage(mxEvent: MatrixEvent): Required<CustomMessageRenderHints> {
|
||||
const moduleEv = CustomComponentsApi.getModuleMatrixEvent(mxEvent);
|
||||
const renderer = moduleEv && this.selectRenderer(moduleEv);
|
||||
if (renderer) {
|
||||
return {
|
||||
...renderer.hints,
|
||||
// Convert from js-sdk style events to module events automatically.
|
||||
allowDownloadingMedia: renderer.hints.allowDownloadingMedia
|
||||
? () => renderer.hints.allowDownloadingMedia!(moduleEv)
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
return {
|
||||
...DEFAULT_HINTS,
|
||||
...renderer?.hints,
|
||||
// Convert from js-sdk style events to module events automatically.
|
||||
allowDownloadingMedia: renderer?.hints.allowDownloadingMedia
|
||||
? () => renderer.hints.allowDownloadingMedia!(moduleEv!)
|
||||
: DEFAULT_HINTS.allowDownloadingMedia,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user