screensTable.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {Fragment} from 'react';
  2. import type {
  3. GridColumn,
  4. GridColumnHeader,
  5. GridColumnSortBy,
  6. } from 'sentry/components/gridEditable';
  7. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  8. import SortLink from 'sentry/components/gridEditable/sortLink';
  9. import Pagination from 'sentry/components/pagination';
  10. import {defined} from 'sentry/utils';
  11. import {trackAnalytics} from 'sentry/utils/analytics';
  12. import type {TableData, TableDataRow} from 'sentry/utils/discover/discoverQuery';
  13. import type EventView from 'sentry/utils/discover/eventView';
  14. import {isFieldSortable} from 'sentry/utils/discover/eventView';
  15. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  16. import {fieldAlignment} from 'sentry/utils/discover/fields';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import {PercentChangeCell} from 'sentry/views/insights/common/components/tableCells/percentChangeCell';
  20. import type {ModuleName} from 'sentry/views/insights/types';
  21. type Props = {
  22. columnNameMap: Record<string, string>;
  23. columnOrder: string[];
  24. data: TableData | undefined;
  25. defaultSort: GridColumnSortBy<string>[];
  26. eventView: EventView;
  27. isLoading: boolean;
  28. pageLinks: string | undefined;
  29. customBodyCellRenderer?: (
  30. column: GridColumn<string>,
  31. row: TableDataRow
  32. ) => React.ReactNode;
  33. moduleName?: ModuleName;
  34. };
  35. export function ScreensTable({
  36. data,
  37. eventView,
  38. isLoading,
  39. pageLinks,
  40. columnNameMap,
  41. columnOrder,
  42. defaultSort,
  43. customBodyCellRenderer,
  44. moduleName,
  45. }: Props) {
  46. const location = useLocation();
  47. const organization = useOrganization();
  48. function renderBodyCell(
  49. column: GridColumn<string>,
  50. row: TableDataRow
  51. ): React.ReactNode {
  52. if (!data?.meta || !data?.meta.fields) {
  53. return row[column.key];
  54. }
  55. if (defined(customBodyCellRenderer)) {
  56. const customRenderedCell = customBodyCellRenderer(column, row);
  57. if (defined(customRenderedCell)) {
  58. return customRenderedCell;
  59. }
  60. }
  61. if (data.meta.fields[column.key] === 'percent_change') {
  62. return (
  63. <PercentChangeCell
  64. deltaValue={parseFloat(row[column.key] as string) || 0}
  65. preferredPolarity="-"
  66. />
  67. );
  68. }
  69. const renderer = getFieldRenderer(column.key, data?.meta.fields, false);
  70. return renderer(row, {
  71. location,
  72. organization,
  73. unit: data?.meta.units?.[column.key],
  74. });
  75. }
  76. function renderHeadCell(column: GridColumnHeader): React.ReactNode {
  77. const fieldType = data?.meta?.fields?.[column.key];
  78. const alignment = fieldAlignment(column.key as string, fieldType);
  79. const field = {
  80. field: column.key as string,
  81. width: column.width,
  82. };
  83. function generateSortLink() {
  84. if (!data?.meta) {
  85. return undefined;
  86. }
  87. const nextEventView = eventView.sortOnField(field, data?.meta);
  88. const queryStringObject = nextEventView.generateQueryStringObject();
  89. return {
  90. ...location,
  91. query: {...location.query, sort: queryStringObject.sort},
  92. };
  93. }
  94. const currentSort = eventView.sortForField(field, data?.meta);
  95. const currentSortKind = currentSort ? currentSort.kind : undefined;
  96. const canSort = isFieldSortable(field, data?.meta);
  97. const sortLink = (
  98. <SortLink
  99. align={alignment}
  100. title={column.name}
  101. direction={currentSortKind}
  102. canSort={canSort}
  103. generateSortLink={generateSortLink}
  104. />
  105. );
  106. return sortLink;
  107. }
  108. return (
  109. <Fragment>
  110. <GridEditable
  111. isLoading={isLoading}
  112. data={data?.data as TableDataRow[]}
  113. columnOrder={columnOrder.map(columnKey => {
  114. return {
  115. key: columnKey,
  116. name: columnNameMap[columnKey],
  117. width: COL_WIDTH_UNDEFINED,
  118. };
  119. })}
  120. columnSortBy={defaultSort}
  121. grid={{
  122. renderHeadCell,
  123. renderBodyCell,
  124. }}
  125. />
  126. <Pagination
  127. pageLinks={pageLinks}
  128. paginationAnalyticsEvent={(direction: string) => {
  129. if (moduleName !== undefined) {
  130. trackAnalytics('insight.general.table_paginate', {
  131. organization,
  132. source: moduleName,
  133. direction,
  134. });
  135. }
  136. }}
  137. />
  138. </Fragment>
  139. );
  140. }