You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use client'
  2. import React, { useState } from 'react'
  3. import { useRouter } from 'next/navigation'
  4. import { useTranslation } from 'react-i18next'
  5. import { useContext } from 'use-context-selector'
  6. import s from './index.module.css'
  7. import cn from '@/utils/classnames'
  8. import Modal from '@/app/components/base/modal'
  9. import Input from '@/app/components/base/input'
  10. import Button from '@/app/components/base/button'
  11. import { ToastContext } from '@/app/components/base/toast'
  12. import { createEmptyDataset } from '@/service/datasets'
  13. import { useResetDatasetList } from '@/service/knowledge/use-dataset'
  14. type IProps = {
  15. show: boolean
  16. onHide: () => void
  17. }
  18. const EmptyDatasetCreationModal = ({
  19. show = false,
  20. onHide,
  21. }: IProps) => {
  22. const [inputValue, setInputValue] = useState('')
  23. const { t } = useTranslation()
  24. const { notify } = useContext(ToastContext)
  25. const router = useRouter()
  26. const resetDatasetList = useResetDatasetList()
  27. const submit = async () => {
  28. if (!inputValue) {
  29. notify({ type: 'error', message: t('datasetCreation.stepOne.modal.nameNotEmpty') })
  30. return
  31. }
  32. if (inputValue.length > 40) {
  33. notify({ type: 'error', message: t('datasetCreation.stepOne.modal.nameLengthInvalid') })
  34. return
  35. }
  36. try {
  37. const dataset = await createEmptyDataset({ name: inputValue })
  38. resetDatasetList()
  39. onHide()
  40. router.push(`/datasets/${dataset.id}/documents`)
  41. }
  42. catch {
  43. notify({ type: 'error', message: t('datasetCreation.stepOne.modal.failed') })
  44. }
  45. }
  46. return (
  47. <Modal
  48. isShow={show}
  49. onClose={onHide}
  50. className={cn(s.modal, '!max-w-[520px]', 'px-8')}
  51. >
  52. <div className={s.modalHeader}>
  53. <div className={s.title}>{t('datasetCreation.stepOne.modal.title')}</div>
  54. <span className={s.close} onClick={onHide} />
  55. </div>
  56. <div className={s.tip}>{t('datasetCreation.stepOne.modal.tip')}</div>
  57. <div className={s.form}>
  58. <div className={s.label}>{t('datasetCreation.stepOne.modal.input')}</div>
  59. <Input value={inputValue} placeholder={t('datasetCreation.stepOne.modal.placeholder') || ''} onChange={e => setInputValue(e.target.value)} />
  60. </div>
  61. <div className='flex flex-row-reverse'>
  62. <Button className='ml-2 w-24' variant='primary' onClick={submit}>{t('datasetCreation.stepOne.modal.confirmButton')}</Button>
  63. <Button className='w-24' onClick={onHide}>{t('datasetCreation.stepOne.modal.cancelButton')}</Button>
  64. </div>
  65. </Modal>
  66. )
  67. }
  68. export default EmptyDatasetCreationModal