Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: memoize components #607

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions components/block-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ function PureBlockActions({
}

export const BlockActions = memo(PureBlockActions, (prevProps, nextProps) => {
if (
prevProps.block.status === 'streaming' &&
nextProps.block.status === 'streaming'
) {
return true;
}
if (prevProps.block.status !== nextProps.block.status) return false;
if (prevProps.currentVersionIndex !== nextProps.currentVersionIndex)
return false;
if (prevProps.isCurrentVersion !== nextProps.isCurrentVersion) return false;

return false;
return true;
});
14 changes: 13 additions & 1 deletion components/message-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
TooltipProvider,
TooltipTrigger,
} from './ui/tooltip';
import { memo } from 'react';
import equal from 'fast-deep-equal';

export function MessageActions({
export function PureMessageActions({
chatId,
message,
vote,
Expand Down Expand Up @@ -164,3 +166,13 @@ export function MessageActions({
</TooltipProvider>
);
}

export const MessageActions = memo(
PureMessageActions,
(prevProps, nextProps) => {
if (!equal(prevProps.vote, nextProps.vote)) return false;
if (prevProps.isLoading !== nextProps.isLoading) return false;

return true;
},
);
11 changes: 9 additions & 2 deletions components/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,16 @@ export const PreviewMessage = memo(
PurePreviewMessage,
(prevProps, nextProps) => {
if (prevProps.isLoading !== nextProps.isLoading) return false;
if (prevProps.isLoading && nextProps.isLoading) return false;
if (prevProps.message.content && nextProps.message.content) return false;
if (prevProps.message.content !== nextProps.message.content) return false;
if (
!equal(
prevProps.message.toolInvocations,
nextProps.message.toolInvocations,
)
)
return false;
if (!equal(prevProps.vote, nextProps.vote)) return false;

return true;
},
);
Expand Down
19 changes: 8 additions & 11 deletions components/messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Overview } from './overview';
import { UIBlock } from './block';
import { Dispatch, memo, SetStateAction } from 'react';
import { Vote } from '@/lib/db/schema';
import equal from 'fast-deep-equal';

interface MessagesProps {
chatId: string;
Expand Down Expand Up @@ -74,15 +75,11 @@ function PureMessages({
);
}

function areEqual(prevProps: MessagesProps, nextProps: MessagesProps) {
if (
prevProps.block.status === 'streaming' &&
nextProps.block.status === 'streaming'
) {
return true;
}
export const Messages = memo(PureMessages, (prevProps, nextProps) => {
if (prevProps.isLoading !== nextProps.isLoading) return false;
if (prevProps.isLoading && nextProps.isLoading) return false;
if (prevProps.messages.length !== nextProps.messages.length) return false;
if (!equal(prevProps.votes, nextProps.votes)) return false;

return false;
}

export const Messages = memo(PureMessages, areEqual);
return true;
});
115 changes: 84 additions & 31 deletions components/multimodal-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
);
}
Expand All @@ -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;
});
5 changes: 4 additions & 1 deletion components/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,8 @@ const PureToolbar = ({
};

export const Toolbar = memo(PureToolbar, (prevProps, nextProps) => {
return equal(prevProps, nextProps);
if (prevProps.isLoading !== nextProps.isLoading) return false;
if (prevProps.isToolbarVisible !== nextProps.isToolbarVisible) return false;

return true;
});