Skip to content

Commit

Permalink
Fix autofill image name
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolt committed Jul 6, 2024
1 parent 8378a05 commit 1f398f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/pages/ImportFromImage/ImportForm/hooks/useImageUrl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { capitalize } from '@/utils/string';
import { useEffect } from 'react';

interface ImageUrlHookParams {
imageUrl?: string;
setImage: (value: FileList) => void;
setImage: (files: FileList, name: string) => void;
}

export const useImageUrl = ({ imageUrl, setImage }: ImageUrlHookParams): void => {
Expand All @@ -13,9 +14,10 @@ export const useImageUrl = ({ imageUrl, setImage }: ImageUrlHookParams): void =>
const blob = await res.blob();
const dataTransfer = new DataTransfer();
const mimeType = 'image/svg+xml';
const name = imageUrl.split('/').at(-1) ?? '';
dataTransfer.items.add(new File([blob], name, { type: mimeType }));
setImage(dataTransfer.files);
const filename = imageUrl.split('/').at(-1) ?? '';
const name = capitalize(filename.split('.')[0].replaceAll('-', ' '));
dataTransfer.items.add(new File([blob], filename, { type: mimeType }));
setImage(dataTransfer.files, name);
})();
}
}, [imageUrl, setImage]);
Expand Down
8 changes: 5 additions & 3 deletions src/pages/ImportFromImage/ImportForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ export const ImportForm = ({ setBitmap, onSubmit, imageUrl }: ImportFormProps) =
}, [onReset, reset]);

const setImage = useCallback(
(value: FileList) => {
(files: FileList, name: string) => {
const elem = document.getElementById(ID_FILE_INPUT);
if (elem) {
setValue('files', value);
(elem as HTMLInputElement).files = value;
setValue('files', files);
(elem as HTMLInputElement).files = files;
setValue('name', name);
setFocus('name');
}
},
[setValue, setFocus],
);

useImageUrl({ imageUrl, setImage });

useEffect(() => {
Expand Down

0 comments on commit 1f398f9

Please sign in to comment.