custom-create-card.tsx 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use client'
  2. import { useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import {
  6. RiAddLine,
  7. } from '@remixicon/react'
  8. import type { CustomCollectionBackend } from '../types'
  9. import I18n from '@/context/i18n'
  10. import { getLanguage } from '@/i18n/language'
  11. import { BookOpen01 } from '@/app/components/base/icons/src/vender/line/education'
  12. import { ArrowUpRight } from '@/app/components/base/icons/src/vender/line/arrows'
  13. import EditCustomToolModal from '@/app/components/tools/edit-custom-collection-modal'
  14. import { createCustomCollection } from '@/service/tools'
  15. import Toast from '@/app/components/base/toast'
  16. import { useAppContext } from '@/context/app-context'
  17. type Props = {
  18. onRefreshData: () => void
  19. }
  20. const Contribute = ({ onRefreshData }: Props) => {
  21. const { t } = useTranslation()
  22. const { locale } = useContext(I18n)
  23. const language = getLanguage(locale)
  24. const { isCurrentWorkspaceManager } = useAppContext()
  25. const linkUrl = useMemo(() => {
  26. if (language.startsWith('zh_'))
  27. return 'https://docs.dify.ai/zh-hans/guides/tools#ru-he-chuang-jian-zi-ding-yi-gong-ju'
  28. return 'https://docs.dify.ai/guides/tools#how-to-create-custom-tools'
  29. }, [language])
  30. const [isShowEditCollectionToolModal, setIsShowEditCustomCollectionModal] = useState(false)
  31. const doCreateCustomToolCollection = async (data: CustomCollectionBackend) => {
  32. await createCustomCollection(data)
  33. Toast.notify({
  34. type: 'success',
  35. message: t('common.api.actionSuccess'),
  36. })
  37. setIsShowEditCustomCollectionModal(false)
  38. onRefreshData()
  39. }
  40. return (
  41. <>
  42. {isCurrentWorkspaceManager && (
  43. <div className='flex flex-col col-span-1 bg-gray-200 border-[0.5px] border-black/5 rounded-xl min-h-[160px] transition-all duration-200 ease-in-out cursor-pointer hover:bg-gray-50 hover:shadow-lg'>
  44. <div className='group grow rounded-t-xl hover:bg-white' onClick={() => setIsShowEditCustomCollectionModal(true)}>
  45. <div className='shrink-0 flex items-center p-4 pb-3'>
  46. <div className='w-10 h-10 flex items-center justify-center border border-gray-200 bg-gray-100 rounded-lg group-hover:border-primary-100 group-hover:bg-primary-50'>
  47. <RiAddLine className='w-4 h-4 text-gray-500 group-hover:text-primary-600'/>
  48. </div>
  49. <div className='ml-3 text-sm font-semibold leading-5 text-gray-800 group-hover:text-primary-600'>{t('tools.createCustomTool')}</div>
  50. </div>
  51. </div>
  52. <div className='px-4 py-3 rounded-b-xl border-t-[0.5px] border-black/5 text-gray-500 hover:text-[#155EEF] hover:bg-white'>
  53. <a href={linkUrl} target='_blank' rel='noopener noreferrer' className='flex items-center space-x-1'>
  54. <BookOpen01 className='shrink-0 w-3 h-3' />
  55. <div className='grow leading-[18px] text-xs font-normal truncate' title={t('tools.customToolTip') || ''}>{t('tools.customToolTip')}</div>
  56. <ArrowUpRight className='shrink-0 w-3 h-3' />
  57. </a>
  58. </div>
  59. </div>
  60. )}
  61. {isShowEditCollectionToolModal && (
  62. <EditCustomToolModal
  63. payload={null}
  64. onHide={() => setIsShowEditCustomCollectionModal(false)}
  65. onAdd={doCreateCustomToolCollection}
  66. />
  67. )}
  68. </>
  69. )
  70. }
  71. export default Contribute