BucketsTable.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { useRef } from 'react'
  2. import { Table, TableBody } from 'ui'
  3. import { LoadMoreRow } from './BucketsTable.LoadMoreRow'
  4. import { BucketTableHeader } from './BucketTableHeader'
  5. import { BucketTableEmptyState, BucketTableRow } from './BucketTableRow'
  6. import type { AllowedBucketType, BucketsTablePaginationProps } from './types'
  7. import { VirtualizedTable, VirtualizedTableBody } from '@/components/ui/VirtualizedTable'
  8. import { type Bucket } from '@/data/storage/buckets-query'
  9. type BucketsTableProps = {
  10. buckets: Bucket[]
  11. projectRef: string
  12. filterString: string
  13. formattedGlobalUploadLimit: string
  14. onSelectBucket: (bucket: Bucket) => void
  15. allowedBucketType: AllowedBucketType
  16. pagination: BucketsTablePaginationProps
  17. }
  18. export const BucketsTable = (props: BucketsTableProps) => {
  19. const isVirtualized = props.buckets.length > 50
  20. return isVirtualized ? (
  21. <BucketsTableVirtualized {...props} />
  22. ) : (
  23. <BucketsTableUnvirtualized {...props} />
  24. )
  25. }
  26. const BucketsTableUnvirtualized = ({
  27. buckets,
  28. filterString,
  29. formattedGlobalUploadLimit,
  30. onSelectBucket,
  31. allowedBucketType,
  32. pagination: { hasMore = false, isLoadingMore = false, onLoadMore },
  33. }: BucketsTableProps) => {
  34. const showSearchEmptyState = buckets.length === 0 && filterString.length > 0
  35. return (
  36. <Table
  37. containerProps={{
  38. containerClassName: 'h-full overflow-auto',
  39. className: 'overflow-visible',
  40. }}
  41. >
  42. <BucketTableHeader mode="standard" hasBuckets={buckets.length > 0} />
  43. <TableBody>
  44. {showSearchEmptyState ? (
  45. <BucketTableEmptyState mode="standard" filterString={filterString} />
  46. ) : (
  47. buckets.map((bucket) => (
  48. <BucketTableRow
  49. mode="standard"
  50. key={bucket.id}
  51. bucket={bucket}
  52. onSelectBucket={onSelectBucket}
  53. allowedBucketType={allowedBucketType}
  54. formattedGlobalUploadLimit={formattedGlobalUploadLimit}
  55. />
  56. ))
  57. )}
  58. <LoadMoreRow
  59. mode="standard"
  60. colSpan={6}
  61. hasMore={hasMore}
  62. isLoadingMore={isLoadingMore}
  63. onLoadMore={onLoadMore}
  64. />
  65. </TableBody>
  66. </Table>
  67. )
  68. }
  69. const BucketsTableVirtualized = ({
  70. buckets,
  71. filterString,
  72. formattedGlobalUploadLimit,
  73. onSelectBucket,
  74. allowedBucketType,
  75. pagination: { hasMore = false, isLoadingMore = false, onLoadMore },
  76. }: BucketsTableProps) => {
  77. const showSearchEmptyState = buckets.length === 0 && filterString.length > 0
  78. const scrollContainerRef = useRef<HTMLDivElement>(null)
  79. return (
  80. <VirtualizedTable
  81. data={buckets}
  82. estimateSize={() => 59}
  83. getItemKey={(bucket) => bucket.id}
  84. scrollContainerRef={scrollContainerRef}
  85. >
  86. <BucketTableHeader mode="virtualized" hasBuckets={buckets.length > 0} />
  87. <VirtualizedTableBody<Bucket>
  88. paddingColSpan={5}
  89. emptyContent={
  90. showSearchEmptyState ? (
  91. <BucketTableEmptyState mode="virtualized" filterString={filterString} />
  92. ) : undefined
  93. }
  94. trailingContent={
  95. <LoadMoreRow
  96. mode="virtualized"
  97. colSpan={6}
  98. hasMore={hasMore}
  99. isLoadingMore={isLoadingMore}
  100. onLoadMore={onLoadMore}
  101. />
  102. }
  103. >
  104. {(bucket) => (
  105. <BucketTableRow
  106. mode="virtualized"
  107. key={bucket.id}
  108. bucket={bucket}
  109. onSelectBucket={onSelectBucket}
  110. allowedBucketType={allowedBucketType}
  111. formattedGlobalUploadLimit={formattedGlobalUploadLimit}
  112. />
  113. )}
  114. </VirtualizedTableBody>
  115. </VirtualizedTable>
  116. )
  117. }