MoveItemsModal.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { noop } from 'lodash'
  2. import { useEffect, useState } from 'react'
  3. import { Button, Input, Modal } from 'ui'
  4. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  5. import { StorageItemWithColumn } from '../Storage.types'
  6. interface MoveItemsModalProps {
  7. bucketName: string
  8. visible: boolean
  9. selectedItemsToMove: StorageItemWithColumn[]
  10. onSelectCancel: () => void
  11. onSelectMove: (path: string) => void
  12. }
  13. export const MoveItemsModal = ({
  14. bucketName = '',
  15. visible = false,
  16. selectedItemsToMove = [],
  17. onSelectCancel = noop,
  18. onSelectMove = noop,
  19. }: MoveItemsModalProps) => {
  20. const [moving, setMoving] = useState(false)
  21. const [newPath, setNewPath] = useState('')
  22. useEffect(() => {
  23. setMoving(false)
  24. setNewPath('')
  25. }, [visible])
  26. const multipleFiles = selectedItemsToMove.length > 1
  27. const title = multipleFiles
  28. ? `Moving ${selectedItemsToMove.length} items within ${bucketName}`
  29. : selectedItemsToMove.length === 1
  30. ? `Moving ${selectedItemsToMove[0]?.name} within ${bucketName}`
  31. : ``
  32. const description = `Enter the path to where you'd like to move the file${
  33. multipleFiles ? 's' : ''
  34. } to.`
  35. const onConfirmMove = (event: any) => {
  36. if (event) {
  37. event.preventDefault()
  38. }
  39. setMoving(true)
  40. const formattedPath = newPath[0] === '/' ? newPath.slice(1) : newPath
  41. onSelectMove(formattedPath)
  42. }
  43. return (
  44. <Modal
  45. visible={visible}
  46. header={title}
  47. description={description}
  48. size="medium"
  49. onCancel={onSelectCancel}
  50. customFooter={
  51. <div className="flex items-center gap-2">
  52. <Button type="default" onClick={onSelectCancel}>
  53. Cancel
  54. </Button>
  55. <Button type="primary" loading={moving} onClick={onConfirmMove}>
  56. {moving ? 'Moving files' : 'Move files'}
  57. </Button>
  58. </div>
  59. }
  60. >
  61. <Modal.Content>
  62. <form>
  63. <FormItemLayout
  64. label={`Path to new directory in ${bucketName}`}
  65. description="Leave blank to move items to the root of the bucket"
  66. layout="vertical"
  67. isReactForm={false}
  68. >
  69. <Input
  70. autoFocus
  71. type="text"
  72. placeholder="e.g folder1/subfolder2"
  73. value={newPath}
  74. onChange={(event) => setNewPath(event.target.value)}
  75. />
  76. </FormItemLayout>
  77. <button className="hidden" type="submit" onClick={onConfirmMove} />
  78. </form>
  79. </Modal.Content>
  80. </Modal>
  81. )
  82. }