Files
ThreadNet-Web/src/components/views/messages/DownloadActionButton.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

151 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-07-16 15:55:07 -06:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2021-07-16 15:55:07 -06:00
Copyright 2021 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2021-07-16 15:55:07 -06:00
*/
2025-02-05 13:25:06 +00:00
import { type MatrixEvent } from "matrix-js-sdk/src/matrix";
import React, { type JSX } from "react";
2021-10-22 17:23:32 -05:00
import classNames from "classnames";
import { DownloadIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
import { logger } from "matrix-js-sdk/src/logger";
2021-10-22 17:23:32 -05:00
2025-02-05 13:25:06 +00:00
import { type MediaEventHelper } from "../../../utils/MediaEventHelper";
import { RovingAccessibleButton } from "../../../accessibility/RovingTabIndex";
2021-07-16 15:55:07 -06:00
import Spinner from "../elements/Spinner";
2025-02-05 13:25:06 +00:00
import { _t, _td, type TranslationKey } from "../../../languageHandler";
2021-07-29 15:36:50 -06:00
import { FileDownloader } from "../../../utils/FileDownloader";
import Modal from "../../../Modal";
import ErrorDialog from "../dialogs/ErrorDialog";
import ModuleApi from "../../../modules/Api";
2021-07-16 15:55:07 -06:00
interface IProps {
mxEvent: MatrixEvent;
// XXX: It can take a cycle or two for the MessageActionBar to have all the props/setup
// required to get us a MediaEventHelper, so we use a getter function instead to prod for
// one.
2023-07-12 15:56:51 +01:00
mediaEventHelperGet: () => MediaEventHelper | undefined;
2021-07-16 15:55:07 -06:00
}
interface IState {
canDownload: null | boolean;
2021-07-16 15:55:07 -06:00
loading: boolean;
blob?: Blob;
tooltip: TranslationKey;
2021-07-16 15:55:07 -06:00
}
2021-07-18 17:06:30 -06:00
export default class DownloadActionButton extends React.PureComponent<IProps, IState> {
2021-07-29 15:36:50 -06:00
private downloader = new FileDownloader();
2021-07-16 15:55:07 -06:00
public constructor(props: IProps) {
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,
});
});
}
2021-07-16 15:55:07 -06:00
this.state = {
loading: false,
tooltip: _td("timeline|download_action_downloading"),
...downloadState,
2021-07-16 15:55:07 -06:00
};
}
private onDownloadClick = async (): Promise<void> => {
try {
await this.doDownload();
} catch (e) {
Modal.createDialog(ErrorDialog, {
title: _t("timeline|download_failed"),
description: (
<>
<div>{_t("timeline|download_failed_description")}</div>
<div>{e instanceof Error ? e.toString() : ""}</div>
</>
),
});
this.setState({ loading: false });
}
};
private async doDownload(): Promise<void> {
2023-07-12 15:56:51 +01:00
const mediaEventHelper = this.props.mediaEventHelperGet();
if (this.state.loading || !mediaEventHelper) return;
2021-07-16 15:55:07 -06:00
2023-07-12 15:56:51 +01:00
if (mediaEventHelper.media.isEncrypted) {
this.setState({ tooltip: _td("timeline|download_action_decrypting") });
}
2021-07-16 15:55:07 -06:00
this.setState({ loading: true });
if (this.state.blob) {
// Cheat and trigger a download, again.
return this.downloadBlob(this.state.blob);
2021-07-16 15:55:07 -06:00
}
2023-07-12 15:56:51 +01:00
const blob = await mediaEventHelper.sourceBlob.value;
2021-07-16 15:55:07 -06:00
this.setState({ blob });
await this.downloadBlob(blob);
}
2021-07-16 15:55:07 -06:00
private async downloadBlob(blob: Blob): Promise<void> {
2021-07-29 15:36:50 -06:00
await this.downloader.download({
blob,
2023-07-12 15:56:51 +01:00
name: this.props.mediaEventHelperGet()!.fileName,
2021-07-29 15:36:50 -06:00
});
this.setState({ loading: false });
}
2021-07-16 15:55:07 -06:00
public render(): React.ReactNode {
let spinner: JSX.Element | undefined;
2021-07-16 15:55:07 -06:00
if (this.state.loading) {
spinner = <Spinner w={18} h={18} />;
}
if (this.state.canDownload === null) {
spinner = <Spinner w={18} h={18} />;
}
if (this.state.canDownload === false) {
return null;
}
2021-07-16 15:55:07 -06:00
const classes = classNames({
mx_MessageActionBar_iconButton: true,
2021-07-16 15:55:07 -06:00
mx_MessageActionBar_downloadButton: true,
mx_MessageActionBar_downloadSpinnerButton: !!spinner,
});
return (
<RovingAccessibleButton
2021-07-16 15:55:07 -06:00
className={classes}
title={spinner ? _t(this.state.tooltip) : _t("action|download")}
2021-07-16 15:55:07 -06:00
onClick={this.onDownloadClick}
disabled={!!spinner}
placement="left"
2021-07-16 15:55:07 -06:00
>
<DownloadIcon />
2021-07-16 15:55:07 -06:00
{spinner}
</RovingAccessibleButton>
2021-07-16 15:55:07 -06:00
);
}
}