Skip to content

The Open Standard for Generative UI

Add memo to message (#50)

abhithesys1cf6a089eb02
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index e23bf1d0d..309d298c3 100644
--- a/js/packages/react-ui/package.json
+++ b/js/packages/react-ui/package.json
@@ -2,7 +2,7 @@
"type": "module",
"name": "@crayonai/react-ui",
"license": "MIT",
- "version": "0.5.3",
+ "version": "0.5.4",
"description": "Component library for Generative UI SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
diff --git a/js/packages/react-ui/src/components/Shell/MobileHeader.tsx b/js/packages/react-ui/src/components/Shell/MobileHeader.tsx
index 8a8987a60..9629f3247 100644
--- a/js/packages/react-ui/src/components/Shell/MobileHeader.tsx
+++ b/js/packages/react-ui/src/components/Shell/MobileHeader.tsx
@@ -1,9 +1,10 @@
import { useThreadListActions } from "@crayonai/react-core";
+import clsx from "clsx";
import { Menu, Plus } from "lucide-react";
import { IconButton } from "../IconButton";
import { useShellStore } from "./store";
-export const MobileHeader = () => {
+export const MobileHeader = ({ className }: { className?: string }) => {
const { switchToNewThread } = useThreadListActions();
const { logoUrl, agentName, setIsSidebarOpen } = useShellStore((state) => ({
logoUrl: state.logoUrl,
@@ -12,7 +13,7 @@ export const MobileHeader = () => {
}));
return (
- <div className="crayon-shell-mobile-header">
+ <div className={clsx("crayon-shell-mobile-header", className)}>
<IconButton
size="medium"
icon={<Menu size="1em" />}
diff --git a/js/packages/react-ui/src/components/Shell/NewChatButton.tsx b/js/packages/react-ui/src/components/Shell/NewChatButton.tsx
index 3670befec..bf3d89a24 100644
--- a/js/packages/react-ui/src/components/Shell/NewChatButton.tsx
+++ b/js/packages/react-ui/src/components/Shell/NewChatButton.tsx
@@ -1,13 +1,14 @@
import { useThreadListActions } from "@crayonai/react-core";
+import clsx from "clsx";
import { Plus } from "lucide-react";
import { Button } from "../Button";
-export const NewChatButton = () => {
+export const NewChatButton = ({ className }: { className?: string }) => {
const { switchToNewThread } = useThreadListActions();
return (
<Button
- className="crayon-shell-new-chat-button"
+ className={clsx("crayon-shell-new-chat-button", className)}
iconRight={<Plus />}
onClick={switchToNewThread}
>
diff --git a/js/packages/react-ui/src/components/Shell/Sidebar.tsx b/js/packages/react-ui/src/components/Shell/Sidebar.tsx
index 61011696d..eb9bb0a4b 100644
--- a/js/packages/react-ui/src/components/Shell/Sidebar.tsx
+++ b/js/packages/react-ui/src/components/Shell/Sidebar.tsx
@@ -5,7 +5,13 @@ import { useLayoutContext } from "../../context/LayoutContext";
import { IconButton } from "../IconButton";
import { useShellStore } from "./store";
-export const SidebarContainer = ({ children }: { children?: React.ReactNode }) => {
+export const SidebarContainer = ({
+ children,
+ className,
+}: {
+ children?: React.ReactNode;
+ className?: string;
+}) => {
const { isSidebarOpen, setIsSidebarOpen } = useShellStore((state) => ({
isSidebarOpen: state.isSidebarOpen,
setIsSidebarOpen: state.setIsSidebarOpen,
@@ -35,9 +41,13 @@ export const SidebarContainer = ({ children }: { children?: React.ReactNode }) =
/>
)}
<div
- className={clsx("crayon-shell-sidebar-container", {
- "crayon-shell-sidebar-container--collapsed": !isSidebarOpen,
- })}
+ className={clsx(
+ "crayon-shell-sidebar-container",
+ {
+ "crayon-shell-sidebar-container--collapsed": !isSidebarOpen,
+ },
+ className,
+ )}
>
{children}
</div>
@@ -45,7 +55,7 @@ export const SidebarContainer = ({ children }: { children?: React.ReactNode }) =
);
};
-export const SidebarHeader = () => {
+export const SidebarHeader = ({ className }: { className?: string }) => {
const { agentName, logoUrl, setIsSidebarOpen, isSidebarOpen } = useShellStore((state) => ({
agentName: state.agentName,
logoUrl: state.logoUrl,
@@ -54,7 +64,7 @@ export const SidebarHeader = () => {
}));
return (
- <div className="crayon-shell-sidebar-header">
+ <div className={clsx("crayon-shell-sidebar-header", className)}>
<img src={logoUrl} alt={agentName} className="crayon-shell-sidebar-header__logo" />
<div className="crayon-shell-sidebar-header__agent-name">{agentName}</div>
<IconButton
@@ -70,7 +80,13 @@ export const SidebarHeader = () => {
);
};
-export const SidebarContent = ({ children }: { children?: React.ReactNode }) => {
+export const SidebarContent = ({
+ children,
+ className,
+}: {
+ children?: React.ReactNode;
+ className?: string;
+}) => {
const { isSidebarOpen } = useShellStore((state) => ({
isSidebarOpen: state.isSidebarOpen,
}));
@@ -79,7 +95,7 @@ export const SidebarContent = ({ children }: { children?: React.ReactNode }) =>
return null;
}
- return <div className="crayon-shell-sidebar-content">{children}</div>;
+ return <div className={clsx("crayon-shell-sidebar-content", className)}>{children}</div>;
};
export const SidebarSeparator = () => {
diff --git a/js/packages/react-ui/src/components/Shell/Thread.tsx b/js/packages/react-ui/src/components/Shell/Thread.tsx
index 47c07f54c..6658deb58 100644
--- a/js/packages/react-ui/src/components/Shell/Thread.tsx
+++ b/js/packages/react-ui/src/components/Shell/Thread.tsx
@@ -7,7 +7,7 @@ import {
} from "@crayonai/react-core";
import clsx from "clsx";
import { ArrowRight, Square } from "lucide-react";
-import React, { useLayoutEffect, useRef } from "react";
+import React, { memo, useLayoutEffect, useRef } from "react";
import { useComposerState } from "../../hooks/useComposerState";
import { useScrollToBottom } from "../../hooks/useScrollToBottom";
import { IconButton } from "../IconButton";
@@ -109,43 +109,45 @@ export const UserMessageContainer = ({
);
};
-export const RenderMessage = ({ message, className }: { message: Message; className?: string }) => {
- const responseTemplates = useThreadManagerSelector((store) => store.responseTemplates);
- const MessageContainer =
- message.role === "user" ? UserMessageContainer : AssistantMessageContainer;
-
- if (message.role === "assistant") {
- return (
- <MessageContainer className={className}>
- {message.message?.map((stringOrTemplate, i) => {
- if (stringOrTemplate.type === "text") {
- const TextRenderer = responseTemplates["text"]?.Component || DefaultTextRenderer;
-
- return (
- <TextRenderer key={i} className="crayon-shell-thread-message-assistant__text">
- {stringOrTemplate.text}
- </TextRenderer>
+export const RenderMessage = memo(
+ ({ message, className }: { message: Message; className?: string }) => {
+ const responseTemplates = useThreadManagerSelector((store) => store.responseTemplates);
+ const MessageContainer =
+ message.role === "user" ? UserMessageContainer : AssistantMessageContainer;
+
+ if (message.role === "assistant") {
+ return (
+ <MessageContainer className={className}>
+ {message.message?.map((stringOrTemplate, i) => {
+ if (stringOrTemplate.type === "text") {
+ const TextRenderer = responseTemplates["text"]?.Component || DefaultTextRenderer;
+
+ return (
+ <TextRenderer key={i} className="crayon-shell-thread-message-assistant__text">
+ {stringOrTemplate.text}
+ </TextRenderer>
+ );
+ }
+
+ const Template = responseTemplates[stringOrTemplate.name];
+ const Fallback = responseTemplates["fallback"]?.Component || FallbackTemplate;
+ return Template ? (
+ <Template.Component key={i} {...stringOrTemplate.templateProps} />
+ ) : (
+ <Fallback
+ key={i}
+ name={stringOrTemplate.name}
+ templateProps={stringOrTemplate.templateProps}
+ />
);
- }
-
- const Template = responseTemplates[stringOrTemplate.name];
- const Fallback = responseTemplates["fallback"]?.Component || FallbackTemplate;
- return Template ? (
- <Template.Component key={i} {...stringOrTemplate.templateProps} />
- ) : (
- <Fallback
- key={i}
- name={stringOrTemplate.name}
- templateProps={stringOrTemplate.templateProps}
- />
- );
- })}
- </MessageContainer>
- );
- }
-
- return <MessageContainer>{message.message}</MessageContainer>;
-};
+ })}
+ </MessageContainer>
+ );
+ }
+
+ return <MessageContainer>{message.message}</MessageContainer>;
+ },
+);
export const Messages = ({ className }: { className?: string }) => {
const { messages } = useThreadState();