✨ (chatInput): add useHandleFileChange hook to handle file input changes
This commit is contained in:
parent
0d17211177
commit
43ba306b34
1 changed files with 29 additions and 0 deletions
|
|
@ -0,0 +1,29 @@
|
||||||
|
import ShortUniqueId from "short-unique-id";
|
||||||
|
import useFileUpload from "./use-file-upload";
|
||||||
|
|
||||||
|
export const useHandleFileChange = (setFiles, currentFlowId) => {
|
||||||
|
const handleFileChange = async (
|
||||||
|
event: React.ChangeEvent<HTMLInputElement>,
|
||||||
|
) => {
|
||||||
|
const file = event.target.files?.[0];
|
||||||
|
if (file) {
|
||||||
|
const uid = new ShortUniqueId({ length: 3 });
|
||||||
|
const id = uid();
|
||||||
|
const type = file.type.split("/")[0];
|
||||||
|
const blob = file;
|
||||||
|
|
||||||
|
setFiles((prevFiles) => [
|
||||||
|
...prevFiles,
|
||||||
|
{ file: blob, loading: true, error: false, id, type },
|
||||||
|
]);
|
||||||
|
|
||||||
|
useFileUpload(blob, currentFlowId, setFiles, id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
handleFileChange,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useHandleFileChange;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue