chatbot-template/app/chat.tsx

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-05-19 12:33:56 -04:00
"use client";
import { useRouter } from "next/navigation";
import { Prompt } from "./prompt";
2023-06-02 11:57:44 -04:00
import { useChat, type Message } from "ai-connector";
2023-06-02 13:26:58 -04:00
import { ChatList } from "./chat-list";
2023-05-19 12:33:56 -04:00
export interface ChatProps {
// create?: (input: string) => Chat | undefined;
2023-06-02 11:15:04 -04:00
initialMessages?: Message[];
2023-05-19 12:33:56 -04:00
id?: string;
}
export function Chat({
2023-05-22 13:10:52 +02:00
id,
2023-05-19 12:33:56 -04:00
// create,
2023-06-02 11:15:04 -04:00
initialMessages,
2023-05-19 12:33:56 -04:00
}: ChatProps) {
const router = useRouter();
2023-06-02 11:15:04 -04:00
const { isLoading, messages, reload, append } = useChat({
initialMessages: initialMessages as any[],
id,
// onCreate: (id: string) => {
// router.push(`/chat/${id}`);
// },
});
2023-05-19 12:33:56 -04:00
return (
<main className="transition-width relative min-h-full w-full flex-1 overflow-y-auto flex flex-col">
<div className="flex-1">
2023-06-02 11:15:04 -04:00
<ChatList messages={messages} />
2023-05-19 12:33:56 -04:00
</div>
<div className="sticky light-gradient dark:bg-gradient-to-b dark:from-zinc-900 dark:to-zinc-950 bottom-0 left-0 w-full border-t bg-white dark:bg-black md:border-t-0 py-4 md:border-transparent md:!bg-transparent md:dark:border-transparent pr-0 lg:pr-[260px] flex dark:border-transparent items-center justify-center">
<Prompt
2023-06-02 11:15:04 -04:00
onSubmit={(value) => {
append({
content: value,
role: "user",
});
}}
onRefresh={messages.length ? reload : undefined}
2023-05-19 12:33:56 -04:00
isLoading={isLoading}
/>
</div>
</main>
);
}
Chat.displayName = "Chat";