chore: memoize components (#607)

This commit is contained in:
Jeremy 2024-12-10 17:54:10 +05:30 committed by GitHub
parent 906d7d34da
commit 3df0fd4c0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 123 additions and 53 deletions

View file

@ -247,40 +247,16 @@ function PureMultimodalInput({
/>
{isLoading ? (
<Button
className="rounded-full p-1.5 h-fit absolute bottom-2 right-2 m-0.5 border dark:border-zinc-600"
onClick={(event) => {
event.preventDefault();
stop();
setMessages((messages) => sanitizeUIMessages(messages));
}}
>
<StopIcon size={14} />
</Button>
<StopButton stop={stop} setMessages={setMessages} />
) : (
<Button
className="rounded-full p-1.5 h-fit absolute bottom-2 right-2 m-0.5 border dark:border-zinc-600"
onClick={(event) => {
event.preventDefault();
submitForm();
}}
disabled={input.length === 0 || uploadQueue.length > 0}
>
<ArrowUpIcon size={14} />
</Button>
<SendButton
input={input}
submitForm={submitForm}
uploadQueue={uploadQueue}
/>
)}
<Button
className="rounded-full p-1.5 h-fit absolute bottom-2 right-11 m-0.5 dark:border-zinc-700"
onClick={(event) => {
event.preventDefault();
fileInputRef.current?.click();
}}
variant="outline"
disabled={isLoading}
>
<PaperclipIcon size={14} />
</Button>
<AttachmentsButton fileInputRef={fileInputRef} isLoading={isLoading} />
</div>
);
}
@ -295,3 +271,80 @@ export const MultimodalInput = memo(
return true;
},
);
function PureAttachmentsButton({
fileInputRef,
isLoading,
}: {
fileInputRef: React.MutableRefObject<HTMLInputElement | null>;
isLoading: boolean;
}) {
return (
<Button
className="rounded-full p-1.5 h-fit absolute bottom-2 right-11 m-0.5 dark:border-zinc-700"
onClick={(event) => {
event.preventDefault();
fileInputRef.current?.click();
}}
variant="outline"
disabled={isLoading}
>
<PaperclipIcon size={14} />
</Button>
);
}
const AttachmentsButton = memo(PureAttachmentsButton);
function PureStopButton({
stop,
setMessages,
}: {
stop: () => void;
setMessages: Dispatch<SetStateAction<Array<Message>>>;
}) {
return (
<Button
className="rounded-full p-1.5 h-fit absolute bottom-2 right-2 m-0.5 border dark:border-zinc-600"
onClick={(event) => {
event.preventDefault();
stop();
setMessages((messages) => sanitizeUIMessages(messages));
}}
>
<StopIcon size={14} />
</Button>
);
}
const StopButton = memo(PureStopButton);
function PureSendButton({
submitForm,
input,
uploadQueue,
}: {
submitForm: () => void;
input: string;
uploadQueue: Array<string>;
}) {
return (
<Button
className="rounded-full p-1.5 h-fit absolute bottom-2 right-2 m-0.5 border dark:border-zinc-600"
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;
if (!prevProps.input !== !nextProps.input) return false;
return true;
});