diff --git a/assets/src/dashboard/parts/connected/dashboard/index.js b/assets/src/dashboard/parts/connected/dashboard/index.js index b0aa594f..968d6c88 100644 --- a/assets/src/dashboard/parts/connected/dashboard/index.js +++ b/assets/src/dashboard/parts/connected/dashboard/index.js @@ -13,9 +13,9 @@ import { } from '@wordpress/components'; import { sprintf } from '@wordpress/i18n'; - +import { useMemo } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; -import { warning, help } from '@wordpress/icons'; +import { warning, external, help } from '@wordpress/icons'; import { clearCache } from '../../../utils/api'; @@ -26,40 +26,25 @@ import { bolt, update, offloadImage, - settings + settings, + globe, + arrowDownToLine, + cloudDownload, + chartBarDecreasing } from '../../../utils/icons'; import ProgressBar from '../../components/ProgressBar'; -import DashboardMetricBox from '../../components/DashboardMetricBox'; import LastImages from './LastImages'; -import { useMemo } from 'react'; const cardClasses = 'flex p-6 bg-light-blue border border-blue-300 rounded-md'; -const metrics = [ - { - label: optimoleDashboardApp.strings.metrics.metricsTitle2, - description: optimoleDashboardApp.strings.metrics.metricsSubtitle2, - value: 'saved_size' - }, - { - label: optimoleDashboardApp.strings.metrics.metricsTitle3, - description: optimoleDashboardApp.strings.metrics.metricsSubtitle3, - value: 'compression_percentage' - }, - { - label: optimoleDashboardApp.strings.metrics.metricsTitle4, - description: optimoleDashboardApp.strings.metrics.metricsSubtitle4, - value: 'traffic' - } -]; - const settingsTab = { offload_image: 1, advance: 2 }; + const navigate = ( tabId ) => { const links = window.optimoleDashboardApp.submenu_links; const settingsLink = links.find( link => '#settings' === link.hash ); @@ -140,6 +125,50 @@ const Dashboard = () => { }; }); + const availableMetrics = useMemo( () => { + const metrics = [ + { + icon: arrowDownToLine, + label: optimoleDashboardApp.strings.metrics.metricsTitle2, + description: optimoleDashboardApp.strings.metrics.metricsSubtitle2, + value: 'saved_size', + hasButton: true, + buttonText: optimoleDashboardApp.strings.metrics.adjust_compression + }, + { + icon: chartBarDecreasing, + label: optimoleDashboardApp.strings.metrics.metricsTitle3, + description: optimoleDashboardApp.strings.metrics.metricsSubtitle3, + value: 'compression_percentage', + hasButton: true, + buttonText: optimoleDashboardApp.strings.metrics.adjust_compression + }, + { + icon: globe, + label: optimoleDashboardApp.strings.metrics.metricsTitle4, + description: optimoleDashboardApp.strings.metrics.metricsSubtitle4, + value: 'traffic', + hasButton: true, + buttonText: optimoleDashboardApp.strings.metrics.view_analytics + } + ]; + + if ( userData?.can_use_offloading ) { + metrics.push( + { + icon: cloudDownload, + label: optimoleDashboardApp.strings.metrics.metricsTitle5, + description: optimoleDashboardApp.strings.metrics.metricsSubtitle5, + value: 'offloaded_images', + hasButton: true, + buttonText: optimoleDashboardApp.strings.metrics.manage_offloading + } + ); + } + + return metrics; + }, [ userData ]); + const visitorsLimitPercent = ( ( userData.visitors / userData.visitors_limit ) * 100 ).toFixed( 0 ); const renewalDate = useMemo( () => { @@ -159,11 +188,15 @@ const Dashboard = () => { // Fallback for missing data if ( undefined === value ) { - value = 'saved_size' === type ? - Math.floor( Math.random() * 2500000 ) + 500000 : // Mock KB - 'traffic' === type ? - Math.floor( Math.random() * 2500 ) + 500 : // Mock MB - Math.floor( Math.random() * 40 ) + 10; // Mock Percentage + if ( 'saved_size' === type ) { + value = Math.floor( Math.random() * 2500000 ) + 500000; // Mock KB + } else if ( 'traffic' === type ) { + value = Math.floor( Math.random() * 2500 ) + 500; // Mock MB + } else if ( 'offloaded_images' === type ) { + value = Math.floor( Math.random() * 500 ) + 50; // Mock images count + } else { + value = Math.floor( Math.random() * 40 ) + 10; // Mock Percentage + } } switch ( type ) { @@ -196,6 +229,10 @@ const Dashboard = () => { unit = 'TB'; } break; + case 'offloaded_images': + formattedValue = parseInt( value ); + unit = 1 === formattedValue ? optimoleDashboardApp.strings.metrics.image : optimoleDashboardApp.strings.metrics.images; + break; default: formattedValue = parseFloat( value ).toFixed( 2 ); unit = ''; @@ -204,6 +241,19 @@ const Dashboard = () => { return { formattedValue, unit }; }; + const getMetricButtonAction = ( metricValue ) => { + switch ( metricValue ) { + case 'saved_size': + return { onClick: () => navigate( settingsTab.advance ) }; + case 'compression_percentage': + return { onClick: () => navigate( settingsTab.advance ) }; + case 'traffic': + return { href: window.optimoleDashboardApp.optimoleDashMetrics, target: '_blank' }; + case 'offloaded_images': + return { onClick: () => navigate( settingsTab.offload_image ) }; + } + }; + return (