4
4
* See License-AGPL.txt in the project root for license information.
5
5
*/
6
6
7
- import { useContext } from "react" ;
7
+ import { Team } from "@gitpod/gitpod-protocol" ;
8
+ import { useContext , useEffect , useState } from "react" ;
8
9
import { Link } from "react-router-dom" ;
10
+ import getSettingsMenu from "./settings-menu" ;
11
+ import { ReactComponent as Spinner } from "../icons/Spinner.svg" ;
12
+ import DropDown from "../components/DropDown" ;
9
13
import { PageWithSubMenu } from "../components/PageWithSubMenu" ;
10
14
import { PaymentContext } from "../payment-context" ;
11
- import getSettingsMenu from "./settings-menu" ;
15
+ import { getGitpodService } from "../service/service" ;
16
+ import { TeamsContext } from "../teams/teams-context" ;
12
17
13
18
export default function Billing ( ) {
14
19
const { showPaymentUI, showUsageBasedUI } = useContext ( PaymentContext ) ;
20
+ const { teams } = useContext ( TeamsContext ) ;
21
+ const [ teamsWithBillingEnabled , setTeamsWithBillingEnabled ] = useState < Team [ ] | undefined > ( ) ;
22
+
23
+ useEffect ( ( ) => {
24
+ if ( ! teams ) {
25
+ setTeamsWithBillingEnabled ( undefined ) ;
26
+ return ;
27
+ }
28
+ const teamsWithBilling : Team [ ] = [ ] ;
29
+ Promise . all (
30
+ teams . map ( async ( t ) => {
31
+ const subscriptionId = await getGitpodService ( ) . server . findStripeSubscriptionIdForTeam ( t . id ) ;
32
+ if ( subscriptionId ) {
33
+ teamsWithBilling . push ( t ) ;
34
+ }
35
+ } ) ,
36
+ ) . then ( ( ) => setTeamsWithBillingEnabled ( teamsWithBilling ) ) ;
37
+ } , [ teams ] ) ;
15
38
16
39
return (
17
40
< PageWithSubMenu
@@ -21,13 +44,40 @@ export default function Billing() {
21
44
>
22
45
< h3 > Usage-Based Billing</ h3 >
23
46
< h2 className = "text-gray-500" > Manage usage-based billing, spending limit, and payment method.</ h2 >
24
- < p className = "mt-8" >
25
- Hint:{ " " }
26
- < Link className = "gp-link" to = "/teams/new" >
27
- Create a team
28
- </ Link > { " " }
29
- to set up usage-based billing.
30
- </ p >
47
+ < div className = "mt-8" >
48
+ < h3 > Billing Account</ h3 >
49
+ { teamsWithBillingEnabled === undefined && < Spinner className = "m-2 h-5 w-5 animate-spin" /> }
50
+ { teamsWithBillingEnabled && teamsWithBillingEnabled . length === 0 && (
51
+ < div className = "flex space-x-4" >
52
+ < span >
53
+ < Link className = "gp-link" to = "/teams/new" >
54
+ Create a team
55
+ </ Link > { " " }
56
+ to set up usage-based billing.
57
+ </ span >
58
+ </ div >
59
+ ) }
60
+ { teamsWithBillingEnabled && teamsWithBillingEnabled . length > 0 && (
61
+ < div className = "flex space-x-4" >
62
+ < span > Bill all my usage to:</ span >
63
+ < DropDown
64
+ customClasses = "w-32"
65
+ renderAsLink = { true }
66
+ entries = { [
67
+ {
68
+ title : "(none)" ,
69
+ onClick : ( ) => { } ,
70
+ } ,
71
+ ] . concat (
72
+ teamsWithBillingEnabled . map ( ( t ) => ( {
73
+ title : t . name ,
74
+ onClick : ( ) => { } ,
75
+ } ) ) ,
76
+ ) }
77
+ />
78
+ </ div >
79
+ ) }
80
+ </ div >
31
81
</ PageWithSubMenu >
32
82
) ;
33
83
}
0 commit comments