mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-07 13:23:15 +00:00
Add drag/drop file upload support to the chat input area
This commit is contained in:
@@ -78,6 +78,7 @@ export default function ChatInputArea(props: ChatInputProps) {
|
|||||||
const [mediaRecorder, setMediaRecorder] = useState<MediaRecorder | null>(null);
|
const [mediaRecorder, setMediaRecorder] = useState<MediaRecorder | null>(null);
|
||||||
|
|
||||||
const [progressValue, setProgressValue] = useState(0);
|
const [progressValue, setProgressValue] = useState(0);
|
||||||
|
const [isDragAndDropping, setIsDragAndDropping] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!uploading) {
|
if (!uploading) {
|
||||||
@@ -123,6 +124,19 @@ export default function ChatInputArea(props: ChatInputProps) {
|
|||||||
function handleFileChange(event: React.ChangeEvent<HTMLInputElement>) {
|
function handleFileChange(event: React.ChangeEvent<HTMLInputElement>) {
|
||||||
if (!event.target.files) return;
|
if (!event.target.files) return;
|
||||||
|
|
||||||
|
uploadFiles(event.target.files);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDragAndDropFiles(event: React.DragEvent<HTMLDivElement>) {
|
||||||
|
event.preventDefault();
|
||||||
|
setIsDragAndDropping(false);
|
||||||
|
|
||||||
|
if (!event.dataTransfer.files) return;
|
||||||
|
|
||||||
|
uploadFiles(event.dataTransfer.files);
|
||||||
|
}
|
||||||
|
|
||||||
|
function uploadFiles(files: FileList) {
|
||||||
if (!props.isLoggedIn) {
|
if (!props.isLoggedIn) {
|
||||||
setLoginRedirectMessage("Whoa! You need to login to upload files");
|
setLoginRedirectMessage("Whoa! You need to login to upload files");
|
||||||
setShowLoginPrompt(true);
|
setShowLoginPrompt(true);
|
||||||
@@ -130,7 +144,7 @@ export default function ChatInputArea(props: ChatInputProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uploadDataForIndexing(
|
uploadDataForIndexing(
|
||||||
event.target.files,
|
files,
|
||||||
setWarning,
|
setWarning,
|
||||||
setUploading,
|
setUploading,
|
||||||
setError,
|
setError,
|
||||||
@@ -263,6 +277,16 @@ export default function ChatInputArea(props: ChatInputProps) {
|
|||||||
Math.max(chatInputRef.current.scrollHeight - 24, 64) + "px";
|
Math.max(chatInputRef.current.scrollHeight - 24, 64) + "px";
|
||||||
}, [message]);
|
}, [message]);
|
||||||
|
|
||||||
|
function handleDragOver(event: React.DragEvent<HTMLDivElement>) {
|
||||||
|
event.preventDefault();
|
||||||
|
setIsDragAndDropping(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDragLeave(event: React.DragEvent<HTMLDivElement>) {
|
||||||
|
event.preventDefault();
|
||||||
|
setIsDragAndDropping(false);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{showLoginPrompt && loginRedirectMessage && (
|
{showLoginPrompt && loginRedirectMessage && (
|
||||||
@@ -374,6 +398,9 @@ export default function ChatInputArea(props: ChatInputProps) {
|
|||||||
)}
|
)}
|
||||||
<div
|
<div
|
||||||
className={`${styles.actualInputArea} items-center justify-between dark:bg-neutral-700`}
|
className={`${styles.actualInputArea} items-center justify-between dark:bg-neutral-700`}
|
||||||
|
onDragOver={handleDragOver}
|
||||||
|
onDragLeave={handleDragLeave}
|
||||||
|
onDrop={handleDragAndDropFiles}
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
type="file"
|
type="file"
|
||||||
@@ -460,6 +487,7 @@ export default function ChatInputArea(props: ChatInputProps) {
|
|||||||
<ArrowUp className="w-6 h-6" weight="bold" />
|
<ArrowUp className="w-6 h-6" weight="bold" />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
{isDragAndDropping && <div className="text-muted-foreground">Drop file to upload</div>}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user