forked from TanStack/tanstack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenSourceStats.tsx
More file actions
207 lines (198 loc) · 7.47 KB
/
OpenSourceStats.tsx
File metadata and controls
207 lines (198 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { useSuspenseQuery } from '@tanstack/react-query'
import { BlankErrorBoundary } from './BlankErrorBoundary'
import { Suspense } from 'react'
import { Library } from '~/libraries'
import { ossStatsQuery } from '~/queries/stats'
import { useNpmDownloadCounter } from '~/hooks/useNpmDownloadCounter'
import { Box, Download, Star, Users } from 'lucide-react'
import { Card } from './Card'
const NpmDownloadCounter = ({
npmData,
}: {
npmData: {
totalDownloads: number
ratePerDay?: number
updatedAt?: number
tag?: string
}
}) => {
const ref = useNpmDownloadCounter(npmData)
// Provide initial SSR value, hook will update it on client
const initialCount = npmData.totalDownloads ?? 0
return (
<span ref={ref} style={{ fontVariantNumeric: 'tabular-nums' }}>
{initialCount.toLocaleString()}
</span>
)
}
function isValidMetric(value: number | undefined | null): boolean {
return (
value !== undefined &&
value !== null &&
!Number.isNaN(value) &&
value > 0 &&
Number.isFinite(value)
)
}
function OssStatsContent({ library }: { library?: Library }) {
const { data: stats } = useSuspenseQuery(ossStatsQuery({ library }))
const npmDownloads = stats.npm?.totalDownloads ?? 0
const starCount = stats.github?.starCount ?? 0
const contributorCount = stats.github?.contributorCount ?? 0
const dependentCount = stats.github?.dependentCount ?? 0
const hasNpmDownloads = isValidMetric(npmDownloads)
const hasStarCount = isValidMetric(starCount)
const hasContributorCount = isValidMetric(contributorCount)
const hasDependentCount = isValidMetric(dependentCount)
const hasAnyData =
hasNpmDownloads || hasStarCount || hasContributorCount || hasDependentCount
return (
<div>
<Card
className="relative p-8 grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-8 items-center
justify-center xl:place-items-center rounded-[2rem]"
>
{!hasAnyData && (
<div className="absolute inset-0 z-10 flex items-center justify-center pointer-events-none">
<div className="inline-flex items-center gap-2 px-4 py-2 rounded-full bg-gradient-to-r from-pink-500/10 to-purple-500/10 border border-pink-500/20 dark:border-pink-500/30 backdrop-blur-sm pointer-events-auto">
<span className="text-2xl">🚀</span>
<p className="text-sm font-medium text-pink-600 dark:text-pink-400">
<span className="font-bold">Fresh out of the oven!</span> This
library just launched. Be among the first to star, download, and
contribute!
</p>
</div>
</div>
)}
<a
href="https://www.npmjs.com/org/tanstack"
target="_blank"
rel="noreferrer"
className={`group flex gap-4 items-center ${
!hasNpmDownloads ? 'opacity-50' : ''
} ${!hasAnyData ? 'blur-sm' : ''}`}
>
<Download className="text-2xl group-hover:text-emerald-500 transition-colors duration-200" />
<div>
<div className="text-2xl font-bold opacity-80 relative group-hover:text-emerald-500 transition-colors duration-200">
{hasNpmDownloads ? (
<NpmDownloadCounter npmData={stats.npm} />
) : (
<span>0</span>
)}
</div>
<div className="text-sm opacity-60 font-medium italic group-hover:text-emerald-500 transition-colors duration-200">
NPM Downloads
</div>
</div>
</a>
<a
href={
library
? `https://github.com/${library.repo}`
: 'https://github.com/orgs/TanStack/repositories?q=sort:stars'
}
target="_blank"
rel="noreferrer"
className={`group flex gap-4 items-center ${
!hasStarCount ? 'opacity-50' : ''
} ${!hasAnyData ? 'blur-sm' : ''}`}
>
<Star className="group-hover:text-yellow-500 text-2xl transition-colors duration-200" />
<div>
<div className="text-2xl font-bold opacity-80 leading-none group-hover:text-yellow-500 transition-colors duration-200 relative">
{hasStarCount ? starCount.toLocaleString() : '0'}
</div>
<div className="text-sm opacity-60 font-medium italic -mt-1 group-hover:text-yellow-500 transition-colors duration-200">
Stars on GitHub
</div>
</div>
</a>
<div
className={`flex gap-4 items-center ${
!hasContributorCount ? 'opacity-50' : ''
} ${!hasAnyData ? 'blur-sm' : ''}`}
>
<Users className="text-2xl" />
<div className="">
<div className="text-2xl font-bold opacity-80 relative">
{hasContributorCount ? contributorCount.toLocaleString() : '0'}
</div>
<div className="text-sm opacity-60 font-medium italic -mt-1">
Contributors on GitHub
</div>
</div>
</div>
<div
className={`flex gap-4 items-center ${
!hasDependentCount ? 'opacity-50' : ''
} ${!hasAnyData ? 'blur-sm' : ''}`}
>
<Box className="text-2xl" />
<div className="">
<div className="text-2xl font-bold opacity-80 relative">
{hasDependentCount ? dependentCount.toLocaleString() : '0'}
</div>
<div className="text-sm opacity-60 font-medium italic -mt-1">
Dependents on GitHub
</div>
</div>
</div>
</Card>
</div>
)
}
function OssStatsSkeleton() {
return (
<Card
className="relative p-8 grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-8 items-center
justify-center xl:place-items-center rounded-[2rem]"
>
<div className="flex gap-4 items-center">
<Download className="text-2xl" />
<div>
<div className="text-2xl font-bold opacity-80 h-7 w-24 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
<div className="text-sm opacity-60 font-medium italic">
NPM Downloads
</div>
</div>
</div>
<div className="flex gap-4 items-center">
<Star className="text-2xl" />
<div>
<div className="text-2xl font-bold opacity-80 h-7 w-20 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
<div className="text-sm opacity-60 font-medium italic -mt-1">
Stars on GitHub
</div>
</div>
</div>
<div className="flex gap-4 items-center">
<Users className="text-2xl" />
<div>
<div className="text-2xl font-bold opacity-80 h-7 w-16 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
<div className="text-sm opacity-60 font-medium italic -mt-1">
Contributors on GitHub
</div>
</div>
</div>
<div className="flex gap-4 items-center">
<Box className="text-2xl" />
<div>
<div className="text-2xl font-bold opacity-80 h-7 w-16 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
<div className="text-sm opacity-60 font-medium italic -mt-1">
Dependents on GitHub
</div>
</div>
</div>
</Card>
)
}
export default function OssStats({ library }: { library?: Library }) {
return (
<Suspense fallback={<OssStatsSkeleton />}>
<BlankErrorBoundary>
<OssStatsContent library={library} />
</BlankErrorBoundary>
</Suspense>
)
}