chatbot-template/components/multimodal-input.tsx

396 lines
10 KiB
TypeScript
Raw Normal View History

'use client';
2024-10-11 18:00:22 +05:30
2025-04-03 01:05:07 -07:00
import type { Attachment, UIMessage } from 'ai';
2024-10-30 16:01:24 +05:30
import cx from 'classnames';
2024-11-15 12:18:17 -05:00
import type React from 'react';
import {
2024-10-11 18:00:22 +05:30
useRef,
useEffect,
useState,
useCallback,
2024-11-15 12:18:17 -05:00
type Dispatch,
type SetStateAction,
type ChangeEvent,
memo,
} from 'react';
import { toast } from 'sonner';
import { useLocalStorage, useWindowSize } from 'usehooks-ts';
2024-10-11 18:00:22 +05:30
2024-12-20 23:07:23 +05:30
import { ArrowUpIcon, PaperclipIcon, StopIcon } from './icons';
import { PreviewAttachment } from './preview-attachment';
2024-11-15 10:14:25 -05:00
import { Button } from './ui/button';
import { Textarea } from './ui/textarea';
import { SuggestedActions } from './suggested-actions';
import equal from 'fast-deep-equal';
2025-04-03 01:05:07 -07:00
import type { UseChatHelpers } from '@ai-sdk/react';
import { AnimatePresence, motion } from 'framer-motion';
import { ArrowDown } from 'lucide-react';
import { useScrollToBottom } from '@/hooks/use-scroll-to-bottom';
2024-10-11 18:00:22 +05:30
function PureMultimodalInput({
2024-11-05 17:15:51 +03:00
chatId,
2024-10-11 18:00:22 +05:30
input,
setInput,
status,
2024-10-11 18:00:22 +05:30
stop,
attachments,
setAttachments,
messages,
2024-10-30 16:01:24 +05:30
setMessages,
2024-10-11 18:00:22 +05:30
append,
handleSubmit,
2024-10-30 16:01:24 +05:30
className,
2024-10-11 18:00:22 +05:30
}: {
2024-11-05 17:15:51 +03:00
chatId: string;
input: UseChatHelpers['input'];
setInput: UseChatHelpers['setInput'];
status: UseChatHelpers['status'];
2024-10-11 18:00:22 +05:30
stop: () => void;
attachments: Array<Attachment>;
setAttachments: Dispatch<SetStateAction<Array<Attachment>>>;
2025-03-20 14:10:45 -07:00
messages: Array<UIMessage>;
setMessages: UseChatHelpers['setMessages'];
append: UseChatHelpers['append'];
handleSubmit: UseChatHelpers['handleSubmit'];
2024-10-30 16:01:24 +05:30
className?: string;
2024-10-11 18:00:22 +05:30
}) {
const textareaRef = useRef<HTMLTextAreaElement>(null);
const { width } = useWindowSize();
2024-10-11 18:00:22 +05:30
useEffect(() => {
if (textareaRef.current) {
adjustHeight();
}
}, []);
const adjustHeight = () => {
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
2024-10-11 18:00:22 +05:30
textareaRef.current.style.height = `${textareaRef.current.scrollHeight + 2}px`;
}
};
const resetHeight = () => {
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
textareaRef.current.style.height = '98px';
}
};
const [localStorageInput, setLocalStorageInput] = useLocalStorage(
'input',
2024-11-15 13:00:15 -05:00
'',
);
useEffect(() => {
if (textareaRef.current) {
const domValue = textareaRef.current.value;
// Prefer DOM value over localStorage to handle hydration
const finalValue = domValue || localStorageInput || '';
setInput(finalValue);
adjustHeight();
}
// Only run once after hydration
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
setLocalStorageInput(input);
}, [input, setLocalStorageInput]);
2024-10-11 18:00:22 +05:30
const handleInput = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
setInput(event.target.value);
adjustHeight();
};
const fileInputRef = useRef<HTMLInputElement>(null);
const [uploadQueue, setUploadQueue] = useState<Array<string>>([]);
const submitForm = useCallback(() => {
2024-11-05 17:15:51 +03:00
window.history.replaceState({}, '', `/chat/${chatId}`);
2024-10-11 18:00:22 +05:30
handleSubmit(undefined, {
experimental_attachments: attachments,
});
setAttachments([]);
setLocalStorageInput('');
resetHeight();
if (width && width > 768) {
textareaRef.current?.focus();
}
2024-11-05 17:15:51 +03:00
}, [
attachments,
handleSubmit,
setAttachments,
setLocalStorageInput,
width,
chatId,
]);
2024-10-11 18:00:22 +05:30
const uploadFile = async (file: File) => {
const formData = new FormData();
formData.append('file', file);
2024-10-11 18:00:22 +05:30
try {
2024-11-15 12:18:17 -05:00
const response = await fetch('/api/files/upload', {
method: 'POST',
2024-10-11 18:00:22 +05:30
body: formData,
});
if (response.ok) {
const data = await response.json();
const { url, pathname, contentType } = data;
return {
url,
name: pathname,
contentType: contentType,
};
}
2024-11-15 12:18:17 -05:00
const { error } = await response.json();
toast.error(error);
2024-10-11 18:00:22 +05:30
} catch (error) {
toast.error('Failed to upload file, please try again!');
2024-10-11 18:00:22 +05:30
}
};
const handleFileChange = useCallback(
async (event: ChangeEvent<HTMLInputElement>) => {
const files = Array.from(event.target.files || []);
setUploadQueue(files.map((file) => file.name));
try {
const uploadPromises = files.map((file) => uploadFile(file));
const uploadedAttachments = await Promise.all(uploadPromises);
const successfullyUploadedAttachments = uploadedAttachments.filter(
2024-11-15 13:00:15 -05:00
(attachment) => attachment !== undefined,
2024-10-11 18:00:22 +05:30
);
setAttachments((currentAttachments) => [
...currentAttachments,
...successfullyUploadedAttachments,
]);
} catch (error) {
console.error('Error uploading files!', error);
2024-10-11 18:00:22 +05:30
} finally {
setUploadQueue([]);
}
},
2024-11-15 13:00:15 -05:00
[setAttachments],
2024-10-11 18:00:22 +05:30
);
const { isAtBottom, scrollToBottom } = useScrollToBottom();
useEffect(() => {
if (status === 'submitted') {
scrollToBottom();
}
}, [status, scrollToBottom]);
2024-10-11 18:00:22 +05:30
return (
<div className="relative w-full flex flex-col gap-4">
<AnimatePresence>
{!isAtBottom && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 10 }}
transition={{ type: 'spring', stiffness: 300, damping: 20 }}
className="absolute left-1/2 bottom-28 -translate-x-1/2 z-50"
>
<Button
data-testid="scroll-to-bottom-button"
className="rounded-full"
size="icon"
variant="outline"
onClick={(event) => {
event.preventDefault();
scrollToBottom();
}}
>
<ArrowDown />
</Button>
</motion.div>
)}
</AnimatePresence>
2024-10-11 18:00:22 +05:30
{messages.length === 0 &&
attachments.length === 0 &&
uploadQueue.length === 0 && (
<SuggestedActions append={append} chatId={chatId} />
2024-10-11 18:00:22 +05:30
)}
<input
type="file"
className="fixed -top-4 -left-4 size-0.5 opacity-0 pointer-events-none"
ref={fileInputRef}
multiple
onChange={handleFileChange}
tabIndex={-1}
/>
{(attachments.length > 0 || uploadQueue.length > 0) && (
2025-03-04 17:25:46 -08:00
<div
data-testid="attachments-preview"
className="flex flex-row gap-2 overflow-x-scroll items-end"
>
2024-10-11 18:00:22 +05:30
{attachments.map((attachment) => (
<PreviewAttachment key={attachment.url} attachment={attachment} />
))}
{uploadQueue.map((filename) => (
<PreviewAttachment
key={filename}
attachment={{
url: '',
2024-10-11 18:00:22 +05:30
name: filename,
contentType: '',
2024-10-11 18:00:22 +05:30
}}
isUploading={true}
/>
))}
</div>
)}
<Textarea
2025-03-04 17:25:46 -08:00
data-testid="multimodal-input"
2024-10-11 18:00:22 +05:30
ref={textareaRef}
placeholder="Send a message..."
value={input}
onChange={handleInput}
2024-10-30 16:01:24 +05:30
className={cx(
'min-h-[24px] max-h-[calc(75dvh)] overflow-hidden resize-none rounded-2xl !text-base bg-muted pb-10 dark:border-zinc-700',
2024-11-15 13:00:15 -05:00
className,
2024-10-30 16:01:24 +05:30
)}
rows={2}
2024-11-05 11:54:57 +03:00
autoFocus
2024-10-11 18:00:22 +05:30
onKeyDown={(event) => {
2025-03-05 12:46:07 -08:00
if (
2025-03-11 14:39:36 -07:00
event.key === 'Enter' &&
2025-03-05 12:46:07 -08:00
!event.shiftKey &&
!event.nativeEvent.isComposing
) {
2024-10-11 18:00:22 +05:30
event.preventDefault();
if (status !== 'ready') {
toast.error('Please wait for the model to finish its response!');
2024-10-11 18:00:22 +05:30
} else {
submitForm();
}
}
}}
/>
<div className="absolute bottom-0 p-2 w-fit flex flex-row justify-start">
<AttachmentsButton fileInputRef={fileInputRef} status={status} />
</div>
<div className="absolute bottom-0 right-0 p-2 w-fit flex flex-row justify-end">
{status === 'submitted' ? (
<StopButton stop={stop} setMessages={setMessages} />
) : (
<SendButton
input={input}
submitForm={submitForm}
uploadQueue={uploadQueue}
/>
)}
</div>
2024-10-11 18:00:22 +05:30
</div>
);
}
export const MultimodalInput = memo(
PureMultimodalInput,
(prevProps, nextProps) => {
if (prevProps.input !== nextProps.input) return false;
if (prevProps.status !== nextProps.status) return false;
if (!equal(prevProps.attachments, nextProps.attachments)) return false;
return true;
},
);
2024-12-10 17:54:10 +05:30
function PureAttachmentsButton({
fileInputRef,
status,
2024-12-10 17:54:10 +05:30
}: {
fileInputRef: React.MutableRefObject<HTMLInputElement | null>;
status: UseChatHelpers['status'];
2024-12-10 17:54:10 +05:30
}) {
return (
<Button
2025-03-04 17:25:46 -08:00
data-testid="attachments-button"
className="rounded-md rounded-bl-lg p-[7px] h-fit dark:border-zinc-700 hover:dark:bg-zinc-900 hover:bg-zinc-200"
2024-12-10 17:54:10 +05:30
onClick={(event) => {
event.preventDefault();
fileInputRef.current?.click();
}}
disabled={status !== 'ready'}
variant="ghost"
2024-12-10 17:54:10 +05:30
>
2024-12-20 23:07:23 +05:30
<PaperclipIcon size={14} />
2024-12-10 17:54:10 +05:30
</Button>
);
}
const AttachmentsButton = memo(PureAttachmentsButton);
function PureStopButton({
stop,
setMessages,
}: {
stop: () => void;
2025-03-20 14:10:45 -07:00
setMessages: UseChatHelpers['setMessages'];
2024-12-10 17:54:10 +05:30
}) {
return (
<Button
2025-03-04 17:25:46 -08:00
data-testid="stop-button"
className="rounded-full p-1.5 h-fit border dark:border-zinc-600"
2024-12-10 17:54:10 +05:30
onClick={(event) => {
event.preventDefault();
stop();
setMessages((messages) => messages);
2024-12-10 17:54:10 +05:30
}}
>
<StopIcon size={14} />
</Button>
);
}
const StopButton = memo(PureStopButton);
function PureSendButton({
submitForm,
input,
uploadQueue,
}: {
submitForm: () => void;
input: string;
uploadQueue: Array<string>;
}) {
return (
<Button
2025-03-04 17:25:46 -08:00
data-testid="send-button"
className="rounded-full p-1.5 h-fit border dark:border-zinc-600"
2024-12-10 17:54:10 +05:30
onClick={(event) => {
event.preventDefault();
submitForm();
}}
disabled={input.length === 0 || uploadQueue.length > 0}
>
<ArrowUpIcon size={14} />
</Button>
);
}
const SendButton = memo(PureSendButton, (prevProps, nextProps) => {
if (prevProps.uploadQueue.length !== nextProps.uploadQueue.length)
return false;
2024-12-20 23:07:23 +05:30
if (prevProps.input !== nextProps.input) return false;
2024-12-10 17:54:10 +05:30
return true;
});