Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
style: chat and chat message
Marc Seitzce0cd3e0a132
diff --git a/components/chat/chat-input.tsx b/components/chat/chat-input.tsxnew file mode 100644index 00000000..d48f3336--- /dev/null+++ b/components/chat/chat-input.tsx@@ -0,0 +1,81 @@+import { AssistantStatus, type Message } from "ai/react";++import { Button } from "@/components/ui/button";+import Textarea from "react-textarea-autosize";+import { useEffect, useRef } from "react";+import { cn } from "@/lib/utils";+import { useEnterSubmit } from "@/lib/utils/use-enter-submit";++export function ChatInput({+ status,+ error,+ input,+ handleInputChange,+ submitMessage,+ messages,+}: {+ status: AssistantStatus;+ error: unknown;+ input: string;+ handleInputChange: (e: any) => void;+ submitMessage: (e: any) => Promise<void>;+ messages: Message[];+}) {+ const { formRef, onKeyDown } = useEnterSubmit();+ const inputRef = useRef<HTMLTextAreaElement>(null);+ useEffect(() => {+ if (status === "awaiting_message") {+ inputRef.current?.focus();+ }+ }, [status]);++ return (+ <div className="fixed inset-x-0 bottom-0">+ <div className="mx-auto sm:max-w-3xl sm:px-4">+ <div className="space-y-4 bg-background px-4 py-4 shadow-lg md:py-4">+ <form onSubmit={submitMessage} ref={formRef}>+ <div className="relative flex max-h-60 w-full flex-col overflow-hidden bg-background pr-8 rounded-md sm:border sm:pr-12 focus-within:ring-1 focus-within:ring-foreground">+ <Textarea+ ref={inputRef}+ tabIndex={0}+ rows={1}+ onKeyDown={onKeyDown}+ disabled={status !== "awaiting_message"}+ className="min-h-[60px] w-full resize-none bg-transparent px-4 py-[1.3rem] focus:ring-0 sm:text-sm border-none"+ value={input}+ placeholder="Message Papermark Assistant..."+ onChange={handleInputChange}+ spellCheck={false}+ />+ <div className="absolute top-3 right-2.5">+ <Button+ type="submit"+ size="icon"+ disabled={status === "in_progress" || input === ""}+ title="Send message"+ >+ <IconArrowElbow />+ <span className="sr-only">Send message</span>+ </Button>+ </div>+ </div>+ </form>+ </div>+ </div>+ </div>+ );+}++function IconArrowElbow({ className, ...props }: React.ComponentProps<"svg">) {+ return (+ <svg+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 256 256"+ fill="currentColor"+ className={cn("h-4 w-4", className)}+ {...props}+ >+ <path d="M200 32v144a8 8 0 0 1-8 8H67.31l34.35 34.34a8 8 0 0 1-11.32 11.32l-48-48a8 8 0 0 1 0-11.32l48-48a8 8 0 0 1 11.32 11.32L67.31 168H184V32a8 8 0 0 1 16 0Z" />+ </svg>+ );+}diff --git a/components/chat/chat-list.tsx b/components/chat/chat-list.tsxnew file mode 100644index 00000000..b9da0995--- /dev/null+++ b/components/chat/chat-list.tsx@@ -0,0 +1,67 @@+import { type Message } from "ai";++import { Separator } from "@/components/ui/separator";+import { ChatMessage } from "./chat-message";+import { cn } from "@/lib/utils";+import Skeleton from "../Skeleton";++export interface ChatList {+ messages: Message[];+ status: "in_progress" | "awaiting_message";+}++export function ChatList({ messages, status }: ChatList) {+ if (!messages.length) {+ return null;+ }++ return (+ <div className="relative mx-auto max-w-2xl px-4">+ {messages.map((message, index) => (+ <div key={index}>+ <ChatMessage message={message} />+ {index < messages.length - 1 && (+ <Separator className="my-4 md:my-8" />+ )}+ </div>+ ))}+ {status === "in_progress" && (+ <>+ <Separator className="my-4 md:my-8" />+ <div+ key={"loading-message"}+ className="group relative mb-4 flex items-start md:-ml-12 whitespace-pre-wrap"+ >+ <div className="flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border shadow bg-primary text-primary-foreground">+ <IconOpenAI />+ </div>+ <div className="flex-1 px-1 ml-4 space-y-2 overflow-hidden">+ <div className="font-semibold select-none">+ Papermark Assistant+ </div>+ <Skeleton className="h-4 w-64" />+ <Skeleton className="h-4 w-48" />+ <Skeleton className="h-4 w-24" />+ </div>+ </div>+ </>+ )}+ </div>+ );+}++function IconOpenAI({ className, ...props }: React.ComponentProps<"svg">) {+ return (+ <svg+ fill="currentColor"+ viewBox="0 0 24 24"+ role="img"+ xmlns="http://www.w3.org/2000/svg"+ className={cn("h-4 w-4", className)}+ {...props}+ >+ <title>OpenAI icon</title>+ <path d="M22.2819 9.8211a5.9847 5.9847 0 0 0-.5157-4.9108 6.0462 6.0462 0 0 0-6.5098-2.9A6.0651 6.0651 0 0 0 4.9807 4.1818a5.9847 5.9847 0 0 0-3.9977 2.9 6.0462 6.0462 0 0 0 .7427 7.0966 5.98 5.98 0 0 0 .511 4.9107 6.051 6.051 0 0 0 6.5146 2.9001A5.9847 5.9847 0 0 0 13.2599 24a6.0557 6.0557 0 0 0 5.7718-4.2058 5.9894 5.9894 0 0 0 3.9977-2.9001 6.0557 6.0557 0 0 0-.7475-7.0729zm-9.022 12.6081a4.4755 4.4755 0 0 1-2.8764-1.0408l.1419-.0804 4.7783-2.7582a.7948.7948 0 0 0 .3927-.6813v-6.7369l2.02 1.1686a.071.071 0 0 1 .038.052v5.5826a4.504 4.504 0 0 1-4.4945 4.4944zm-9.6607-4.1254a4.4708 4.4708 0 0 1-.5346-3.0137l.142.0852 4.783 2.7582a.7712.7712 0 0 0 .7806 0l5.8428-3.3685v2.3324a.0804.0804 0 0 1-.0332.0615L9.74 19.9502a4.4992 4.4992 0 0 1-6.1408-1.6464zM2.3408 7.8956a4.485 4.485 0 0 1 2.3655-1.9728V11.6a.7664.7664 0 0 0 .3879.6765l5.8144 3.3543-2.0201 1.1685a.0757.0757 0 0 1-.071 0l-4.8303-2.7865A4.504 4.504 0 0 1 2.3408 7.872zm16.5963 3.8558L13.1038 8.364 15.1192 7.2a.0757.0757 0 0 1 .071 0l4.8303 2.7913a4.4944 4.4944 0 0 1-.6765 8.1042v-5.6772a.79.79 0 0 0-.407-.667zm2.0107-3.0231l-.142-.0852-4.7735-2.7818a.7759.7759 0 0 0-.7854 0L9.409 9.2297V6.8974a.0662.0662 0 0 1 .0284-.0615l4.8303-2.7866a4.4992 4.4992 0 0 1 6.6802 4.66zM8.3065 12.863l-2.02-1.1638a.0804.0804 0 0 1-.038-.0567V6.0742a4.4992 4.4992 0 0 1 7.3757-3.4537l-.142.0805L8.704 5.459a.7948.7948 0 0 0-.3927.6813zm1.0976-2.3654l2.602-1.4998 2.6069 1.4998v2.9994l-2.5974 1.4997-2.6067-1.4997Z" />+ </svg>+ );+}diff --git a/components/chat/chat-message-actions.tsx b/components/chat/chat-message-actions.tsxnew file mode 100644index 00000000..be8e140d--- /dev/null+++ b/components/chat/chat-message-actions.tsx@@ -0,0 +1,41 @@+"use client";++import { type Message } from "ai";++import { Button } from "@/components/ui/button";+import { useCopyToClipboard } from "@/lib/utils/use-copy-to-clipboard";+import { cn } from "@/lib/utils";+import Check from "../shared/icons/check";+import Copy from "../shared/icons/copy";++interface ChatMessageActionsProps extends React.ComponentProps<"div"> {+ message: Message;+}++export function ChatMessageActions({+ message,+ className,+ ...props+}: ChatMessageActionsProps) {+ const { isCopied, copyToClipboard } = useCopyToClipboard({ timeout: 2000 });++ const onCopy = () => {+ if (isCopied) return;+ copyToClipboard(message.content);+ };++ return (+ <div+ className={cn(+ "flex items-center justify-end md:absolute md:-right-10 md:-top-2",+ className,+ )}+ {...props}+ >+ <Button variant="ghost" size="icon" onClick={onCopy}>+ {isCopied ? <Check /> : <Copy />}+ <span className="sr-only">Copy message</span>+ </Button>+ </div>+ );+}diff --git a/components/chat/chat-message.tsx b/components/chat/chat-message.tsxnew file mode 100644index 00000000..f775b2c8--- /dev/null+++ b/components/chat/chat-message.tsx@@ -0,0 +1,72 @@+import { type Message } from "ai";++import { cn } from "@/lib/utils";+import { ChatMessageActions } from "./chat-message-actions";++export interface ChatMessageProps {+ message: Message;+}++export function ChatMessage({ message, ...props }: ChatMessageProps) {+ return (+ <div+ key={message.id}+ className={cn(+ "group relative mb-4 flex items-start md:-ml-12 whitespace-pre-wrap",+ )}+ {...props}+ >+ <div+ className={cn(+ "flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border shadow",+ message.role === "user"+ ? "bg-background"+ : "bg-primary text-primary-foreground",+ )}+ >+ {message.role === "user" ? <IconUser /> : <IconOpenAI />}+ </div>+ <div className="flex-1 px-1 ml-4 space-y-2 overflow-hidden">+ <div className="relative flex w-[calc(100%-50px)] flex-col lg:w-[calc(100%-115px)]">+ <div className="font-semibold select-none">+ {message.role === "user" ? "You" : "Papermark Assistant"}+ </div>+ <div className="prose break-words dark:prose-invert prose-p:leading-relaxed prose-pre:p-0 font-light">+ <p className="mb-2 last:mb-0">{message.content}</p>+ </div>+ </div>+ <ChatMessageActions message={message} />+ </div>+ </div>+ );+}++function IconOpenAI({ className, ...props }: React.ComponentProps<"svg">) {+ return (+ <svg+ fill="currentColor"+ viewBox="0 0 24 24"+ role="img"+ xmlns="http://www.w3.org/2000/svg"+ className={cn("h-4 w-4", className)}+ {...props}+ >+ <title>OpenAI icon</title>+ <path d="M22.2819 9.8211a5.9847 5.9847 0 0 0-.5157-4.9108 6.0462 6.0462 0 0 0-6.5098-2.9A6.0651 6.0651 0 0 0 4.9807 4.1818a5.9847 5.9847 0 0 0-3.9977 2.9 6.0462 6.0462 0 0 0 .7427 7.0966 5.98 5.98 0 0 0 .511 4.9107 6.051 6.051 0 0 0 6.5146 2.9001A5.9847 5.9847 0 0 0 13.2599 24a6.0557 6.0557 0 0 0 5.7718-4.2058 5.9894 5.9894 0 0 0 3.9977-2.9001 6.0557 6.0557 0 0 0-.7475-7.0729zm-9.022 12.6081a4.4755 4.4755 0 0 1-2.8764-1.0408l.1419-.0804 4.7783-2.7582a.7948.7948 0 0 0 .3927-.6813v-6.7369l2.02 1.1686a.071.071 0 0 1 .038.052v5.5826a4.504 4.504 0 0 1-4.4945 4.4944zm-9.6607-4.1254a4.4708 4.4708 0 0 1-.5346-3.0137l.142.0852 4.783 2.7582a.7712.7712 0 0 0 .7806 0l5.8428-3.3685v2.3324a.0804.0804 0 0 1-.0332.0615L9.74 19.9502a4.4992 4.4992 0 0 1-6.1408-1.6464zM2.3408 7.8956a4.485 4.485 0 0 1 2.3655-1.9728V11.6a.7664.7664 0 0 0 .3879.6765l5.8144 3.3543-2.0201 1.1685a.0757.0757 0 0 1-.071 0l-4.8303-2.7865A4.504 4.504 0 0 1 2.3408 7.872zm16.5963 3.8558L13.1038 8.364 15.1192 7.2a.0757.0757 0 0 1 .071 0l4.8303 2.7913a4.4944 4.4944 0 0 1-.6765 8.1042v-5.6772a.79.79 0 0 0-.407-.667zm2.0107-3.0231l-.142-.0852-4.7735-2.7818a.7759.7759 0 0 0-.7854 0L9.409 9.2297V6.8974a.0662.0662 0 0 1 .0284-.0615l4.8303-2.7866a4.4992 4.4992 0 0 1 6.6802 4.66zM8.3065 12.863l-2.02-1.1638a.0804.0804 0 0 1-.038-.0567V6.0742a4.4992 4.4992 0 0 1 7.3757-3.4537l-.142.0805L8.704 5.459a.7948.7948 0 0 0-.3927.6813zm1.0976-2.3654l2.602-1.4998 2.6069 1.4998v2.9994l-2.5974 1.4997-2.6067-1.4997Z" />+ </svg>+ );+}++function IconUser({ className, ...props }: React.ComponentProps<"svg">) {+ return (+ <svg+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 256 256"+ fill="currentColor"+ className={cn("h-4 w-4", className)}+ {...props}+ >+ <path d="M230.92 212c-15.23-26.33-38.7-45.21-66.09-54.16a72 72 0 1 0-73.66 0c-27.39 8.94-50.86 27.82-66.09 54.16a8 8 0 1 0 13.85 8c18.84-32.56 52.14-52 89.07-52s70.23 19.44 89.07 52a8 8 0 1 0 13.85-8ZM72 96a56 56 0 1 1 56 56 56.06 56.06 0 0 1-56-56Z" />+ </svg>+ );+}diff --git a/components/chat/chat-scroll-anchor.tsx b/components/chat/chat-scroll-anchor.tsxnew file mode 100644index 00000000..c1c3694c--- /dev/null+++ b/components/chat/chat-scroll-anchor.tsx@@ -0,0 +1,29 @@+"use client";++import * as React from "react";+import { useInView } from "react-intersection-observer";++import { useAtBottom } from "@/lib/utils/use-at-bottom";++interface ChatScrollAnchorProps {+ trackVisibility?: boolean;+}++export function ChatScrollAnchor({ trackVisibility }: ChatScrollAnchorProps) {+ const isAtBottom = useAtBottom();+ const { ref, entry, inView } = useInView({+ trackVisibility,+ delay: 100,+ rootMargin: "0px 0px -100px 0px",+ });++ React.useEffect(() => {+ if (isAtBottom && trackVisibility && !inView) {+ entry?.target.scrollIntoView({+ block: "start",+ });+ }+ }, [inView, entry, isAtBottom, trackVisibility]);++ return <div ref={ref} className="h-px w-full" />;+}diff --git a/components/chat/chat.tsx b/components/chat/chat.tsxnew file mode 100644index 00000000..0c4f1ce0--- /dev/null+++ b/components/chat/chat.tsx@@ -0,0 +1,95 @@+"use client";++import {+ experimental_useAssistant as useAssistant,+ type Message,+} from "ai/react";++import { cn } from "@/lib/utils";+import { ChatList } from "@/components/chat/chat-list";+import { ChatInput } from "@/components/chat/chat-input";+// import { EmptyScreen } from "@/components/empty-screen";+import { ChatScrollAnchor } from "@/components/chat/chat-scroll-anchor";+import { useEffect, useState } from "react";++export interface ChatProps extends React.ComponentProps<"div"> {+ initialMessages: Message[];+ threadId: string;+}++export function Chat({ initialMessages, threadId, className }: ChatProps) {+ const {+ status,+ messages: hookMessages,+ input,+ submitMessage,+ handleInputChange,+ error,+ } = useAssistant({+ api: "/api/assistants/chat",+ threadId: threadId,+ });++ const [combinedMessages, setCombinedMessages] = useState<Message[]>([]);++ useEffect(() => {+ // Concatenate existing messages with messages from the hook+ // and reverse the order so that the newest messages are at the bottom:+ const reversedMessages = [...initialMessages].reverse();+ setCombinedMessages([...reversedMessages, ...hookMessages]);+ }, [initialMessages, hookMessages]);++ let isLoading;+ if (status === "in_progress") {+ isLoading = true;+ } else if (status === "awaiting_message") {+ isLoading = true;+ }++ return (+ <>+ <Nav />+ <div className={cn("pb-[100px] pt-24", className)}>+ {combinedMessages.length ? (+ <>+ <ChatList messages={combinedMessages} status={status} />+ <ChatScrollAnchor trackVisibility={isLoading} />+ </>+ ) : // <EmptyScreen handleInputChange={handleInputChange} />+ null}+ </div>+ <ChatInput+ status={status}+ error={error}+ messages={combinedMessages}+ input={input}+ submitMessage={submitMessage}+ handleInputChange={handleInputChange}+ />+ </>+ );+}++function Nav() {+ return (+ <nav className="bg-black fixed top-0 inset-x-0 z-10">+ <div className="mx-auto px-2 sm:px-6 lg:px-8">+ <div className="relative flex h-16 items-center justify-between">+ <div className="flex flex-1 items-center justify-center sm:items-stretch sm:justify-start">+ <div className="flex flex-shrink-0 items-center">+ <p className="text-2xl font-bold tracking-tighter text-white">+ Papermark+ </p>+ </div>+ </div>+ <div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">+ <div className="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium">+ {/* <span>{pageNumber}</span>+ <span className="text-gray-400"> / {numPages}</span> */}+ </div>+ </div>+ </div>+ </div>+ </nav>+ );+}diff --git a/lib/utils/use-at-bottom.ts b/lib/utils/use-at-bottom.tsnew file mode 100644index 00000000..cf8624d2--- /dev/null+++ b/lib/utils/use-at-bottom.ts@@ -0,0 +1,23 @@+import * as React from "react";++export function useAtBottom(offset = 0) {+ const [isAtBottom, setIsAtBottom] = React.useState(false);++ React.useEffect(() => {+ const handleScroll = () => {+ setIsAtBottom(+ window.innerHeight + window.scrollY >=+ document.body.offsetHeight - offset,+ );+ };++ window.addEventListener("scroll", handleScroll, { passive: true });+ handleScroll();++ return () => {+ window.removeEventListener("scroll", handleScroll);+ };+ }, [offset]);++ return isAtBottom;+}diff --git a/lib/utils/use-copy-to-clipboard.ts b/lib/utils/use-copy-to-clipboard.tsnew file mode 100644index 00000000..e011d69b--- /dev/null+++ b/lib/utils/use-copy-to-clipboard.ts@@ -0,0 +1,33 @@+"use client";++import * as React from "react";++export interface useCopyToClipboardProps {+ timeout?: number;+}++export function useCopyToClipboard({+ timeout = 2000,+}: useCopyToClipboardProps) {+ const [isCopied, setIsCopied] = React.useState<Boolean>(false);++ const copyToClipboard = (value: string) => {+ if (typeof window === "undefined" || !navigator.clipboard?.writeText) {+ return;+ }++ if (!value) {+ return;+ }++ navigator.clipboard.writeText(value).then(() => {+ setIsCopied(true);++ setTimeout(() => {+ setIsCopied(false);+ }, timeout);+ });+ };++ return { isCopied, copyToClipboard };+}diff --git a/lib/utils/use-enter-submit.ts b/lib/utils/use-enter-submit.tsnew file mode 100644index 00000000..3aaeb882--- /dev/null+++ b/lib/utils/use-enter-submit.ts@@ -0,0 +1,23 @@+import { useRef, type RefObject } from "react";++export function useEnterSubmit(): {+ formRef: RefObject<HTMLFormElement>;+ onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void;+} {+ const formRef = useRef<HTMLFormElement>(null);++ const handleKeyDown = (+ event: React.KeyboardEvent<HTMLTextAreaElement>,+ ): void => {+ if (+ event.key === "Enter" &&+ !event.shiftKey &&+ !event.nativeEvent.isComposing+ ) {+ formRef.current?.requestSubmit();+ event.preventDefault();+ }+ };++ return { formRef, onKeyDown: handleKeyDown };+}diff --git a/package-lock.json b/package-lock.jsonindex 00380945..8f430400 100644--- a/package-lock.json+++ b/package-lock.json@@ -67,7 +67,9 @@"react-dom": "^18.2.0","react-dropzone": "^14.2.3","react-email": "^1.9.5",+ "react-intersection-observer": "^9.5.3","react-pdf": "^7.5.1",+ "react-textarea-autosize": "^8.5.3","resend": "^2.0.0","sonner": "^1.2.0","stripe": "^13.9.0",@@ -9176,6 +9178,14 @@"node": ">=10"}},+ "node_modules/react-intersection-observer": {+ "version": "9.5.3",+ "resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.5.3.tgz",+ "integrity": "sha512-NJzagSdUPS5rPhaLsHXYeJbsvdpbJwL6yCHtMk91hc0ufQ2BnXis+0QQ9NBh6n9n+Q3OyjR6OQLShYbaNBkThQ==",+ "peerDependencies": {+ "react": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0"+ }+ },"node_modules/react-is": {"version": "16.13.1","resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",@@ -9330,6 +9340,22 @@}}},+ "node_modules/react-textarea-autosize": {+ "version": "8.5.3",+ "resolved": "https://registry.npmjs.org/react-textarea-autosize/-/react-textarea-autosize-8.5.3.tgz",+ "integrity": "sha512-XT1024o2pqCuZSuBt9FwHlaDeNtVrtCXu0Rnz88t1jUGheCLa3PhjE1GH8Ctm2axEtvdCl5SUHYschyQ0L5QHQ==",+ "dependencies": {+ "@babel/runtime": "^7.20.13",+ "use-composed-ref": "^1.3.0",+ "use-latest": "^1.2.1"+ },+ "engines": {+ "node": ">=10"+ },+ "peerDependencies": {+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0"+ }+ },"node_modules/react-transition-group": {"version": "4.4.5","resolved": "https://registry.npmjs.org/react-transition-group/-/react-transition-group-4.4.5.tgz",@@ -11063,6 +11089,43 @@}}},+ "node_modules/use-composed-ref": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/use-composed-ref/-/use-composed-ref-1.3.0.tgz",+ "integrity": "sha512-GLMG0Jc/jiKov/3Ulid1wbv3r54K9HlMW29IWcDFPEqFkSO2nS0MuefWgMJpeHQ9YJeXDL3ZUF+P3jdXlZX/cQ==",+ "peerDependencies": {+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0"+ }+ },+ "node_modules/use-isomorphic-layout-effect": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/use-isomorphic-layout-effect/-/use-isomorphic-layout-effect-1.1.2.tgz",+ "integrity": "sha512-49L8yCO3iGT/ZF9QttjwLF/ZD9Iwto5LnH5LmEdk/6cFmXddqi2ulF0edxTwjj+7mqvpVVGQWvbXZdn32wRSHA==",+ "peerDependencies": {+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0"+ },+ "peerDependenciesMeta": {+ "@types/react": {+ "optional": true+ }+ }+ },+ "node_modules/use-latest": {+ "version": "1.2.1",+ "resolved": "https://registry.npmjs.org/use-latest/-/use-latest-1.2.1.tgz",+ "integrity": "sha512-xA+AVm/Wlg3e2P/JiItTziwS7FK92LWrDB0p+hgXloIMuVCeJJ8v6f0eeHyPZaJrM+usM1FkFfbNCrJGs8A/zw==",+ "dependencies": {+ "use-isomorphic-layout-effect": "^1.1.1"+ },+ "peerDependencies": {+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0"+ },+ "peerDependenciesMeta": {+ "@types/react": {+ "optional": true+ }+ }+ },"node_modules/use-sidecar": {"version": "1.1.2","resolved": "https://registry.npmjs.org/use-sidecar/-/use-sidecar-1.1.2.tgz",@@ -17644,6 +17707,12 @@}}},+ "react-intersection-observer": {+ "version": "9.5.3",+ "resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.5.3.tgz",+ "integrity": "sha512-NJzagSdUPS5rPhaLsHXYeJbsvdpbJwL6yCHtMk91hc0ufQ2BnXis+0QQ9NBh6n9n+Q3OyjR6OQLShYbaNBkThQ==",+ "requires": {}+ },"react-is": {"version": "16.13.1","resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",@@ -17738,6 +17807,16 @@"tslib": "^2.0.0"}},+ "react-textarea-autosize": {+ "version": "8.5.3",+ "resolved": "https://registry.npmjs.org/react-textarea-autosize/-/react-textarea-autosize-8.5.3.tgz",+ "integrity": "sha512-XT1024o2pqCuZSuBt9FwHlaDeNtVrtCXu0Rnz88t1jUGheCLa3PhjE1GH8Ctm2axEtvdCl5SUHYschyQ0L5QHQ==",+ "requires": {+ "@babel/runtime": "^7.20.13",+ "use-composed-ref": "^1.3.0",+ "use-latest": "^1.2.1"+ }+ },"react-transition-group": {"version": "4.4.5","resolved": "https://registry.npmjs.org/react-transition-group/-/react-transition-group-4.4.5.tgz",@@ -18987,6 +19066,26 @@"tslib": "^2.0.0"}},+ "use-composed-ref": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/use-composed-ref/-/use-composed-ref-1.3.0.tgz",+ "integrity": "sha512-GLMG0Jc/jiKov/3Ulid1wbv3r54K9HlMW29IWcDFPEqFkSO2nS0MuefWgMJpeHQ9YJeXDL3ZUF+P3jdXlZX/cQ==",+ "requires": {}+ },+ "use-isomorphic-layout-effect": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/use-isomorphic-layout-effect/-/use-isomorphic-layout-effect-1.1.2.tgz",+ "integrity": "sha512-49L8yCO3iGT/ZF9QttjwLF/ZD9Iwto5LnH5LmEdk/6cFmXddqi2ulF0edxTwjj+7mqvpVVGQWvbXZdn32wRSHA==",+ "requires": {}+ },+ "use-latest": {+ "version": "1.2.1",+ "resolved": "https://registry.npmjs.org/use-latest/-/use-latest-1.2.1.tgz",+ "integrity": "sha512-xA+AVm/Wlg3e2P/JiItTziwS7FK92LWrDB0p+hgXloIMuVCeJJ8v6f0eeHyPZaJrM+usM1FkFfbNCrJGs8A/zw==",+ "requires": {+ "use-isomorphic-layout-effect": "^1.1.1"+ }+ },"use-sidecar": {"version": "1.1.2","resolved": "https://registry.npmjs.org/use-sidecar/-/use-sidecar-1.1.2.tgz",diff --git a/package.json b/package.jsonindex ed6df71f..d025a764 100644--- a/package.json+++ b/package.json@@ -74,7 +74,9 @@"react-dom": "^18.2.0","react-dropzone": "^14.2.3","react-email": "^1.9.5",+ "react-intersection-observer": "^9.5.3","react-pdf": "^7.5.1",+ "react-textarea-autosize": "^8.5.3","resend": "^2.0.0","sonner": "^1.2.0","stripe": "^13.9.0",diff --git a/pages/view/[linkId]/chat.tsx b/pages/view/[linkId]/chat.tsxindex c6b5bc8c..197ac40c 100644--- a/pages/view/[linkId]/chat.tsx+++ b/pages/view/[linkId]/chat.tsx@@ -4,6 +4,7 @@ import prisma from "@/lib/prisma";import { getServerSession } from "next-auth";import { authOptions } from "@/pages/api/auth/[...nextauth]";import { CustomUser } from "@/lib/types";+import { Chat } from "@/components/chat/chat";const roleToColorMap: Record<Message["role"], string> = {system: "red",@@ -80,79 +81,16 @@ export const getServerSideProps = async (context: any) => {};};-export default function Chat({+export default function ChatPage({threadId,messages,}: {threadId: string;messages: Message[];}) {- const {- status,- messages: hookMessages,- input,- submitMessage,- handleInputChange,- error,- } = useAssistant({- api: "/api/assistants/chat",- threadId: threadId,- });-- const [combinedMessages, setCombinedMessages] = useState<Message[]>([]);-- useEffect(() => {- // Concatenate existing messages with messages from the hook- // and reverse the order so that the newest messages are at the bottom:- const reversedMessages = [...messages].reverse();- setCombinedMessages([...reversedMessages, ...hookMessages]);- }, [messages, hookMessages]);-- // When status changes to accepting messages, focus the input:- const inputRef = useRef<HTMLInputElement>(null);- useEffect(() => {- if (status === "awaiting_message") {- inputRef.current?.focus();- }- }, [status]);-return (- <div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">- {error != null && (- <div className="relative bg-red-500 text-white px-6 py-4 rounded-md">- <span className="block sm:inline">- Error: {(error as any).toString()}- </span>- </div>- )}-- {combinedMessages.map((m: Message) => (- <div- key={m.id}- className="whitespace-pre-wrap"- style={{ color: roleToColorMap[m.role] }}- >- <strong>{`${roleToNameMap[m.role]}: `}</strong>- {m.content}- <br />- <br />- </div>- ))}-- {status === "in_progress" && (- <div className="h-8 w-full max-w-md p-2 mb-8 bg-gray-300 dark:bg-gray-600 rounded-lg animate-pulse" />- )}-- <form onSubmit={submitMessage}>- <input- ref={inputRef}- disabled={status !== "awaiting_message"}- className="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl"- value={input}- placeholder="Summarize this document"- onChange={handleInputChange}- />- </form>- </div>+ <>+ <Chat initialMessages={messages} threadId={threadId} />+ </>);}