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 sequential same file uploads; #338

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 54 additions & 44 deletions packages/common-app/src/form/files/FileSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
import { getNewId } from '@homebase-id/js-lib/helpers';
import { ReactNode } from 'react';
import { forwardRef, ReactNode } from 'react';

export const FileSelector = ({
children,
onChange,
className,
accept,
maxSize,
}: {
children?: ReactNode;
onChange?: (files: File[]) => void;
className?: string;
accept?: string;
maxSize?: number;
}) => {
const id = getNewId();
export const FileSelector = forwardRef(
(
{
children,
onChange,
className,
accept,
maxSize,
}: {
children?: ReactNode;
onChange?: (files: File[]) => void;
className?: string;
accept?: string;
maxSize?: number;
},
ref: React.Ref<HTMLInputElement>
) => {
const id = getNewId();

return (
<>
<label htmlFor={id} className={`block cursor-pointer select-none ${className ?? ''}`}>
{children ?? '+'}
</label>
<input
id={id}
name={id}
onChange={(e) => {
const files = e.target?.files;
if (files) {
if (maxSize) {
for (let i = 0; i < files.length; i++) {
if (files[i].size > maxSize) {
alert(
`File ${files[i].name} is too big. Max size is ${maxSize / 1024 / 1024} MB.`
);
e.target.value = '';
return;
return (
<>
<label htmlFor={id} className={`block cursor-pointer select-none ${className ?? ''}`}>
{children ?? '+'}
</label>
<input
id={id}
name={id}
onChange={(e) => {
const files = e.target?.files;
if (files) {
if (maxSize) {
for (let i = 0; i < files.length; i++) {
if (files[i].size > maxSize) {
alert(
`File ${files[i].name} is too big. Max size is ${maxSize / 1024 / 1024} MB.`
);
e.target.value = '';
return;
}
}
}
onChange && onChange(Array.from(files));
}
onChange && onChange(Array.from(files));
}}
type="file"
accept={
accept || 'image/png, image/jpeg, image/tiff, image/webp, image/svg+xml, image/gif'
}
}}
type="file"
accept={accept || 'image/png, image/jpeg, image/tiff, image/webp, image/svg+xml, image/gif'}
multiple
className={`sr-only`}
/>
</>
);
};
multiple
className={`sr-only`}
ref={ref}
/>
</>
);
}
);

FileSelector.displayName = 'FileSelector';
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const PostComposer = ({

const { savePost, postState, processingProgress, error } = usePostComposer();
const selectRef = useRef<HTMLSelectElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);

const [caption, setCaption] = useState<string>('');

Expand Down Expand Up @@ -113,6 +114,7 @@ export const PostComposer = ({
setCaption('');
// setChannel(BlogConfig.PublicChannelNewDsr);
setFiles(undefined);
fileInputRef.current?.value && (fileInputRef.current.value = '');
setStateIndex((i) => i + 1);
};

Expand Down Expand Up @@ -216,6 +218,7 @@ export const PostComposer = ({
accept="image/png, image/jpeg, image/tiff, image/webp, image/svg+xml, image/gif, video/mp4, application/pdf"
className="text-foreground hover:text-opacity-70"
maxSize={HUNDRED_MEGA_BYTES}
ref={fileInputRef}
/>
</div>

Expand Down
Loading