feat: Better sidebar
This commit is contained in:
@@ -2,6 +2,7 @@ import { Button } from "@/components/ui/button";
|
|||||||
import { useThreads } from "@/providers/Thread";
|
import { useThreads } from "@/providers/Thread";
|
||||||
import { Thread } from "@langchain/langgraph-sdk";
|
import { Thread } from "@langchain/langgraph-sdk";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
import { getContentString } from "../utils";
|
import { getContentString } from "../utils";
|
||||||
import { useQueryParam, StringParam, BooleanParam } from "use-query-params";
|
import { useQueryParam, StringParam, BooleanParam } from "use-query-params";
|
||||||
import {
|
import {
|
||||||
@@ -11,6 +12,9 @@ import {
|
|||||||
SheetTitle,
|
SheetTitle,
|
||||||
} from "@/components/ui/sheet";
|
} from "@/components/ui/sheet";
|
||||||
import { Skeleton } from "@/components/ui/skeleton";
|
import { Skeleton } from "@/components/ui/skeleton";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
import { PanelRightOpen } from "lucide-react";
|
||||||
|
import { useMediaQuery } from "@/hooks/useMediaQuery";
|
||||||
|
|
||||||
function ThreadList({
|
function ThreadList({
|
||||||
threads,
|
threads,
|
||||||
@@ -22,7 +26,7 @@ function ThreadList({
|
|||||||
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
|
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full flex flex-col gap-2 items-start justify-start overflow-y-scroll [&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-gray-300 [&::-webkit-scrollbar-track]:bg-transparent">
|
<div className="h-full flex flex-col w-full gap-2 items-start justify-start overflow-y-scroll [&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-gray-300 [&::-webkit-scrollbar-track]:bg-transparent">
|
||||||
{threads.map((t) => {
|
{threads.map((t) => {
|
||||||
let itemText = t.thread_id;
|
let itemText = t.thread_id;
|
||||||
if (
|
if (
|
||||||
@@ -36,10 +40,10 @@ function ThreadList({
|
|||||||
itemText = getContentString(firstMessage.content);
|
itemText = getContentString(firstMessage.content);
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<div key={t.thread_id} className="w-full">
|
<div key={t.thread_id} className="w-full px-1">
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
className="truncate text-left items-start justify-start w-[264px]"
|
className="text-left items-start justify-start font-normal w-[280px]"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
onThreadClick?.(t.thread_id);
|
onThreadClick?.(t.thread_id);
|
||||||
@@ -47,7 +51,7 @@ function ThreadList({
|
|||||||
setThreadId(t.thread_id);
|
setThreadId(t.thread_id);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{itemText}
|
<p className="truncate text-ellipsis">{itemText}</p>
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -58,15 +62,16 @@ function ThreadList({
|
|||||||
|
|
||||||
function ThreadHistoryLoading() {
|
function ThreadHistoryLoading() {
|
||||||
return (
|
return (
|
||||||
<div className="h-full flex flex-col gap-2 items-start justify-start overflow-y-scroll [&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-gray-300 [&::-webkit-scrollbar-track]:bg-transparent">
|
<div className="h-full flex flex-col w-full gap-2 items-start justify-start overflow-y-scroll [&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-gray-300 [&::-webkit-scrollbar-track]:bg-transparent">
|
||||||
{Array.from({ length: 30 }).map((_, i) => (
|
{Array.from({ length: 30 }).map((_, i) => (
|
||||||
<Skeleton key={`skeleton-${i}`} className="w-[264px] h-10" />
|
<Skeleton key={`skeleton-${i}`} className="w-[280px] h-10" />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ThreadHistory() {
|
export default function ThreadHistory() {
|
||||||
|
const isLargeScreen = useMediaQuery("(min-width: 1024px)");
|
||||||
const [chatHistoryOpen, setChatHistoryOpen] = useQueryParam(
|
const [chatHistoryOpen, setChatHistoryOpen] = useQueryParam(
|
||||||
"chatHistoryOpen",
|
"chatHistoryOpen",
|
||||||
BooleanParam,
|
BooleanParam,
|
||||||
@@ -86,25 +91,44 @@ export default function ThreadHistory() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="hidden lg:flex flex-col border-r-[1px] border-slate-300 items-start justify-start gap-6 h-screen w-[300px] shrink-0 px-2 py-4 shadow-inner-right">
|
<div className="hidden lg:flex flex-col border-r-[1px] border-slate-300 items-start justify-start gap-6 h-screen w-[300px] shrink-0 shadow-inner-right">
|
||||||
<h1 className="text-2xl font-medium pl-4">Thread History</h1>
|
<div className="flex items-center justify-between w-full p-2">
|
||||||
|
<Button
|
||||||
|
className="hover:bg-gray-100"
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => setChatHistoryOpen((p) => !p)}
|
||||||
|
>
|
||||||
|
<PanelRightOpen />
|
||||||
|
</Button>
|
||||||
|
<h1 className="text-xl font-semibold tracking-tight">
|
||||||
|
Thread History
|
||||||
|
</h1>
|
||||||
|
</div>
|
||||||
{threadsLoading ? (
|
{threadsLoading ? (
|
||||||
<ThreadHistoryLoading />
|
<ThreadHistoryLoading />
|
||||||
) : (
|
) : (
|
||||||
<ThreadList threads={threads} />
|
<ThreadList threads={threads} />
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<Sheet open={!!chatHistoryOpen} onOpenChange={setChatHistoryOpen}>
|
<div className="lg:hidden">
|
||||||
<SheetContent side="left" className="lg:hidden flex">
|
<Sheet
|
||||||
<SheetHeader>
|
open={!!chatHistoryOpen && !isLargeScreen}
|
||||||
<SheetTitle>Thread History</SheetTitle>
|
onOpenChange={(open) => {
|
||||||
</SheetHeader>
|
if (isLargeScreen) return;
|
||||||
<ThreadList
|
setChatHistoryOpen(open);
|
||||||
threads={threads}
|
}}
|
||||||
onThreadClick={() => setChatHistoryOpen((o) => !o)}
|
>
|
||||||
/>
|
<SheetContent side="left" className="lg:hidden flex">
|
||||||
</SheetContent>
|
<SheetHeader>
|
||||||
</Sheet>
|
<SheetTitle>Thread History</SheetTitle>
|
||||||
|
</SheetHeader>
|
||||||
|
<ThreadList
|
||||||
|
threads={threads}
|
||||||
|
onThreadClick={() => setChatHistoryOpen((o) => !o)}
|
||||||
|
/>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>
|
||||||
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
import { ReactNode, useEffect, useRef } from "react";
|
import { ReactNode, useEffect, useRef } from "react";
|
||||||
|
import { motion } from "framer-motion";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { useStreamContext } from "@/providers/Stream";
|
import { useStreamContext } from "@/providers/Stream";
|
||||||
import { useState, FormEvent } from "react";
|
import { useState, FormEvent } from "react";
|
||||||
@@ -24,6 +25,7 @@ import { BooleanParam, StringParam, useQueryParam } from "use-query-params";
|
|||||||
import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
|
import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
|
||||||
import ThreadHistory from "./history";
|
import ThreadHistory from "./history";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
import { useMediaQuery } from "@/hooks/useMediaQuery";
|
||||||
|
|
||||||
function StickyToBottomContent(props: {
|
function StickyToBottomContent(props: {
|
||||||
content: ReactNode;
|
content: ReactNode;
|
||||||
@@ -65,12 +67,13 @@ function ScrollToBottom(props: { className?: string }) {
|
|||||||
|
|
||||||
export function Thread() {
|
export function Thread() {
|
||||||
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
|
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
|
||||||
const [_, setChatHistoryOpen] = useQueryParam(
|
const [chatHistoryOpen, setChatHistoryOpen] = useQueryParam(
|
||||||
"chatHistoryOpen",
|
"chatHistoryOpen",
|
||||||
BooleanParam,
|
BooleanParam,
|
||||||
);
|
);
|
||||||
const [input, setInput] = useState("");
|
const [input, setInput] = useState("");
|
||||||
const [firstTokenReceived, setFirstTokenReceived] = useState(false);
|
const [firstTokenReceived, setFirstTokenReceived] = useState(false);
|
||||||
|
const isLargeScreen = useMediaQuery("(min-width: 1024px)");
|
||||||
|
|
||||||
const stream = useStreamContext();
|
const stream = useStreamContext();
|
||||||
const messages = stream.messages;
|
const messages = stream.messages;
|
||||||
@@ -166,16 +169,72 @@ export function Thread() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex w-full h-screen overflow-hidden">
|
<div className="flex w-full h-screen overflow-hidden">
|
||||||
<ThreadHistory />
|
<div className="relative lg:flex hidden">
|
||||||
<div
|
<motion.div
|
||||||
|
className="absolute h-full border-r bg-white overflow-hidden z-20"
|
||||||
|
style={{ width: 300 }}
|
||||||
|
animate={
|
||||||
|
isLargeScreen
|
||||||
|
? { x: chatHistoryOpen ? 0 : -300 }
|
||||||
|
: { x: chatHistoryOpen ? 0 : -300 }
|
||||||
|
}
|
||||||
|
initial={{ x: -300 }}
|
||||||
|
transition={
|
||||||
|
isLargeScreen
|
||||||
|
? { type: "spring", stiffness: 300, damping: 30 }
|
||||||
|
: { duration: 0 }
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<div className="relative h-full" style={{ width: 300 }}>
|
||||||
|
<ThreadHistory />
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
</div>
|
||||||
|
<motion.div
|
||||||
className={cn(
|
className={cn(
|
||||||
"flex-1 flex flex-col min-w-0 overflow-hidden",
|
"flex-1 flex flex-col min-w-0 overflow-hidden relative",
|
||||||
!chatStarted && "grid-rows-[1fr]",
|
!chatStarted && "grid-rows-[1fr]",
|
||||||
)}
|
)}
|
||||||
|
layout={isLargeScreen}
|
||||||
|
animate={{
|
||||||
|
marginLeft: chatHistoryOpen ? (isLargeScreen ? 300 : 0) : 0,
|
||||||
|
width: chatHistoryOpen
|
||||||
|
? isLargeScreen
|
||||||
|
? "calc(100% - 300px)"
|
||||||
|
: "100%"
|
||||||
|
: "100%",
|
||||||
|
}}
|
||||||
|
transition={
|
||||||
|
isLargeScreen
|
||||||
|
? { type: "spring", stiffness: 300, damping: 30 }
|
||||||
|
: { duration: 0 }
|
||||||
|
}
|
||||||
>
|
>
|
||||||
|
{!chatStarted && (
|
||||||
|
<div className="flex items-center justify-between gap-3 p-2 pl-4 z-10 relative">
|
||||||
|
{(!chatHistoryOpen || !isLargeScreen) && (
|
||||||
|
<Button
|
||||||
|
className="hover:bg-gray-100"
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => setChatHistoryOpen((p) => !p)}
|
||||||
|
>
|
||||||
|
<PanelRightOpen />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
{chatStarted && (
|
{chatStarted && (
|
||||||
<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">
|
||||||
<div className="flex gap-2 items-center justify-start">
|
<div className="flex items-center justify-start gap-2">
|
||||||
|
{(!chatHistoryOpen || !isLargeScreen) && (
|
||||||
|
<Button
|
||||||
|
className="hover:bg-gray-100"
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => setChatHistoryOpen((p) => !p)}
|
||||||
|
>
|
||||||
|
<PanelRightOpen />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
<button
|
<button
|
||||||
className="flex gap-2 items-center cursor-pointer"
|
className="flex gap-2 items-center cursor-pointer"
|
||||||
onClick={() => setThreadId(null)}
|
onClick={() => setThreadId(null)}
|
||||||
@@ -185,13 +244,6 @@ export function Thread() {
|
|||||||
LangGraph Chat
|
LangGraph Chat
|
||||||
</span>
|
</span>
|
||||||
</button>
|
</button>
|
||||||
<Button
|
|
||||||
className="flex lg:hidden"
|
|
||||||
variant="ghost"
|
|
||||||
onClick={() => setChatHistoryOpen((p) => !p)}
|
|
||||||
>
|
|
||||||
<PanelRightOpen />
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<TooltipIconButton
|
<TooltipIconButton
|
||||||
@@ -254,7 +306,7 @@ export function Thread() {
|
|||||||
|
|
||||||
<ScrollToBottom className="absolute bottom-full left-1/2 -translate-x-1/2 mb-4 animate-in fade-in-0 zoom-in-95" />
|
<ScrollToBottom className="absolute bottom-full left-1/2 -translate-x-1/2 mb-4 animate-in fade-in-0 zoom-in-95" />
|
||||||
|
|
||||||
<div className="bg-background rounded-2xl border shadow-md mx-auto w-full max-w-4xl">
|
<div className="bg-background rounded-2xl border shadow-md mx-auto w-full max-w-4xl relative z-10">
|
||||||
<form
|
<form
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
className="grid grid-rows-[1fr_auto] gap-2 max-w-4xl mx-auto"
|
className="grid grid-rows-[1fr_auto] gap-2 max-w-4xl mx-auto"
|
||||||
@@ -288,7 +340,7 @@ export function Thread() {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</StickToBottom>
|
</StickToBottom>
|
||||||
</div>
|
</motion.div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/hooks/useMediaQuery.tsx
Normal file
16
src/hooks/useMediaQuery.tsx
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
export function useMediaQuery(query: string) {
|
||||||
|
const [matches, setMatches] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const media = window.matchMedia(query);
|
||||||
|
setMatches(media.matches);
|
||||||
|
|
||||||
|
const listener = (e: MediaQueryListEvent) => setMatches(e.matches);
|
||||||
|
media.addEventListener("change", listener);
|
||||||
|
return () => media.removeEventListener("change", listener);
|
||||||
|
}, [query]);
|
||||||
|
|
||||||
|
return matches;
|
||||||
|
}
|
||||||
@@ -118,9 +118,11 @@
|
|||||||
* {
|
* {
|
||||||
@apply border-border outline-ring/50;
|
@apply border-border outline-ring/50;
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
@apply bg-background text-foreground;
|
@apply bg-background text-foreground;
|
||||||
}
|
}
|
||||||
|
|
||||||
:root {
|
:root {
|
||||||
--chart-1: 12 76% 61%;
|
--chart-1: 12 76% 61%;
|
||||||
--chart-2: 173 58% 39%;
|
--chart-2: 173 58% 39%;
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
/** @type {import('tailwindcss').Config} */
|
/** @type {import('tailwindcss').Config} */
|
||||||
module.exports = {
|
module.exports = {
|
||||||
darkMode: ["class"],
|
darkMode: ["class"],
|
||||||
content: ["./index.html", "./src/**/*.{ts,tsx,js,jsx}"],
|
content: [
|
||||||
|
"./index.html",
|
||||||
|
"./src/**/*.{ts,tsx,js,jsx}",
|
||||||
|
"./agent/**/*.{ts,tsx,js,jsx}",
|
||||||
|
],
|
||||||
theme: {
|
theme: {
|
||||||
extend: {
|
extend: {
|
||||||
borderRadius: {
|
borderRadius: {
|
||||||
|
|||||||
Reference in New Issue
Block a user