diff --git a/src/components/views/elements/ImageView.tsx b/src/components/views/elements/ImageView.tsx index b43564cf37..66ed3b7bf2 100644 --- a/src/components/views/elements/ImageView.tsx +++ b/src/components/views/elements/ImageView.tsx @@ -606,19 +606,24 @@ function DownloadButton({ setCanDownload(true); return; } + // Get the hint. If the hint is static then we can immediately set it, + // otherwise set to false until the promise completes. const hints = ModuleApi.customComponents.getHintsForMessage(mxEvent); - // 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); - }); + if (typeof hints.allowDownloadingMedia === "boolean") { + setCanDownload(hints.allowDownloadingMedia); + } else { + 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 { diff --git a/src/components/views/messages/DownloadActionButton.tsx b/src/components/views/messages/DownloadActionButton.tsx index 2cc2c02944..6b5dce6fed 100644 --- a/src/components/views/messages/DownloadActionButton.tsx +++ b/src/components/views/messages/DownloadActionButton.tsx @@ -43,21 +43,23 @@ export default class DownloadActionButton extends React.PureComponent = { canDownload: null }; - moduleHints - .allowDownloadingMedia() - .then((canDownload) => { - this.setState({ - canDownload, + if (typeof hints.allowDownloadingMedia === "boolean") { + downloadState.canDownload = hints.allowDownloadingMedia; + } else { + downloadState.canDownload = false; + hints + .allowDownloadingMedia() + .then((downloadable) => { + this.setState({ canDownload: downloadable }); + }) + .catch((ex) => { + logger.error(`Failed to check if media from ${props.mxEvent.getId()} could be downloaded`, ex); + // Err on the side of safety. + this.setState({ canDownload: false }); }); - }) - .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, diff --git a/src/modules/customComponentApi.ts b/src/modules/customComponentApi.ts index 2729823c79..81043baeb0 100644 --- a/src/modules/customComponentApi.ts +++ b/src/modules/customComponentApi.ts @@ -31,13 +31,13 @@ interface CustomMessageComponentProps extends Omit { - // Note. This just makes it easier to use this API on Element Web as we already have the moduleized event stored. - allowDownloadingMedia?: () => Promise; + // Note. This just makes it easier to use this API on Element Web as we already have the modularized event stored. + allowDownloadingMedia?: (() => Promise) | boolean; } const DEFAULT_HINTS: Required = { allowEditingEvent: true, - allowDownloadingMedia: () => Promise.resolve(true), + allowDownloadingMedia: true, }; export class CustomComponentsApi implements ICustomComponentsApi {