import React, { FC, PropsWithChildren } from 'react' import { Link, Stack, Text } from '@wordpress/ui' import { useI18n } from '@wordpress/react-i18n' import { sprintf } from '@wordpress/i18n' import useOrderOverview from './useOrderOverview' import Skeleton from './Skeleton' const DAY_MS = 24 * 60 * 60 * 1000 const startOfDay = (date: Date) => new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime() const calendarDaysSince = (date: Date, now: Date) => Math.round((startOfDay(now) - startOfDay(date)) / DAY_MS) const placedRelativeToToday = (createdAt: string) => new Intl.RelativeTimeFormat(document.documentElement.lang || undefined, { numeric: 'auto', }).format(-calendarDaysSince(new Date(createdAt), new Date()), 'day') const GlanceGroup: FC> = ({ title, children, }) => (

{title}

{children}
) const GlanceFigure: FC<{ value: number; label: string }> = ({ value, label, }) => (
{value} {label}
) const AtAGlance: FC = () => { const { __ } = useI18n() const overview = useOrderOverview() if (overview === 'loading') { return } if (overview === 'failed') { return ( {__( "The figures couldn't be loaded right now. Try again later.", 'posten-bring-checkout' )} ) } const { unfulfilled, openReturns } = overview return (
{unfulfilled.count > 0 && unfulfilled.oldestCreatedAt && ( {sprintf( /* translators: %s: when the oldest order was placed, e.g. "3 days ago" */ __('Oldest placed %s', 'posten-bring-checkout'), placedRelativeToToday(unfulfilled.oldestCreatedAt) )} )} {unfulfilled.count > 0 && ( {__('View orders', 'posten-bring-checkout')} )}
) } export default AtAGlance