index.tsx 746 B

12345678910111213141516171819202122232425262728
  1. 'use client'
  2. import { useQuery } from '@tanstack/react-query'
  3. import type { FC } from 'react'
  4. import type { GithubRepo } from '@/models/common'
  5. const getStar = async () => {
  6. const res = await fetch('https://api.github.com/repos/langgenius/dify')
  7. if (!res.ok)
  8. throw new Error('Failed to fetch github star')
  9. return res.json()
  10. }
  11. const GithubStar: FC<{ className: string }> = (props) => {
  12. const { isFetching, data } = useQuery<GithubRepo>({
  13. queryKey: ['github-star'],
  14. queryFn: getStar,
  15. enabled: process.env.NODE_ENV !== 'development',
  16. initialData: { stargazers_count: 6000 },
  17. })
  18. if (isFetching)
  19. return null
  20. return <span {...props}>{data.stargazers_count.toLocaleString()}</span>
  21. }
  22. export default GithubStar