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.

5.4 KB
Raw Blame
'use client'

import type { MarketplaceTemplate } from '@dify/contracts/marketplace'
import type { MarketplaceSearchSelection } from '../home/marketplace-search-autocomplete'
import type { CreatorCreation, CreatorCreationAction, LoadedCreatorProfile } from './model'
import type { Plugin } from '@/app/components/plugins/types'
import { useMemo, useState } from 'react'
import useCheckInstalled from '@/app/components/plugins/install-plugin/hooks/use-check-installed'
import { useRouter } from '@/next/navigation'
import MarketplaceDetailDialog from '../detail-dialog'
import TemplateDetailDialog from '../templates/template-detail-dialog'
import { useMarketplaceDetailNavigation } from '../use-detail-navigation'
import { getFormattedPlugin } from '../utils'
import CreatorProfileHeader from './header'
import CreatorProfileView from './view'

type SelectedCreation =
  | { kind: 'plugin'; plugin: Plugin }
  | { kind: 'template'; template: MarketplaceTemplate }

type DifyCreatorProfileProps = {
  loadedProfile: LoadedCreatorProfile
  locale: string
}

const normalizePlugin = (plugin: Plugin): Plugin => ({
  ...plugin,
  label: plugin.label ?? {},
  brief: plugin.brief ?? {},
  description: plugin.description ?? {},
  tags: plugin.tags ?? [],
  badges: plugin.badges ?? null,
})

export default function DifyCreatorProfile({ loadedProfile, locale }: DifyCreatorProfileProps) {
  const router = useRouter()
  const navigation = useMarketplaceDetailNavigation()
  const [selected, setSelected] = useState<SelectedCreation | null>(null)
  const [sourceProfile, setSourceProfile] = useState(loadedProfile)
  const [pluginsByCreationId, setPluginsByCreationId] = useState(loadedProfile.pluginsByCreationId)
  const [templatesByCreationId, setTemplatesByCreationId] = useState(
    loadedProfile.templatesByCreationId,
  )
  if (loadedProfile !== sourceProfile) {
    setSourceProfile(loadedProfile)
    setPluginsByCreationId(loadedProfile.pluginsByCreationId)
    setTemplatesByCreationId(loadedProfile.templatesByCreationId)
  }

  const profilePlugins = Object.values(pluginsByCreationId)
  const pluginIds = useMemo(
    () =>
      Array.from(
        new Set([
          ...profilePlugins
            .filter((plugin) => !navigation.pluginHref(plugin))
            .map((plugin) => plugin.plugin_id),
          ...(selected?.kind === 'plugin' ? [selected.plugin.plugin_id] : []),
        ]),
      ).sort(),
    [navigation, profilePlugins, selected],
  )
  const { installedInfo } = useCheckInstalled({
    pluginIds,
    enabled: pluginIds.length > 0,
  })

  const selectCreation = (creation: CreatorCreation) => {
    if (creation.kind === 'plugin') {
      const plugin = pluginsByCreationId[creation.id]
      if (plugin) setSelected({ kind: 'plugin', plugin: normalizePlugin(plugin) })
      return
    }

    const template = templatesByCreationId[creation.id]
    if (template) setSelected({ kind: 'template', template })
  }

  const selectSearchResult = (selection: MarketplaceSearchSelection) => {
    if (selection.kind === 'plugin') {
      if (navigation.openPlugin(selection.plugin)) return { preserveQuery: true }
      setSelected({
        kind: 'plugin',
        plugin: normalizePlugin(getFormattedPlugin(selection.plugin)),
      })
      return
    }
    if (navigation.openTemplate(selection.template)) return { preserveQuery: true }
    setSelected({ kind: 'template', template: selection.template })
  }

  const getCreationAction = (creation: CreatorCreation): CreatorCreationAction => {
    const target = creation.target
    const href =
      target.type === 'plugin'
        ? navigation.pluginHref({ org: target.org, name: target.name, type: target.pluginType })
        : navigation.templateHref({ id: target.id, publisher_unique_handle: target.publisher })
    return href
      ? { type: 'link', href, target: '_blank' }
      : { type: 'select', onSelect: () => selectCreation(creation) }
  }

  const closeSelected = () => setSelected(null)
  const selectedPlugin = selected?.kind === 'plugin' ? selected.plugin : null
  const selectedTemplate = selected?.kind === 'template' ? selected.template : null

  return (
    <>
      <CreatorProfileView
        profile={loadedProfile.viewModel}
        homeHref="/marketplace"
        isMarketplacePlatform
        inventory={loadedProfile.inventory}
        locale={locale}
        onRecordsLoaded={(records) => {
          setPluginsByCreationId((current) => ({ ...current, ...records.pluginsByCreationId }))
          setTemplatesByCreationId((current) => ({
            ...current,
            ...records.templatesByCreationId,
          }))
        }}
        getCreationAction={getCreationAction}
        header={<CreatorProfileHeader locale={locale} onSuggestionSelect={selectSearchResult} />}
      />

      {selectedPlugin && (
        <MarketplaceDetailDialog
          isInstalled={Boolean(installedInfo?.[selectedPlugin.plugin_id])}
          open
          plugin={selectedPlugin}
          onOpenChange={(open) => {
            if (!open) closeSelected()
          }}
        />
      )}
      {selectedTemplate && (
        <TemplateDetailDialog
          open
          template={selectedTemplate}
          onInstall={() => {
            closeSelected()
            router.push(`/apps?template-id=${encodeURIComponent(selectedTemplate.id)}`)
          }}
          onOpenChange={(open) => {
            if (!open) closeSelected()
          }}
        />
      )}
    </>
  )
}