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.6 KB
Raw Blame
import type { RefObject } from 'react'
import type { PluginDetail } from '../types'
import type { EmbeddedMarketplaceCategory } from './category-marketplace'
import type { PluginPageContentInset } from './content-inset'
import type { Collection } from '@/app/components/tools/types'
import type { MarketplaceLinks } from '@/features/marketplace/links'
import { Button } from '@langgenius/dify-ui/button'
import { cn } from '@langgenius/dify-ui/cn'
import { useEffect, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
import IntegrationsToolProviderCard from '@/app/components/integrations/tool-provider-card'
import { BuiltinMarketplacePanel } from '@/app/components/tools/marketplace/builtin-marketplace-panel'
import CategoryEmptyState from './category-empty-state'
import CategoryMarketplacePanel from './category-marketplace-panel'
import List from './list'

type PluginsPanelResultsProps = {
  autoLoadNextPage: boolean
  canDeletePlugin: boolean
  canUpdatePlugin: boolean
  categoryEmptyState?: EmbeddedMarketplaceCategory
  categoryMarketplace?: EmbeddedMarketplaceCategory
  containerRef: RefObject<HTMLDivElement | null>
  contentFrameClassName: string
  contentInset: PluginPageContentInset
  currentBuiltinToolID?: string
  firstBuiltinToolTarget?: string
  firstPluginTarget?: string
  filteredBuiltinTools: Collection[]
  filteredList: Array<PluginDetail & { latest_version: string }>
  hasToolMarketplacePanel: boolean
  marketplace: MarketplaceLinks | null
  hasVisibleBuiltinTools: boolean
  hasVisiblePlugins: boolean
  hasEmbeddedMarketplace: boolean
  isFetching: boolean
  isLastPage: boolean
  keywords: string
  loadNextPage: () => void
  onSelectBuiltinTool: (id: string) => void
  scrollAreaLabel?: string
  showCategoryEmptyState: boolean
  tagFilterValue: string[]
}

const PluginsPanelResults = ({
  autoLoadNextPage,
  canDeletePlugin,
  canUpdatePlugin,
  categoryEmptyState,
  categoryMarketplace,
  containerRef,
  contentFrameClassName,
  contentInset,
  currentBuiltinToolID,
  firstBuiltinToolTarget,
  firstPluginTarget,
  filteredBuiltinTools,
  filteredList,
  hasToolMarketplacePanel,
  marketplace,
  hasVisibleBuiltinTools,
  hasVisiblePlugins,
  hasEmbeddedMarketplace,
  isFetching,
  isLastPage,
  keywords,
  loadNextPage,
  onSelectBuiltinTool,
  scrollAreaLabel,
  showCategoryEmptyState,
  tagFilterValue,
}: PluginsPanelResultsProps) => {
  const { t } = useTranslation(['common', 'plugin', 'workflow'])
  const loadMoreAnchorRef = useRef<HTMLDivElement>(null)
  const loadNextPageRequestedRef = useRef(false)

  useEffect(() => {
    const anchor = loadMoreAnchorRef.current
    const root = containerRef.current

    if (!isFetching) loadNextPageRequestedRef.current = false

    if (
      !autoLoadNextPage ||
      !anchor ||
      !root ||
      isFetching ||
      isLastPage ||
      !globalThis.IntersectionObserver
    )
      return

    const observer = new IntersectionObserver(
      (entries) => {
        if (!entries[0]?.isIntersecting || loadNextPageRequestedRef.current) return

        loadNextPageRequestedRef.current = true
        loadNextPage()
      },
      {
        root,
        rootMargin: '200px',
        threshold: 0.1,
      },
    )

    observer.observe(anchor)
    return () => observer.disconnect()
  }, [autoLoadNextPage, containerRef, isFetching, isLastPage, loadNextPage])

  return (
    <>
      <div role="status" aria-atomic="true" className="sr-only">
        {isFetching
          ? t(($) => $.loading, { ns: 'common' })
          : t(($) => $['marketplace.pluginsResult'], {
              ns: 'plugin',
              num:
                (hasVisiblePlugins ? filteredList.length : 0) +
                (hasVisibleBuiltinTools ? filteredBuiltinTools.length : 0),
            })}
      </div>
      <div
        ref={containerRef}
        aria-label={scrollAreaLabel}
        className={cn(
          'flex min-h-0 grow flex-col self-stretch overflow-y-auto overscroll-contain bg-components-panel-bg',
          hasEmbeddedMarketplace && 'pt-1',
          contentFrameClassName,
        )}
        role={scrollAreaLabel ? 'region' : undefined}
      >
        {showCategoryEmptyState && categoryEmptyState && (
          <CategoryEmptyState
            category={categoryEmptyState}
            showMarketplaceLink={!!categoryMarketplace}
          />
        )}
        {(hasVisiblePlugins || hasVisibleBuiltinTools) && (
          <List
            pluginList={filteredList}
            canDeletePlugin={canDeletePlugin}
            canUpdatePlugin={canUpdatePlugin}
            firstPluginTarget={firstPluginTarget}
          >
            {filteredBuiltinTools.map((collection, index) => (
              <button
                key={collection.id}
                type="button"
                aria-current={currentBuiltinToolID === collection.id ? 'true' : undefined}
                className="min-w-0 cursor-pointer appearance-none border-0 bg-transparent p-0 text-start"
                data-step-by-step-tour-target={
                  filteredList.length === 0 && index === 0 ? firstBuiltinToolTarget : undefined
                }
                onClick={() => onSelectBuiltinTool(collection.id)}
              >
                <IntegrationsToolProviderCard
                  collection={collection}
                  current={currentBuiltinToolID === collection.id}
                  showBuiltInBadge
                />
              </button>
            ))}
          </List>
        )}
        {!isLastPage && (
          <div className="flex w-full justify-center py-4">
            {isFetching ? (
              <LoadingPlaceholder className="size-8" />
            ) : autoLoadNextPage ? null : (
              <Button onClick={loadNextPage}>
                {t(($) => $['common.loadMore'], { ns: 'workflow' })}
              </Button>
            )}
            {autoLoadNextPage && (
              <div ref={loadMoreAnchorRef} className="h-px w-full" aria-hidden />
            )}
          </div>
        )}
        {marketplace && hasToolMarketplacePanel && (
          <BuiltinMarketplacePanel
            marketplace={marketplace}
            containerRef={containerRef}
            contentInset={contentInset}
            keywords={keywords}
            tagFilterValue={tagFilterValue}
          />
        )}
        {marketplace && categoryMarketplace && (
          <CategoryMarketplacePanel
            marketplace={marketplace}
            category={categoryMarketplace}
            searchText={keywords}
            tags={categoryMarketplace === 'trigger' ? tagFilterValue : []}
          />
        )}
      </div>
    </>
  )
}

export default PluginsPanelResults