Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Prevent uploading more files than allowed by maxFilesLeft #593

Merged
merged 2 commits into from Oct 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 23 additions & 21 deletions src/components/MessageInput/hooks/messageInput.js
Expand Up @@ -617,21 +617,35 @@ export default function useMessageInputState(props) {
// TODO: cancel upload if still uploading
}, []);

// Number of files that the user can still add. Should never be more than the amount allowed by the API.
// If multipleUploads is false, we only want to allow a single upload.
const maxFilesAllowed = useMemo(() => {
if (!channelContext.multipleUploads) return 1;
if (channelContext.maxNumberOfFiles === undefined) {
return apiMaxNumberOfFiles;
}
return channelContext.maxNumberOfFiles;
}, [channelContext.maxNumberOfFiles, channelContext.multipleUploads]);

const maxFilesLeft = maxFilesAllowed - numberOfUploads;

const uploadNewFiles = useCallback(
/**
* @param {FileList} files
*/
(files) => {
Array.from(files).forEach((file) => {
const id = generateRandomId();
if (file.type.startsWith('image/')) {
dispatch({ type: 'setImageUpload', id, file, state: 'uploading' });
} else if (file instanceof File && !noFiles) {
dispatch({ type: 'setFileUpload', id, file, state: 'uploading' });
}
});
Array.from(files)
.slice(0, maxFilesLeft)
.forEach((file) => {
const id = generateRandomId();
if (file.type.startsWith('image/')) {
dispatch({ type: 'setImageUpload', id, file, state: 'uploading' });
} else if (file instanceof File && !noFiles) {
dispatch({ type: 'setFileUpload', id, file, state: 'uploading' });
}
});
},
[noFiles],
[maxFilesLeft, noFiles],
);

const onPaste = useCallback(
Expand Down Expand Up @@ -675,18 +689,6 @@ export default function useMessageInputState(props) {
[uploadNewFiles, insertText],
);

// Number of files that the user can still add. Should never be more than the amount allowed by the API.
// If multipleUploads is false, we only want to allow a single upload.
const maxFilesAllowed = useMemo(() => {
if (!channelContext.multipleUploads) return 1;
if (channelContext.maxNumberOfFiles === undefined) {
return apiMaxNumberOfFiles;
}
return channelContext.maxNumberOfFiles;
}, [channelContext.maxNumberOfFiles, channelContext.multipleUploads]);

const maxFilesLeft = maxFilesAllowed - numberOfUploads;

const isUploadEnabled = channel?.getConfig?.()?.uploads !== false;

return {
Expand Down