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.1 KB
Raw Blame
'use client'
import type { FC } from 'react'
import type { ActivePluginType } from '../../marketplace/constants'
import type { PluginCategoryEnum } from '../../types'
import { cn } from '@langgenius/dify-ui/cn'
import { Popover, PopoverContent, PopoverTrigger } from '@langgenius/dify-ui/popover'
import * as React from 'react'
import { useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
import { PluginSearchInput } from '@/app/components/plugins/plugin-search-input'
import { useInstalledPluginList } from '@/service/use-plugins'
import { PLUGIN_TYPE_SEARCH_MAP } from '../../marketplace/constants'
import { PluginSource } from '../../types'
import NoDataPlaceholder from './no-data-placeholder'
import ToolItem from './tool-item'

type Props = Readonly<{
  trigger: React.ReactNode
  value: string[]
  onChange: (value: string[]) => void
  isShow: boolean
  onShowChange: (isShow: boolean) => void
  integrationCategory?: PluginCategoryEnum
}>

const ToolPicker: FC<Props> = ({
  trigger,
  value,
  onChange,
  isShow,
  onShowChange,
  integrationCategory,
}) => {
  const { t } = useTranslation(['plugin'])

  const allTabs = [
    { key: PLUGIN_TYPE_SEARCH_MAP.all, name: t(($) => $['category.all'], { ns: 'plugin' }) },
    { key: PLUGIN_TYPE_SEARCH_MAP.model, name: t(($) => $['category.models'], { ns: 'plugin' }) },
    { key: PLUGIN_TYPE_SEARCH_MAP.tool, name: t(($) => $['category.tools'], { ns: 'plugin' }) },
    { key: PLUGIN_TYPE_SEARCH_MAP.agent, name: t(($) => $['category.agents'], { ns: 'plugin' }) },
    {
      key: PLUGIN_TYPE_SEARCH_MAP.extension,
      name: t(($) => $['category.extensions'], { ns: 'plugin' }),
    },
    {
      key: PLUGIN_TYPE_SEARCH_MAP.datasource,
      name: t(($) => $['category.datasources'], { ns: 'plugin' }),
    },
    {
      key: PLUGIN_TYPE_SEARCH_MAP.trigger,
      name: t(($) => $['category.triggers'], { ns: 'plugin' }),
    },
    { key: PLUGIN_TYPE_SEARCH_MAP.bundle, name: t(($) => $['category.bundles'], { ns: 'plugin' }) },
  ]
  const tabs = integrationCategory
    ? allTabs.filter((tab) => tab.key === integrationCategory)
    : allTabs

  const [pluginType, setPluginType] = useState<ActivePluginType>(PLUGIN_TYPE_SEARCH_MAP.all)
  const effectivePluginType = integrationCategory ?? pluginType
  const [query, setQuery] = useState('')
  const [tags, setTags] = useState<string[]>([])
  const { data, isLoading } = useInstalledPluginList()
  const filteredList = useMemo(() => {
    const list = data ? data.plugins : []
    return list.filter((plugin) => {
      const isFromMarketPlace = plugin.source === PluginSource.marketplace
      return (
        isFromMarketPlace &&
        (effectivePluginType === PLUGIN_TYPE_SEARCH_MAP.all ||
          plugin.declaration.category === effectivePluginType) &&
        (tags.length === 0 || tags.some((tag) => plugin.declaration.tags.includes(tag))) &&
        (query === '' || plugin.plugin_id.toLowerCase().includes(query.toLowerCase()))
      )
    })
  }, [data, effectivePluginType, query, tags])

  const handleCheckChange = (pluginId: string) => {
    const newValue = value.includes(pluginId)
      ? value.filter((id) => id !== pluginId)
      : [...value, pluginId]
    onChange(newValue)
  }

  const listContent = (
    <div className="max-h-99 overflow-y-auto p-1">
      {filteredList.map((item) => (
        <ToolItem
          key={item.plugin_id}
          payload={item}
          isChecked={value.includes(item.plugin_id)}
          onCheckChange={() => handleCheckChange(item.plugin_id)}
        />
      ))}
    </div>
  )

  const loadingContent = (
    <div className="flex h-99 items-center justify-center">
      <LoadingPlaceholder />
    </div>
  )

  const noData = <NoDataPlaceholder className="h-99" noPlugins={!query} />

  const resolvedTrigger = React.isValidElement(trigger) ? trigger : <div>{trigger}</div>

  return (
    <Popover open={isShow} onOpenChange={onShowChange}>
      <PopoverTrigger render={resolvedTrigger} />
      <PopoverContent
        side="top"
        sideOffset={4}
        className="border-none bg-transparent p-0 shadow-none backdrop-blur-none"
      >
        <div className="relative min-h-20 w-108 max-w-[calc(100vw-32px)] overflow-hidden rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-lg backdrop-blur-xs">
          <div className="flex flex-col overflow-hidden rounded-t-lg border-b border-divider-subtle bg-background-section-burn">
            <div className="bg-components-panel-bg p-2">
              <PluginSearchInput
                value={query}
                onValueChange={setQuery}
                tags={tags}
                onTagsChange={setTags}
                placeholder={t(($) => $.searchTools, { ns: 'plugin' })!}
              />
            </div>
            <div className="flex items-center justify-between bg-components-panel-bg px-3 pb-2">
              <div className="flex min-w-0 items-center gap-0.5 overflow-x-auto">
                {tabs.map((tab) => (
                  <button
                    type="button"
                    aria-pressed={effectivePluginType === tab.key}
                    className={cn(
                      'flex h-6 shrink-0 cursor-pointer items-center rounded-md px-2 system-xs-medium text-text-tertiary hover:bg-state-base-hover focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-state-accent-solid',
                      effectivePluginType === tab.key &&
                        'bg-state-base-hover-alt system-xs-semibold text-text-primary',
                    )}
                    key={tab.key}
                    onClick={() => setPluginType(tab.key)}
                  >
                    {tab.name}
                  </button>
                ))}
              </div>
            </div>
          </div>
          {!isLoading && filteredList.length > 0 && listContent}
          {!isLoading && filteredList.length === 0 && noData}
          {isLoading && loadingContent}
        </div>
      </PopoverContent>
    </Popover>
  )
}

export default React.memo(ToolPicker)