index.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. type IProps = {
  14. show: boolean
  15. onHide: () => void
  16. }
  17. const EmptyDatasetCreationModal = ({
  18. show = false,
  19. onHide,
  20. }: IProps) => {
  21. const [inputValue, setInputValue] = useState('')
  22. const { t } = useTranslation()
  23. const { notify } = useContext(ToastContext)
  24. const router = useRouter()
  25. const submit = async () => {
  26. if (!inputValue) {
  27. notify({ type: 'error', message: t('datasetCreation.stepOne.modal.nameNotEmpty') })
  28. return
  29. }
  30. if (inputValue.length > 40) {
  31. notify({ type: 'error', message: t('datasetCreation.stepOne.modal.nameLengthInvalid') })
  32. return
  33. }
  34. try {
  35. const dataset = await createEmptyDataset({ name: inputValue })
  36. onHide()
  37. router.push(`/datasets/${dataset.id}/documents`)
  38. }
  39. catch (err) {
  40. notify({ type: 'error', message: t('datasetCreation.stepOne.modal.failed') })
  41. }
  42. }
  43. return (
  44. <Modal
  45. isShow={show}
  46. onClose={onHide}
  47. className={cn(s.modal, '!max-w-[520px]', 'px-8')}
  48. >
  49. <div className={s.modalHeader}>
  50. <div className={s.title}>{t('datasetCreation.stepOne.modal.title')}</div>
  51. <span className={s.close} onClick={onHide} />
  52. </div>
  53. <div className={s.tip}>{t('datasetCreation.stepOne.modal.tip')}</div>
  54. <div className={s.form}>
  55. <div className={s.label}>{t('datasetCreation.stepOne.modal.input')}</div>
  56. <Input value={inputValue} placeholder={t('datasetCreation.stepOne.modal.placeholder') || ''} onChange={e => setInputValue(e.target.value)} />
  57. </div>
  58. <div className='flex flex-row-reverse'>
  59. <Button className='w-24 ml-2' variant='primary' onClick={submit}>{t('datasetCreation.stepOne.modal.confirmButton')}</Button>
  60. <Button className='w-24' onClick={onHide}>{t('datasetCreation.stepOne.modal.cancelButton')}</Button>
  61. </div>
  62. </Modal>
  63. )
  64. }
  65. export default EmptyDatasetCreationModal