rename-modal.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useBoolean } from 'ahooks'
  6. import Toast from '../../base/toast'
  7. import Modal from '@/app/components/base/modal'
  8. import Button from '@/app/components/base/button'
  9. import Input from '@/app/components/base/input'
  10. import { renameDocumentName } from '@/service/datasets'
  11. type Props = {
  12. datasetId: string
  13. documentId: string
  14. name: string
  15. onClose: () => void
  16. onSaved: () => void
  17. }
  18. const RenameModal: FC<Props> = ({
  19. documentId,
  20. datasetId,
  21. name,
  22. onClose,
  23. onSaved,
  24. }) => {
  25. const { t } = useTranslation()
  26. const [newName, setNewName] = useState(name)
  27. const [saveLoading, {
  28. setTrue: setSaveLoadingTrue,
  29. setFalse: setSaveLoadingFalse,
  30. }] = useBoolean(false)
  31. const handleSave = async () => {
  32. setSaveLoadingTrue()
  33. try {
  34. await renameDocumentName({
  35. datasetId,
  36. documentId,
  37. name: newName,
  38. })
  39. Toast.notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  40. onSaved()
  41. onClose()
  42. }
  43. catch (error) {
  44. if (error)
  45. Toast.notify({ type: 'error', message: error.toString() })
  46. }
  47. finally {
  48. setSaveLoadingFalse()
  49. }
  50. }
  51. return (
  52. <Modal
  53. title={t('datasetDocuments.list.table.rename')}
  54. isShow
  55. onClose={onClose}
  56. >
  57. <div className={'mt-6 font-medium text-sm leading-[21px] text-gray-900'}>{t('datasetDocuments.list.table.name')}</div>
  58. <Input
  59. className='mt-2 h-10'
  60. value={newName}
  61. onChange={e => setNewName(e.target.value)}
  62. />
  63. <div className='mt-10 flex justify-end'>
  64. <Button className='mr-2 flex-shrink-0' onClick={onClose}>{t('common.operation.cancel')}</Button>
  65. <Button variant='primary' className='flex-shrink-0' onClick={handleSave} loading={saveLoading}>{t('common.operation.save')}</Button>
  66. </div>
  67. </Modal>
  68. )
  69. }
  70. export default React.memo(RenameModal)