diff --git a/client/src/component/Blog.jsx b/client/src/component/Blog.jsx index 2de9897..b4061f9 100644 --- a/client/src/component/Blog.jsx +++ b/client/src/component/Blog.jsx @@ -1,7 +1,5 @@ -import { useEffect, useState } from "react"; - +import { useEffect, useState, useRef } from "react"; import PropTypes from 'prop-types'; - import { Search } from "lucide-react"; import { Link } from "react-router-dom"; @@ -11,7 +9,7 @@ import img3 from "../assets/blogs/3.png"; import img4 from "../assets/blogs/4.jpeg"; import img5 from "../assets/blogs/5.jpeg"; import img6 from "../assets/blogs/6.png"; - + const images = [ { src: img1, category: "Web Development" }, { src: img2, category: "Mobile Development" }, @@ -23,12 +21,16 @@ const images = [ BlogPage.propTypes = { mode: PropTypes.string.isRequired, - }; + export default function BlogPage(props) { const [searchTerm, setSearchTerm] = useState(""); const [selectedCategory, setSelectedCategory] = useState("All"); const [blogPosts, setBlogPosts] = useState([]); + const [isPaused, setIsPaused] = useState(false); + const [currentPosition, setCurrentPosition] = useState(0); + const [isPlaying, setIsPlaying] = useState(false); + const speechInstanceRef = useRef(null); const categories = ["All", ...new Set(blogPosts.map((post) => post.category))]; @@ -62,11 +64,49 @@ export default function BlogPage(props) { let response = await fetch("http://localhost:5000/api/blog/all-blog"); let data = await response.json(); setBlogPosts(data.blogs); + console.log(data.blogs); } catch (error) { console.error(error); } }; + const handlePlay = (id, title, excerpt) => { + const textToSpeak = `${title}. ${excerpt}`; + document.getElementsByClassName(`pause-button-${id}`)[0].classList.remove('hidden') + document.getElementsByClassName(`speak-button-${id}`)[0].classList.add('hidden') + console.log(textToSpeak) + if (isPaused && speechInstanceRef.current) { + setIsPaused(true); + window.speechSynthesis.speak(speechInstanceRef.current); + } else { + const speechInstance = new SpeechSynthesisUtterance(textToSpeak.slice(currentPosition)); + speechInstance.lang = 'en-US'; + speechInstance.pitch = 1; + speechInstance.rate = 1; + + speechInstance.onboundary = (event) => { + if (event.name === 'word') { + setCurrentPosition(event.charIndex); + } + }; + + speechInstanceRef.current = speechInstance; + window.speechSynthesis.speak(speechInstance); + } + setIsPlaying(true); + }; + + const handlePause = (id) => { + document.getElementsByClassName(`pause-button-${id}`)[0].classList.add('hidden') + document.getElementsByClassName(`speak-button-${id}`)[0].classList.remove('hidden') + if (speechInstanceRef.current && !isPaused) { + window.speechSynthesis.cancel(); + setIsPaused(false); + } + setIsPlaying(false); + setCurrentPosition(0) + }; + useEffect(() => { fetchData(); }, []); @@ -124,30 +164,53 @@ export default function BlogPage(props) {
- The page you are looking for doesnt exist. Here are some + The page you are looking for doesnt exist. Here are some helpful links: - +