import React, { useState, useEffect, useRef } from "react"; import { useNavigate, useLocation } from "react-router-dom"; import Navbar from "./Components/Navbar"; import axios from "axios"; import "./App.css"; import YouTube from "react-youtube"; function App() { const navigate = useNavigate(); const [messages, setMessages] = useState([]); const [userInput, setUserInput] = useState(""); const location = useLocation(); // Access the location object const videoLink = location.state?.videoLink; // Access the videoLink from the state const [embedLink, setEmbedLink] = useState(""); const [videoId, setVideoId] = useState(""); const endOfMessagesRef = useRef(null); const scrollToBottom = () => { if (endOfMessagesRef.current) { endOfMessagesRef.current.scrollIntoView({ behavior: "smooth" }); } }; useEffect(() => { scrollToBottom(); }, [messages]); const [loading, setLoading] = useState(false); console.log(videoLink); useEffect(() => { function convertToEmbedUrl(watchUrl) { return watchUrl.replace("/watch?v=", "/embed/"); } if (videoLink) { setEmbedLink(convertToEmbedUrl(videoLink)); setVideoId(videoLink.split("v=")[1]); } else { navigate("/"); } }, []); const sendMessage = async () => { try { console.log("Sending message..."); setLoading(true); const response = await axios.post("http://localhost:5000/message", { prompt: userInput, }); setMessages([ ...messages, { role: "user", content: userInput }, { role: "assistant", content: response.data.assistant_message }, ]); setUserInput(""); setLoading(false); } catch (error) { console.log(error); } }; const handleKeyPress = (e) => { if (e.key === "Enter" && userInput.trim() !== "") { sendMessage(); e.preventDefault(); // To prevent any default behavior, such as a line break in a textarea } }; return (
{/* YouTube Video Player */}

Chat with Video!

{messages.map((msg, idx) => (
{msg.content}
))} {loading && (
Getting response...
)}
setUserInput(e.target.value)} onKeyPress={handleKeyPress} placeholder="Type your query here..." className="flex-grow px-2 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring focus:border-indigo-500" />
); } export default App;