BrivenGrid.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // @ts-nocheck
  2. import { keepPreviousData } from '@tanstack/react-query'
  3. import { useParams } from 'common'
  4. import { PropsWithChildren, useRef } from 'react'
  5. import { DataGridHandle } from 'react-data-grid'
  6. import { Shortcuts } from './components/common/Shortcuts'
  7. import { Footer } from './components/footer/Footer'
  8. import { Grid } from './components/grid/Grid'
  9. import { Header, HeaderProps } from './components/header/Header'
  10. import { useTableSort } from './hooks/useTableSort'
  11. import { validateMsSqlSorting } from './MsSqlValidation'
  12. import { GridProps } from './types'
  13. import { formatGridDataWithOperationValues } from './utils/queueOperationUtils'
  14. import { isMsSqlForeignTable } from '@/data/table-editor/table-editor-types'
  15. import { useTableRowsQuery } from '@/data/table-rows/table-rows-query'
  16. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  17. import { RoleImpersonationState } from '@/lib/role-impersonation'
  18. import { EMPTY_ARR } from '@/lib/void'
  19. import { useRoleImpersonationStateSnapshot } from '@/state/role-impersonation-state'
  20. import { useTableEditorStateSnapshot } from '@/state/table-editor'
  21. import { QueuedOperation } from '@/state/table-editor-operation-queue.types'
  22. import { useTableEditorTableStateSnapshot } from '@/state/table-editor-table'
  23. export const BrivenGrid = ({
  24. customHeader,
  25. gridProps,
  26. children,
  27. }: PropsWithChildren<
  28. Pick<HeaderProps, 'customHeader'> & {
  29. gridProps?: GridProps
  30. }
  31. >) => {
  32. const { id: _id } = useParams()
  33. const tableId = _id ? Number(_id) : undefined
  34. const { data: project } = useSelectedProjectQuery()
  35. const tableEditorSnap = useTableEditorStateSnapshot()
  36. const snap = useTableEditorTableStateSnapshot()
  37. const preflightCheck = !tableEditorSnap.tablesToIgnorePreflightCheck.includes(tableId ?? -1)
  38. const gridRef = useRef<DataGridHandle>(null)
  39. const filters = snap.filters
  40. const { sorts, onApplySorts } = useTableSort()
  41. const roleImpersonationState = useRoleImpersonationStateSnapshot()
  42. const msSqlWarning = isMsSqlForeignTable(snap.originalTable)
  43. ? validateMsSqlSorting({ filters, sorts, table: snap.originalTable })
  44. : { warning: null }
  45. const tableQueriesEnabled = msSqlWarning.warning === null
  46. const {
  47. data,
  48. error,
  49. isSuccess,
  50. isError,
  51. isPending: isLoading,
  52. isRefetching,
  53. } = useTableRowsQuery(
  54. {
  55. projectRef: project?.ref,
  56. tableId,
  57. sorts,
  58. filters,
  59. page: snap.page,
  60. preflightCheck,
  61. limit: tableEditorSnap.rowsPerPage,
  62. roleImpersonationState: roleImpersonationState as RoleImpersonationState,
  63. },
  64. {
  65. placeholderData: keepPreviousData,
  66. enabled: tableQueriesEnabled,
  67. retry: (_, error: any) => {
  68. const doesNotExistError = error && error.message?.includes('does not exist')
  69. if (doesNotExistError) onApplySorts([])
  70. return false
  71. },
  72. }
  73. )
  74. const operations = (tableEditorSnap.operationQueue.operations as QueuedOperation[]).filter(
  75. (op) => op.tableId === tableId
  76. )
  77. const baseRows = data?.rows ?? EMPTY_ARR
  78. const rows = formatGridDataWithOperationValues({ operations, rows: baseRows })
  79. return (
  80. <div className="sb-grid h-full flex flex-col">
  81. <Header
  82. customHeader={customHeader}
  83. isRefetching={isRefetching}
  84. tableQueriesEnabled={tableQueriesEnabled}
  85. />
  86. {msSqlWarning.warning !== null && <msSqlWarning.Component />}
  87. {children || (
  88. <>
  89. <Grid
  90. ref={gridRef}
  91. {...gridProps}
  92. rows={rows}
  93. error={error}
  94. isDisabled={!tableQueriesEnabled}
  95. isLoading={isLoading}
  96. isSuccess={isSuccess}
  97. isError={isError}
  98. />
  99. <Footer enableForeignRowsQuery={tableQueriesEnabled} />
  100. <Shortcuts gridRef={gridRef} rows={rows} />
  101. </>
  102. )}
  103. </div>
  104. )
  105. }