From d99451c165d62cce0c35d83f28054f646b62687e Mon Sep 17 00:00:00 2001 From: Tetiana Mohorian Date: Mon, 28 Apr 2025 23:02:44 +0000 Subject: [PATCH] Aktualizovat sk1/frontend/src/components/InfoBox.jsx --- sk1/frontend/src/components/InfoBox.jsx | 182 +++++++++++++++++++----- 1 file changed, 148 insertions(+), 34 deletions(-) diff --git a/sk1/frontend/src/components/InfoBox.jsx b/sk1/frontend/src/components/InfoBox.jsx index 7e67568..e47dd80 100644 --- a/sk1/frontend/src/components/InfoBox.jsx +++ b/sk1/frontend/src/components/InfoBox.jsx @@ -1,34 +1,148 @@ -import React, { useState } from 'react'; -import ChatInput from './ChatInput.jsx'; - -const InfoBox = () => { - - - - const [headerText, setHeaderText] = useState('Analyzujte text na nenávistný jazyk'); - const [paragraphText, setParagraphText] = useState('Tento nástroj využíva umelú inteligenciu na identifikáciu toxického obsahu v textoch. Stačí zadať text a zistiť, či obsahuje nenávistný jazyk.'); - - const handleSendMessage = (userMessage, serverResponse) => { - console.log('Отправлено сообщение:', userMessage); - console.log('Ответ от сервера:', serverResponse); - - setHeaderText(serverResponse); // В заголовок пишем ответ от сервера - setParagraphText(`Váš text bol: "${userMessage}"`);// В параграф пишем сообщение пользователя - }; - - - return ( - - - -
-

{headerText}

-

{paragraphText}

- - {} - -
-); -} - -export default InfoBox; +import React, { useState, useEffect } from 'react'; +import ChatInput from './ChatInput.jsx'; +import Historia from './Historia.jsx'; +import { ChevronDown, ChevronUp } from 'lucide-react'; + +const InfoBox = () => { + const [headerText, setHeaderText] = useState('Analyzujte text na nenávistný jazyk'); + const [paragraphText, setParagraphText] = useState('Tento nástroj využíva umelú inteligenciu na identifikáciu toxického obsahu v textoch. Stačí zadať text a zistiť, či obsahuje nenávistný jazyk.'); + const [history, setHistory] = useState([]); + const [showHistory, setShowHistory] = useState(false); + const [isLoading, setIsLoading] = useState(false); + + const fetchHistory = async () => { + try { + const response = await fetch("https://hate-backend-production.up.railway.app/api/history"); + const data = await response.json(); + setHistory(data.reverse()); + } catch (err) { + console.error("Nepodarilo sa načítať históriu:", err); + } + }; + + const handleSendMessage = async (userMessage) => { + setHeaderText("Analyzujem text..."); + setParagraphText("Analyzujeme váš text, prosím čakajte..."); + setIsLoading(true); + const startTime = Date.now(); + + try { + const response = await fetch("https://hate-backend-production.up.railway.app/api/predict", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ text: userMessage }), + }); + const data = await response.json(); + const elapsed = Date.now() - startTime; + const minimumDelay = 5000; + + if (elapsed < minimumDelay) { + await new Promise(resolve => setTimeout(resolve, minimumDelay - elapsed)); + } + + if (response.ok) { + setHeaderText(data.prediction); + setParagraphText(`Váš text bol: "${userMessage}"`); + fetchHistory(); + } else { + console.error("Server vrátil chybu:", data.error); + } + } catch (error) { + console.error("Chyba pri odosielaní:", error); + } finally { + setIsLoading(false); + } + }; + + useEffect(() => { + fetchHistory(); + }, []); + + + return ( +
+

{isLoading ? 'Analyzujem text...' : headerText}

+

{isLoading ? 'Prosím čakajte, prebieha analýza.' : paragraphText}

+ +
+ + + {isLoading && ( +
+
+
+ )} + +
+ + + {showHistory && ( +
+
+
+ +
+
+
+ )} +
+
+
+ ); +}; + +export default InfoBox;