import * as Tooltip from '@radix-ui/react-tooltip'; import React, {ReactNode, useEffect, useRef, useState} from "react"; import {ArchiveBoxIcon, BoltIcon, CheckCircleIcon, DocumentMinusIcon} from "@heroicons/react/24/solid"; import SpeedInsightGroup from "./group"; import AppButton from "components/ui/app-button"; import {useAppContext} from "../../../context/app"; import {useDispatch, useSelector} from "react-redux"; import {Skeleton} from "components/ui/skeleton" import {optimizerData} from "../../../store/app/appSelector"; import TooltipText from "components/ui/tooltip-text"; import { HoverCard, HoverCardContent, HoverCardTrigger, } from "@/components/ui/hover-card" import {HoverCardPortal} from "@radix-ui/react-hover-card"; import {Archive, Circle, Dot, FileCode2, FileMinus2, GaugeCircle, Loader, Monitor} from "lucide-react"; import {useToast} from "components/ui/use-toast"; import {ComputerDesktopIcon, DevicePhoneMobileIcon} from "@heroicons/react/24/outline"; import {ThunkDispatch} from "redux-thunk"; import {AppAction, RootState} from "../../../store/app/appTypes"; import {changeReport} from "../../../store/app/appActions"; import ThemeSwitcher from "components/ui/theme-switcher"; import {Toaster} from "components/ui/toaster"; import PerformanceProgressBar from "components/performance-progress-bar"; import {ExclamationCircleIcon} from "@heroicons/react/20/solid"; import ErrorFetch from "components/ErrorFetch"; import {cn} from "lib/utils"; import PageSpeedWidget from "app/dashboard/components/performance-widgets/PageSpeedWidget"; import useCommonDispatch from "hooks/useCommonDispatch"; import {setCommonState} from "../../../store/common/commonActions"; const Content = ({ dashboard = false }) => { const { options, version} = useAppContext() const {data, error, loading, activeReport} = useSelector(optimizerData); const [performance, setPerformance] = useState(0) const { toast } = useToast() const [progressbarColor, setProgressbarColor] = useState('#ECECED'); const [on, setOn] = useState(false) const dispatch: ThunkDispatch = useDispatch(); const progressBarColor = () => { const performance = data?.performance?? 0; if (performance < 50) { setProgressbarColor('#FF3333'); } else if (performance < 90) { setProgressbarColor('#FFAA33'); } else if (performance < 101) { setProgressbarColor('#09B42F'); } }; useEffect(() => { progressBarColor(); if (!loading && data) { let currentNumber = 0; const timer = setInterval(() => { currentNumber += 1; if (currentNumber <=data.performance) { setPerformance(currentNumber) } else { clearInterval(timer); } }, 10); // Change the delay (in milliseconds) as desired return () => clearInterval(timer); } }, [data, loading]) const calculateOpacity = () => { if (!data) { return 0; } const targetNumber =data.performance; const maxOpacity = 1; const minOpacity = 0; const opacityIncrement = (maxOpacity - minOpacity) / targetNumber; return minOpacity + opacityIncrement * performance; }; const PopupActions = () => { if (!options.actions) { return <> } let [actions, setActions] = useState(options.actions.map((a :WordPressOptions['actions'][0]) => ({ ...a, loading: false }))) let icons = { clear_cache : , clear_page_cache : , clear_optimization : } const triggerAction = async (action: RapidLoadGlobalAction) => { try { setActions(prev => prev.map(a => a.icon === action.icon ? { ...a, loading: true } : a )) let result = await fetch(action.href.replace(/&/g, '&')); toast({ description:
Successfully completed
}) } catch (e) { console.error(e); } setActions(prev => prev.map(a => a.icon === action.icon ? { ...a, loading: false } : a )) } return ( <> {actions.map(action => ( triggerAction(action)} className='rounded-[15px] dark:bg-brand-950/50 bg-brand-100/50' variant='outline'> {action.loading && } {icons[action.icon]} ))} ); } const newUrl = (() => { if (!options.dashboard_url) { return '#'; } const newQueryParam = `optimize-url=${options.optimizer_url}`; const [baseUrl, queryParams] = options.dashboard_url.split('?'); return `${baseUrl}?${newQueryParam}${queryParams ? '&' + queryParams : ''}#/optimize`; })(); return (
{dashboard && ( <>
dispatch(changeReport('mobile'))} className={`relative z-1 text-sm flex flex-column gap-2 px-4 py-3 font-medium rounded-2xl`}>
dispatch(changeReport('desktop'))} className={`relative z-1 text-sm flex flex-column gap-2 pl-2 px-4 py-3 font-medium rounded-2xl`}>
)}
{loading || on || error ? ( ) : ( dashboard ? ( ) : ( ) )}
{!dashboard && ( <>
0-49
50-89
89-100
)}
Speed insights v{version}
{error ?
: <> {loading || on ? (
) : (
)}
}
) } const SpeedInsights = ({children, dashboardMode }: { children?: ReactNode, dashboardMode?: boolean, }) => { const {options} = useAppContext() const root = options?.plugin_url if (dashboardMode) { return ; } return (
{children && children}
); } export default SpeedInsights