index.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use client'
  2. import React, { useEffect, useState } from 'react'
  3. import { Github } from '@/app/components/base/icons/src/public/common'
  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 data')
  9. return res.json()
  10. }
  11. const GithubStar = () => {
  12. const [githubRepo, setGithubRepo] = useState<GithubRepo>({ stargazers_count: 6000 })
  13. const [isFetched, setIsFetched] = useState(false)
  14. useEffect(() => {
  15. (async () => {
  16. try {
  17. if (process.env.NODE_ENV === 'development')
  18. return
  19. await setGithubRepo(await getStar())
  20. setIsFetched(true)
  21. }
  22. catch (e) {
  23. }
  24. })()
  25. }, [])
  26. if (!isFetched)
  27. return null
  28. return (
  29. <a
  30. href='https://github.com/langgenius/dify'
  31. target='_blank' rel='noopener noreferrer'
  32. className='flex items-center leading-[18px] border border-gray-200 rounded-md text-xs text-gray-700 font-semibold overflow-hidden'>
  33. <div className='flex items-center px-2 py-1 bg-gray-100'>
  34. <Github className='mr-1 w-[18px] h-[18px]' />
  35. Star
  36. </div>
  37. <div className='px-2 py-1 bg-white border-l border-gray-200'>{`${githubRepo.stargazers_count}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}</div>
  38. </a>
  39. )
  40. }
  41. export default GithubStar