Cleanup
This commit is contained in:
@@ -18,23 +18,6 @@ import { SquarePen } from "lucide-react";
|
|||||||
import { StringParam, useQueryParam } from "use-query-params";
|
import { StringParam, useQueryParam } from "use-query-params";
|
||||||
import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
|
import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
|
||||||
|
|
||||||
function NewThread() {
|
|
||||||
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
|
|
||||||
if (!threadId) return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<TooltipIconButton
|
|
||||||
size="lg"
|
|
||||||
className="p-4"
|
|
||||||
tooltip="New thread"
|
|
||||||
variant="ghost"
|
|
||||||
onClick={() => setThreadId(null)}
|
|
||||||
>
|
|
||||||
<SquarePen className="size-5" />
|
|
||||||
</TooltipIconButton>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function StickyToBottomContent(props: {
|
function StickyToBottomContent(props: {
|
||||||
content: ReactNode;
|
content: ReactNode;
|
||||||
footer?: ReactNode;
|
footer?: ReactNode;
|
||||||
@@ -58,17 +41,16 @@ function StickyToBottomContent(props: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function Thread() {
|
export function Thread() {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
|
||||||
const [_, setThreadId] = useQueryParam("threadId", StringParam);
|
|
||||||
const [input, setInput] = useState("");
|
const [input, setInput] = useState("");
|
||||||
const [firstTokenReceived, setFirstTokenReceived] = useState(false);
|
const [firstTokenReceived, setFirstTokenReceived] = useState(false);
|
||||||
|
|
||||||
const stream = useStreamContext();
|
const stream = useStreamContext();
|
||||||
const messages = stream.messages;
|
const messages = stream.messages;
|
||||||
const isLoading = stream.isLoading;
|
const isLoading = stream.isLoading;
|
||||||
|
|
||||||
const prevMessageLength = useRef(0);
|
|
||||||
|
|
||||||
// TODO: this should be part of the useStream hook
|
// TODO: this should be part of the useStream hook
|
||||||
|
const prevMessageLength = useRef(0);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
messages.length !== prevMessageLength.current &&
|
messages.length !== prevMessageLength.current &&
|
||||||
@@ -123,20 +105,15 @@ export function Thread() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const chatStarted = isLoading || messages.length > 0;
|
|
||||||
const renderMessages = messages.filter(
|
|
||||||
(m) => !m.id?.startsWith(DO_NOT_RENDER_ID_PREFIX),
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col w-full h-screen overflow-hidden">
|
<div className="flex flex-col w-full h-screen overflow-hidden">
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"grow grid grid-rows-[auto_1fr]",
|
"grow grid grid-rows-[auto_1fr]",
|
||||||
!chatStarted && "grid-rows-[1fr]",
|
!threadId && "grid-rows-[1fr]",
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{chatStarted && (
|
{threadId && (
|
||||||
<div className="flex items-center justify-between gap-3 p-2 pl-4 z-10 relative">
|
<div className="flex items-center justify-between gap-3 p-2 pl-4 z-10 relative">
|
||||||
<button
|
<button
|
||||||
className="flex gap-2 items-center cursor-pointer"
|
className="flex gap-2 items-center cursor-pointer"
|
||||||
@@ -146,7 +123,15 @@ export function Thread() {
|
|||||||
<span className="text-xl font-medium">LangGraph Chat</span>
|
<span className="text-xl font-medium">LangGraph Chat</span>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<NewThread />
|
<TooltipIconButton
|
||||||
|
size="lg"
|
||||||
|
className="p-4"
|
||||||
|
tooltip="New thread"
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => setThreadId(null)}
|
||||||
|
>
|
||||||
|
<SquarePen className="size-5" />
|
||||||
|
</TooltipIconButton>
|
||||||
|
|
||||||
<div className="absolute inset-x-0 top-full h-5 bg-gradient-to-b from-background to-background/0" />
|
<div className="absolute inset-x-0 top-full h-5 bg-gradient-to-b from-background to-background/0" />
|
||||||
</div>
|
</div>
|
||||||
@@ -156,30 +141,24 @@ export function Thread() {
|
|||||||
<StickyToBottomContent
|
<StickyToBottomContent
|
||||||
className={cn(
|
className={cn(
|
||||||
"absolute inset-0",
|
"absolute inset-0",
|
||||||
!chatStarted && "flex flex-col items-stretch mt-[25vh]",
|
!threadId && "flex flex-col items-stretch mt-[25vh]",
|
||||||
chatStarted && "grid grid-rows-[1fr_auto]",
|
threadId && "grid grid-rows-[1fr_auto]",
|
||||||
)}
|
)}
|
||||||
contentClassName="pt-8 pb-16 px-4 max-w-4xl mx-auto flex flex-col gap-4 w-full empty:hidden"
|
contentClassName="pt-8 pb-16 px-4 max-w-4xl mx-auto flex flex-col gap-4 w-full"
|
||||||
content={
|
content={
|
||||||
<>
|
<>
|
||||||
{renderMessages.map((message, index) =>
|
{messages
|
||||||
|
.filter((m) => !m.id?.startsWith(DO_NOT_RENDER_ID_PREFIX))
|
||||||
|
.map((message, index) =>
|
||||||
message.type === "human" ? (
|
message.type === "human" ? (
|
||||||
<HumanMessage
|
<HumanMessage
|
||||||
key={
|
key={message.id || `${message.type}-${index}`}
|
||||||
"id" in message
|
|
||||||
? message.id
|
|
||||||
: `${message.type}-${index}`
|
|
||||||
}
|
|
||||||
message={message}
|
message={message}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<AssistantMessage
|
<AssistantMessage
|
||||||
key={
|
key={message.id || `${message.type}-${index}`}
|
||||||
"id" in message
|
|
||||||
? message.id
|
|
||||||
: `${message.type}-${index}`
|
|
||||||
}
|
|
||||||
message={message}
|
message={message}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
handleRegenerate={handleRegenerate}
|
handleRegenerate={handleRegenerate}
|
||||||
@@ -193,7 +172,7 @@ export function Thread() {
|
|||||||
}
|
}
|
||||||
footer={
|
footer={
|
||||||
<div className="sticky flex flex-col items-center gap-8 bottom-8 px-4">
|
<div className="sticky flex flex-col items-center gap-8 bottom-8 px-4">
|
||||||
{!chatStarted && (
|
{!threadId && (
|
||||||
<div className="flex gap-3 items-center">
|
<div className="flex gap-3 items-center">
|
||||||
<LangGraphLogoSVG className="flex-shrink-0 h-8" />
|
<LangGraphLogoSVG className="flex-shrink-0 h-8" />
|
||||||
<h1 className="text-2xl font-medium">LangGraph Chat</h1>
|
<h1 className="text-2xl font-medium">LangGraph Chat</h1>
|
||||||
|
|||||||
Reference in New Issue
Block a user