Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.7 KB
Raw Blame
'use client'

import type { Dependency, PluginDeclaration, PluginManifestInMarket } from '../../types'
import { useEffect, useState } from 'react'
import { parseMarketplacePluginId } from '@/app/components/plugins/plugin-routes'
import { getMarketplacePluginIconUrl } from '@/features/marketplace/api'
import { usePluginInstallation } from '@/hooks/use-query-params'
import {
  fetchBundleInfoFromMarketPlace,
  fetchManifestFromMarketPlace,
  fetchPluginInfoFromMarketPlace,
} from '@/service/plugins'

type MarketplaceInstall = {
  manifest: PluginDeclaration | PluginManifestInMarket
  packageId: string
  uniqueIdentifier: string
}

async function resolveMarketplaceUniqueIdentifier(packageId: string) {
  const pluginId = parseMarketplacePluginId(packageId)
  if (!pluginId) return packageId

  const { data } = await fetchPluginInfoFromMarketPlace(pluginId)
  return data.plugin.latest_package_identifier
}

export type UseInstallFromMarketplaceQueryOptions = {
  canInstallPlugin: boolean
  isPermissionLoading?: boolean
  onPackageCategoryResolved?: (category: string | undefined, packageId: string) => boolean
}

export const useInstallFromMarketplaceQuery = ({
  canInstallPlugin,
  isPermissionLoading = false,
  onPackageCategoryResolved,
}: UseInstallFromMarketplaceQueryOptions) => {
  const [{ packageId, bundleInfo }, setInstallState] = usePluginInstallation()
  const [marketplaceInstall, setMarketplaceInstall] = useState<MarketplaceInstall | null>(null)
  const [dependencies, setDependencies] = useState<Dependency[]>([])
  const [isShowInstallFromMarketplace, setIsShowInstallFromMarketplace] = useState(false)

  useEffect(() => {
    if (!packageId && !bundleInfo) return

    if (isPermissionLoading) return

    if (!canInstallPlugin) {
      setInstallState(null)
      return
    }

    let ignore = false

    const loadMarketplaceInstall = async () => {
      if (packageId) {
        try {
          const uniqueIdentifier = await resolveMarketplaceUniqueIdentifier(packageId)
          const { data } = await fetchManifestFromMarketPlace(encodeURIComponent(uniqueIdentifier))
          if (ignore) return

          const { plugin, version } = data
          const redirected = onPackageCategoryResolved?.(plugin.category, packageId)
          if (redirected) return

          setMarketplaceInstall({
            packageId,
            uniqueIdentifier,
            manifest: {
              ...plugin,
              version: version.version,
              icon: getMarketplacePluginIconUrl(`${plugin.org}/${plugin.name}`),
            },
          })
          setIsShowInstallFromMarketplace(true)
        } catch (error) {
          if (!ignore) console.error('Failed to load marketplace plugin manifest:', error)
        }
        return
      }

      if (bundleInfo) {
        try {
          const { data } = await fetchBundleInfoFromMarketPlace(bundleInfo)
          if (ignore) return

          setDependencies(data.version.dependencies)
          setIsShowInstallFromMarketplace(true)
        } catch (error) {
          if (!ignore) console.error('Failed to load bundle info:', error)
        }
      }
    }

    loadMarketplaceInstall()

    return () => {
      ignore = true
    }
  }, [
    bundleInfo,
    canInstallPlugin,
    isPermissionLoading,
    onPackageCategoryResolved,
    packageId,
    setInstallState,
  ])

  const hideInstallFromMarketplace = () => {
    setMarketplaceInstall(null)
    setDependencies([])
    setIsShowInstallFromMarketplace(false)
    setInstallState(null)
  }

  return {
    bundleInfo,
    dependencies,
    hideInstallFromMarketplace,
    isShowInstallFromMarketplace,
    marketplaceInstall: marketplaceInstall?.packageId === packageId ? marketplaceInstall : null,
  }
}