2021-06-20 17:09:18 -04:00
|
|
|
import { useContext } from 'preact/hooks';
|
|
|
|
import styles from './Attachment.module.scss';
|
|
|
|
import IconButton from '../../../ui/IconButton';
|
|
|
|
import { Attachment } from "revolt.js/dist/api/objects";
|
2021-06-21 16:11:53 -04:00
|
|
|
import { determineFileSize } from '../../../../lib/fileSize';
|
2021-06-20 17:09:18 -04:00
|
|
|
import { AppContext } from '../../../../context/revoltjs/RevoltClient';
|
2021-06-27 06:17:59 -04:00
|
|
|
import { Download, LinkExternal, File, Headphone, Video } from '@styled-icons/boxicons-regular';
|
2021-06-20 17:09:18 -04:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
attachment: Attachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function AttachmentActions({ attachment }: Props) {
|
|
|
|
const client = useContext(AppContext);
|
|
|
|
const { filename, metadata, size } = attachment;
|
|
|
|
|
2021-06-21 04:02:38 -04:00
|
|
|
const url = client.generateFileURL(attachment)!;
|
2021-06-20 17:09:18 -04:00
|
|
|
const open_url = `${url}/${filename}`;
|
|
|
|
const download_url = url.replace('attachments', 'attachments/download')
|
|
|
|
|
2021-07-04 07:09:39 -04:00
|
|
|
// for some reason revolt.js says the size is a string even though it's a number
|
|
|
|
const filesize = determineFileSize(size as unknown as number);
|
2021-06-20 17:09:18 -04:00
|
|
|
|
|
|
|
switch (metadata.type) {
|
|
|
|
case 'Image':
|
|
|
|
return (
|
|
|
|
<div className={styles.actions}>
|
|
|
|
<div className={styles.info}>
|
|
|
|
<span className={styles.filename}>{filename}</span>
|
|
|
|
<span className={styles.filesize}>{metadata.width + 'x' + metadata.height} ({filesize})</span>
|
|
|
|
</div>
|
|
|
|
<a href={open_url} target="_blank">
|
|
|
|
<IconButton>
|
2021-06-27 06:17:59 -04:00
|
|
|
<LinkExternal size={24} />
|
2021-06-20 17:09:18 -04:00
|
|
|
</IconButton>
|
|
|
|
</a>
|
|
|
|
<a href={download_url} download target="_blank">
|
|
|
|
<IconButton>
|
|
|
|
<Download size={24} />
|
|
|
|
</IconButton>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
case 'Audio':
|
|
|
|
return (
|
|
|
|
<div className={styles.actions}>
|
2021-06-27 06:17:59 -04:00
|
|
|
<Headphone size={24} />
|
2021-06-20 17:09:18 -04:00
|
|
|
<div className={styles.info}>
|
|
|
|
<span className={styles.filename}>{filename}</span>
|
|
|
|
<span className={styles.filesize}>{filesize}</span>
|
|
|
|
</div>
|
|
|
|
<a href={download_url} download target="_blank">
|
|
|
|
<IconButton>
|
2021-06-27 06:17:59 -04:00
|
|
|
<Download size={24} />
|
2021-06-20 17:09:18 -04:00
|
|
|
</IconButton>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
case 'Video':
|
|
|
|
return (
|
|
|
|
<div className={styles.actions}>
|
2021-06-27 06:17:59 -04:00
|
|
|
<Video size={24} />
|
2021-06-20 17:09:18 -04:00
|
|
|
<div className={styles.info}>
|
|
|
|
<span className={styles.filename}>{filename}</span>
|
|
|
|
<span className={styles.filesize}>{metadata.width + 'x' + metadata.height} ({filesize})</span>
|
|
|
|
</div>
|
|
|
|
<a href={download_url} download target="_blank">
|
|
|
|
<IconButton>
|
2021-06-27 06:17:59 -04:00
|
|
|
<Download size={24} />
|
2021-06-20 17:09:18 -04:00
|
|
|
</IconButton>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return (
|
|
|
|
<div className={styles.actions}>
|
2021-06-27 06:17:59 -04:00
|
|
|
<File size={24} />
|
2021-06-20 17:09:18 -04:00
|
|
|
<div className={styles.info}>
|
|
|
|
<span className={styles.filename}>{filename}</span>
|
|
|
|
<span className={styles.filesize}>{filesize}</span>
|
|
|
|
</div>
|
|
|
|
<a href={download_url} download target="_blank">
|
|
|
|
<IconButton>
|
2021-06-27 06:17:59 -04:00
|
|
|
<Download size={24} />
|
2021-06-20 17:09:18 -04:00
|
|
|
</IconButton>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|