File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed
web/src/app/publishers/[id]/agents/[agentId]/[version] Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ 'use client'
2+
3+ import { useQuery } from '@tanstack/react-query'
4+ import { Badge } from '@/components/ui/badge'
5+ import { Skeleton } from '@/components/ui/skeleton'
6+
7+ interface VersionUsageBadgeProps {
8+ publisherId : string
9+ agentId : string
10+ version : string
11+ }
12+
13+ interface AgentData {
14+ id : string
15+ publisher : {
16+ id : string
17+ }
18+ version_stats ?: Record <
19+ string ,
20+ {
21+ total_invocations : number
22+ }
23+ >
24+ }
25+
26+ const formatUsageCount = ( count ?: number ) => {
27+ if ( ! count ) return '0'
28+ if ( count >= 1000000 ) return `${ ( count / 1000000 ) . toFixed ( 1 ) } M`
29+ if ( count >= 1000 ) return `${ ( count / 1000 ) . toFixed ( 1 ) } K`
30+ return count . toString ( )
31+ }
32+
33+ export const VersionUsageBadge = ( {
34+ publisherId,
35+ agentId,
36+ version,
37+ } : VersionUsageBadgeProps ) => {
38+ const { data : agents , isLoading } = useQuery < AgentData [ ] > ( {
39+ queryKey : [ 'agents' ] ,
40+ queryFn : async ( ) => {
41+ const response = await fetch ( '/api/agents' )
42+ if ( ! response . ok ) {
43+ throw new Error ( 'Failed to fetch agents' )
44+ }
45+ return await response . json ( )
46+ } ,
47+ } )
48+
49+ const agent = agents ?. find (
50+ ( agent ) => agent . id === agentId && agent . publisher . id === publisherId
51+ )
52+
53+ const totalRuns = agent ?. version_stats ?. [ version ] ?. total_invocations || 0
54+
55+ if ( isLoading ) {
56+ return < Skeleton className = "h-4 w-8" />
57+ }
58+
59+ if ( totalRuns === 0 ) {
60+ return null
61+ }
62+
63+ return (
64+ < Badge variant = "secondary" className = "text-xs px-1.5 py-0 ml-2" >
65+ { formatUsageCount ( totalRuns ) } runs
66+ </ Badge >
67+ )
68+ }
You can’t perform that action at this time.
0 commit comments