@@ -16,6 +16,7 @@ import {isFlagEnabled} from 'src/shared/utils/featureFlag'
1616// Constants
1717import { PAYG_CREDIT_DAYS } from 'src/shared/constants'
1818import { DEFAULT_USAGE_TIME_RANGE } from 'src/shared/constants/timeRanges'
19+ import { MILLISECONDS_IN_ONE_DAY } from 'src/utils/datetime/constants'
1920
2021// Types
2122import {
@@ -79,6 +80,16 @@ export const UsageContext = React.createContext<UsageContextType>(
7980 DEFAULT_CONTEXT
8081)
8182
83+ export const calculateCreditDaysUsed = ( creditStartDate : string ) : number => {
84+ if ( ! creditStartDate ) {
85+ return NaN
86+ }
87+ const startDate = new Date ( creditStartDate )
88+ const current = new Date ( )
89+ const diffTime = current . getTime ( ) - startDate . getTime ( )
90+ return Math . floor ( diffTime / MILLISECONDS_IN_ONE_DAY )
91+ }
92+
8293export const UsageProvider : FC < Props > = React . memo ( ( { children} ) => {
8394 const [ billingDateTime , setBillingDateTime ] = useState ( '' )
8495 const [ usageVectors , setUsageVectors ] = useState ( [ ] )
@@ -103,12 +114,12 @@ export const UsageProvider: FC<Props> = React.memo(({children}) => {
103114 const { quartzMe} = useSelector ( getMe )
104115 const parser = isFlagEnabled ( 'fastFromFlux' ) ? fastFromFlux : fromFlux
105116
106- const creditDaysUsed = useMemo ( ( ) => {
107- const startDate = new Date ( quartzMe ?. paygCreditStartDate )
108- const current = new Date ( )
109- const diffTime = Math . abs ( current . getTime ( ) - startDate . getTime ( ) )
110- return Math . floor ( diffTime / ( 1000 * 60 * 60 * 24 ) )
111- } , [ quartzMe ?. paygCreditStartDate ] )
117+ const paygCreditStartDate = quartzMe ?. paygCreditStartDate ?? ''
118+
119+ const creditDaysUsed = useMemo (
120+ ( ) => calculateCreditDaysUsed ( paygCreditStartDate ) ,
121+ [ paygCreditStartDate ]
122+ )
112123
113124 const paygCreditEnabled =
114125 creditDaysUsed >= 0 && creditDaysUsed < PAYG_CREDIT_DAYS
0 commit comments