dify/web/app/components/plugins/plugin-detail-panel/detail-header.tsx

302 lines
10 KiB
TypeScript
Raw Normal View History

2024-11-17 12:57:34 +08:00
import React, { useCallback, useMemo, useState } from 'react'
2024-10-19 13:53:55 +08:00
import { useTranslation } from 'react-i18next'
import { useBoolean } from 'ahooks'
import {
2024-11-17 12:57:34 +08:00
RiArrowLeftRightLine,
2024-10-19 13:53:55 +08:00
RiBugLine,
RiCloseLine,
RiHardDrive3Line,
RiVerifiedBadgeLine,
} from '@remixicon/react'
import type { PluginDetail } from '../types'
2024-10-19 13:53:55 +08:00
import { PluginSource } from '../types'
import Description from '../card/base/description'
import Icon from '../card/base/card-icon'
import Title from '../card/base/title'
import OrgInfo from '../card/base/org-info'
2024-11-08 17:01:49 +08:00
import { useGitHubReleases } from '../install-plugin/hooks'
import { compareVersion, getLatestVersion } from '@/utils/semver'
2024-11-17 12:57:34 +08:00
import PluginVersionPicker from '@/app/components/plugins/update-plugin/plugin-version-picker'
import UpdateFromMarketplace from '@/app/components/plugins/update-plugin/from-market-place'
import OperationDropdown from '@/app/components/plugins/plugin-detail-panel/operation-dropdown'
2024-10-19 13:53:55 +08:00
import PluginInfo from '@/app/components/plugins/plugin-page/plugin-info'
import ActionButton from '@/app/components/base/action-button'
import Button from '@/app/components/base/button'
import Badge from '@/app/components/base/badge'
import Confirm from '@/app/components/base/confirm'
import Tooltip from '@/app/components/base/tooltip'
2024-11-08 17:01:49 +08:00
import Toast from '@/app/components/base/toast'
2024-10-19 13:53:55 +08:00
import { BoxSparkleFill } from '@/app/components/base/icons/src/vender/plugin'
import { Github } from '@/app/components/base/icons/src/public/common'
2024-11-02 14:10:45 +08:00
import { uninstallPlugin } from '@/service/plugins'
2024-11-02 13:21:06 +08:00
import { useGetLanguage } from '@/context/i18n'
2024-11-08 17:01:49 +08:00
import { useModalContext } from '@/context/modal-context'
2024-11-02 13:21:06 +08:00
import { API_PREFIX, MARKETPLACE_URL_PREFIX } from '@/config'
2024-10-19 13:53:55 +08:00
import cn from '@/utils/classnames'
const i18nPrefix = 'plugin.action'
type Props = {
detail: PluginDetail
2024-10-19 13:53:55 +08:00
onHide: () => void
2024-11-08 17:01:49 +08:00
onUpdate: () => void
2024-10-19 13:53:55 +08:00
}
const DetailHeader = ({
detail,
onHide,
2024-11-08 17:01:49 +08:00
onUpdate,
2024-10-19 13:53:55 +08:00
}: Props) => {
const { t } = useTranslation()
2024-11-02 13:21:06 +08:00
const locale = useGetLanguage()
2024-11-08 17:01:49 +08:00
const { fetchReleases } = useGitHubReleases()
const { setShowUpdatePluginModal } = useModalContext()
2024-10-19 13:53:55 +08:00
2024-11-02 13:21:06 +08:00
const {
2024-11-02 14:10:45 +08:00
installation_id,
2024-11-02 13:21:06 +08:00
source,
tenant_id,
version,
latest_unique_identifier,
2024-11-02 13:21:06 +08:00
latest_version,
meta,
2024-11-17 12:57:34 +08:00
plugin_id,
2024-11-02 13:21:06 +08:00
} = detail
const { author, name, label, description, icon, verified } = detail.declaration
2024-11-06 16:42:04 +08:00
const isFromGitHub = source === PluginSource.github
const isFromMarketplace = source === PluginSource.marketplace
2024-11-08 17:01:49 +08:00
2024-11-17 12:57:34 +08:00
const [isShow, setIsShow] = useState(false)
const [targetVersion, setTargetVersion] = useState({
version: latest_version,
unique_identifier: latest_unique_identifier,
})
2024-10-19 13:53:55 +08:00
const hasNewVersion = useMemo(() => {
if (isFromGitHub)
return latest_version !== version
if (isFromMarketplace)
return !!latest_version && latest_version !== version
return false
}, [isFromGitHub, isFromMarketplace, latest_version, version])
const [isShowUpdateModal, {
setTrue: showUpdateModal,
setFalse: hideUpdateModal,
}] = useBoolean(false)
2024-10-19 13:53:55 +08:00
2024-11-08 17:01:49 +08:00
const handleUpdate = async () => {
if (isFromMarketplace) {
showUpdateModal()
return
}
2024-11-08 17:01:49 +08:00
try {
const fetchedReleases = await fetchReleases(author, name)
if (fetchedReleases.length === 0)
return
const versions = fetchedReleases.map(release => release.tag_name)
const latestVersion = getLatestVersion(versions)
if (compareVersion(latestVersion, version) === 1) {
setShowUpdatePluginModal({
onSaveCallback: () => {
onUpdate()
},
payload: {
type: PluginSource.github,
github: {
originalPackageInfo: {
id: installation_id,
repo: meta!.repo,
version: meta!.version,
package: meta!.package,
releases: fetchedReleases,
},
},
},
})
}
else {
Toast.notify({
type: 'info',
message: 'No new version available',
})
}
}
catch {
Toast.notify({
type: 'error',
message: 'Failed to compare versions',
})
}
}
2024-10-19 13:53:55 +08:00
const handleUpdatedFromMarketplace = () => {
onUpdate()
hideUpdateModal()
}
2024-10-19 13:53:55 +08:00
const [isShowPluginInfo, {
setTrue: showPluginInfo,
setFalse: hidePluginInfo,
}] = useBoolean(false)
const [isShowDeleteConfirm, {
setTrue: showDeleteConfirm,
setFalse: hideDeleteConfirm,
}] = useBoolean(false)
2024-11-02 14:10:45 +08:00
const [deleting, {
setTrue: showDeleting,
setFalse: hideDeleting,
}] = useBoolean(false)
const handleDelete = useCallback(async () => {
showDeleting()
const res = await uninstallPlugin(installation_id)
hideDeleting()
if (res.success) {
hideDeleteConfirm()
2024-11-08 17:01:49 +08:00
onUpdate()
2024-11-02 14:10:45 +08:00
}
2024-11-08 17:01:49 +08:00
}, [hideDeleteConfirm, hideDeleting, installation_id, showDeleting, onUpdate])
2024-11-02 14:10:45 +08:00
2024-11-02 13:21:06 +08:00
// #plugin TODO# used in apps
2024-11-02 14:10:45 +08:00
// const usedInApps = 3
2024-10-19 13:53:55 +08:00
return (
<div className={cn('shrink-0 p-4 pb-3 border-b border-divider-subtle bg-components-panel-bg')}>
<div className="flex">
2024-11-06 17:19:51 +08:00
<div className='overflow-hidden border-components-panel-border-subtle border rounded-xl'>
<Icon src={`${API_PREFIX}/workspaces/current/plugin/icon?tenant_id=${tenant_id}&filename=${icon}`} />
</div>
2024-10-19 13:53:55 +08:00
<div className="ml-3 w-0 grow">
<div className="flex items-center h-5">
2024-11-02 13:21:06 +08:00
<Title title={label[locale]} />
{verified && <RiVerifiedBadgeLine className="shrink-0 ml-0.5 w-4 h-4 text-text-accent" />}
2024-11-17 12:57:34 +08:00
<PluginVersionPicker
disabled={!isFromMarketplace || !hasNewVersion}
isShow={isShow}
onShowChange={setIsShow}
pluginID={plugin_id}
currentVersion={version}
onSelect={(state) => {
setTargetVersion(state)
handleUpdate()
}}
trigger={
<Badge
className={cn(
'mx-1',
isShow && 'bg-state-base-hover',
(isShow || isFromMarketplace) && 'hover:bg-state-base-hover',
)}
uppercase={false}
text={
<>
<div>{version}</div>
{isFromMarketplace && <RiArrowLeftRightLine className='ml-1 w-3 h-3 text-text-tertiary' />}
</>
}
hasRedCornerMark={hasNewVersion}
/>
}
2024-10-19 13:53:55 +08:00
/>
{hasNewVersion && (
<Button variant='secondary-accent' size='small' className='!h-5' onClick={handleUpdate}>{t('plugin.detailPanel.operation.update')}</Button>
)}
</div>
<div className='mb-1 flex justify-between items-center h-4'>
2024-11-02 13:21:06 +08:00
<div className='mt-0.5 flex items-center'>
2024-10-19 13:53:55 +08:00
<OrgInfo
packageNameClassName='w-auto'
2024-11-02 13:21:06 +08:00
orgName={author}
packageName={name}
2024-10-19 13:53:55 +08:00
/>
<div className='ml-1 mr-0.5 text-text-quaternary system-xs-regular'>·</div>
{detail.source === PluginSource.marketplace && (
<Tooltip popupContent={t('plugin.detailPanel.categoryTip.marketplace')} >
2024-11-02 13:21:06 +08:00
<div><BoxSparkleFill className='w-3.5 h-3.5 text-text-tertiary hover:text-text-accent' /></div>
2024-10-19 13:53:55 +08:00
</Tooltip>
)}
{detail.source === PluginSource.github && (
<Tooltip popupContent={t('plugin.detailPanel.categoryTip.github')} >
2024-11-02 13:21:06 +08:00
<div><Github className='w-3.5 h-3.5 text-text-secondary hover:text-text-primary' /></div>
2024-10-19 13:53:55 +08:00
</Tooltip>
)}
{detail.source === PluginSource.local && (
<Tooltip popupContent={t('plugin.detailPanel.categoryTip.local')} >
2024-11-02 13:21:06 +08:00
<div><RiHardDrive3Line className='w-3.5 h-3.5 text-text-tertiary' /></div>
2024-10-19 13:53:55 +08:00
</Tooltip>
)}
{detail.source === PluginSource.debugging && (
<Tooltip popupContent={t('plugin.detailPanel.categoryTip.debugging')} >
2024-11-02 13:21:06 +08:00
<div><RiBugLine className='w-3.5 h-3.5 text-text-tertiary hover:text-text-warning' /></div>
2024-10-19 13:53:55 +08:00
</Tooltip>
)}
</div>
</div>
</div>
<div className='flex gap-1'>
<OperationDropdown
2024-11-08 17:01:49 +08:00
source={detail.source}
2024-10-19 13:53:55 +08:00
onInfo={showPluginInfo}
2024-11-02 13:21:06 +08:00
onCheckVersion={handleUpdate}
2024-10-19 13:53:55 +08:00
onRemove={showDeleteConfirm}
2024-11-02 13:21:06 +08:00
detailUrl={`${MARKETPLACE_URL_PREFIX}/plugin/${author}/${name}`}
2024-10-19 13:53:55 +08:00
/>
<ActionButton onClick={onHide}>
<RiCloseLine className='w-4 h-4' />
</ActionButton>
</div>
</div>
2024-11-02 13:21:06 +08:00
<Description className='mt-3' text={description[locale]} descriptionLineRows={2}></Description>
2024-10-19 13:53:55 +08:00
{isShowPluginInfo && (
<PluginInfo
2024-11-06 16:42:04 +08:00
repository={isFromGitHub ? meta?.repo : ''}
2024-11-02 13:21:06 +08:00
release={version}
2024-11-06 17:19:51 +08:00
packageName={meta?.package || ''}
2024-10-19 13:53:55 +08:00
onHide={hidePluginInfo}
/>
)}
{isShowDeleteConfirm && (
<Confirm
isShow
title={t(`${i18nPrefix}.delete`)}
content={
<div>
2024-11-02 13:21:06 +08:00
{t(`${i18nPrefix}.deleteContentLeft`)}<span className='system-md-semibold'>{label[locale]}</span>{t(`${i18nPrefix}.deleteContentRight`)}<br />
2024-11-02 14:10:45 +08:00
{/* {usedInApps > 0 && t(`${i18nPrefix}.usedInApps`, { num: usedInApps })} */}
2024-10-19 13:53:55 +08:00
</div>
}
onCancel={hideDeleteConfirm}
2024-11-02 14:10:45 +08:00
onConfirm={handleDelete}
isLoading={deleting}
isDisabled={deleting}
2024-10-19 13:53:55 +08:00
/>
)}
{
isShowUpdateModal && (
<UpdateFromMarketplace
payload={{
originalPackageInfo: {
id: detail.plugin_unique_identifier,
payload: detail.declaration,
},
targetPackageInfo: {
2024-11-17 12:57:34 +08:00
id: targetVersion.unique_identifier,
version: targetVersion.version,
},
}}
onCancel={hideUpdateModal}
onSave={handleUpdatedFromMarketplace}
/>
)
}
2024-10-19 13:53:55 +08:00
</div>
)
}
export default DetailHeader