fix: pasting files not working (#2548)

* fix: pasting files not working

* [autofix.ci] apply automated fixes

* refactor: change new import

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: cristhianzl <cristhian.lousa@gmail.com>
Co-authored-by: anovazzi1 <otavio2204@gmail.com>
This commit is contained in:
Igor Carvalho 2024-07-05 10:57:48 -03:00 • committed by GitHub
commit 6f827ca3dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 21 deletions

View file

@ -47,7 +47,7 @@ export default function InputFileComponent({
setMyValue(value); setMyValue(value);
}, [value]); }, [value]);
const mutation = usePostUploadFile(); const { mutate } = usePostUploadFile();
const handleButtonClick = (): void => { const handleButtonClick = (): void => {
// Create a file input element // Create a file input element
@ -66,7 +66,7 @@ export default function InputFileComponent({
// Check if the file type is correct // Check if the file type is correct
if (file && checkFileType(file.name)) { if (file && checkFileType(file.name)) {
// Upload the file // Upload the file
mutation.mutate( mutate(
{ file, id: currentFlowId }, { file, id: currentFlowId },
{ {
onSuccess: (data) => { onSuccess: (data) => {

View file

@ -67,7 +67,7 @@ export default function IOFileInput({ field, updateValue }: IOFileInputProps) {
setIsDragging(false); setIsDragging(false);
}; };
const mutation = usePostUploadFile(); const { mutate } = usePostUploadFile();
const upload = async (file) => { const upload = async (file) => {
if (file) { if (file) {
@ -83,7 +83,7 @@ export default function IOFileInput({ field, updateValue }: IOFileInputProps) {
document.body.appendChild(imgElement); // Add the image to the body or replace this with your desired location document.body.appendChild(imgElement); // Add the image to the body or replace this with your desired location
}; };
fileReader.readAsDataURL(file); fileReader.readAsDataURL(file);
mutation.mutate( mutate(
{ file, id: currentFlowId }, { file, id: currentFlowId },
{ {
onSuccess: (data) => { onSuccess: (data) => {

View file

@ -1,6 +1,6 @@
import { usePostUploadFile } from "@/controllers/API/queries/files/use-post-upload-file"; import { usePostUploadFile } from "@/controllers/API/queries/files/use-post-upload-file";
import useAlertStore from "@/stores/alertStore"; import useAlertStore from "@/stores/alertStore";
import { useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import ShortUniqueId from "short-unique-id"; import ShortUniqueId from "short-unique-id";
import { import {
ALLOWED_IMAGE_INPUT_EXTENSIONS, ALLOWED_IMAGE_INPUT_EXTENSIONS,
@ -9,7 +9,6 @@ import {
FS_ERROR_TEXT, FS_ERROR_TEXT,
SN_ERROR_TEXT, SN_ERROR_TEXT,
} from "../../../../../constants/constants"; } from "../../../../../constants/constants";
import { uploadFile } from "../../../../../controllers/API";
import useFlowsManagerStore from "../../../../../stores/flowsManagerStore"; import useFlowsManagerStore from "../../../../../stores/flowsManagerStore";
import { import {
ChatInputType, ChatInputType,
@ -22,7 +21,7 @@ import UploadFileButton from "./components/uploadFileButton";
import { getClassNamesFilePreview } from "./helpers/get-class-file-preview"; import { getClassNamesFilePreview } from "./helpers/get-class-file-preview";
import useAutoResizeTextArea from "./hooks/use-auto-resize-text-area"; import useAutoResizeTextArea from "./hooks/use-auto-resize-text-area";
import useFocusOnUnlock from "./hooks/use-focus-unlock"; import useFocusOnUnlock from "./hooks/use-focus-unlock";
export default function ChatInput({ export default function ChattInput({
lockChat, lockChat,
chatValue, chatValue,
sendMessage, sendMessage,
@ -39,16 +38,33 @@ export default function ChatInput({
const [inputFocus, setInputFocus] = useState<boolean>(false); const [inputFocus, setInputFocus] = useState<boolean>(false);
const fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);
const setErrorData = useAlertStore((state) => state.setErrorData); const setErrorData = useAlertStore((state) => state.setErrorData);
const [id, setId] = useState<string>("");
useFocusOnUnlock(lockChat, inputRef); useFocusOnUnlock(lockChat, inputRef);
useAutoResizeTextArea(chatValue, inputRef); useAutoResizeTextArea(chatValue, inputRef);
const { mutate, isPending } = usePostUploadFile();
const handleFileChange = async ( const handleFileChange = async (
event: React.ChangeEvent<HTMLInputElement>, event: React.ChangeEvent<HTMLInputElement> | ClipboardEvent,
) => { ) => {
const fileInput = event.target; let file: File | null = null;
const file = fileInput.files?.[0];
if ("clipboardData" in event) {
const items = event.clipboardData?.items;
if (items) {
for (let i = 0; i < items.length; i++) {
const blob = items[i].getAsFile();
if (blob) {
file = blob;
break;
}
}
}
} else {
const fileInput = event.target as HTMLInputElement;
file = fileInput.files?.[0] ?? null;
}
if (file) { if (file) {
const fileExtension = file.name.split(".").pop()?.toLowerCase(); const fileExtension = file.name.split(".").pop()?.toLowerCase();
@ -65,18 +81,16 @@ export default function ChatInput({
const uid = new ShortUniqueId(); const uid = new ShortUniqueId();
const id = uid.randomUUID(10); const id = uid.randomUUID(10);
setId(id);
const type = file.type.split("/")[0]; const type = file.type.split("/")[0];
const blob = file;
setFiles((prevFiles) => [ setFiles((prevFiles) => [
...prevFiles, ...prevFiles,
{ file: blob, loading: true, error: false, id, type }, { file, loading: true, error: false, id, type },
]); ]);
mutation.mutate( mutate(
{ file: blob, id: currentFlowId }, { file, id: currentFlowId },
{ {
onSuccess: (data) => { onSuccess: (data) => {
setFiles((prev) => { setFiles((prev) => {
@ -100,10 +114,17 @@ export default function ChatInput({
); );
} }
fileInput.value = ""; if ("target" in event && event.target instanceof HTMLInputElement) {
event.target.value = "";
}
}; };
const mutation = usePostUploadFile(); useEffect(() => {
document.addEventListener("paste", handleFileChange);
return () => {
document.removeEventListener("paste", handleFileChange);
};
}, [handleFileChange, currentFlowId, lockChat]);
const send = () => { const send = () => {
sendMessage({ sendMessage({

View file

@ -165,6 +165,8 @@ export default function ChatView({
setIsDragging(false); setIsDragging(false);
}; };
const { mutate } = usePostUploadFile();
const handleFiles = (files, setFiles, currentFlowId, setErrorData) => { const handleFiles = (files, setFiles, currentFlowId, setErrorData) => {
if (files) { if (files) {
const file = files?.[0]; const file = files?.[0];
@ -192,7 +194,7 @@ export default function ChatView({
{ file: blob, loading: true, error: false, id, type }, { file: blob, loading: true, error: false, id, type },
]); ]);
mutation.mutate( mutate(
{ file: blob, id: currentFlowId }, { file: blob, id: currentFlowId },
{ {
onSuccess: (data) => { onSuccess: (data) => {
@ -218,8 +220,6 @@ export default function ChatView({
} }
}; };
const mutation = usePostUploadFile();
return ( return (
<div <div
className="eraser-column-arrangement" className="eraser-column-arrangement"