custom-create-card.tsx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use client'
  2. import { useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import type { CustomCollectionBackend } from '../types'
  6. import I18n from '@/context/i18n'
  7. import { getLanguage } from '@/i18n/language'
  8. import { Plus } from '@/app/components/base/icons/src/vender/line/general'
  9. import { BookOpen01 } from '@/app/components/base/icons/src/vender/line/education'
  10. import { ArrowUpRight } from '@/app/components/base/icons/src/vender/line/arrows'
  11. import EditCustomToolModal from '@/app/components/tools/edit-custom-collection-modal'
  12. import { createCustomCollection } from '@/service/tools'
  13. import Toast from '@/app/components/base/toast'
  14. type Props = {
  15. onRefreshData: () => void
  16. }
  17. const Contribute = ({ onRefreshData }: Props) => {
  18. const { t } = useTranslation()
  19. const { locale } = useContext(I18n)
  20. const language = getLanguage(locale)
  21. const linkUrl = useMemo(() => {
  22. if (language.startsWith('zh_'))
  23. return 'https://docs.dify.ai/v/zh-hans/guides/gong-ju/quick-tool-integration'
  24. return 'https://docs.dify.ai/tutorials/quick-tool-integration'
  25. }, [language])
  26. const [isShowEditCollectionToolModal, setIsShowEditCustomCollectionModal] = useState(false)
  27. const doCreateCustomToolCollection = async (data: CustomCollectionBackend) => {
  28. await createCustomCollection(data)
  29. Toast.notify({
  30. type: 'success',
  31. message: t('common.api.actionSuccess'),
  32. })
  33. setIsShowEditCustomCollectionModal(false)
  34. onRefreshData()
  35. }
  36. return (
  37. <>
  38. <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'>
  39. <div className='group grow rounded-t-xl hover:bg-white' onClick={() => setIsShowEditCustomCollectionModal(true)}>
  40. <div className='shrink-0 flex items-center p-4 pb-3'>
  41. <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'>
  42. <Plus className='w-4 h-4 text-gray-500 group-hover:text-primary-600'/>
  43. </div>
  44. <div className='ml-3 text-sm font-semibold leading-5 text-gray-800 group-hover:text-primary-600'>{t('tools.createCustomTool')}</div>
  45. </div>
  46. </div>
  47. <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'>
  48. <a href={linkUrl} target='_blank' rel='noopener noreferrer' className='flex items-center space-x-1'>
  49. <BookOpen01 className='shrink-0 w-3 h-3' />
  50. <div className='grow leading-[18px] text-xs font-normal truncate' title={t('tools.customToolTip') || ''}>{t('tools.customToolTip')}</div>
  51. <ArrowUpRight className='shrink-0 w-3 h-3' />
  52. </a>
  53. </div>
  54. </div>
  55. {isShowEditCollectionToolModal && (
  56. <EditCustomToolModal
  57. payload={null}
  58. onHide={() => setIsShowEditCustomCollectionModal(false)}
  59. onAdd={doCreateCustomToolCollection}
  60. />
  61. )}
  62. </>
  63. )
  64. }
  65. export default Contribute