import { createStaticRanges } from 'react-date-range' ; import { TFunction } from 'i18next' ; import { addDays , endOfDay , startOfDay , startOfMonth , endOfMonth , addMonths , startOfWeek , endOfWeek , isSameDay , differenceInCalendarDays , } from 'date-fns' ; type Range = { startDate : Date ; endDate : Date ; key : string ; } ; const defineds = { startOfWeek : startOfWeek ( new Date ( ) ) , endOfWeek : endOfWeek ( new Date ( ) ) , startOfLastWeek : startOfWeek ( addDays ( new Date ( ) , - 7 ) ) , endOfLastWeek : endOfWeek ( addDays ( new Date ( ) , - 7 ) ) , startOfToday : startOfDay ( new Date ( ) ) , endOfToday : endOfDay ( new Date ( ) ) , startOfYesterday : startOfDay ( addDays ( new Date ( ) , - 1 ) ) , endOfYesterday : endOfDay ( addDays ( new Date ( ) , - 1 ) ) , startOfMonth : startOfMonth ( new Date ( ) ) , endOfMonth : endOfMonth ( new Date ( ) ) , startOfLastMonth : startOfMonth ( addMonths ( new Date ( ) , - 1 ) ) , endOfLastMonth : endOfMonth ( addMonths ( new Date ( ) , - 1 ) ) , } ; export const getStaticRanges = ( t ) => createStaticRanges ( [ { label : t ( 'common.dateRanges.today' ) , range : ( ) => ( { startDate : defineds . startOfToday , endDate : defineds . endOfToday , } ) , } , { label : t ( 'common.dateRanges.yesterday' ) , range : ( ) => ( { startDate : defineds . startOfYesterday , endDate : defineds . endOfYesterday , } ) , } , { label : t ( 'common.dateRanges.thisWeek' ) , range : ( ) => ( { startDate : defineds . startOfWeek , endDate : defineds . endOfWeek , } ) , } , { label : t ( 'common.dateRanges.lastWeek' ) , range : ( ) => ( { startDate : defineds . startOfLastWeek , endDate : defineds . endOfLastWeek , } ) , } , { label : t ( 'common.dateRanges.thisMonth' ) , range : ( ) => ( { startDate : defineds . startOfMonth , endDate : defineds . endOfMonth , } ) , } , { label : t ( 'common.dateRanges.lastMonth' ) , range : ( ) => ( { startDate : defineds . startOfLastMonth , endDate : defineds . endOfLastMonth , } ) , } , ] ) ; export const getInputRanges = ( t ) => [ { label : t ( 'common.dateRanges.daysUpToToday' ) , range ( value ) { return { startDate : addDays ( defineds . startOfToday , ( Math . max ( Number ( value ) , 1 ) - 1 ) * - 1 ) , endDate : defineds . endOfToday , } ; } , getCurrentValue ( range ) { if ( ! isSameDay ( range . endDate , defineds . endOfToday ) ) return '-' ; if ( ! range . startDate ) return '∞' ; return differenceInCalendarDays ( defineds . endOfToday , range . startDate ) + 1 ; } , } , { label : t ( 'common.dateRanges.daysStartingToday' ) , range ( value ) { const today = new Date ( ) ; return { startDate : today , endDate : addDays ( today , Math . max ( Number ( value ) , 1 ) - 1 ) , } ; } , getCurrentValue ( range ) { if ( ! isSameDay ( range . startDate , defineds . startOfToday ) ) return '-' ; if ( ! range . endDate ) return '∞' ; return differenceInCalendarDays ( range . endDate , defineds . startOfToday ) + 1 ; } , } , ] ;