|
| 1 | +import { convexQuery } from '@convex-dev/react-query' |
| 2 | +import { useNpmDownloadCounter } from '@erquhart/convex-oss-stats/react' |
| 3 | +import NumberFlow from '@number-flow/react' |
| 4 | +import { useSuspenseQuery } from '@tanstack/react-query' |
| 5 | +import { api } from 'convex/_generated/api' |
| 6 | +import { FaCube, FaStar, FaUsers } from 'react-icons/fa' |
| 7 | +import { FaDownload } from 'react-icons/fa' |
| 8 | +import convexImageWhite from '~/images/convex-white.svg' |
| 9 | +import convexImageDark from '~/images/convex-dark.svg' |
| 10 | + |
| 11 | +const counterIntervalMs = 500 |
| 12 | + |
| 13 | +const StableCounter = ({ value }: { value?: number }) => { |
| 14 | + const dummyString = Number( |
| 15 | + Array(value?.toString().length ?? 1) |
| 16 | + .fill('8') |
| 17 | + .join('') |
| 18 | + ).toLocaleString() |
| 19 | + |
| 20 | + return ( |
| 21 | + <> |
| 22 | + {/* Dummy span to prevent layout shift */} |
| 23 | + <span className="opacity-0">{dummyString}</span> |
| 24 | + <span className="absolute -top-0.5 left-0"> |
| 25 | + <NumberFlow |
| 26 | + transformTiming={{ |
| 27 | + duration: counterIntervalMs, |
| 28 | + easing: 'linear', |
| 29 | + }} |
| 30 | + value={value} |
| 31 | + trend={1} |
| 32 | + continuous |
| 33 | + isolate |
| 34 | + willChange |
| 35 | + /> |
| 36 | + </span> |
| 37 | + </> |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +const NpmDownloadCounter = ({ |
| 42 | + npmData, |
| 43 | +}: { |
| 44 | + npmData: Parameters<typeof useNpmDownloadCounter>[0] |
| 45 | +}) => { |
| 46 | + const liveNpmDownloadCount = useNpmDownloadCounter(npmData, { |
| 47 | + intervalMs: counterIntervalMs, |
| 48 | + }) |
| 49 | + return <StableCounter value={liveNpmDownloadCount} /> |
| 50 | +} |
| 51 | + |
| 52 | +export default function OssStats() { |
| 53 | + const { data: github } = useSuspenseQuery( |
| 54 | + convexQuery(api.stats.getGithubOwner, { |
| 55 | + owner: 'tanstack', |
| 56 | + }) |
| 57 | + ) |
| 58 | + const { data: npm } = useSuspenseQuery( |
| 59 | + convexQuery(api.stats.getNpmOrg, { |
| 60 | + name: 'tanstack', |
| 61 | + }) |
| 62 | + ) |
| 63 | + |
| 64 | + return ( |
| 65 | + <div> |
| 66 | + <div className="p-8 grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-8 items-center justify-center xl:place-items-center bg-white/50 dark:bg-gray-700/30 dark:shadow-none rounded-xl shadow-xl"> |
| 67 | + <a |
| 68 | + href="https://www.npmjs.com/org/tanstack" |
| 69 | + target="_blank" |
| 70 | + rel="noreferrer" |
| 71 | + className="group flex gap-4 items-center" |
| 72 | + > |
| 73 | + <FaDownload className="text-2xl group-hover:text-emerald-500 transition-colors duration-200" /> |
| 74 | + <div> |
| 75 | + <div className="text-2xl font-bold opacity-80 relative group-hover:text-emerald-500 transition-colors duration-200"> |
| 76 | + <NpmDownloadCounter npmData={npm} /> |
| 77 | + </div> |
| 78 | + <div className="text-sm opacity-50 font-medium italic group-hover:text-emerald-500 transition-colors duration-200"> |
| 79 | + NPM Downloads |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </a> |
| 83 | + <a |
| 84 | + href="https://github.com/orgs/TanStack/repositories?q=sort:stars" |
| 85 | + target="_blank" |
| 86 | + rel="noreferrer" |
| 87 | + className="group flex gap-4 items-center" |
| 88 | + > |
| 89 | + <FaStar className="group-hover:text-yellow-500 text-2xl transition-colors duration-200" /> |
| 90 | + <div> |
| 91 | + <div className="text-2xl font-bold opacity-80 leading-none group-hover:text-yellow-500 transition-colors duration-200"> |
| 92 | + <NumberFlow value={github?.starCount} /> |
| 93 | + </div> |
| 94 | + <div className="text-sm opacity-50 font-medium italic -mt-1 group-hover:text-yellow-500 transition-colors duration-200"> |
| 95 | + Stars on Github |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </a> |
| 99 | + <div className="flex gap-4 items-center"> |
| 100 | + <FaUsers className="text-2xl" /> |
| 101 | + <div className=""> |
| 102 | + <div className="text-2xl font-bold opacity-80"> |
| 103 | + <NumberFlow value={github?.contributorCount} /> |
| 104 | + </div> |
| 105 | + <div className="text-sm opacity-50 font-medium italic -mt-1"> |
| 106 | + Contributors on GitHub |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + <div className="flex gap-4 items-center"> |
| 111 | + <FaCube className="text-2xl" /> |
| 112 | + <div className=""> |
| 113 | + <div className="text-2xl font-bold opacity-80 relative"> |
| 114 | + <NumberFlow value={github?.dependentCount} /> |
| 115 | + </div> |
| 116 | + <div className="text-sm opacity-50 font-medium italic -mt-1"> |
| 117 | + Dependents on GitHub |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + <div className="px-4 py-2 flex justify-end"> |
| 123 | + <a |
| 124 | + href="https://www.convex.dev/?utm_source=tanstack" |
| 125 | + className="group flex items-center gap-2" |
| 126 | + > |
| 127 | + <div className="h-2 w-2 animate-pulse rounded-full bg-green-500"></div> |
| 128 | + <div className="flex items-center gap-1"> |
| 129 | + <span className="text-[.75rem] opacity-30 relative -top-px"> |
| 130 | + Powered by |
| 131 | + </span> |
| 132 | + <img |
| 133 | + className="dark:hidden opacity-30 group-hover:opacity-50" |
| 134 | + src={convexImageDark} |
| 135 | + alt="Convex Logo" |
| 136 | + width={80} |
| 137 | + /> |
| 138 | + <img |
| 139 | + className="hidden dark:block opacity-30 group-hover:opacity-50" |
| 140 | + src={convexImageWhite} |
| 141 | + alt="Convex Logo" |
| 142 | + width={80} |
| 143 | + /> |
| 144 | + </div> |
| 145 | + </a> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + ) |
| 149 | +} |
0 commit comments