| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | import React, { useState } from 'react' | 
					
						
							|  |  |  | import { useRouter } from 'next/navigation' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { useContext } from 'use-context-selector' | 
					
						
							| 
									
										
										
										
											2024-01-04 15:37:51 +08:00
										 |  |  | import s from './index.module.css' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import Modal from '@/app/components/base/modal' | 
					
						
							|  |  |  | import Input from '@/app/components/base/input' | 
					
						
							|  |  |  | import Button from '@/app/components/base/button' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { ToastContext } from '@/app/components/base/toast' | 
					
						
							|  |  |  | import { createEmptyDataset } from '@/service/datasets' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  | type IProps = { | 
					
						
							| 
									
										
										
										
											2024-01-04 15:37:51 +08:00
										 |  |  |   show: boolean | 
					
						
							|  |  |  |   onHide: () => void | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const EmptyDatasetCreationModal = ({ | 
					
						
							|  |  |  |   show = false, | 
					
						
							|  |  |  |   onHide, | 
					
						
							|  |  |  | }: IProps) => { | 
					
						
							|  |  |  |   const [inputValue, setInputValue] = useState('') | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const { notify } = useContext(ToastContext) | 
					
						
							|  |  |  |   const router = useRouter() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 15:37:51 +08:00
										 |  |  |   const submit = async () => { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     if (!inputValue) { | 
					
						
							|  |  |  |       notify({ type: 'error', message: t('datasetCreation.stepOne.modal.nameNotEmpty') }) | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (inputValue.length > 40) { | 
					
						
							| 
									
										
										
										
											2024-09-08 12:14:11 +07:00
										 |  |  |       notify({ type: 'error', message: t('datasetCreation.stepOne.modal.nameLengthInvalid') }) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       return | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const dataset = await createEmptyDataset({ name: inputValue }) | 
					
						
							|  |  |  |       onHide() | 
					
						
							|  |  |  |       router.push(`/datasets/${dataset.id}/documents`) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-04-14 11:27:14 +08:00
										 |  |  |     catch { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       notify({ type: 'error', message: t('datasetCreation.stepOne.modal.failed') }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <Modal | 
					
						
							|  |  |  |       isShow={show} | 
					
						
							|  |  |  |       onClose={onHide} | 
					
						
							|  |  |  |       className={cn(s.modal, '!max-w-[520px]', 'px-8')} | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <div className={s.modalHeader}> | 
					
						
							|  |  |  |         <div className={s.title}>{t('datasetCreation.stepOne.modal.title')}</div> | 
					
						
							| 
									
										
										
										
											2024-06-19 14:13:16 +08:00
										 |  |  |         <span className={s.close} onClick={onHide} /> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       </div> | 
					
						
							|  |  |  |       <div className={s.tip}>{t('datasetCreation.stepOne.modal.tip')}</div> | 
					
						
							|  |  |  |       <div className={s.form}> | 
					
						
							|  |  |  |         <div className={s.label}>{t('datasetCreation.stepOne.modal.input')}</div> | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |         <Input value={inputValue} placeholder={t('datasetCreation.stepOne.modal.placeholder') || ''} onChange={e => setInputValue(e.target.value)} /> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       </div> | 
					
						
							|  |  |  |       <div className='flex flex-row-reverse'> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <Button className='ml-2 w-24' variant='primary' onClick={submit}>{t('datasetCreation.stepOne.modal.confirmButton')}</Button> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         <Button className='w-24' onClick={onHide}>{t('datasetCreation.stepOne.modal.cancelButton')}</Button> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </Modal> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default EmptyDatasetCreationModal |