mirror of
https://github.com/langgenius/dify.git
synced 2026-04-26 02:06:35 +08:00
feat: download package
This commit is contained in:
parent
b01c18ae7f
commit
11ed86f2a8
@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import React, { useCallback, useRef } from 'react'
|
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { RiMoreFill } from '@remixicon/react'
|
import { RiMoreFill } from '@remixicon/react'
|
||||||
import ActionButton from '@/app/components/base/action-button'
|
import ActionButton from '@/app/components/base/action-button'
|
||||||
@ -12,12 +12,15 @@ import {
|
|||||||
} from '@/app/components/base/portal-to-follow-elem'
|
} from '@/app/components/base/portal-to-follow-elem'
|
||||||
import cn from '@/utils/classnames'
|
import cn from '@/utils/classnames'
|
||||||
import { MARKETPLACE_URL_PREFIX } from '@/config'
|
import { MARKETPLACE_URL_PREFIX } from '@/config'
|
||||||
|
import { useDownloadPlugin } from '@/service/use-plugins'
|
||||||
|
import { downloadFile } from '@/utils/format'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
open: boolean
|
open: boolean
|
||||||
onOpenChange: (v: boolean) => void
|
onOpenChange: (v: boolean) => void
|
||||||
author: string
|
author: string
|
||||||
name: string
|
name: string
|
||||||
|
version: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const OperationDropdown: FC<Props> = ({
|
const OperationDropdown: FC<Props> = ({
|
||||||
@ -25,6 +28,7 @@ const OperationDropdown: FC<Props> = ({
|
|||||||
onOpenChange,
|
onOpenChange,
|
||||||
author,
|
author,
|
||||||
name,
|
name,
|
||||||
|
version,
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const openRef = useRef(open)
|
const openRef = useRef(open)
|
||||||
@ -37,6 +41,25 @@ const OperationDropdown: FC<Props> = ({
|
|||||||
setOpen(!openRef.current)
|
setOpen(!openRef.current)
|
||||||
}, [setOpen])
|
}, [setOpen])
|
||||||
|
|
||||||
|
const [needDownload, setNeedDownload] = useState(false)
|
||||||
|
const { data: blob, isLoading } = useDownloadPlugin({
|
||||||
|
organization: author,
|
||||||
|
pluginName: name,
|
||||||
|
version,
|
||||||
|
}, needDownload)
|
||||||
|
const handleDownload = useCallback(() => {
|
||||||
|
if (isLoading) return
|
||||||
|
setNeedDownload(true)
|
||||||
|
}, [isLoading])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (blob) {
|
||||||
|
const fileName = `${author}-${name}_${version}.zip`
|
||||||
|
downloadFile({ data: blob, fileName })
|
||||||
|
setNeedDownload(false)
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [blob])
|
||||||
return (
|
return (
|
||||||
<PortalToFollowElem
|
<PortalToFollowElem
|
||||||
open={open}
|
open={open}
|
||||||
@ -54,7 +77,7 @@ const OperationDropdown: FC<Props> = ({
|
|||||||
</PortalToFollowElemTrigger>
|
</PortalToFollowElemTrigger>
|
||||||
<PortalToFollowElemContent className='z-[9999]'>
|
<PortalToFollowElemContent className='z-[9999]'>
|
||||||
<div className='w-[112px] p-1 bg-components-panel-bg-blur rounded-xl border-[0.5px] border-components-panel-border shadow-lg'>
|
<div className='w-[112px] p-1 bg-components-panel-bg-blur rounded-xl border-[0.5px] border-components-panel-border shadow-lg'>
|
||||||
<div className='px-3 py-1.5 rounded-lg text-text-secondary system-md-regular cursor-pointer hover:bg-state-base-hover'>{t('common.operation.download')}</div>
|
<div onClick={handleDownload} className='px-3 py-1.5 rounded-lg text-text-secondary system-md-regular cursor-pointer hover:bg-state-base-hover'>{t('common.operation.download')}</div>
|
||||||
<a href={`${MARKETPLACE_URL_PREFIX}/plugins/${author}/${name}`} target='_blank' className='block px-3 py-1.5 rounded-lg text-text-secondary system-md-regular cursor-pointer hover:bg-state-base-hover'>{t('common.operation.viewDetails')}</a>
|
<a href={`${MARKETPLACE_URL_PREFIX}/plugins/${author}/${name}`} target='_blank' className='block px-3 py-1.5 rounded-lg text-text-secondary system-md-regular cursor-pointer hover:bg-state-base-hover'>{t('common.operation.viewDetails')}</a>
|
||||||
</div>
|
</div>
|
||||||
</PortalToFollowElemContent>
|
</PortalToFollowElemContent>
|
||||||
|
|||||||
@ -59,6 +59,7 @@ const Item: FC<Props> = ({
|
|||||||
onOpenChange={setOpen}
|
onOpenChange={setOpen}
|
||||||
author={payload.org}
|
author={payload.org}
|
||||||
name={payload.name}
|
name={payload.name}
|
||||||
|
version={payload.latest_version}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{isShowInstallModal && (
|
{isShowInstallModal && (
|
||||||
|
|||||||
@ -12,6 +12,7 @@ export const ContentType = {
|
|||||||
audio: 'audio/mpeg',
|
audio: 'audio/mpeg',
|
||||||
form: 'application/x-www-form-urlencoded; charset=UTF-8',
|
form: 'application/x-www-form-urlencoded; charset=UTF-8',
|
||||||
download: 'application/octet-stream', // for download
|
download: 'application/octet-stream', // for download
|
||||||
|
downloadZip: 'application/zip', // for download
|
||||||
upload: 'multipart/form-data', // for upload
|
upload: 'multipart/form-data', // for upload
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,7 +194,7 @@ async function base<T>(url: string, options: FetchOptionType = {}, otherOptions:
|
|||||||
const contentType = res.headers.get('content-type')
|
const contentType = res.headers.get('content-type')
|
||||||
if (
|
if (
|
||||||
contentType
|
contentType
|
||||||
&& [ContentType.download, ContentType.audio].includes(contentType)
|
&& [ContentType.download, ContentType.audio, ContentType.downloadZip].includes(contentType)
|
||||||
)
|
)
|
||||||
return await res.blob() as T
|
return await res.blob() as T
|
||||||
|
|
||||||
|
|||||||
@ -344,3 +344,12 @@ export const useMutationCheckDependenciesBeforeImportDSL = () => {
|
|||||||
|
|
||||||
return mutation
|
return mutation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const useDownloadPlugin = (info: { organization: string; pluginName: string; version: string }, needDownload: boolean) => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: [NAME_SPACE, 'downloadPlugin', info],
|
||||||
|
queryFn: () => getMarketplace<Blob>(`/plugins/${info.organization}/${info.pluginName}/${info.version}/download`),
|
||||||
|
enabled: needDownload,
|
||||||
|
retry: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@ -34,3 +34,14 @@ export const formatTime = (num: number) => {
|
|||||||
}
|
}
|
||||||
return `${num.toFixed(2)} ${units[index]}`
|
return `${num.toFixed(2)} ${units[index]}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const downloadFile = ({ data, fileName }: { data: Blob; fileName: string }) => {
|
||||||
|
const url = window.URL.createObjectURL(data)
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.href = url
|
||||||
|
a.download = fileName
|
||||||
|
document.body.appendChild(a)
|
||||||
|
a.click()
|
||||||
|
a.remove()
|
||||||
|
window.URL.revokeObjectURL(url)
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user