/** * WordPress dependencies */ import { Button, Modal, SelectControl } from '@safe-wordpress/components'; import { _x, _nx, sprintf } from '@safe-wordpress/i18n'; /** * External dependencies */ import { ProgressBar } from '@nelio-content/components'; /** * Internal dependencies */ import { refreshAnalytics } from './utils'; import { useAttributes } from '../hooks'; import { DEFAULT_ATTRS } from './config'; import type { Attrs } from './config'; export type AnalyticsRefreshButtonProps = { readonly className: string; readonly name: string; }; export const AnalyticsRefreshButton = ( { className, name, }: AnalyticsRefreshButtonProps ): JSX.Element => { const [ attributes, setAttributes ] = useAttributes( name, DEFAULT_ATTRS ); const { error, refreshDisabled, isRefreshDialogOpen: isDialogOpen, isRefreshing, isStartingRefresh, isRefreshingOver, refreshPeriod: period, refreshPostCount: postCount, refreshPostIndex: postIndex, } = attributes; const openDialog = () => setAttributes( { error: false, isRefreshDialogOpen: true, isRefreshing: false, refreshPeriod: 'month', } ); const closeDialog = () => setAttributes( { error: false, isRefreshDialogOpen: false, isRefreshing: false, isStartingRefresh: false, isRefreshingOver: false, refreshPeriod: 'month', refreshPostCount: 0, refreshPostIndex: 0, } ); const setPeriod = ( refreshPeriod: Attrs[ 'refreshPeriod' ] ) => setAttributes( { refreshPeriod } ); const refresh = () => refreshAnalytics( period, ( attrs ) => setAttributes( attrs ) ); return ( <> { isDialogOpen && ( void null } >
{ ! isRefreshing && ! isRefreshingOver && ( ) } { isStartingRefresh && ( ) } { isRefreshing && ! isStartingRefresh && ( ) } { isRefreshingOver && ( ) } { !! error && (

{ error }

) }
{ ! isRefreshingOver && ( ) }
) } ); }; // ==== // DATA // ==== const PERIOD_OPTIONS: ReadonlyArray< { readonly value: Attrs[ 'refreshPeriod' ]; readonly label: string; } > = [ { value: 'month', label: _x( 'Posts from last month', 'text', 'nelio-content' ), }, { value: 'year', label: _x( 'Posts from last year', 'text', 'nelio-content' ), }, { value: 'all', label: _x( 'All posts', 'text', 'nelio-content' ), }, ];