You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useMemo } from 'react'
  4. import { useTheme } from 'next-themes'
  5. import {
  6. RiArrowRightUpLine,
  7. RiBugLine,
  8. RiErrorWarningLine,
  9. RiHardDrive3Line,
  10. RiLoginCircleLine,
  11. RiVerifiedBadgeLine,
  12. } from '@remixicon/react'
  13. import { useTranslation } from 'react-i18next'
  14. import { usePluginPageContext } from '../plugin-page/context'
  15. import { Github } from '../../base/icons/src/public/common'
  16. import Badge from '../../base/badge'
  17. import { type PluginDetail, PluginSource, PluginType } from '../types'
  18. import CornerMark from '../card/base/corner-mark'
  19. import Description from '../card/base/description'
  20. import OrgInfo from '../card/base/org-info'
  21. import Title from '../card/base/title'
  22. import Action from './action'
  23. import cn from '@/utils/classnames'
  24. import { API_PREFIX } from '@/config'
  25. import { useSingleCategories } from '../hooks'
  26. import { useRenderI18nObject } from '@/hooks/use-i18n'
  27. import useRefreshPluginList from '@/app/components/plugins/install-plugin/hooks/use-refresh-plugin-list'
  28. import { useAppContext } from '@/context/app-context'
  29. import { gte } from 'semver'
  30. import Tooltip from '@/app/components/base/tooltip'
  31. import { getMarketplaceUrl } from '@/utils/var'
  32. import { useGlobalPublicStore } from '@/context/global-public-context'
  33. type Props = {
  34. className?: string
  35. plugin: PluginDetail
  36. }
  37. const PluginItem: FC<Props> = ({
  38. className,
  39. plugin,
  40. }) => {
  41. const { t } = useTranslation()
  42. const { theme } = useTheme()
  43. const { categoriesMap } = useSingleCategories()
  44. const currentPluginID = usePluginPageContext(v => v.currentPluginID)
  45. const setCurrentPluginID = usePluginPageContext(v => v.setCurrentPluginID)
  46. const { refreshPluginList } = useRefreshPluginList()
  47. const {
  48. source,
  49. tenant_id,
  50. installation_id,
  51. plugin_unique_identifier,
  52. endpoints_active,
  53. meta,
  54. plugin_id,
  55. status,
  56. deprecated_reason,
  57. } = plugin
  58. const { category, author, name, label, description, icon, verified, meta: declarationMeta } = plugin.declaration
  59. const orgName = useMemo(() => {
  60. return [PluginSource.github, PluginSource.marketplace].includes(source) ? author : ''
  61. }, [source, author])
  62. const { langGeniusVersionInfo } = useAppContext()
  63. const isDifyVersionCompatible = useMemo(() => {
  64. if (!langGeniusVersionInfo.current_version)
  65. return true
  66. return gte(langGeniusVersionInfo.current_version, declarationMeta.minimum_dify_version ?? '0.0.0')
  67. }, [declarationMeta.minimum_dify_version, langGeniusVersionInfo.current_version])
  68. const isDeprecated = useMemo(() => {
  69. return status === 'deleted' && !!deprecated_reason
  70. }, [status, deprecated_reason])
  71. const handleDelete = useCallback(() => {
  72. refreshPluginList({ category } as any)
  73. }, [category, refreshPluginList])
  74. const getValueFromI18nObject = useRenderI18nObject()
  75. const title = getValueFromI18nObject(label)
  76. const descriptionText = getValueFromI18nObject(description)
  77. const { enable_marketplace } = useGlobalPublicStore(s => s.systemFeatures)
  78. return (
  79. <div
  80. className={cn(
  81. 'relative overflow-hidden rounded-xl border-[1.5px] border-background-section-burn p-1',
  82. currentPluginID === plugin_id && 'border-components-option-card-option-selected-border',
  83. source === PluginSource.debugging
  84. ? 'bg-[repeating-linear-gradient(-45deg,rgba(16,24,40,0.04),rgba(16,24,40,0.04)_5px,rgba(0,0,0,0.02)_5px,rgba(0,0,0,0.02)_10px)]'
  85. : 'bg-background-section-burn',
  86. )}
  87. onClick={() => {
  88. setCurrentPluginID(plugin.plugin_id)
  89. }}
  90. >
  91. <div className={cn('hover-bg-components-panel-on-panel-item-bg relative z-10 rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-on-panel-item-bg p-4 pb-3 shadow-xs', className)}>
  92. <CornerMark text={categoriesMap[category].label} />
  93. {/* Header */}
  94. <div className='flex'>
  95. <div className='flex h-10 w-10 items-center justify-center overflow-hidden rounded-xl border-[1px] border-components-panel-border-subtle'>
  96. <img
  97. className='h-full w-full'
  98. src={`${API_PREFIX}/workspaces/current/plugin/icon?tenant_id=${tenant_id}&filename=${icon}`}
  99. alt={`plugin-${plugin_unique_identifier}-logo`}
  100. />
  101. </div>
  102. <div className='ml-3 w-0 grow'>
  103. <div className='flex h-5 items-center'>
  104. <Title title={title} />
  105. {verified && <RiVerifiedBadgeLine className='ml-0.5 h-4 w-4 shrink-0 text-text-accent' />}
  106. {!isDifyVersionCompatible && <Tooltip popupContent={
  107. t('plugin.difyVersionNotCompatible', { minimalDifyVersion: declarationMeta.minimum_dify_version })
  108. }><RiErrorWarningLine color='red' className='ml-0.5 h-4 w-4 shrink-0 text-text-accent' /></Tooltip>}
  109. <Badge className='ml-1 shrink-0'
  110. text={source === PluginSource.github ? plugin.meta!.version : plugin.version}
  111. hasRedCornerMark={(source === PluginSource.marketplace) && !!plugin.latest_version && plugin.latest_version !== plugin.version}
  112. />
  113. </div>
  114. <div className='flex items-center justify-between'>
  115. <Description text={descriptionText} descriptionLineRows={1}></Description>
  116. <div onClick={e => e.stopPropagation()}>
  117. <Action
  118. pluginUniqueIdentifier={plugin_unique_identifier}
  119. installationId={installation_id}
  120. author={author}
  121. pluginName={name}
  122. usedInApps={5}
  123. isShowFetchNewVersion={source === PluginSource.github}
  124. isShowInfo={source === PluginSource.github}
  125. isShowDelete
  126. meta={meta}
  127. onDelete={handleDelete}
  128. category={category}
  129. />
  130. </div>
  131. </div>
  132. </div>
  133. </div>
  134. </div>
  135. <div className='mb-1 mt-1.5 flex h-4 items-center gap-x-2 px-4'>
  136. {/* Organization & Name */}
  137. <div className='flex grow items-center overflow-hidden'>
  138. <OrgInfo
  139. className='mt-0.5'
  140. orgName={orgName}
  141. packageName={name}
  142. packageNameClassName='w-auto max-w-[150px]'
  143. />
  144. {category === PluginType.extension && (
  145. <>
  146. <div className='system-xs-regular mx-2 text-text-quaternary'>·</div>
  147. <div className='system-xs-regular flex space-x-1 overflow-hidden text-text-tertiary'>
  148. <RiLoginCircleLine className='h-4 w-4 shrink-0' />
  149. <span
  150. className='truncate'
  151. title={t('plugin.endpointsEnabled', { num: endpoints_active })}
  152. >
  153. {t('plugin.endpointsEnabled', { num: endpoints_active })}
  154. </span>
  155. </div>
  156. </>
  157. )}
  158. </div>
  159. {/* Source */}
  160. <div className='flex shrink-0 items-center'>
  161. {source === PluginSource.github
  162. && <>
  163. <a href={`https://github.com/${meta!.repo}`} target='_blank' className='flex items-center gap-1'>
  164. <div className='system-2xs-medium-uppercase text-text-tertiary'>{t('plugin.from')}</div>
  165. <div className='flex items-center space-x-0.5 text-text-secondary'>
  166. <Github className='h-3 w-3' />
  167. <div className='system-2xs-semibold-uppercase'>GitHub</div>
  168. <RiArrowRightUpLine className='h-3 w-3' />
  169. </div>
  170. </a>
  171. </>
  172. }
  173. {source === PluginSource.marketplace && enable_marketplace
  174. && <>
  175. <a href={getMarketplaceUrl(`/plugins/${author}/${name}`, { theme })} target='_blank' className='flex items-center gap-0.5'>
  176. <div className='system-2xs-medium-uppercase text-text-tertiary'>{t('plugin.from')} <span className='text-text-secondary'>marketplace</span></div>
  177. <RiArrowRightUpLine className='h-3 w-3 text-text-tertiary' />
  178. </a>
  179. </>
  180. }
  181. {source === PluginSource.local
  182. && <>
  183. <div className='flex items-center gap-1'>
  184. <RiHardDrive3Line className='h-3 w-3 text-text-tertiary' />
  185. <div className='system-2xs-medium-uppercase text-text-tertiary'>Local Plugin</div>
  186. </div>
  187. </>
  188. }
  189. {source === PluginSource.debugging
  190. && <>
  191. <div className='flex items-center gap-1'>
  192. <RiBugLine className='h-3 w-3 text-text-warning' />
  193. <div className='system-2xs-medium-uppercase text-text-warning'>Debugging Plugin</div>
  194. </div>
  195. </>
  196. }
  197. </div>
  198. {/* Deprecated */}
  199. {source === PluginSource.marketplace && enable_marketplace && isDeprecated && (
  200. <div className='system-2xs-medium-uppercase flex shrink-0 items-center gap-x-2'>
  201. <span className='text-text-tertiary'>·</span>
  202. <span className='text-text-warning'>
  203. {t('plugin.deprecated')}
  204. </span>
  205. </div>
  206. )}
  207. </div>
  208. {/* BG Effect for Deprecated Plugin */}
  209. {source === PluginSource.marketplace && enable_marketplace && isDeprecated && (
  210. <div className='absolute bottom-[-71px] right-[-45px] z-0 size-40 bg-components-badge-status-light-warning-halo opacity-60 blur-[120px]' />
  211. )}
  212. </div>
  213. )
  214. }
  215. export default React.memo(PluginItem)