2021-06-21 16:11:53 -04:00
|
|
|
import { Text } from "preact-i18n";
|
|
|
|
import styled from "styled-components";
|
2021-06-22 12:45:34 -04:00
|
|
|
import { CAN_UPLOAD_AT_ONCE, UploadState } from "../MessageBox";
|
2021-06-21 16:11:53 -04:00
|
|
|
import { useEffect, useState } from 'preact/hooks';
|
|
|
|
import { determineFileSize } from '../../../../lib/fileSize';
|
2021-06-22 12:45:34 -04:00
|
|
|
import { XCircle, Plus, Share, X, FileText } from "@styled-icons/feather";
|
2021-06-21 16:11:53 -04:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
state: UploadState,
|
|
|
|
addFile: () => void,
|
|
|
|
removeFile: (index: number) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
const Container = styled.div`
|
|
|
|
gap: 4px;
|
|
|
|
padding: 8px;
|
|
|
|
display: flex;
|
|
|
|
user-select: none;
|
|
|
|
flex-direction: column;
|
|
|
|
background: var(--message-box);
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Carousel = styled.div`
|
|
|
|
gap: 8px;
|
|
|
|
display: flex;
|
|
|
|
overflow-x: scroll;
|
|
|
|
flex-direction: row;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Entry = styled.div`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
|
|
img {
|
|
|
|
height: 100px;
|
|
|
|
margin-bottom: 4px;
|
|
|
|
border-radius: 4px;
|
|
|
|
object-fit: contain;
|
|
|
|
background: var(--secondary-background);
|
|
|
|
}
|
|
|
|
|
2021-06-22 12:45:34 -04:00
|
|
|
&.fade {
|
|
|
|
opacity: 0.4;
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:11:53 -04:00
|
|
|
span.fn {
|
|
|
|
margin: auto;
|
|
|
|
font-size: .8em;
|
|
|
|
overflow: hidden;
|
|
|
|
max-width: 180px;
|
|
|
|
text-align: center;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
color: var(--secondary-foreground);
|
|
|
|
}
|
|
|
|
|
|
|
|
span.size {
|
|
|
|
font-size: .6em;
|
|
|
|
color: var(--tertiary-foreground);
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
|
2021-06-22 12:45:34 -04:00
|
|
|
div:first-child {
|
2021-06-21 16:11:53 -04:00
|
|
|
position: relative;
|
|
|
|
height: 0;
|
|
|
|
|
|
|
|
div {
|
|
|
|
display: grid;
|
|
|
|
height: 100px;
|
|
|
|
cursor: pointer;
|
|
|
|
border-radius: 4px;
|
|
|
|
place-items: center;
|
|
|
|
|
|
|
|
opacity: 0;
|
|
|
|
transition: 0.1s ease opacity;
|
2021-06-22 12:45:34 -04:00
|
|
|
background: rgba(0, 0, 0, 0.8);
|
2021-06-21 16:11:53 -04:00
|
|
|
|
|
|
|
&:hover {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Description = styled.div`
|
|
|
|
gap: 4px;
|
|
|
|
display: flex;
|
|
|
|
font-size: 0.9em;
|
|
|
|
align-items: center;
|
|
|
|
color: var(--secondary-foreground);
|
|
|
|
`;
|
|
|
|
|
2021-06-22 12:45:34 -04:00
|
|
|
const Divider = styled.div`
|
|
|
|
width: 4px;
|
|
|
|
height: 130px;
|
|
|
|
flex-shrink: 0;
|
|
|
|
border-radius: 4px;
|
|
|
|
background: var(--tertiary-background);
|
|
|
|
`;
|
|
|
|
|
2021-06-21 16:11:53 -04:00
|
|
|
const EmptyEntry = styled.div`
|
|
|
|
width: 100px;
|
|
|
|
height: 100px;
|
|
|
|
display: grid;
|
|
|
|
flex-shrink: 0;
|
|
|
|
cursor: pointer;
|
|
|
|
border-radius: 4px;
|
|
|
|
place-items: center;
|
|
|
|
background: var(--primary-background);
|
|
|
|
transition: 0.1s ease background-color;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background: var(--secondary-background);
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2021-06-22 12:45:34 -04:00
|
|
|
function FileEntry({ file, remove, index }: { file: File, remove?: () => void, index: number }) {
|
2021-06-21 16:11:53 -04:00
|
|
|
if (!file.type.startsWith('image/')) return (
|
2021-06-22 12:45:34 -04:00
|
|
|
<Entry className={index >= CAN_UPLOAD_AT_ONCE ? 'fade' : ''}>
|
2021-06-21 16:11:53 -04:00
|
|
|
<div><div onClick={remove}><XCircle size={36} /></div></div>
|
2021-06-22 12:45:34 -04:00
|
|
|
<EmptyEntry>
|
|
|
|
<FileText size={36} />
|
|
|
|
</EmptyEntry>
|
2021-06-21 16:11:53 -04:00
|
|
|
<span class="fn">{file.name}</span>
|
|
|
|
<span class="size">{determineFileSize(file.size)}</span>
|
|
|
|
</Entry>
|
|
|
|
);
|
|
|
|
|
|
|
|
const [ url, setURL ] = useState('');
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let url: string = URL.createObjectURL(file);
|
|
|
|
setURL(url);
|
|
|
|
return () => URL.revokeObjectURL(url);
|
|
|
|
}, [ file ]);
|
|
|
|
|
|
|
|
return (
|
2021-06-22 12:45:34 -04:00
|
|
|
<Entry className={index >= CAN_UPLOAD_AT_ONCE ? 'fade' : ''}>
|
2021-06-21 16:11:53 -04:00
|
|
|
{ remove && <div><div onClick={remove}><XCircle size={36} /></div></div> }
|
|
|
|
<img src={url}
|
|
|
|
alt={file.name} />
|
|
|
|
<span class="fn">{file.name}</span>
|
|
|
|
<span class="size">{determineFileSize(file.size)}</span>
|
|
|
|
</Entry>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function FilePreview({ state, addFile, removeFile }: Props) {
|
|
|
|
if (state.type === 'none') return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<Carousel>
|
2021-06-22 12:45:34 -04:00
|
|
|
{ state.files.map((file, index) =>
|
|
|
|
<>
|
|
|
|
{ index === CAN_UPLOAD_AT_ONCE && <Divider /> }
|
|
|
|
<FileEntry index={index} file={file} key={file.name} remove={state.type === 'attached' ? () => removeFile(index) : undefined} />
|
|
|
|
</>
|
|
|
|
) }
|
2021-06-21 16:11:53 -04:00
|
|
|
{ state.type === 'attached' && <EmptyEntry onClick={addFile}><Plus size={48} /></EmptyEntry> }
|
|
|
|
</Carousel>
|
|
|
|
{ state.type === 'uploading' && <Description>
|
|
|
|
<Share size={24} />
|
|
|
|
<Text id="app.main.channel.uploading_file" /> ({state.percent}%)
|
|
|
|
</Description> }
|
|
|
|
{ state.type === 'sending' && <Description>
|
|
|
|
<Share size={24} />
|
|
|
|
Sending...
|
|
|
|
</Description> }
|
|
|
|
{ state.type === 'failed' && <Description>
|
|
|
|
<X size={24} />
|
|
|
|
<Text id={`error.${state.error}`} />
|
|
|
|
</Description> }
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|