Skip to content

Commit

Permalink
fix: make suggested actions reappear on new chat
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyphilemon committed Dec 20, 2024
1 parent f3e2aea commit d44db93
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions components/multimodal-input.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use client';
"use client";

import type {
Attachment,
ChatRequestOptions,
CreateMessage,
Message,
} from 'ai';
import cx from 'classnames';
import type { User } from 'next-auth';
import type React from 'react';
} from "ai";
import cx from "classnames";
import type { User } from "next-auth";
import type React from "react";
import {
useRef,
useEffect,
Expand All @@ -18,18 +18,18 @@ import {
type SetStateAction,
type ChangeEvent,
memo,
} from 'react';
import { toast } from 'sonner';
import { useLocalStorage, useWindowSize } from 'usehooks-ts';
} from "react";
import { toast } from "sonner";
import { useLocalStorage, useWindowSize } from "usehooks-ts";

import { sanitizeUIMessages } from '@/lib/utils';
import { sanitizeUIMessages } from "@/lib/utils";

import { ArrowUpIcon, ImageIcon, PaperclipIcon, StopIcon } from './icons';
import { PreviewAttachment } from './preview-attachment';
import { Button } from './ui/button';
import { Textarea } from './ui/textarea';
import { SuggestedActions } from './suggested-actions';
import equal from 'fast-deep-equal';
import { ArrowUpIcon, ImageIcon, PaperclipIcon, StopIcon } from "./icons";
import { PreviewAttachment } from "./preview-attachment";
import { Button } from "./ui/button";
import { Textarea } from "./ui/textarea";
import { SuggestedActions } from "./suggested-actions";
import equal from "fast-deep-equal";

function PureMultimodalInput({
chatId,
Expand Down Expand Up @@ -81,21 +81,21 @@ function PureMultimodalInput({

const adjustHeight = () => {
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
textareaRef.current.style.height = "auto";
textareaRef.current.style.height = `${textareaRef.current.scrollHeight + 2}px`;
}
};

const [localStorageInput, setLocalStorageInput] = useLocalStorage(
'input',
'',
"input",
"",
);

useEffect(() => {
if (textareaRef.current) {
const domValue = textareaRef.current.value;
// Prefer DOM value over localStorage to handle hydration
const finalValue = domValue || localStorageInput || '';
const finalValue = domValue || localStorageInput || "";
setInput(finalValue);
adjustHeight();
}
Expand All @@ -117,15 +117,15 @@ function PureMultimodalInput({

const submitForm = useCallback(() => {
if (user) {
window.history.replaceState({}, '', `/chat/${chatId}`);
window.history.replaceState({}, "", `/chat/${chatId}`);
}

handleSubmit(undefined, {
experimental_attachments: attachments,
});

setAttachments([]);
setLocalStorageInput('');
setLocalStorageInput("");

if (width && width > 768) {
textareaRef.current?.focus();
Expand All @@ -142,11 +142,11 @@ function PureMultimodalInput({

const uploadFile = async (file: File) => {
const formData = new FormData();
formData.append('file', file);
formData.append("file", file);

try {
const response = await fetch('/api/files/upload', {
method: 'POST',
const response = await fetch("/api/files/upload", {
method: "POST",
body: formData,
});

Expand All @@ -163,7 +163,7 @@ function PureMultimodalInput({
const { error } = await response.json();
toast.error(error);
} catch (error) {
toast.error('Failed to upload file, please try again!');
toast.error("Failed to upload file, please try again!");
}
};

Expand All @@ -185,7 +185,7 @@ function PureMultimodalInput({
...successfullyUploadedAttachments,
]);
} catch (error) {
console.error('Error uploading files!', error);
console.error("Error uploading files!", error);
} finally {
setUploadQueue([]);
}
Expand Down Expand Up @@ -220,9 +220,9 @@ function PureMultimodalInput({
<PreviewAttachment
key={filename}
attachment={{
url: '',
url: "",
name: filename,
contentType: '',
contentType: "",
}}
isUploading={true}
/>
Expand All @@ -236,17 +236,17 @@ function PureMultimodalInput({
value={input}
onChange={handleInput}
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',
"min-h-[24px] max-h-[calc(75dvh)] overflow-hidden resize-none rounded-2xl !text-base bg-muted pb-10 dark:border-zinc-700",
className,
)}
rows={2}
autoFocus
onKeyDown={(event) => {
if (event.key === 'Enter' && !event.shiftKey) {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();

if (isLoading) {
toast.error('Please wait for the model to finish its response!');
toast.error("Please wait for the model to finish its response!");
} else {
submitForm();
}
Expand Down Expand Up @@ -279,6 +279,7 @@ export const MultimodalInput = memo(
if (prevProps.input !== nextProps.input) return false;
if (prevProps.isLoading !== nextProps.isLoading) return false;
if (!equal(prevProps.attachments, nextProps.attachments)) return false;
if (prevProps.messages.length !== nextProps.messages.length) return false;

return true;
},
Expand Down

0 comments on commit d44db93

Please sign in to comment.