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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { 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. type Props = {
  33. className?: string
  34. plugin: PluginDetail
  35. }
  36. const PluginItem: FC<Props> = ({
  37. className,
  38. plugin,
  39. }) => {
  40. const { t } = useTranslation()
  41. const { theme } = useTheme()
  42. const { categoriesMap } = useSingleCategories()
  43. const currentPluginID = usePluginPageContext(v => v.currentPluginID)
  44. const setCurrentPluginID = usePluginPageContext(v => v.setCurrentPluginID)
  45. const { refreshPluginList } = useRefreshPluginList()
  46. const {
  47. source,
  48. tenant_id,
  49. installation_id,
  50. plugin_unique_identifier,
  51. endpoints_active,
  52. meta,
  53. plugin_id,
  54. } = plugin
  55. const { category, author, name, label, description, icon, verified, meta: declarationMeta } = plugin.declaration
  56. const orgName = useMemo(() => {
  57. return [PluginSource.github, PluginSource.marketplace].includes(source) ? author : ''
  58. }, [source, author])
  59. const { langeniusVersionInfo } = useAppContext()
  60. const isDifyVersionCompatible = useMemo(() => {
  61. if (!langeniusVersionInfo.current_version)
  62. return true
  63. return gte(langeniusVersionInfo.current_version, declarationMeta.minimum_dify_version ?? '0.0.0')
  64. }, [declarationMeta.minimum_dify_version, langeniusVersionInfo.current_version])
  65. const handleDelete = () => {
  66. refreshPluginList({ category } as any)
  67. }
  68. const getValueFromI18nObject = useRenderI18nObject()
  69. const title = getValueFromI18nObject(label)
  70. const descriptionText = getValueFromI18nObject(description)
  71. return (
  72. <div
  73. className={cn(
  74. 'rounded-xl border-[1.5px] border-background-section-burn p-1',
  75. currentPluginID === plugin_id && 'border-components-option-card-option-selected-border',
  76. source === PluginSource.debugging
  77. ? '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)]'
  78. : 'bg-background-section-burn',
  79. )}
  80. onClick={() => {
  81. setCurrentPluginID(plugin.plugin_id)
  82. }}
  83. >
  84. <div className={cn('hover-bg-components-panel-on-panel-item-bg relative rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-on-panel-item-bg p-4 pb-3 shadow-xs', className)}>
  85. <CornerMark text={categoriesMap[category].label} />
  86. {/* Header */}
  87. <div className="flex">
  88. <div className='flex h-10 w-10 items-center justify-center overflow-hidden rounded-xl border-[1px] border-components-panel-border-subtle'>
  89. <img
  90. className='h-full w-full'
  91. src={`${API_PREFIX}/workspaces/current/plugin/icon?tenant_id=${tenant_id}&filename=${icon}`}
  92. alt={`plugin-${plugin_unique_identifier}-logo`}
  93. />
  94. </div>
  95. <div className="ml-3 w-0 grow">
  96. <div className="flex h-5 items-center">
  97. <Title title={title} />
  98. {verified && <RiVerifiedBadgeLine className="ml-0.5 h-4 w-4 shrink-0 text-text-accent" />}
  99. {!isDifyVersionCompatible && <Tooltip popupContent={
  100. t('plugin.difyVersionNotCompatible', { minimalDifyVersion: declarationMeta.minimum_dify_version })
  101. }><RiErrorWarningLine color='red' className="ml-0.5 h-4 w-4 shrink-0 text-text-accent" /></Tooltip>}
  102. <Badge className='ml-1 shrink-0'
  103. text={source === PluginSource.github ? plugin.meta!.version : plugin.version}
  104. hasRedCornerMark={(source === PluginSource.marketplace) && !!plugin.latest_version && plugin.latest_version !== plugin.version}
  105. />
  106. </div>
  107. <div className='flex items-center justify-between'>
  108. <Description text={descriptionText} descriptionLineRows={1}></Description>
  109. <div onClick={e => e.stopPropagation()}>
  110. <Action
  111. pluginUniqueIdentifier={plugin_unique_identifier}
  112. installationId={installation_id}
  113. author={author}
  114. pluginName={name}
  115. usedInApps={5}
  116. isShowFetchNewVersion={source === PluginSource.github}
  117. isShowInfo={source === PluginSource.github}
  118. isShowDelete
  119. meta={meta}
  120. onDelete={handleDelete}
  121. category={category}
  122. />
  123. </div>
  124. </div>
  125. </div>
  126. </div>
  127. </div>
  128. <div className='mb-1 mt-1.5 flex h-4 items-center justify-between px-4'>
  129. <div className='flex items-center'>
  130. <OrgInfo
  131. className="mt-0.5"
  132. orgName={orgName}
  133. packageName={name}
  134. packageNameClassName='w-auto max-w-[150px]'
  135. />
  136. {category === PluginType.extension && (
  137. <>
  138. <div className='system-xs-regular mx-2 text-text-quaternary'>·</div>
  139. <div className='system-xs-regular flex space-x-1 text-text-tertiary'>
  140. <RiLoginCircleLine className='h-4 w-4' />
  141. <span>{t('plugin.endpointsEnabled', { num: endpoints_active })}</span>
  142. </div>
  143. </>
  144. )}
  145. </div>
  146. <div className='flex items-center'>
  147. {source === PluginSource.github
  148. && <>
  149. <a href={`https://github.com/${meta!.repo}`} target='_blank' className='flex items-center gap-1'>
  150. <div className='system-2xs-medium-uppercase text-text-tertiary'>{t('plugin.from')}</div>
  151. <div className='flex items-center space-x-0.5 text-text-secondary'>
  152. <Github className='h-3 w-3' />
  153. <div className='system-2xs-semibold-uppercase'>GitHub</div>
  154. <RiArrowRightUpLine className='h-3 w-3' />
  155. </div>
  156. </a>
  157. </>
  158. }
  159. {source === PluginSource.marketplace
  160. && <>
  161. <a href={getMarketplaceUrl(`/plugins/${author}/${name}`, { theme })} target='_blank' className='flex items-center gap-0.5'>
  162. <div className='system-2xs-medium-uppercase text-text-tertiary'>{t('plugin.from')} <span className='text-text-secondary'>marketplace</span></div>
  163. <RiArrowRightUpLine className='h-3 w-3 text-text-tertiary' />
  164. </a>
  165. </>
  166. }
  167. {source === PluginSource.local
  168. && <>
  169. <div className='flex items-center gap-1'>
  170. <RiHardDrive3Line className='h-3 w-3 text-text-tertiary' />
  171. <div className='system-2xs-medium-uppercase text-text-tertiary'>Local Plugin</div>
  172. </div>
  173. </>
  174. }
  175. {source === PluginSource.debugging
  176. && <>
  177. <div className='flex items-center gap-1'>
  178. <RiBugLine className='h-3 w-3 text-text-warning' />
  179. <div className='system-2xs-medium-uppercase text-text-warning'>Debugging Plugin</div>
  180. </div>
  181. </>
  182. }
  183. </div>
  184. </div>
  185. </div>
  186. )
  187. }
  188. export default React.memo(PluginItem)