Skip to content

Commit

Permalink
Merge pull request #14 from e-roy:remove-memos-and-callback-hooks
Browse files Browse the repository at this point in the history
remove memos and callback hooks
  • Loading branch information
e-roy authored Dec 31, 2023
2 parents 2a5a09d + 3836059 commit f5e4904
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 71 deletions.
29 changes: 6 additions & 23 deletions src/components/ChatContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
// components/ChatContainer.tsx
import React, { useRef, useEffect, FormEvent, useCallback } from "react";
import React, { useRef, useEffect, useCallback } from "react";
import { Message, useChat } from "ai/react";
import { Card } from "./ui/card";

Expand Down Expand Up @@ -33,26 +33,9 @@ export const ChatContainer = () => {

const messagesEndRef = useRef<HTMLDivElement>(null);

const scrollToBottom = useCallback(() => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, []);

useEffect(() => {
scrollToBottom();
}, [messages, scrollToBottom]);

const handleFormSubmit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
handleSubmit(event);
};

const handleClearChat = useCallback(() => {
setMessages([]);
}, [setMessages]);

const handleRefreshMessage = useCallback(() => {
reload();
}, [reload]);
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messages]);

const messagesRef = useRef<Message[]>(messages);

Expand All @@ -79,7 +62,7 @@ export const ChatContainer = () => {
variant={`secondary`}
type={`button`}
size={`sm`}
onClick={handleClearChat}
onClick={() => setMessages([])}
>
<MessageCircleX className={`mr-2`} /> Clear chat history
</Button>
Expand All @@ -92,7 +75,7 @@ export const ChatContainer = () => {
message={message}
isLastMessage={messages.length === index + 1}
isLoading={isLoading}
onRefresh={handleRefreshMessage}
onRefresh={reload}
onRemove={() => handleRemoveMessage(message.id)}
/>
))}
Expand All @@ -105,7 +88,7 @@ export const ChatContainer = () => {
placeholder="Chat with Gemini Pro"
loading={isLoading}
onInputChange={handleInputChange}
onFormSubmit={handleFormSubmit}
onFormSubmit={handleSubmit}
isSubmittable={input.trim() !== ""}
/>
</Card>
Expand Down
78 changes: 30 additions & 48 deletions src/components/markdown-viewer/MarkdownViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"use client";
// components/MarkdownViewer.tsx
import React, { useMemo } from "react";
import React from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import rehypeRaw from "rehype-raw";
import rehypeSanitize from "rehype-sanitize";

import { UlComponent, OlComponent, LiComponent } from "./list";

import { PreComponent, CodeBlock } from "./code";

import {
TableComponent,
TheadComponent,
Expand Down Expand Up @@ -37,56 +35,40 @@ const Anchor: React.FC<AnchorProps> = ({ href, children }) => {
);
};

const MemoizedAnchor = React.memo(Anchor);

const MemoizedUlComponent = React.memo(UlComponent);
const MemoizedOlComponent = React.memo(OlComponent);
const MemoizedLiComponent = React.memo(LiComponent);

const MemoizedPreComponent = React.memo(PreComponent);
const MemoizedCodeBlock = React.memo(CodeBlock);

const MemoizedTableComponent = React.memo(TableComponent);
const MemoizedTheadComponent = React.memo(TheadComponent);
const MemoizedTbodyComponent = React.memo(TbodyComponent);
const MemoizedTrComponent = React.memo(TrComponent);
const MemoizedThComponent = React.memo(ThComponent);
const MemoizedTdComponent = React.memo(TdComponent);

interface IMarkdownViewerProps {
text: string;
}

export const MarkdownViewer: React.FC<IMarkdownViewerProps> = ({ text }) => {
const components = useMemo(
() => ({
a: MemoizedAnchor,
export const MarkdownViewer: React.FC<IMarkdownViewerProps> = React.memo(
function MarkdownViewer({ text }) {
const components = {
a: Anchor,

ul: MemoizedUlComponent,
ol: MemoizedOlComponent,
li: MemoizedLiComponent,
ul: UlComponent,
ol: OlComponent,
li: LiComponent,

pre: MemoizedPreComponent,
code: MemoizedCodeBlock,
pre: PreComponent,
code: CodeBlock,

table: MemoizedTableComponent,
thead: MemoizedTheadComponent,
tbody: MemoizedTbodyComponent,
tr: MemoizedTrComponent,
th: MemoizedThComponent,
td: MemoizedTdComponent,
}),
[]
);
table: TableComponent,
thead: TheadComponent,
tbody: TbodyComponent,
tr: TrComponent,
th: ThComponent,
td: TdComponent,
};

return (
<ReactMarkdown
// @ts-ignore
components={components}
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw, rehypeSanitize]}
>
{text}
</ReactMarkdown>
);
};
return (
<ReactMarkdown
// @ts-ignore
components={components}
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw, rehypeSanitize]}
>
{text}
</ReactMarkdown>
);
},
(prevProps, nextProps) => prevProps.text === nextProps.text
);

0 comments on commit f5e4904

Please sign in to comment.