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.

6.9 KB
Raw Blame
import type { SearchParamsFromCollection } from '@dify/contracts/marketplace'
import type { ToolsContentInset } from '../content-inset'
import type { useToolMarketplace } from './hooks'
import type { MarketplaceLinks } from '@/features/marketplace/links'
import { cn } from '@langgenius/dify-ui/cn'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { useTranslation } from 'react-i18next'
import { useLocale } from '#i18n'
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
import List from '@/app/components/plugins/marketplace/list'
import { usePluginSettingsAccess } from '@/app/components/plugins/plugin-page/use-reference-setting'
import { useResolvedTheme } from '@/features/theme/hooks'
import { useRouter } from '@/next/navigation'
import { toolsContentInsetClassNames, toolsUnifiedContentFrameClassName } from '../content-inset'

type MarketplaceProps = {
  marketplace: MarketplaceLinks
  searchPluginText: string
  filterPluginTags: string[]
  isMarketplaceArrowVisible: boolean
  showMarketplacePanel: () => void
  marketplaceContext: ReturnType<typeof useToolMarketplace>
  contentInset?: ToolsContentInset
}
const Marketplace = ({
  marketplace,
  searchPluginText,
  filterPluginTags,
  isMarketplaceArrowVisible,
  showMarketplacePanel,
  marketplaceContext,
  contentInset = 'default',
}: MarketplaceProps) => {
  const locale = useLocale()
  const { t } = useTranslation(['common', 'plugin'])
  const theme = useResolvedTheme()
  const router = useRouter()
  const { canInstallPlugin } = usePluginSettingsAccess()
  const { isLoading, marketplaceCollections, marketplaceCollectionPluginsMap, plugins, page } =
    marketplaceContext
  const contentPaddingClassName = toolsContentInsetClassNames[contentInset]
  const marketplaceFrameClassName = cn(contentPaddingClassName, toolsUnifiedContentFrameClassName)
  const cardContainerClassName = 'grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3'
  const handleCollectionMoreClick = (searchParams?: SearchParamsFromCollection) => {
    const params = new URLSearchParams({ category: 'tool' })

    if (searchParams?.query) params.set('q', searchParams.query)
    if (searchParams?.sort_by) params.set('sort_by', searchParams.sort_by)
    if (searchParams?.sort_order) params.set('sort_order', searchParams.sort_order)

    router.push(`/marketplace?${params.toString()}`)
  }

  return (
    <>
      <div className="sticky bottom-0 flex shrink-0 flex-col bg-background-default-subtle pt-2 pb-3.5">
        {isMarketplaceArrowVisible && (
          <IconButton
            aria-label={t(($) => $['marketplace.moreFrom'], { ns: 'plugin' })}
            className="absolute top-2 left-1/2 z-10 -translate-x-1/2 text-text-quaternary"
            onClick={showMarketplacePanel}
            size="md"
            variant="ghost"
          >
            <span aria-hidden="true" className="i-ri-arrow-up-double-line size-4" />
          </IconButton>
        )}
        <div className={cn('pt-4 pb-3', marketplaceFrameClassName)}>
          <div className="bg-linear-to-r from-[rgba(11,165,236,0.95)] to-[rgba(21,90,239,0.95)] bg-clip-text title-2xl-semi-bold text-transparent">
            {t(($) => $['marketplace.moreFrom'], { ns: 'plugin' })}
          </div>
          <div className="flex items-center text-center body-md-regular text-text-tertiary">
            {t(($) => $['marketplace.discover'], { ns: 'plugin' })}
            <span className="relative ms-1 body-md-medium text-text-secondary after:absolute after:inset-s-0 after:bottom-[1.5px] after:h-2 after:w-full after:bg-text-text-selected after:content-['']">
              {t(($) => $['category.models'], { ns: 'plugin' })}
            </span>
            ,
            <span className="relative ms-1 body-md-medium text-text-secondary after:absolute after:inset-s-0 after:bottom-[1.5px] after:h-2 after:w-full after:bg-text-text-selected after:content-['']">
              {t(($) => $['category.tools'], { ns: 'plugin' })}
            </span>
            ,
            <span className="relative ms-1 body-md-medium text-text-secondary after:absolute after:inset-s-0 after:bottom-[1.5px] after:h-2 after:w-full after:bg-text-text-selected after:content-['']">
              {t(($) => $['category.datasources'], { ns: 'plugin' })}
            </span>
            ,
            <span className="relative ms-1 body-md-medium text-text-secondary after:absolute after:inset-s-0 after:bottom-[1.5px] after:h-2 after:w-full after:bg-text-text-selected after:content-['']">
              {t(($) => $['category.triggers'], { ns: 'plugin' })}
            </span>
            ,
            <span className="relative ms-1 body-md-medium text-text-secondary after:absolute after:inset-s-0 after:bottom-[1.5px] after:h-2 after:w-full after:bg-text-text-selected after:content-['']">
              {t(($) => $['category.agents'], { ns: 'plugin' })}
            </span>
            ,
            <span className="relative ms-1 me-1 body-md-medium text-text-secondary after:absolute after:inset-s-0 after:bottom-[1.5px] after:h-2 after:w-full after:bg-text-text-selected after:content-['']">
              {t(($) => $['category.extensions'], { ns: 'plugin' })}
            </span>
            {t(($) => $['marketplace.and'], { ns: 'plugin' })}
            <span className="relative ms-1 me-1 body-md-medium text-text-secondary after:absolute after:inset-s-0 after:bottom-[1.5px] after:h-2 after:w-full after:bg-text-text-selected after:content-['']">
              {t(($) => $['category.bundles'], { ns: 'plugin' })}
            </span>
            {t(($) => $['operation.in'], { ns: 'common' })}
            <a
              href={marketplace.page('', {
                language: locale,
                q: searchPluginText,
                tags: filterPluginTags.join(','),
                theme,
              })}
              className="ms-1 flex items-center system-sm-medium text-text-accent"
              target="_blank"
            >
              {t(($) => $['marketplace.difyMarketplace'], { ns: 'plugin' })}
              <span
                aria-hidden="true"
                className="i-ri-arrow-right-up-line size-4 rtl:-scale-x-100"
              />
            </a>
          </div>
        </div>
      </div>
      <div className="-mt-3.5 shrink-0 grow bg-background-default-subtle pb-2">
        {isLoading && page === 1 && (
          <div className="absolute top-1/2 left-1/2 -translate-1/2">
            <LoadingPlaceholder />
          </div>
        )}
        {(!isLoading || page > 1) && (
          <div className={marketplaceFrameClassName}>
            <List
              marketplaceCollections={marketplaceCollections || []}
              marketplaceCollectionPluginsMap={marketplaceCollectionPluginsMap || {}}
              plugins={plugins}
              showInstallButton={canInstallPlugin}
              cardContainerClassName={cardContainerClassName}
              onCollectionMoreClick={handleCollectionMoreClick}
            />
          </div>
        )}
      </div>
    </>
  )
}

export default Marketplace