@@ -45,30 +45,34 @@ export default function UsageBasedBillingConfig({ attributionId }: Props) {
4545
4646 const localStorageKey = `pendingStripeSubscriptionFor${ attributionId } ` ;
4747 const now = dayjs ( ) . utc ( true ) ;
48- const billingPeriodFrom = now . startOf ( "month" ) ;
49- const billingPeriodTo = now . endOf ( "month" ) ;
48+ const [ billingCycleFrom , setBillingCycleFrom ] = useState < dayjs . Dayjs > ( now . startOf ( "month" ) ) ;
49+ const [ billingCycleTo , setBillingCycleTo ] = useState < dayjs . Dayjs > ( now . endOf ( "month" ) ) ;
50+
51+ const refreshSubscriptionDetails = async ( attributionId : string ) => {
52+ setStripeSubscriptionId ( undefined ) ;
53+ setIsLoadingStripeSubscription ( true ) ;
54+ try {
55+ const [ subscriptionId , costCenter ] = await Promise . all ( [
56+ getGitpodService ( ) . server . findStripeSubscriptionId ( attributionId ) ,
57+ getGitpodService ( ) . server . getCostCenter ( attributionId ) ,
58+ ] ) ;
59+ setStripeSubscriptionId ( subscriptionId ) ;
60+ setUsageLimit ( costCenter ?. spendingLimit ) ;
61+ setBillingCycleFrom ( dayjs ( costCenter ?. billingCycleStart || now . startOf ( "month" ) ) . utc ( true ) ) ;
62+ setBillingCycleTo ( dayjs ( costCenter ?. nextBillingTime || now . endOf ( "month" ) ) . utc ( true ) ) ;
63+ } catch ( error ) {
64+ console . error ( "Could not get Stripe subscription details." , error ) ;
65+ setErrorMessage ( `Could not get Stripe subscription details. ${ error ?. message || String ( error ) } ` ) ;
66+ } finally {
67+ setIsLoadingStripeSubscription ( false ) ;
68+ }
69+ } ;
5070
5171 useEffect ( ( ) => {
5272 if ( ! attributionId ) {
5373 return ;
5474 }
55- ( async ( ) => {
56- setStripeSubscriptionId ( undefined ) ;
57- setIsLoadingStripeSubscription ( true ) ;
58- try {
59- const [ subscriptionId , limit ] = await Promise . all ( [
60- getGitpodService ( ) . server . findStripeSubscriptionId ( attributionId ) ,
61- getGitpodService ( ) . server . getUsageLimit ( attributionId ) ,
62- ] ) ;
63- setStripeSubscriptionId ( subscriptionId ) ;
64- setUsageLimit ( limit ) ;
65- } catch ( error ) {
66- console . error ( "Could not get Stripe subscription details." , error ) ;
67- setErrorMessage ( `Could not get Stripe subscription details. ${ error ?. message || String ( error ) } ` ) ;
68- } finally {
69- setIsLoadingStripeSubscription ( false ) ;
70- }
71- } ) ( ) ;
75+ refreshSubscriptionDetails ( attributionId ) ;
7276 } , [ attributionId ] ) ;
7377
7478 useEffect ( ( ) => {
@@ -156,8 +160,7 @@ export default function UsageBasedBillingConfig({ attributionId }: Props) {
156160 if ( ! pollStripeSubscriptionTimeout ) {
157161 // Refresh Stripe subscription in 5 seconds in order to poll for upgrade confirmation
158162 const timeout = setTimeout ( async ( ) => {
159- const subscriptionId = await getGitpodService ( ) . server . findStripeSubscriptionId ( attributionId ) ;
160- setStripeSubscriptionId ( subscriptionId ) ;
163+ await refreshSubscriptionDetails ( attributionId ) ;
161164 setPollStripeSubscriptionTimeout ( undefined ) ;
162165 } , 5000 ) ;
163166 setPollStripeSubscriptionTimeout ( timeout ) ;
@@ -178,12 +181,12 @@ export default function UsageBasedBillingConfig({ attributionId }: Props) {
178181 const response = await getGitpodService ( ) . server . listUsage ( {
179182 attributionId,
180183 order : Ordering . ORDERING_DESCENDING ,
181- from : billingPeriodFrom . toDate ( ) . getTime ( ) ,
184+ from : billingCycleFrom . toDate ( ) . getTime ( ) ,
182185 to : Date . now ( ) ,
183186 } ) ;
184187 setCurrentUsage ( response . creditsUsed ) ;
185188 } ) ( ) ;
186- } , [ attributionId ] ) ;
189+ } , [ attributionId , billingCycleFrom ] ) ;
187190
188191 const showSpinner = ! attributionId || isLoadingStripeSubscription || ! ! pendingStripeSubscription ;
189192 const showBalance = ! showSpinner && ! ( AttributionId . parse ( attributionId ) ?. kind === "team" && ! stripeSubscriptionId ) ;
@@ -255,8 +258,15 @@ export default function UsageBasedBillingConfig({ attributionId }: Props) {
255258 < div className = "flex-grow" >
256259 < div className = "uppercase text-sm text-gray-400 dark:text-gray-500" > Current Period</ div >
257260 < div className = "text-sm font-medium text-gray-500 dark:text-gray-400" >
258- < span className = "font-semibold" > { `${ billingPeriodFrom . format ( "MMMM YYYY" ) } ` } </ span > { " " }
259- { `(${ billingPeriodFrom . format ( "MMM D" ) } ` + ` - ${ billingPeriodTo . format ( "MMM D" ) } )` }
261+ < span className = "font-semibold" > { `${ billingCycleFrom . format ( "MMMM YYYY" ) } ` } </ span > (
262+ < span title = { billingCycleFrom . toDate ( ) . toUTCString ( ) . replace ( "GMT" , "UTC" ) } >
263+ { billingCycleFrom . format ( "MMM D" ) }
264+ </ span > { " " }
265+ -{ " " }
266+ < span title = { billingCycleTo . toDate ( ) . toUTCString ( ) . replace ( "GMT" , "UTC" ) } >
267+ { billingCycleTo . format ( "MMM D" ) }
268+ </ span >
269+ )
260270 </ div >
261271 </ div >
262272 < div >
0 commit comments