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

@ -3,7 +3,7 @@
import type { Message } from 'ai';
import cx from 'classnames';
import { motion } from 'framer-motion';
import type { Dispatch, SetStateAction } from 'react';
import { memo, type Dispatch, type SetStateAction } from 'react';
import type { Vote } from '@/lib/db/schema';
@ -14,8 +14,9 @@ import { Markdown } from './markdown';
import { MessageActions } from './message-actions';
import { PreviewAttachment } from './preview-attachment';
import { Weather } from './weather';
import equal from 'fast-deep-equal';
export const PreviewMessage = ({
const PurePreviewMessage = ({
chatId,
message,
block,
@ -152,6 +153,16 @@ export const PreviewMessage = ({
);
};
export const PreviewMessage = memo(
PurePreviewMessage,
(prevProps, nextProps) => {
if (prevProps.isLoading !== nextProps.isLoading) return false;
if (prevProps.isLoading && nextProps.isLoading) return false;
if (!equal(prevProps.vote, nextProps.vote)) return false;
return true;
},
);
export const ThinkingMessage = () => {
const role = 'assistant';