EmptyBucketModal.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { useParams } from 'common'
  2. import { toast } from 'sonner'
  3. import {
  4. Button,
  5. Dialog,
  6. DialogContent,
  7. DialogFooter,
  8. DialogHeader,
  9. DialogSection,
  10. DialogSectionSeparator,
  11. DialogTitle,
  12. } from 'ui'
  13. import { Admonition } from 'ui-patterns'
  14. import { useBucketEmptyMutation } from '@/data/storage/bucket-empty-mutation'
  15. import type { Bucket } from '@/data/storage/buckets-query'
  16. import { useStorageExplorerStateSnapshot } from '@/state/storage-explorer'
  17. export interface EmptyBucketModalProps {
  18. visible: boolean
  19. bucket?: Bucket
  20. onClose: () => void
  21. }
  22. export const EmptyBucketModal = ({ visible, bucket, onClose }: EmptyBucketModalProps) => {
  23. const { ref: projectRef } = useParams()
  24. const { fetchFolderContents } = useStorageExplorerStateSnapshot()
  25. const { mutate: emptyBucket, isPending } = useBucketEmptyMutation({
  26. onSuccess: async () => {
  27. if (bucket === undefined) return
  28. await fetchFolderContents({
  29. bucketId: bucket.id,
  30. folderId: bucket.id,
  31. folderName: bucket.name,
  32. index: -1,
  33. })
  34. toast.success(`Successfully emptied bucket ${bucket!.name}`)
  35. onClose()
  36. },
  37. })
  38. const onEmptyBucket = async () => {
  39. if (!projectRef) return console.error('Project ref is required')
  40. if (!bucket) return console.error('No bucket is selected')
  41. emptyBucket({ projectRef, id: bucket.id })
  42. }
  43. return (
  44. <Dialog
  45. open={visible}
  46. onOpenChange={(open) => {
  47. if (!open) onClose()
  48. }}
  49. >
  50. <DialogContent>
  51. <DialogHeader>
  52. <DialogTitle>{`Empty bucket “${bucket?.name}”`}</DialogTitle>
  53. </DialogHeader>
  54. <DialogSectionSeparator />
  55. <Admonition
  56. type="destructive"
  57. className="rounded-none border-x-0 border-t-0"
  58. title="This action cannot be undone"
  59. description="The contents of your bucket cannot be recovered once deleted."
  60. />
  61. <DialogSection>
  62. <p className="text-sm">
  63. Are you sure you want to remove all contents from the bucket “{bucket?.name}”?
  64. </p>
  65. </DialogSection>
  66. <DialogFooter>
  67. <Button type="default" disabled={isPending} onClick={onClose}>
  68. Cancel
  69. </Button>
  70. <Button type="danger" loading={isPending} onClick={onEmptyBucket}>
  71. Empty bucket
  72. </Button>
  73. </DialogFooter>
  74. </DialogContent>
  75. </Dialog>
  76. )
  77. }