11import { getInAppWalletUsage , getUserOpUsage } from "@/api/analytics" ;
22import type { Team } from "@/api/team" ;
33import type { TeamSubscription } from "@/api/team-subscription" ;
4+ import type { RPCUsageDataItem } from "@/api/usage/rpc" ;
5+ import { ThirdwebBarChart } from "@/components/blocks/charts/bar-chart" ;
46import { Button } from "@/components/ui/button" ;
57import type {
68 Account ,
@@ -28,6 +30,7 @@ type UsageProps = {
2830 image : string | null ;
2931 slug : string ;
3032 } [ ] ;
33+ rpcUsage : RPCUsageDataItem [ ] ;
3134} ;
3235
3336const compactNumberFormatter = new Intl . NumberFormat ( "en-US" , {
@@ -42,24 +45,8 @@ export const Usage: React.FC<UsageProps> = ({
4245 team,
4346 client,
4447 projects,
48+ rpcUsage,
4549} ) => {
46- // TODO - get this from team instead of account
47- const rpcMetrics = useMemo ( ( ) => {
48- if ( ! usageData ) {
49- return { } ;
50- }
51-
52- return {
53- title : "Unlimited Requests" ,
54- total : (
55- < span className = "text-muted-foreground" >
56- { compactNumberFormatter . format ( usageData . rateLimits . rpc ) } Requests Per
57- Second
58- </ span >
59- ) ,
60- } ;
61- } , [ usageData ] ) ;
62-
6350 const gatewayMetrics = useMemo ( ( ) => {
6451 if ( ! usageData ) {
6552 return { } ;
@@ -91,6 +78,37 @@ export const Usage: React.FC<UsageProps> = ({
9178
9279 const storageUsage = team . capabilities . storage . upload ;
9380
81+ const rpcUsageData = useMemo ( ( ) => {
82+ const mappedRPCUsage = rpcUsage . reduce (
83+ ( acc , curr ) => {
84+ switch ( curr . usageType ) {
85+ case "rate-limit" :
86+ acc [ curr . date ] = {
87+ ...( acc [ curr . date ] || { } ) ,
88+ "rate-limit" :
89+ ( acc [ curr . date ] ?. [ "rate-limit" ] || 0 ) + Number ( curr . count ) ,
90+ included : acc [ curr . date ] ?. included || 0 ,
91+ } ;
92+ break ;
93+ default :
94+ acc [ curr . date ] = {
95+ ...( acc [ curr . date ] || { } ) ,
96+ "rate-limit" : acc [ curr . date ] ?. [ "rate-limit" ] || 0 ,
97+ included : ( acc [ curr . date ] ?. included || 0 ) + Number ( curr . count ) ,
98+ } ;
99+ break ;
100+ }
101+ return acc ;
102+ } ,
103+ { } as Record < string , { included : number ; "rate-limit" : number } > ,
104+ ) ;
105+
106+ return Object . entries ( mappedRPCUsage ) . map ( ( [ date , usage ] ) => ( {
107+ time : new Date ( date ) . getTime ( ) ,
108+ ...usage ,
109+ } ) ) ;
110+ } , [ rpcUsage ] ) ;
111+
94112 return (
95113 < div className = "flex grow flex-col gap-8" >
96114 < InAppWalletUsersChartCard
@@ -118,10 +136,25 @@ export const Usage: React.FC<UsageProps> = ({
118136 />
119137
120138 < UsageCard
121- { ...rpcMetrics }
122- name = "RPC"
123- description = "Amount of RPC requests allowed per second in your plan"
124- />
139+ name = "RPC Requests"
140+ description = { `Your plan allows for ${ usageData . rateLimits . rpc } requests per second.` }
141+ >
142+ < ThirdwebBarChart
143+ data = { rpcUsageData }
144+ isPending = { false }
145+ variant = "stacked"
146+ config = { {
147+ included : {
148+ label : "RPC Requests" ,
149+ color : "hsl(var(--chart-1))" ,
150+ } ,
151+ "rate-limit" : {
152+ label : "Rate Limited Requests" ,
153+ color : "hsl(var(--chart-3))" ,
154+ } ,
155+ } }
156+ />
157+ </ UsageCard >
125158
126159 < UsageCard
127160 { ...gatewayMetrics }
0 commit comments