Skip to content

Commit

Permalink
feat: wrap openFilePicker with useCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Forsect committed Apr 24, 2024
1 parent 18a4f6d commit c0ca985
Showing 1 changed file with 53 additions and 38 deletions.
91 changes: 53 additions & 38 deletions src/useFilePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,48 @@ function useFilePicker<
onClear?.();
}, [clear, onClear]);

const parseFile = (file: FileWithPath) =>
new Promise<FileContent<ExtractContentTypeFromConfig<ConfigType>>>(
async (
resolve: (fileContent: FileContent<ExtractContentTypeFromConfig<ConfigType>>) => void,
reject: (reason: UseFilePickerError) => void
) => {
const reader = new FileReader();

//availible reader methods: readAsText, readAsBinaryString, readAsArrayBuffer, readAsDataURL
const readStrategy = reader[`readAs${readAs}` as ReaderMethod] as typeof reader.readAsText;
readStrategy.call(reader, file);

const addError = ({ ...others }: UseFilePickerError) => {
reject({ ...others });
};

reader.onload = async () =>
Promise.all(
validators.map(validator =>
validator.validateAfterParsing(props, file, reader).catch(err => Promise.reject(addError(err)))
)
)
.then(() =>
resolve({
...file,
content: reader.result as string,
name: file.name,
lastModified: file.lastModified,
} as FileContent<ExtractContentTypeFromConfig<ConfigType>>)
const parseFile = useCallback(
(file: FileWithPath) =>
new Promise<FileContent<ExtractContentTypeFromConfig<ConfigType>>>(
async (
resolve: (fileContent: FileContent<ExtractContentTypeFromConfig<ConfigType>>) => void,
reject: (reason: UseFilePickerError) => void
) => {
const reader = new FileReader();

//availible reader methods: readAsText, readAsBinaryString, readAsArrayBuffer, readAsDataURL
const readStrategy = reader[`readAs${readAs}` as ReaderMethod] as typeof reader.readAsText;
readStrategy.call(reader, file);

const addError = ({ ...others }: UseFilePickerError) => {
reject({ ...others });
};

reader.onload = async () =>
Promise.all(
validators.map(validator =>
validator.validateAfterParsing(props, file, reader).catch(err => Promise.reject(addError(err)))
)
)
.catch(() => {});

reader.onerror = () => {
addError({ name: 'FileReaderError', readerError: reader.error, causedByFile: file });
};
}
);
.then(() =>
resolve({
...file,
content: reader.result as string,
name: file.name,
lastModified: file.lastModified,
} as FileContent<ExtractContentTypeFromConfig<ConfigType>>)
)
.catch(() => {});

reader.onerror = () => {
addError({ name: 'FileReaderError', readerError: reader.error, causedByFile: file });
};
}
),
[props, readAs, validators]
);

const openFilePicker = () => {
const openFilePicker = useCallback(() => {
const fileExtensions = accept instanceof Array ? accept.join(',') : accept;
openFileDialog(
fileExtensions,
Expand Down Expand Up @@ -156,7 +159,19 @@ function useFilePicker<
},
initializeWithCustomParameters
);
};
}, [
accept,
clear,
initializeWithCustomParameters,
multiple,
onFilesSelected,
onFilesRejected,
onFilesSuccessfullySelected,
parseFile,
props,
readFilesContent,
validators,
]);

return {
openFilePicker,
Expand Down

0 comments on commit c0ca985

Please sign in to comment.