Skip to content
24 changes: 23 additions & 1 deletion src/hooks/use-expiration-date.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { _n, sprintf } from '@wordpress/i18n';
import { useI18n } from '@wordpress/react-i18n';
import { intervalToDuration, formatDuration, addDays, Duration, addHours } from 'date-fns';

Expand Down Expand Up @@ -36,8 +37,29 @@ export function useExpirationDate( snapshotDate: number ) {
// we show the minutes left.
end: addHours( endDate, 1 ),
} ),
{ format, delimiter: ', ' }
{
format,
delimiter: ', ',
locale: {
formatDistance: ( token, count ) => {
let stringToFormat = '';
switch ( token ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really readable and easy to understand!

case 'xDays':
stringToFormat = _n( '%d day', '%d days', count );
break;
case 'xHours':
stringToFormat = _n( '%d hour', '%d hours', count );
break;
case 'xMinutes':
stringToFormat = _n( '%d minute', '%d minutes', count );
break;
}
return sprintf( stringToFormat, count );
},
},
}
);

return {
isExpired,
countDown: isExpired ? __( 'Expired' ) : countDown,
Expand Down