chatbot-ui starter

This commit is contained in:
Mckay Wrigley
2023-03-13 19:21:14 -06:00
parent 4c9730e4cd
commit a6503fb498
25 changed files with 7259 additions and 71 deletions
+36
View File
@@ -0,0 +1,36 @@
import { Message } from "@/types";
import { FC } from "react";
import { ChatInput } from "./ChatInput";
import { ChatLoader } from "./ChatLoader";
import { ChatMessage } from "./ChatMessage";
interface Props {
messages: Message[];
loading: boolean;
onSend: (message: Message) => void;
}
export const Chat: FC<Props> = ({ messages, loading, onSend }) => {
return (
<div className="flex flex-col rounded-lg px-2 sm:p-4 sm:border border-neutral-300">
{messages.map((message, index) => (
<div
key={index}
className="my-1 sm:my-1.5"
>
<ChatMessage message={message} />
</div>
))}
{loading && (
<div className="my-1 sm:my-1.5">
<ChatLoader />
</div>
)}
<div className="mt-4 sm:mt-8 bottom-[56px] left-0 w-full">
<ChatInput onSend={onSend} />
</div>
</div>
);
};
+65
View File
@@ -0,0 +1,65 @@
import { Message } from "@/types";
import { IconArrowUp } from "@tabler/icons-react";
import { FC, KeyboardEvent, useEffect, useRef, useState } from "react";
interface Props {
onSend: (message: Message) => void;
}
export const ChatInput: FC<Props> = ({ onSend }) => {
const [content, setContent] = useState<string>();
const textareaRef = useRef<HTMLTextAreaElement>(null);
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const value = e.target.value;
if (value.length > 4000) {
alert("Message limit is 4000 characters");
return;
}
setContent(value);
};
const handleSend = () => {
if (!content) {
alert("Please enter a message");
return;
}
onSend({ role: "user", content });
setContent("");
};
const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
useEffect(() => {
if (textareaRef && textareaRef.current) {
textareaRef.current.style.height = "inherit";
textareaRef.current.style.height = `${textareaRef.current?.scrollHeight}px`;
}
}, [content]);
return (
<div className="relative">
<textarea
ref={textareaRef}
className="min-h-[44px] rounded-lg pl-4 pr-12 py-2 w-full focus:outline-none focus:ring-1 focus:ring-neutral-300 border-2 border-neutral-200"
style={{ resize: "none" }}
placeholder="Type a message..."
value={content}
rows={1}
onChange={handleChange}
onKeyDown={handleKeyDown}
/>
<button onClick={() => handleSend()}>
<IconArrowUp className="absolute right-2 bottom-3 h-8 w-8 hover:cursor-pointer rounded-full p-1 bg-blue-500 text-white hover:opacity-80" />
</button>
</div>
);
};
+17
View File
@@ -0,0 +1,17 @@
import { IconDots } from "@tabler/icons-react";
import { FC } from "react";
interface Props {}
export const ChatLoader: FC<Props> = () => {
return (
<div className="flex flex-col flex-start">
<div
className={`flex items-center bg-neutral-200 text-neutral-900 rounded-2xl px-4 py-2 w-fit`}
style={{ overflowWrap: "anywhere" }}
>
<IconDots className="animate-pulse" />
</div>
</div>
);
};
+19
View File
@@ -0,0 +1,19 @@
import { Message } from "@/types";
import { FC } from "react";
interface Props {
message: Message;
}
export const ChatMessage: FC<Props> = ({ message }) => {
return (
<div className={`flex flex-col ${message.role === "assistant" ? "items-start" : "items-end"}`}>
<div
className={`flex items-center ${message.role === "assistant" ? "bg-neutral-200 text-neutral-900" : "bg-blue-500 text-white"} rounded-2xl px-3 py-2 max-w-[67%] whitespace-pre-wrap`}
style={{ overflowWrap: "anywhere" }}
>
{message.content}
</div>
</div>
);
};