github-item.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect } from 'react'
  4. import type { GitHubItemAndMarketPlaceDependency, Plugin } from '../../../types'
  5. import { pluginManifestToCardPluginProps } from '../../utils'
  6. import { useUploadGitHub } from '@/service/use-plugins'
  7. import Loading from '../../base/loading'
  8. import LoadedItem from './loaded-item'
  9. import type { VersionProps } from '@/app/components/plugins/types'
  10. type Props = {
  11. checked: boolean
  12. onCheckedChange: (plugin: Plugin) => void
  13. dependency: GitHubItemAndMarketPlaceDependency
  14. versionInfo: VersionProps
  15. onFetchedPayload: (payload: Plugin) => void
  16. onFetchError: () => void
  17. }
  18. const Item: FC<Props> = ({
  19. checked,
  20. onCheckedChange,
  21. dependency,
  22. versionInfo,
  23. onFetchedPayload,
  24. onFetchError,
  25. }) => {
  26. const info = dependency.value
  27. const { data, error } = useUploadGitHub({
  28. repo: info.repo!,
  29. version: info.release! || info.version!,
  30. package: info.packages! || info.package!,
  31. })
  32. const [payload, setPayload] = React.useState<Plugin | null>(null)
  33. useEffect(() => {
  34. if (data) {
  35. const payload = {
  36. ...pluginManifestToCardPluginProps(data.manifest),
  37. plugin_id: data.unique_identifier,
  38. }
  39. onFetchedPayload(payload)
  40. setPayload(payload)
  41. }
  42. // eslint-disable-next-line react-hooks/exhaustive-deps
  43. }, [data])
  44. useEffect(() => {
  45. if (error)
  46. onFetchError()
  47. // eslint-disable-next-line react-hooks/exhaustive-deps
  48. }, [error])
  49. if (!payload) return <Loading />
  50. return (
  51. <LoadedItem
  52. payload={payload}
  53. versionInfo={versionInfo}
  54. checked={checked}
  55. onCheckedChange={onCheckedChange}
  56. />
  57. )
  58. }
  59. export default React.memo(Item)