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
194 lines (185 loc) · 6.88 KB
/
OpenSourceStats.tsx
File metadata and controls
194 lines (185 loc) · 6.88 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
import { useQuery } from '@tanstack/react-query'
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)
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 StatValue({
loading,
placeholder,
children,
}: {
loading: boolean
placeholder: string
children: React.ReactNode
}) {
return (
<span className="inline-grid [&>*]:col-start-1 [&>*]:row-start-1">
{/* Placeholder — always rendered for sizing, fades out */}
<span
className={`inline-block rounded transition-all duration-500 ease-out ${
loading
? 'bg-gray-200 dark:bg-gray-700 animate-pulse opacity-100'
: 'bg-transparent opacity-0 scale-95'
}`}
aria-hidden
>
<span className="invisible">{placeholder}</span>
</span>
{/* Real value — fades in on top */}
<span
className={`transition-all duration-500 ease-out ${
loading
? 'opacity-0 blur-sm scale-105'
: 'opacity-100 blur-0 scale-100'
}`}
>
{children}
</span>
</span>
)
}
export default function OssStats({ library }: { library?: Library }) {
const { data: stats, isLoading } = useQuery(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 = !isLoading && isValidMetric(npmDownloads)
const hasStarCount = !isLoading && isValidMetric(starCount)
const hasContributorCount = !isLoading && isValidMetric(contributorCount)
const hasDependentCount = !isLoading && isValidMetric(dependentCount)
const hasAnyData =
hasNpmDownloads || hasStarCount || hasContributorCount || hasDependentCount
const loading = isLoading || !stats
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]"
>
{!loading && !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 ${
!loading && !hasNpmDownloads ? 'opacity-50' : ''
} ${!loading && !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">
<StatValue loading={loading} placeholder="0,000,000,000">
{hasNpmDownloads && stats ? (
<NpmDownloadCounter npmData={stats.npm} />
) : (
<span>0</span>
)}
</StatValue>
</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 ${
!loading && !hasStarCount ? 'opacity-50' : ''
} ${!loading && !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 group-hover:text-yellow-500 transition-colors duration-200 relative">
<StatValue loading={loading} placeholder="000,000">
{hasStarCount ? starCount.toLocaleString() : '0'}
</StatValue>
</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 ${
!loading && !hasContributorCount ? 'opacity-50' : ''
} ${!loading && !hasAnyData ? 'blur-sm' : ''}`}
>
<Users className="text-2xl" />
<div>
<div className="text-2xl font-bold opacity-80 relative">
<StatValue loading={loading} placeholder="0,000">
{hasContributorCount ? contributorCount.toLocaleString() : '0'}
</StatValue>
</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 ${
!loading && !hasDependentCount ? 'opacity-50' : ''
} ${!loading && !hasAnyData ? 'blur-sm' : ''}`}
>
<Box className="text-2xl" />
<div>
<div className="text-2xl font-bold opacity-80 relative">
<StatValue loading={loading} placeholder="0,000,000">
{hasDependentCount ? dependentCount.toLocaleString() : '0'}
</StatValue>
</div>
<div className="text-sm opacity-60 font-medium italic -mt-1">
Dependents on GitHub
</div>
</div>
</div>
</Card>
</div>
)
}