um-react/src/SelectFile.tsx

78 lines
1.9 KiB
TypeScript
Raw Normal View History

import { useDropzone } from 'react-dropzone';
2023-05-07 22:29:37 +00:00
import { Box, Text } from '@chakra-ui/react';
import { UnlockIcon } from '@chakra-ui/icons';
import { useAppDispatch } from './hooks';
import { addNewFile, processFile } from './features/file-listing/fileListingSlice';
import { nanoid } from 'nanoid';
2023-05-07 19:11:16 +00:00
export function SelectFile() {
const dispatch = useAppDispatch();
const { getRootProps, getInputProps, isDragActive } = useDropzone({
multiple: true,
onDropAccepted(files, _event) {
console.debug(
'react-dropzone/onDropAccepted(%o, %o)',
files.length,
files.map((x) => x.name)
);
2023-05-07 19:11:16 +00:00
for (const file of files) {
const blobURI = URL.createObjectURL(file);
const fileName = file.name;
const fileId = 'file://' + nanoid();
// FIXME: this should be a single action/thunk that first adds the item, then updates it.
dispatch(
addNewFile({
id: fileId,
blobURI,
fileName,
})
);
dispatch(processFile({ fileId }));
}
},
});
2023-05-07 19:11:16 +00:00
return (
2023-05-07 22:29:37 +00:00
<Box
{...getRootProps()}
2023-05-07 22:29:37 +00:00
w="100%"
maxW={480}
borderWidth="1px"
borderRadius="lg"
transitionDuration="0.5s"
p="6"
cursor="pointer"
display="flex"
flexDir="column"
alignItems="center"
_hover={{
borderColor: 'gray.400',
bg: 'gray.50',
}}
{...(isDragActive && {
bg: 'blue.50',
borderColor: 'blue.700',
})}
2023-05-07 22:29:37 +00:00
>
<input {...getInputProps()} />
2023-05-07 22:29:37 +00:00
<Box pb={3}>
<UnlockIcon boxSize={8} />
</Box>
<Box textAlign="center">
2023-05-07 22:29:37 +00:00
<Text as="span" color="teal.400">
</Text>
<Text fontSize="sm" opacity="50%">
</Text>
</Box>
2023-05-07 19:11:16 +00:00
</Box>
);
}