Files
dify/web/app/components/header/github-star/index.tsx
2026-01-26 11:24:44 +00:00

39 lines
1003 B
TypeScript

'use client'
import type { FC } from 'react'
import type { GithubRepo } from '@/models/common'
import { useQuery } from '@tanstack/react-query'
import { IS_DEV } from '@/config'
const defaultData = {
stargazers_count: 110918,
}
const getStar = async () => {
const res = await fetch('https://api.github.com/repos/langgenius/dify')
if (!res.ok)
throw new Error('Failed to fetch github star')
return res.json()
}
const GithubStar: FC<{ className: string }> = (props) => {
const { isFetching, isError, data } = useQuery<GithubRepo>({
queryKey: ['github-star'],
queryFn: getStar,
enabled: !IS_DEV,
retry: false,
placeholderData: defaultData,
})
if (isFetching)
return <span className="i-ri-loader-2-line size-3 shrink-0 animate-spin text-text-tertiary" />
if (isError)
return <span {...props}>{defaultData.stargazers_count.toLocaleString()}</span>
return <span {...props}>{data?.stargazers_count.toLocaleString()}</span>
}
export default GithubStar