Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

fix(www): guard github/discord counters against invalid API responses (#902)

Yeom-JinHo765c454f9173
diff --git a/apps/www/components/discord-link.tsx b/apps/www/components/discord-link.tsx
index d244871e..d2cd5765 100644
--- a/apps/www/components/discord-link.tsx
+++ b/apps/www/components/discord-link.tsx
@@ -13,6 +13,46 @@ import {
import { Icons } from "@/components/icons"
import { PingDot } from "@/components/ping-dot"
+const formatCompactCount = (value: number) => {
+ if (value >= 1000) {
+ return `${(value / 1000).toFixed(1)}k`
+ }
+
+ return value.toLocaleString()
+}
+
+const getActiveMembersCount = async () => {
+ try {
+ const data = await fetch(
+ "https://discord.com/api/guilds/1151315619246002176/widget.json",
+ {
+ next: { revalidate: 3600 }, // Cache for 1 hour (3600 seconds)
+ }
+ )
+ if (!data.ok) {
+ return 0
+ }
+
+ const json: unknown = await data.json()
+ if (
+ typeof json !== "object" ||
+ json === null ||
+ !("presence_count" in json)
+ ) {
+ return 0
+ }
+
+ const membersCount = json.presence_count
+ if (typeof membersCount !== "number" || !Number.isFinite(membersCount)) {
+ return 0
+ }
+
+ return membersCount
+ } catch {
+ return 0
+ }
+}
+
export function DiscordLink({ className }: { className?: string }) {
return (
<Tooltip>
@@ -50,21 +90,13 @@ export async function ActiveMembersCount({
}: {
className?: string
}) {
- const data = await fetch(
- "https://discord.com/api/guilds/1151315619246002176/widget.json",
- {
- next: { revalidate: 3600 }, // Cache for 1 hour (3600 seconds)
- }
- )
- const json = await data.json()
+ const activeMembersCount = await getActiveMembersCount()
return (
<div className={cn("ml-2 inline-flex items-center gap-1", className)}>
<PingDot />
<span className="min-w-[2rem] text-xs font-medium tabular-nums">
- {json.presence_count >= 1000
- ? `${(json.presence_count / 1000).toFixed(1)}k`
- : json.presence_count.toLocaleString()}
+ {formatCompactCount(activeMembersCount)}
</span>
</div>
)
diff --git a/apps/www/components/github-link.tsx b/apps/www/components/github-link.tsx
index a3b540be..9d51b2b2 100644
--- a/apps/www/components/github-link.tsx
+++ b/apps/www/components/github-link.tsx
@@ -12,6 +12,46 @@ import {
} from "@/components/ui/tooltip"
import { Icons } from "@/components/icons"
+const formatCompactCount = (value: number) => {
+ if (value >= 1000) {
+ return `${(value / 1000).toFixed(1)}k`
+ }
+
+ return value.toLocaleString()
+}
+
+const getStarsCount = async () => {
+ try {
+ const data = await fetch(
+ "https://api.github.com/repos/magicuidesign/magicui",
+ {
+ next: { revalidate: 86400 }, // Cache for 1 day (86400 seconds)
+ }
+ )
+ if (!data.ok) {
+ return 0
+ }
+
+ const json: unknown = await data.json()
+ if (
+ typeof json !== "object" ||
+ json === null ||
+ !("stargazers_count" in json)
+ ) {
+ return 0
+ }
+
+ const starsCount = json.stargazers_count
+ if (typeof starsCount !== "number" || !Number.isFinite(starsCount)) {
+ return 0
+ }
+
+ return starsCount
+ } catch {
+ return 0
+ }
+}
+
export function GitHubLink({ className }: { className?: string }) {
return (
<TooltipProvider>
@@ -40,24 +80,12 @@ export function GitHubLink({ className }: { className?: string }) {
}
export async function StarsCount() {
- const data = await fetch(
- "https://api.github.com/repos/magicuidesign/magicui",
- {
- next: { revalidate: 86400 }, // Cache for 1 day (86400 seconds)
- }
- )
- const json = await data.json()
+ const starsCount = await getStarsCount()
return (
<span className="text-muted-foreground w-8 text-xs tabular-nums">
- <span className="hidden sm:inline">
- {json.stargazers_count.toLocaleString()}
- </span>
- <span className="sm:hidden">
- {json.stargazers_count >= 1000
- ? `${(json.stargazers_count / 1000).toFixed(1)}k`
- : json.stargazers_count.toLocaleString()}
- </span>
+ <span className="hidden sm:inline">{starsCount.toLocaleString()}</span>
+ <span className="sm:hidden">{formatCompactCount(starsCount)}</span>
</span>
)
}