import { __ } from '@wordpress/i18n'; import React, { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { Area, Bar, CartesianGrid, ComposedChart, Legend, ResponsiveContainer, Tooltip, XAxis, YAxis, } from 'recharts'; import { Alert, Card, ChoiceGroup, Page, Spinner, StatCard, } from './UI/index.jsx'; import { api } from '../api/index.js'; import { cn, decodeHtmlEntities } from '../lib/utils.js'; const currencySymbol = decodeHtmlEntities( window.arrayfunnelAdmin?.currency?.symbol || '$' ); const DAY_OPTIONS = [ { value: 7, label: __( '7 Days', 'arrayfunnel' ) }, { value: 30, label: __( '30 Days', 'arrayfunnel' ) }, { value: 90, label: __( '90 Days', 'arrayfunnel' ) }, { value: 365, label: __( '1 Year', 'arrayfunnel' ) }, ]; export default function Home() { const [ days, setDays ] = useState( 7 ); const { data: overview, isLoading: loadingOverview, error: overviewError, } = useQuery( { queryKey: [ 'analytics', 'overview', days ], queryFn: () => api.getOverview( days ), staleTime: 2 * 60_000, gcTime: 15 * 60_000, } ); const { data: chartData, isLoading: loadingChart, error: chartError, } = useQuery( { queryKey: [ 'analytics', 'revenue', days ], queryFn: () => api.getRevenue( days ), staleTime: 2 * 60_000, gcTime: 15 * 60_000, } ); const stats = [ { label: __( 'Total Revenue', 'arrayfunnel' ), value: formatMoney( overview?.revenue ), icon: 'revenue', }, { label: __( 'Orders', 'arrayfunnel' ), value: overview?.orders || 0, icon: 'orders', }, { label: __( 'Avg Order Value', 'arrayfunnel' ), value: formatMoney( overview?.aov ), icon: 'aov', }, { label: __( 'Checkout Conversion', 'arrayfunnel' ), value: `${ overview?.conversion_rate || 0 }%`, icon: 'target', }, { label: __( 'Bump Accept Rate', 'arrayfunnel' ), value: `${ overview?.bump_rate || 0 }%`, icon: 'gift', }, { label: __( 'Recovered Revenue', 'arrayfunnel' ), value: formatMoney( overview?.recovered_revenue ), icon: 'recovery', }, ]; return ( } > { overviewError && ( { overviewError.message || __( 'Could not load the performance summary.', 'arrayfunnel' ) } ) } { loadingOverview ? (
) : overview ? (
{ stats.map( ( stat ) => ( ) ) }
) : null } { chartError ? ( { chartError.message || __( 'Could not load the revenue trend.', 'arrayfunnel' ) } ) : loadingChart ? (
) : chartData?.length ? ( `${ currencySymbol }${ value }` } /> [ entry.dataKey === 'revenue' ? formatMoney( value ) : value, name, ] } /> ) : (

{ __( 'No activity recorded for this period.', 'arrayfunnel' ) }

) }
{ ! loadingOverview && overview && (
) }
); } function MetricRow( { label, value, accent = false } ) { return (
{ label } { value }
); } function formatMoney( value ) { if ( value === null || value === undefined || value === '' ) { return '—'; } const amount = Number( value ); return Number.isFinite( amount ) ? `${ currencySymbol }${ amount.toFixed( 2 ) }` : '—'; }