fix: memoize components and improve performance (#579)

This commit is contained in:
Jeremy 2024-12-03 17:49:38 +03:00 committed by GitHub
parent a13368cfcd
commit e839007580
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 453 additions and 232 deletions

View file

@ -17,6 +17,7 @@ import {
type Dispatch,
type SetStateAction,
type ChangeEvent,
memo,
} from 'react';
import { toast } from 'sonner';
import { useLocalStorage, useWindowSize } from 'usehooks-ts';
@ -27,21 +28,9 @@ import { ArrowUpIcon, PaperclipIcon, StopIcon } from './icons';
import { PreviewAttachment } from './preview-attachment';
import { Button } from './ui/button';
import { Textarea } from './ui/textarea';
import { SuggestedActions } from './suggested-actions';
const suggestedActions = [
{
title: 'What is the weather',
label: 'in San Francisco?',
action: 'What is the weather in San Francisco?',
},
{
title: 'Help me draft an essay',
label: 'about Silicon Valley',
action: 'Help me draft a short essay about Silicon Valley',
},
];
export function MultimodalInput({
function PureMultimodalInput({
chatId,
input,
setInput,
@ -201,36 +190,7 @@ export function MultimodalInput({
{messages.length === 0 &&
attachments.length === 0 &&
uploadQueue.length === 0 && (
<div className="grid sm:grid-cols-2 gap-2 w-full">
{suggestedActions.map((suggestedAction, index) => (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 20 }}
transition={{ delay: 0.05 * index }}
key={`suggested-action-${suggestedAction.title}-${index}`}
className={index > 1 ? 'hidden sm:block' : 'block'}
>
<Button
variant="ghost"
onClick={async () => {
window.history.replaceState({}, '', `/chat/${chatId}`);
append({
role: 'user',
content: suggestedAction.action,
});
}}
className="text-left border rounded-xl px-4 py-3.5 text-sm flex-1 gap-1 sm:flex-col w-full h-auto justify-start items-start"
>
<span className="font-medium">{suggestedAction.title}</span>
<span className="text-muted-foreground">
{suggestedAction.label}
</span>
</Button>
</motion.div>
))}
</div>
<SuggestedActions append={append} chatId={chatId} />
)}
<input
@ -324,3 +284,13 @@ export function MultimodalInput({
</div>
);
}
export const MultimodalInput = memo(
PureMultimodalInput,
(prevProps, currentProps) => {
if (prevProps.input !== currentProps.input) return false;
if (prevProps.isLoading !== currentProps.isLoading) return false;
return true;
},
);