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

84 lines
3.7 KiB
TypeScript
Raw Normal View History

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";
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';
import classNames from 'classnames';
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;
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')
// for some reason revolt.js says the size is a string even though it's a number
2021-07-04 21:09:24 -04:00
const filesize = determineFileSize(size);
2021-06-20 17:09:18 -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})</span>
<a href={open_url} target="_blank" className={styles.iconType} >
2021-06-20 17:09:18 -04:00
<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} className={styles.downloadIcon} download target="_blank">
2021-06-20 17:09:18 -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">
2021-06-20 17:09:18 -04:00
<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={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})</span>
<a href={download_url} className={styles.downloadIcon} download target="_blank">
2021-06-20 17:09:18 -04:00
<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}>
<File 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">
2021-06-20 17:09:18 -04:00
<IconButton>
2021-06-27 06:17:59 -04:00
<Download size={24} />
2021-06-20 17:09:18 -04:00
</IconButton>
</a>
</div>
)
}
}