Skip to content

Commit

Permalink
Memoize further
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyphilemon committed Nov 27, 2024
1 parent 2deb633 commit 522835a
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 67 deletions.
2 changes: 1 addition & 1 deletion components/block-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface BlockActionsProps {
mode: 'read-only' | 'edit' | 'diff';
}

export function PureBlockActions({
function PureBlockActions({
block,
handleVersionChange,
currentVersionIndex,
Expand Down
2 changes: 1 addition & 1 deletion components/block-close-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface BlockCloseButtonProps {
setBlock: (value: SetStateAction<UIBlock>) => void;
}

export function PureBlockCloseButton({ setBlock }: BlockCloseButtonProps) {
function PureBlockCloseButton({ setBlock }: BlockCloseButtonProps) {
return (
<Button
variant="outline"
Expand Down
71 changes: 71 additions & 0 deletions components/block-messages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Dispatch, memo, SetStateAction } from 'react';
import { UIBlock } from './block';
import { PreviewMessage } from './message';
import { useScrollToBottom } from './use-scroll-to-bottom';
import { Vote } from '@/lib/db/schema';
import { Message } from 'ai';

interface BlockMessagesProps {
chatId: string;
block: UIBlock;
setBlock: Dispatch<SetStateAction<UIBlock>>;
isLoading: boolean;
votes: Array<Vote> | undefined;
messages: Array<Message>;
}

function PureBlockMessages({
chatId,
block,
setBlock,
isLoading,
votes,
messages,
}: BlockMessagesProps) {
const [messagesContainerRef, messagesEndRef] =
useScrollToBottom<HTMLDivElement>();

return (
<div
ref={messagesContainerRef}
className="flex flex-col gap-4 h-full items-center overflow-y-scroll px-4 pt-20"
>
{messages.map((message, index) => (
<PreviewMessage
chatId={chatId}
key={message.id}
message={message}
block={block}
setBlock={setBlock}
isLoading={isLoading && index === messages.length - 1}
vote={
votes
? votes.find((vote) => vote.messageId === message.id)
: undefined
}
/>
))}

<div
ref={messagesEndRef}
className="shrink-0 min-w-[24px] min-h-[24px]"
/>
</div>
);
}

function areEqual(
prevProps: BlockMessagesProps,
nextProps: BlockMessagesProps,
) {
if (
prevProps.block.status === 'streaming' &&
nextProps.block.status === 'streaming'
) {
return true;
}

return false;
}

export const BlockMessages = memo(PureBlockMessages, areEqual);
2 changes: 1 addition & 1 deletion components/block-stream-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface BlockStreamHandlerProps {
streamingData: JSONValue[] | undefined;
}

export function PureBlockStreamHandler({
function PureBlockStreamHandler({
setBlock,
streamingData,
}: BlockStreamHandlerProps) {
Expand Down
54 changes: 12 additions & 42 deletions components/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
CreateMessage,
Message,
} from 'ai';
import cx from 'classnames';
import { formatDistance } from 'date-fns';
import { AnimatePresence, motion } from 'framer-motion';
import {
Expand All @@ -15,31 +14,22 @@ import {
useEffect,
useState,
} from 'react';
import { toast } from 'sonner';
import useSWR, { useSWRConfig } from 'swr';
import {
useCopyToClipboard,
useDebounceCallback,
useWindowSize,
} from 'usehooks-ts';
import { useDebounceCallback, useWindowSize } from 'usehooks-ts';

import type { Document, Suggestion, Vote } from '@/lib/db/schema';
import { fetcher } from '@/lib/utils';

import { DiffView } from './diffview';
import { DocumentSkeleton } from './document-skeleton';
import { Editor } from './editor';
import { CopyIcon, CrossIcon, DeltaIcon, RedoIcon, UndoIcon } from './icons';
import { PreviewMessage } from './message';
import { MultimodalInput } from './multimodal-input';
import { Toolbar } from './toolbar';
import { Button } from './ui/button';
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
import { useScrollToBottom } from './use-scroll-to-bottom';
import { VersionFooter } from './version-footer';
import { Markdown } from './markdown';
import { BlockActions } from './block-actions';
import { BlockCloseButton } from './block-close-button';
import { BlockMessages } from './block-messages';

export interface UIBlock {
title: string;
documentId: string;
Expand All @@ -54,7 +44,7 @@ export interface UIBlock {
};
}

export function PureBlock({
function PureBlock({
chatId,
input,
setInput,
Expand Down Expand Up @@ -93,9 +83,6 @@ export function PureBlock({
chatRequestOptions?: ChatRequestOptions,
) => void;
}) {
const [messagesContainerRef, messagesEndRef] =
useScrollToBottom<HTMLDivElement>();

const {
data: documents,
isLoading: isDocumentsFetching,
Expand Down Expand Up @@ -292,31 +279,14 @@ export function PureBlock({
</AnimatePresence>

<div className="flex flex-col h-full justify-between items-center gap-4">
<div
ref={messagesContainerRef}
className="flex flex-col gap-4 h-full items-center overflow-y-scroll px-4 pt-20"
>
{messages.map((message, index) => (
<PreviewMessage
chatId={chatId}
key={message.id}
message={message}
block={block}
setBlock={setBlock}
isLoading={isLoading && index === messages.length - 1}
vote={
votes
? votes.find((vote) => vote.messageId === message.id)
: undefined
}
/>
))}

<div
ref={messagesEndRef}
className="shrink-0 min-w-[24px] min-h-[24px]"
/>
</div>
<BlockMessages
chatId={chatId}
block={block}
isLoading={isLoading}
setBlock={setBlock}
votes={votes}
messages={messages}
/>

<form className="flex flex-row gap-2 relative items-end w-full px-4 pb-4">
<MultimodalInput
Expand Down
6 changes: 1 addition & 5 deletions components/chat-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import { PlusIcon, VercelIcon } from './icons';
import { useSidebar } from './ui/sidebar';
import { memo } from 'react';

export function PureChatHeader({
selectedModelId,
}: {
selectedModelId: string;
}) {
function PureChatHeader({ selectedModelId }: { selectedModelId: string }) {
const router = useRouter();
const { open } = useSidebar();

Expand Down
14 changes: 7 additions & 7 deletions components/document.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SetStateAction } from 'react';
import { memo, type SetStateAction } from 'react';

import type { UIBlock } from './block';
import { FileIcon, LoaderIcon, MessageIcon, PencilEditIcon } from './icons';
Expand Down Expand Up @@ -28,7 +28,7 @@ interface DocumentToolResultProps {
setBlock: (value: SetStateAction<UIBlock>) => void;
}

export function DocumentToolResult({
function PureDocumentToolResult({
type,
result,
setBlock,
Expand Down Expand Up @@ -73,17 +73,15 @@ export function DocumentToolResult({
);
}

export const DocumentToolResult = memo(PureDocumentToolResult, () => true);

interface DocumentToolCallProps {
type: 'create' | 'update' | 'request-suggestions';
args: { title: string };
setBlock: (value: SetStateAction<UIBlock>) => void;
}

export function DocumentToolCall({
type,
args,
setBlock,
}: DocumentToolCallProps) {
function PureDocumentToolCall({ type, args, setBlock }: DocumentToolCallProps) {
return (
<button
type="button"
Expand Down Expand Up @@ -125,3 +123,5 @@ export function DocumentToolCall({
</button>
);
}

export const DocumentToolCall = memo(PureDocumentToolCall, () => true);
3 changes: 2 additions & 1 deletion components/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { PreviewAttachment } from './preview-attachment';
import { Weather } from './weather';
import equal from 'fast-deep-equal';

export const PurePreviewMessage = ({
const PurePreviewMessage = ({
chatId,
message,
block,
Expand Down Expand Up @@ -158,6 +158,7 @@ export const PreviewMessage = memo(
(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;
},
);
Expand Down
3 changes: 1 addition & 2 deletions components/messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 All @@ -16,7 +15,7 @@ interface MessagesProps {
messages: Array<Message>;
}

export function PureMessages({
function PureMessages({
chatId,
block,
setBlock,
Expand Down
2 changes: 1 addition & 1 deletion components/multimodal-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Button } from './ui/button';
import { Textarea } from './ui/textarea';
import { SuggestedActions } from './suggested-actions';

export function PureMultimodalInput({
function PureMultimodalInput({
chatId,
input,
setInput,
Expand Down
5 changes: 1 addition & 4 deletions components/suggested-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ interface SuggestedActionsProps {
) => Promise<string | null | undefined>;
}

export function PureSuggestedActions({
chatId,
append,
}: SuggestedActionsProps) {
function PureSuggestedActions({ chatId, append }: SuggestedActionsProps) {
const suggestedActions = [
{
title: 'What is the weather',
Expand Down
2 changes: 1 addition & 1 deletion components/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export const Tools = ({
);
};

export const PureToolbar = ({
const PureToolbar = ({
isToolbarVisible,
setIsToolbarVisible,
append,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"db:push": "drizzle-kit push",
"db:pull": "drizzle-kit pull",
"db:check": "drizzle-kit check",
"db:up": "drizzle-kit up"
"db:up": "drizzle-kit up",
"scan": "next dev & npx react-scan@latest localhost:3000"
},
"dependencies": {
"@ai-sdk/openai": "1.0.0-canary.3",
Expand Down

0 comments on commit 522835a

Please sign in to comment.