diff --git a/components/dashboard/src/teams/TeamUsage.tsx b/components/dashboard/src/teams/TeamUsage.tsx index ccceb82c9d4a5a..d0ab5e247e5d66 100644 --- a/components/dashboard/src/teams/TeamUsage.tsx +++ b/components/dashboard/src/teams/TeamUsage.tsx @@ -16,6 +16,7 @@ import Pagination from "../components/Pagination"; import Header from "../components/Header"; import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error"; import { FeatureFlagContext } from "../contexts/FeatureFlagContext"; +import { ReactComponent as CreditsSvg } from "../images/credits.svg"; function TeamUsage() { const { teams } = useContext(TeamsContext); @@ -28,8 +29,9 @@ function TeamUsage() { const [errorMessage, setErrorMessage] = useState(""); const today = new Date(); const startOfCurrentMonth = new Date(today.getFullYear(), today.getMonth(), 1); - const timestampOfStart = startOfCurrentMonth.getTime(); - const [startDateOfBillMonth] = useState(timestampOfStart); + const timestampStartOfCurrentMonth = startOfCurrentMonth.getTime(); + const [startDateOfBillMonth, setStartDateOfBillMonth] = useState(timestampStartOfCurrentMonth); + const [endDateOfBillMonth, setEndDateOfBillMonth] = useState(Date.now()); useEffect(() => { if (!team) { @@ -40,8 +42,8 @@ function TeamUsage() { try { const billedUsageResult = await getGitpodService().server.listBilledUsage( attributionId, - startDateOfBillMonth, // TODO: set based on selected month - Date.now(), // TODO: set based on selected month + startDateOfBillMonth, + endDateOfBillMonth, ); setBilledUsage(billedUsageResult); } catch (error) { @@ -50,7 +52,7 @@ function TeamUsage() { } } })(); - }, [team]); + }, [team, startDateOfBillMonth, endDateOfBillMonth]); if (!showUsageBasedPricingUI) { return ; @@ -79,7 +81,34 @@ function TeamUsage() { const calculateTotalUsage = () => { let totalCredits = 0; billedUsage.forEach((session) => (totalCredits += session.credits)); - return totalCredits; + return totalCredits.toFixed(2); + }; + + const handleMonthClick = (start: any, end: any) => { + setStartDateOfBillMonth(start); + setEndDateOfBillMonth(end); + }; + + const getBillingHistory = () => { + let rows = []; + // This goes back 6 months from the current month + for (let i = 1; i < 7; i++) { + const endDateVar = i - 1; + const startDate = new Date(today.getFullYear(), today.getMonth() - i); + const endDate = new Date(today.getFullYear(), today.getMonth() - endDateVar, 0); + const timeStampOfStartDate = startDate.getTime(); + const timeStampOfEndDate = endDate.getTime(); + rows.push( +
handleMonthClick(timeStampOfStartDate, timeStampOfEndDate)} + > + {startDate.toLocaleString("default", { month: "long" })} {startDate.getFullYear()} +
, + ); + } + return rows; }; const lastResultOnCurrentPage = currentPage * resultsPerPage; @@ -90,50 +119,29 @@ function TeamUsage() { return ( <>
-
+
{errorMessage &&

{errorMessage}

} {!errorMessage && (
-
Period
-
+
Current Month
+
handleMonthClick(timestampStartOfCurrentMonth, Date.now())} + > {startOfCurrentMonth.toLocaleString("default", { month: "long" })}{" "} {startOfCurrentMonth.getFullYear()}
+
Previous Months
+ {getBillingHistory()}
Total usage
- - - - - - - - - - {calculateTotalUsage()} Total Credits + + {calculateTotalUsage()} Credits
@@ -147,14 +155,16 @@ function TeamUsage() { {" "} workspaces {" "} - or checked your other teams? + in{" "} + {new Date(startDateOfBillMonth).toLocaleString("default", { + month: "long", + })}{" "} + {new Date(startDateOfBillMonth).getFullYear()} or checked your other teams?

)} {billedUsage.length > 0 && (
-

All Usage

- View usage details of all team members. @@ -167,33 +177,7 @@ function TeamUsage() { Usage - - - - - - - - - + Credits