@@ -147,6 +150,7 @@ export default function Home() {
setPromptValue={setInputValue}
setMessages={setMessages}
handleChat={handleChat}
+ handleInitialChat={handleInitialChat}
topic={topic}
/>
diff --git a/components/Chat.tsx b/components/Chat.tsx
index 6aa7bc3..1253c7e 100644
--- a/components/Chat.tsx
+++ b/components/Chat.tsx
@@ -3,6 +3,7 @@ import FinalInputArea from "./FinalInputArea";
import { useEffect, useRef, useState } from "react";
import simpleLogo from "../public/simple-logo.png";
import Image from "next/image";
+import RelatedTopics from "./RelatedTopics";
export default function Chat({
messages,
@@ -12,6 +13,7 @@ export default function Chat({
setMessages,
handleChat,
topic,
+ handleInitialChat,
}: {
messages: { role: string; content: string }[];
disabled: boolean;
@@ -22,6 +24,7 @@ export default function Chat({
>;
handleChat: () => void;
topic: string;
+ handleInitialChat: () => Promise
;
}) {
const messagesEndRef = useRef(null);
const scrollableContainerRef = useRef(null);
@@ -60,7 +63,7 @@ export default function Chat({
return (
-
+
Topic:
{topic}
@@ -99,7 +102,9 @@ export default function Chat({
{Array.from(Array(10).keys()).map((i) => (
))}
@@ -108,7 +113,13 @@ export default function Chat({
-
+
+
+
>;
+ handleInitialChat: (newTopic?: string) => Promise;
+}) {
+ const [relatedTopics, setRelatedTopics] = useState([]);
+ const [loading, setLoading] = useState(false);
+
+ useEffect(() => {
+ // Function to fetch related topics from the API
+ const fetchRelatedTopics = async () => {
+ setLoading(true);
+ try {
+ const response = await fetch("/api/getRelatedTopics", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ topic }),
+ });
+
+ if (response.ok) {
+ const data = await response.json();
+ setRelatedTopics(data.topics);
+ }
+ } catch (error) {
+ console.error("Failed to fetch related topics:", error);
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ if (topic) {
+ fetchRelatedTopics(); // Fetch topics if a topic is provided
+ }
+ }, [topic]);
+
+ // Function to handle click on a related topic
+ const handleTopicClick = (relatedTopic: string) => {
+ setPromptValue(relatedTopic); // Set the prompt value to the clicked topic
+ handleInitialChat(relatedTopic); // Initiate chat with the clicked topic
+ };
+
+ return (
+
+
+ Related Topics:
+
+ {loading ? (
+
Loading...
+ ) : relatedTopics.length > 0 ? (
+
+ {relatedTopics.map((relatedTopic, index) => (
+
+ {index > 0 && "|"}
+ - handleTopicClick(relatedTopic)}
+ className="text-blue-500 hover:cursor-pointer"
+ >
+ {relatedTopic}
+
+
+ ))}
+
+ ) : null}
+
+ );
+}