Skip to content

Commit 4939625

Browse files
committed
[dashboard] Show billing period start and end dates in usage-based billing settings
1 parent 2b3be0a commit 4939625

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

components/dashboard/src/components/UsageBasedBillingConfig.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export default function UsageBasedBillingConfig({ attributionId }: Props) {
4141
const [billingError, setBillingError] = useState<string | undefined>();
4242

4343
const localStorageKey = `pendingStripeSubscriptionFor${attributionId}`;
44-
const now = new Date();
44+
const billingPeriodFrom = new Date(new Date().toISOString().slice(0, 7) + "-01"); // First day of this month: YYYY-MM-01T00:00:00.000Z
45+
const billingPeriodTo = new Date(billingPeriodFrom.getUTCFullYear(), billingPeriodFrom.getMonth() + 1); // First day of next month
46+
billingPeriodTo.setMilliseconds(billingPeriodTo.getMilliseconds() - 1); // Last millisecond of this month
4547

4648
useEffect(() => {
4749
if (!attributionId) {
@@ -161,7 +163,7 @@ export default function UsageBasedBillingConfig({ attributionId }: Props) {
161163
const response = await getGitpodService().server.listUsage({
162164
attributionId,
163165
order: Ordering.ORDERING_DESCENDING,
164-
from: new Date(now.toISOString().slice(0, 7) + "-01").getTime(),
166+
from: billingPeriodFrom.getTime(),
165167
to: Date.now(),
166168
});
167169
setCurrentUsage(response.creditsUsed);
@@ -225,7 +227,7 @@ export default function UsageBasedBillingConfig({ attributionId }: Props) {
225227
</button>
226228
)}
227229
</span>
228-
{typeof currentUsage === "number" && typeof usageLimit === "number" && (
230+
{typeof currentUsage === "number" && typeof usageLimit === "number" && usageLimit > 0 && (
229231
<span className="text-gray-400 dark:text-gray-500">
230232
{Math.round((100 * currentUsage) / usageLimit)}% used
231233
</span>
@@ -238,7 +240,12 @@ export default function UsageBasedBillingConfig({ attributionId }: Props) {
238240
<div className="flex-grow">
239241
<div className="uppercase text-sm text-gray-400 dark:text-gray-500">Current Period</div>
240242
<div className="text-sm font-medium text-gray-500 dark:text-gray-400">
241-
{now.toLocaleString("default", { month: "long" })} {now.getFullYear()}
243+
<span className="font-semibold">
244+
{billingPeriodFrom.toLocaleString("default", { month: "long" })}{" "}
245+
{billingPeriodFrom.getFullYear()}
246+
</span>{" "}
247+
({billingPeriodFrom.toLocaleString("default", { month: "short", day: "numeric" })} -{" "}
248+
{billingPeriodTo.toLocaleString("default", { month: "short", day: "numeric" })})
242249
</div>
243250
</div>
244251
<div>

0 commit comments

Comments
 (0)