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.2 KB
Raw Blame
'use client'
import type { CustomCollectionBackend } from '../types'
import { Button } from '@langgenius/dify-ui/button'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import EditCustomToolModal from '@/app/components/tools/edit-custom-collection-modal'
import { useCanManageTools } from '@/app/components/tools/hooks/use-tool-permissions'
import { useDocLink } from '@/context/i18n'
import { createCustomCollection } from '@/service/tools'
import CreateEntryCard from './create-entry-card'

type Props = Readonly<{
  onRefreshData: () => void
  stepByStepTourTarget?: string
}>

function useCustomToolCreateAction({ onRefreshData }: Props) {
  const canManageTools = useCanManageTools()
  const [isShowEditCustomCollectionModal, setIsShowEditCustomCollectionModal] = useState(false)

  const doCreateCustomToolCollection = async (data: CustomCollectionBackend) => {
    if (!canManageTools) return

    await createCustomCollection(data)
    setIsShowEditCustomCollectionModal(false)
    onRefreshData()
  }

  return {
    canManageTools,
    doCreateCustomToolCollection,
    isShowEditCustomCollectionModal,
    setIsShowEditCustomCollectionModal,
  }
}

export const NewCustomToolButton = ({ onRefreshData }: Props) => {
  const { t } = useTranslation(['tools'])
  const addSwaggerAPIAsToolLabel = t(($) => $.addSwaggerAPIAsTool, { ns: 'tools' })
  const {
    canManageTools,
    doCreateCustomToolCollection,
    isShowEditCustomCollectionModal,
    setIsShowEditCustomCollectionModal,
  } = useCustomToolCreateAction({ onRefreshData })

  if (!canManageTools) return null

  return (
    <>
      <Button
        variant="secondary"
        className="px-3!"
        onClick={() => setIsShowEditCustomCollectionModal(true)}
        title={addSwaggerAPIAsToolLabel}
      >
        <span aria-hidden className="i-ri-add-line size-4 shrink-0" />
        {addSwaggerAPIAsToolLabel}
      </Button>
      {isShowEditCustomCollectionModal && (
        <EditCustomToolModal
          payload={null}
          onHide={() => setIsShowEditCustomCollectionModal(false)}
          onAdd={doCreateCustomToolCollection}
        />
      )}
    </>
  )
}

const Contribute = ({ onRefreshData, stepByStepTourTarget }: Props) => {
  const { t } = useTranslation(['tools'])
  const docLink = useDocLink()
  const {
    canManageTools,
    doCreateCustomToolCollection,
    isShowEditCustomCollectionModal,
    setIsShowEditCustomCollectionModal,
  } = useCustomToolCreateAction({ onRefreshData })

  return (
    <>
      {canManageTools && (
        <CreateEntryCard
          className="min-w-0"
          title={t(($) => $.createSwaggerAPIAsTool, { ns: 'tools' })}
          linkText={t(($) => $.swaggerAPIAsToolTip, { ns: 'tools' })}
          linkUrl={docLink('/use-dify/workspace/tools#swagger-api')}
          onCreate={() => setIsShowEditCustomCollectionModal(true)}
          stepByStepTourTarget={stepByStepTourTarget}
        />
      )}
      {isShowEditCustomCollectionModal && (
        <EditCustomToolModal
          payload={null}
          onHide={() => setIsShowEditCustomCollectionModal(false)}
          onAdd={doCreateCustomToolCollection}
        />
      )}
    </>
  )
}
export default Contribute