screensTable.tsx 5.1 KB

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