fieldRenderer.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import styled from '@emotion/styled';
  2. import Link from 'sentry/components/links/link';
  3. import TimeSince from 'sentry/components/timeSince';
  4. import type {TableDataRow} from 'sentry/utils/discover/discoverQuery';
  5. import type {EventData, MetaType} from 'sentry/utils/discover/eventView';
  6. import EventView from 'sentry/utils/discover/eventView';
  7. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  8. import {Container} from 'sentry/utils/discover/styles';
  9. import {generateLinkToEventInTraceView} from 'sentry/utils/discover/urls';
  10. import {getShortEventId} from 'sentry/utils/events';
  11. import {generateProfileFlamechartRouteWithQuery} from 'sentry/utils/profiling/routes';
  12. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import CellAction, {updateQuery} from 'sentry/views/discover/table/cellAction';
  16. import type {TableColumn} from 'sentry/views/discover/table/types';
  17. import {TraceViewSources} from 'sentry/views/performance/newTraceDetails/traceHeader/breadcrumbs';
  18. import {getTraceDetailsUrl} from 'sentry/views/performance/traceDetails/utils';
  19. import {ALLOWED_CELL_ACTIONS} from '../components/table';
  20. import {useUserQuery} from '../hooks/useUserQuery';
  21. interface FieldProps {
  22. column: TableColumn<keyof TableDataRow>;
  23. data: EventData;
  24. meta: MetaType;
  25. unit?: string;
  26. }
  27. export function FieldRenderer({data, meta, unit, column}: FieldProps) {
  28. const location = useLocation();
  29. const organization = useOrganization();
  30. const [userQuery, setUserQuery] = useUserQuery();
  31. const dateSelection = EventView.fromLocation(location).normalizeDateSelection(location);
  32. const query = new MutableSearch(userQuery);
  33. const field = column.name;
  34. const renderer = getExploreFieldRenderer(field, meta);
  35. let rendered = renderer(data, {
  36. location,
  37. organization,
  38. unit,
  39. });
  40. if (field === 'timestamp') {
  41. const date = new Date(data.timestamp);
  42. rendered = <StyledTimeSince unitStyle="extraShort" date={date} tooltipShowSeconds />;
  43. }
  44. if (field === 'trace') {
  45. const target = getTraceDetailsUrl({
  46. traceSlug: data.trace,
  47. timestamp: data.timestamp,
  48. organization,
  49. dateSelection,
  50. location,
  51. source: TraceViewSources.TRACES,
  52. });
  53. rendered = <Link to={target}>{rendered}</Link>;
  54. }
  55. if (['id', 'span_id', 'transaction.id'].includes(field)) {
  56. const spanId = field === 'transaction.id' ? undefined : data.span_id ?? data.id;
  57. const target = generateLinkToEventInTraceView({
  58. projectSlug: data.project,
  59. traceSlug: data.trace,
  60. timestamp: data.timestamp,
  61. targetId: data['transaction.span_id'],
  62. eventId: undefined,
  63. organization,
  64. location,
  65. spanId,
  66. source: TraceViewSources.TRACES,
  67. });
  68. rendered = <Link to={target}>{rendered}</Link>;
  69. }
  70. if (field === 'profile.id') {
  71. const target = generateProfileFlamechartRouteWithQuery({
  72. orgSlug: organization.slug,
  73. projectSlug: data.project,
  74. profileId: data['profile.id'],
  75. });
  76. rendered = <Link to={target}>{rendered}</Link>;
  77. }
  78. return (
  79. <CellAction
  80. column={column}
  81. dataRow={data as TableDataRow}
  82. handleCellAction={(actions, value) => {
  83. updateQuery(query, actions, column, value);
  84. setUserQuery(query.formatString());
  85. }}
  86. allowActions={ALLOWED_CELL_ACTIONS}
  87. >
  88. {rendered}
  89. </CellAction>
  90. );
  91. }
  92. function getExploreFieldRenderer(
  93. field: string,
  94. meta: MetaType
  95. ): ReturnType<typeof getFieldRenderer> {
  96. if (field === 'id' || field === 'span_id') {
  97. return eventIdRenderFunc(field);
  98. }
  99. return getFieldRenderer(field, meta, false);
  100. }
  101. function eventIdRenderFunc(field: string) {
  102. function renderer(data: EventData) {
  103. const spanId: string | unknown = data?.[field];
  104. if (typeof spanId !== 'string') {
  105. return null;
  106. }
  107. return <Container>{getShortEventId(spanId)}</Container>;
  108. }
  109. return renderer;
  110. }
  111. const StyledTimeSince = styled(TimeSince)`
  112. width: fit-content;
  113. `;