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.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use client'
  2. import cn from '@/utils/classnames'
  3. import Modal from '@/app/components/base/modal'
  4. import Input from '@/app/components/base/input'
  5. import { useTranslation } from 'react-i18next'
  6. import { useState } from 'react'
  7. import { useContext } from 'use-context-selector'
  8. import s from './index.module.css'
  9. import Button from '@/app/components/base/button'
  10. import { RiCloseLine } from '@remixicon/react'
  11. import { useAppContext } from '@/context/app-context'
  12. import { updateWorkspaceInfo } from '@/service/common'
  13. import { ToastContext } from '@/app/components/base/toast'
  14. import { noop } from 'lodash-es'
  15. type IEditWorkspaceModalProps = {
  16. onCancel: () => void
  17. }
  18. const EditWorkspaceModal = ({
  19. onCancel,
  20. }: IEditWorkspaceModalProps) => {
  21. const { t } = useTranslation()
  22. const { notify } = useContext(ToastContext)
  23. const { currentWorkspace, isCurrentWorkspaceOwner } = useAppContext()
  24. const [name, setName] = useState<string>(currentWorkspace.name)
  25. const changeWorkspaceInfo = async (name: string) => {
  26. try {
  27. await updateWorkspaceInfo({
  28. url: '/workspaces/info',
  29. body: {
  30. name,
  31. },
  32. })
  33. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  34. location.assign(`${location.origin}`)
  35. }
  36. catch {
  37. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  38. }
  39. }
  40. return (
  41. <div className={cn(s.wrap)}>
  42. <Modal overflowVisible isShow onClose={noop} className={cn(s.modal)}>
  43. <div className='mb-2 flex justify-between'>
  44. <div className='text-xl font-semibold text-text-primary'>{t('common.account.editWorkspaceInfo')}</div>
  45. <RiCloseLine className='h-4 w-4 cursor-pointer text-text-tertiary' onClick={onCancel} />
  46. </div>
  47. <div>
  48. <div className='mb-2 text-sm font-medium text-text-primary'>{t('common.account.workspaceName')}</div>
  49. <Input
  50. className='mb-2'
  51. value={name}
  52. placeholder={t('common.account.workspaceNamePlaceholder')}
  53. onChange={(e) => {
  54. setName(e.target.value)
  55. }}
  56. onClear={() => {
  57. setName(currentWorkspace.name)
  58. }}
  59. />
  60. <div className='sticky bottom-0 -mx-2 mt-2 flex flex-wrap items-center justify-end gap-x-2 bg-components-panel-bg px-2 pt-4'>
  61. <Button
  62. size='large'
  63. onClick={onCancel}
  64. >
  65. {t('common.operation.cancel')}
  66. </Button>
  67. <Button
  68. size='large'
  69. variant='primary'
  70. onClick={() => {
  71. changeWorkspaceInfo(name)
  72. onCancel()
  73. }}
  74. disabled={!isCurrentWorkspaceOwner}
  75. >
  76. {t('common.operation.confirm')}
  77. </Button>
  78. </div>
  79. </div>
  80. </Modal>
  81. </div>
  82. )
  83. }
  84. export default EditWorkspaceModal