import {useContext, useEffect, useState} from "react"; import {faArrowDown, faArrowUp, faClockRotateLeft, faPingPongPaddleBall, faWaveSquare} from "@fortawesome/free-solid-svg-icons"; import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; import {generateRelativeTime} from "./utils"; import {StatusContext} from "@/common/contexts/Status"; import {useAlert} from "@/common/contexts/Alert"; import {SpeedtestContext} from "@/common/contexts/Speedtests"; import {ConfigContext} from "@/common/contexts/Config"; import BorderAnimation from "@/common/components/BorderAnimation"; import "./styles.sass"; import {getIconBySpeed} from "@/common/utils/TestUtil"; import {downloadInfo, jitterInfo, latestTestInfo, pingInfo, uploadInfo} from "@/pages/Home/components/LatestTest/utils/dialogs"; import {t} from "i18next"; import {PreferencesContext} from "@/common/contexts/Preferences"; import {convertSpeed, getSpeedUnit} from "@/common/utils/FormatUtil"; const LoadingValue = ({ children }) => { return ( {children} ); }; const LatestTestComponent = () => { const status = useContext(StatusContext)[0]; const [latest, setLatest] = useState(null); const [latestTestTime, setLatestTestTime] = useState("N/A"); const alert = useAlert(); const {speedtests} = useContext(SpeedtestContext); const config = useContext(ConfigContext)[0]; const [preferences] = useContext(PreferencesContext); const speedUnit = getSpeedUnit(preferences); useEffect(() => { setLatest(speedtests.length !== 0 ? speedtests[0] : {ping: "N/A", download: "N/A", upload: "N/A"}); }, [speedtests]); useEffect(() => { if (latest) setLatestTestTime(generateRelativeTime(latest.created)); const interval = setInterval(() => setLatestTestTime(generateRelativeTime(latest ? latest.created : 0)), 1000); return () => clearInterval(interval); }, [latest]); if (Object.entries(config).length === 0) return (<>>); if (latest === null) return (<>>); const getAreaClass = () => { if (status.running) return "analyse-area test-running"; if (status.paused) return "analyse-area tests-paused"; return "analyse-area pulse"; }; const showPingInfo = () => { const info = pingInfo(); alert.openAlert(info.title, info.description, {buttonText: info.buttonText}); }; const showJitterInfo = () => { const info = jitterInfo(); alert.openAlert(info.title, info.description, {buttonText: info.buttonText}); }; const showDownloadInfo = () => { const info = downloadInfo(); alert.openAlert(info.title, info.description, {buttonText: info.buttonText}); }; const showUploadInfo = () => { const info = uploadInfo(); alert.openAlert(info.title, info.description, {buttonText: info.buttonText}); }; const showLatestTestInfo = () => { const info = latestTestInfo(latest, preferences); alert.openAlert(info.title, info.description, {buttonText: info.buttonText}); }; return (