revite/src/components/common/messaging/attachments/AttachmentActions.tsx

132 lines
4.8 KiB
TypeScript
Raw Normal View History

2021-07-05 06:23:23 -04:00
import {
2021-07-05 06:25:20 -04:00
Download,
LinkExternal,
File,
Headphone,
Video,
2021-07-05 06:23:23 -04:00
} from "@styled-icons/boxicons-regular";
2021-07-30 17:40:49 -04:00
import { Attachment } from "revolt-api/types/Autumn";
2021-07-05 06:23:23 -04:00
2021-07-10 10:21:35 -04:00
import styles from "./AttachmentActions.module.scss";
2021-07-05 06:23:23 -04:00
import classNames from "classnames";
import { useContext } from "preact/hooks";
import { determineFileSize } from "../../../../lib/fileSize";
import { AppContext } from "../../../../context/revoltjs/RevoltClient";
import IconButton from "../../../ui/IconButton";
2021-06-20 17:09:18 -04:00
interface Props {
2021-07-05 06:25:20 -04:00
attachment: Attachment;
2021-06-20 17:09:18 -04:00
}
export default function AttachmentActions({ attachment }: Props) {
2021-07-05 06:25:20 -04:00
const client = useContext(AppContext);
const { filename, metadata, size } = attachment;
2021-06-20 17:09:18 -04:00
2021-07-05 06:25:20 -04:00
const url = client.generateFileURL(attachment)!;
const open_url = `${url}/${filename}`;
const download_url = url.replace("attachments", "attachments/download");
2021-06-20 17:09:18 -04:00
2021-07-05 06:25:20 -04:00
const filesize = determineFileSize(size);
2021-06-20 17:09:18 -04:00
2021-07-05 06:25:20 -04:00
switch (metadata.type) {
case "Image":
return (
<div className={classNames(styles.actions, styles.imageAction)}>
<span className={styles.filename}>{filename}</span>
<span className={styles.filesize}>
{`${metadata.width}x${metadata.height}`} ({filesize})
2021-07-05 06:25:20 -04:00
</span>
<a
href={open_url}
target="_blank"
className={styles.iconType}
rel="noreferrer">
2021-07-05 06:25:20 -04:00
<IconButton>
<LinkExternal size={24} />
</IconButton>
</a>
<a
href={download_url}
className={styles.downloadIcon}
download
target="_blank"
rel="noreferrer">
2021-07-05 06:25:20 -04:00
<IconButton>
<Download size={24} />
</IconButton>
</a>
</div>
);
case "Audio":
return (
<div className={classNames(styles.actions, styles.audioAction)}>
<Headphone size={24} className={styles.iconType} />
<span className={styles.filename}>{filename}</span>
<span className={styles.filesize}>{filesize}</span>
<a
href={download_url}
className={styles.downloadIcon}
download
target="_blank"
rel="noreferrer">
2021-07-05 06:25:20 -04:00
<IconButton>
<Download size={24} />
</IconButton>
</a>
</div>
);
case "Video":
return (
<div className={classNames(styles.actions, styles.videoAction)}>
<Video size={24} className={styles.iconType} />
<span className={styles.filename}>{filename}</span>
<span className={styles.filesize}>
{`${metadata.width}x${metadata.height}`} ({filesize})
2021-07-05 06:25:20 -04:00
</span>
<a
href={download_url}
className={styles.downloadIcon}
download
target="_blank"
rel="noreferrer">
2021-07-05 06:25:20 -04:00
<IconButton>
<Download size={24} />
</IconButton>
</a>
</div>
);
default:
return (
<div className={styles.actions}>
<File size={24} className={styles.iconType} />
<span className={styles.filename}>{filename}</span>
<span className={styles.filesize}>{filesize}</span>
2021-07-06 14:29:27 -04:00
{metadata.type === "Text" && (
<a
href={open_url}
target="_blank"
className={styles.externalType}
rel="noreferrer">
2021-07-06 14:29:27 -04:00
<IconButton>
<LinkExternal size={24} />
</IconButton>
</a>
)}
2021-07-05 06:25:20 -04:00
<a
href={download_url}
className={styles.downloadIcon}
download
target="_blank"
rel="noreferrer">
2021-07-05 06:25:20 -04:00
<IconButton>
<Download size={24} />
</IconButton>
</a>
</div>
);
}
2021-06-20 17:09:18 -04:00
}