Skip to content

Commit

Permalink
fix input lag when the messages grow larger by using memo for Message…
Browse files Browse the repository at this point in the history
…Item object
  • Loading branch information
zombierantcasey committed Mar 19, 2024
1 parent 86843e6 commit be8397d
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 273 deletions.
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@types/lodash": "^4.14.191",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@types/react-window": "^1.8.8",
"buffer": "^5.7.1",
"parcel": "^2.8.3",
"parcel-reporter-static-files-copy": "^1.5.0",
Expand Down Expand Up @@ -53,6 +54,7 @@
"react-dom": "^18.2.0",
"react-icons": "^4.8.0",
"react-markdown": "^8.0.6",
"react-window": "^1.8.10",
"remark-gfm": "^3.0.1",
"typescript": "4.9.5"
}
Expand Down
69 changes: 33 additions & 36 deletions src/components/MessageItem.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { memo, useMemo } from 'react';
import {
ActionIcon,
Box,
Expand All @@ -9,61 +10,61 @@ import {
Text,
ThemeIcon,
Tooltip,
} from "@mantine/core";
import { Prism } from "@mantine/prism";
import { IconCopy, IconUser } from "@tabler/icons-react";
import { useMemo } from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { Message } from "../db";
import "../styles/markdown.scss";
import { CreatePromptModal } from "./CreatePromptModal";
import { LogoIcon } from "./Logo";
import { ScrollIntoView } from "./ScrollIntoView";
import "../utils/prisma-setup";
} from '@mantine/core';
import { Prism } from '@mantine/prism';
import { IconCopy, IconUser } from '@tabler/icons-react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { Message } from '../db';
import '../styles/markdown.scss';
import { CreatePromptModal } from './CreatePromptModal';
import { LogoIcon } from './Logo';
import { ScrollIntoView } from './ScrollIntoView';
import '../utils/prisma-setup';

export function MessageItem({ message }: { message: Message }) {
export const MessageItem = memo(({ message }: { message: Message }) => {
const wordCount = useMemo(() => {
var matches = message.content.match(/[\w\d\’\'-\(\)]+/gi);
const matches = message.content.match(/[\w\d\’\'-\(\)]+/gi);
return matches ? matches.length : 0;
}, [message.content]);

return (
<ScrollIntoView>
<Card withBorder>
<Flex gap="sm">
{message.role === "user" && (
{message.role === 'user' && (
<ThemeIcon color="gray" size="lg">
<IconUser size={20} />
</ThemeIcon>
)}
{message.role === "assistant" && <LogoIcon style={{ height: 32 }} />}
{message.role === 'assistant' && <LogoIcon style={{ height: 32 }} />}
<Box sx={{ flex: 1, width: 0 }} className="markdown">
<ReactMarkdown
children={message.content}
remarkPlugins={[remarkGfm]}
components={{
table: ({ node, ...props }) => (
table: (props) => (
<Table verticalSpacing="sm" highlightOnHover {...props} />
),
code: ({ node, inline, className, lang, ...props }) => {
const languageMatch = /language-(\w+)/.exec(className || "");
const language = languageMatch ? languageMatch[1] : undefined;

code: ({ node, inline, className, ...props }) => {
const languageMatch = /language-(\w+)/.exec(className || '');
const language = languageMatch ? languageMatch[1] : '';
return inline ? (
<Code {...props} />
) : (
<Box sx={{ position: "relative" }}>
<Prism
language={language as any}
children={`${props.children as string}`}
/>
</Box>
<Prism
language={language as any}
>
{String(props.children)}
</Prism>
</Box>
);
},
}}
/>
{message.role === "assistant" && (
>
{message.content}
</ReactMarkdown>
{message.role === 'assistant' && (
<Box>
<Text size="sm" color="dimmed">
{wordCount} words
Expand All @@ -75,21 +76,17 @@ export function MessageItem({ message }: { message: Message }) {
<CreatePromptModal content={message.content} />
<CopyButton value={message.content}>
{({ copied, copy }) => (
<Tooltip label={copied ? "Copied" : "Copy"} position="left">
<Tooltip label={copied ? 'Copied' : 'Copy'} position="left">
<ActionIcon onClick={copy}>
<IconCopy opacity={0.5} size={20} />
</ActionIcon>
</Tooltip>
)}
</CopyButton>
{/* <Tooltip label={`${wordCount} words`} position="left">
<ActionIcon>
<IconInfoCircle opacity={0.5} size={20} />
</ActionIcon>
</Tooltip> */}
</Box>
</Flex>
</Card>
</ScrollIntoView>
);
}
});

4 changes: 2 additions & 2 deletions src/routes/ChatRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function ChatRoute() {
`}</style>
</div>
);
};
};

const submit = async () => {
if (submitting) return;
Expand Down Expand Up @@ -320,7 +320,7 @@ export function ChatRoute() {
})}
data={[
{ label: 'GPT-3.5', value: 'gpt-3.5-turbo' },
{ label: 'GPT-4', value: 'gpt-4' }
{ label: 'GPT-4', value: 'gpt-4-1106-preview' }
]}
onChange={async (value: 'gpt-3.5-turbo' | 'gpt-4') => {
const model = value;
Expand Down
Loading

0 comments on commit be8397d

Please sign in to comment.