Skip to content

Commit

Permalink
[dashboard] Show a tooltip with billing cycle dates when hovering ove…
Browse files Browse the repository at this point in the history
…r remaining hours
  • Loading branch information
jankeromnes committed Jun 5, 2021
1 parent b6ce008 commit bc7996c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions components/dashboard/src/settings/Plans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import React, { useState, useEffect, useContext } from "react";
import { countries } from 'countries-list';
import { AccountStatement, Subscription, UserPaidSubscription, AssignedTeamSubscription } from "@gitpod/gitpod-protocol/lib/accounting-protocol";
import { AccountStatement, Subscription, UserPaidSubscription, AssignedTeamSubscription, CreditDescription } from "@gitpod/gitpod-protocol/lib/accounting-protocol";
import { PlanCoupon, GithubUpgradeURL } from "@gitpod/gitpod-protocol/lib/payment-protocol";
import { Plans, Plan, Currency, PlanType } from "@gitpod/gitpod-protocol/lib/plans";
import { ChargebeeClient } from "../chargebee/chargebee-client";
Expand Down Expand Up @@ -433,7 +433,7 @@ export default function () {
{!assignedTs && (
<p className="text-base w-96 m-auto">Upgrade your plan to get access to private repositories or more parallel workspaces.</p>
)}
<p className="mt-2 font-semibold text-gray-500">Remaining hours: {typeof(accountStatement?.remainingHours) === 'number'
<p className="mt-2 font-semibold text-gray-500" title={guessCurrentBillingCycle(currentPlan, accountStatement).map(d => d.toLocaleDateString()).join(' - ')}>Remaining hours: {typeof(accountStatement?.remainingHours) === 'number'
? Math.floor(accountStatement.remainingHours * 10) / 10
: accountStatement?.remainingHours}</p>
{(typeof(accountStatement?.remainingHours) === 'number' && typeof(currentPlan.hoursPerMonth) === 'number')
Expand Down Expand Up @@ -599,3 +599,20 @@ function applyCoupons(plan: Plan, coupons: PlanCoupon[] | undefined): PlanWithOr
originalPrice: plan.pricePerMonth
}
}

// Look for relevant billing cycle dates in the account statement's computed credits.
function guessCurrentBillingCycle(currentPlan: Plan, accountStatement?: AccountStatement): Date[] {
if (!accountStatement) {
return [];
}
try {
const now = new Date().toISOString();
const credit = accountStatement.credits.find(c => c.date < now && c.expiryDate >= now && (c.description as CreditDescription)?.planId === currentPlan.chargebeeId);
if (!!credit) {
return [new Date(credit.date), new Date(credit.expiryDate)];
}
return [];
} catch (error) {
return [];
}
}

0 comments on commit bc7996c

Please sign in to comment.