Files
ThreadNet-Web/apps/web/src/components/views/dialogs/UploadConfirmDialog.tsx
T

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

142 lines
4.7 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
2019-06-21 09:03:28 +01:00
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
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.
*/
import React, { type JSX } from "react";
import { FilesIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
2021-10-22 17:23:32 -05:00
import { _t } from "../../../languageHandler";
2021-07-02 17:19:01 +02:00
import BaseDialog from "./BaseDialog";
import DialogButtons from "../elements/DialogButtons";
import { fileSize } from "../../../utils/FileUtils";
2021-05-06 14:11:34 +01:00
interface IProps {
file: File;
currentIndex: number;
totalFiles: number;
2021-05-06 14:11:34 +01:00
onFinished: (uploadConfirmed: boolean, uploadAll?: boolean) => void;
}
2025-12-01 16:00:09 +00:00
interface IState {
objectUrl?: string;
}
2025-12-01 16:00:09 +00:00
export default class UploadConfirmDialog extends React.Component<IProps, IState> {
public static defaultProps: Partial<IProps> = {
totalFiles: 1,
currentIndex: 0,
2021-06-29 13:11:58 +01:00
};
2023-02-13 11:39:16 +00:00
public constructor(props: IProps) {
super(props);
2025-12-01 16:00:09 +00:00
this.state = {};
}
public componentDidMount(): void {
if (this.props.file.type.startsWith("image/") || this.props.file.type.startsWith("video/")) {
this.setState({
// We do not filter the mimetype using getBlobSafeMimeType here as if the user is uploading the file
// themselves they should be trusting it enough to open/load it, and it will be rendered into a hidden
// canvas for thumbnail generation anyway
objectUrl: URL.createObjectURL(this.props.file),
});
}
}
public componentWillUnmount(): void {
2025-12-01 16:00:09 +00:00
if (this.state.objectUrl) URL.revokeObjectURL(this.state.objectUrl);
}
private onCancelClick = (): void => {
this.props.onFinished(false);
2021-06-29 13:11:58 +01:00
};
private onUploadClick = (): void => {
this.props.onFinished(true);
2021-06-29 13:11:58 +01:00
};
private onUploadAllClick = (): void => {
this.props.onFinished(true, true);
2021-06-29 13:11:58 +01:00
};
public render(): React.ReactNode {
let title: string;
if (this.props.totalFiles > 1 && this.props.currentIndex !== undefined) {
title = _t("upload_file|title_progress", {
2019-04-01 16:52:06 +01:00
current: this.props.currentIndex + 1,
total: this.props.totalFiles,
});
} else {
title = _t("upload_file|title");
}
const fileId = `mx-uploadconfirmdialog-${this.props.file.name}`;
2025-12-01 16:00:09 +00:00
const mimeType = this.props.file.type;
let preview: JSX.Element | undefined;
let placeholder: JSX.Element | undefined;
2025-12-01 16:00:09 +00:00
if (mimeType.startsWith("image/")) {
preview = (
2025-12-01 16:00:09 +00:00
<img
className="mx_UploadConfirmDialog_imagePreview"
src={this.state.objectUrl}
aria-labelledby={fileId}
/>
);
2025-12-01 16:00:09 +00:00
} else if (mimeType.startsWith("video/")) {
preview = (
<video
className="mx_UploadConfirmDialog_imagePreview"
2025-12-01 16:00:09 +00:00
src={this.state.objectUrl}
playsInline
controls={false}
/>
);
2022-12-12 12:24:14 +01:00
} else {
placeholder = <FilesIcon className="mx_UploadConfirmDialog_fileIcon" height="18px" width="18px" />;
}
let uploadAllButton: JSX.Element | undefined;
if (this.props.currentIndex + 1 < this.props.totalFiles) {
uploadAllButton = <button onClick={this.onUploadAllClick}>{_t("upload_file|upload_all_button")}</button>;
}
return (
<BaseDialog
className="mx_UploadConfirmDialog"
2019-04-03 16:27:45 +01:00
fixedWidth={false}
2021-05-06 14:11:34 +01:00
onFinished={this.onCancelClick}
title={title}
contentId="mx_Dialog_content"
>
<div id="mx_Dialog_content">
<div className="mx_UploadConfirmDialog_previewOuter">
<div className="mx_UploadConfirmDialog_previewInner">
{preview && <div>{preview}</div>}
<div id={fileId}>
{placeholder}
{this.props.file.name} ({fileSize(this.props.file.size)})
</div>
</div>
</div>
</div>
<DialogButtons
primaryButton={_t("action|upload")}
hasCancel={false}
2021-05-06 14:11:34 +01:00
onPrimaryButtonClick={this.onUploadClick}
focus={true}
>
{uploadAllButton}
</DialogButtons>
</BaseDialog>
);
}
}