issueFieldRenderers.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import AssigneeSelector from 'sentry/components/assigneeSelector';
  6. import Count from 'sentry/components/count';
  7. import Link from 'sentry/components/links/link';
  8. import {getRelativeSummary} from 'sentry/components/organizations/timeRangeSelector/utils';
  9. import Tooltip from 'sentry/components/tooltip';
  10. import {DEFAULT_STATS_PERIOD} from 'sentry/constants';
  11. import {t} from 'sentry/locale';
  12. import MemberListStore from 'sentry/stores/memberListStore';
  13. import space from 'sentry/styles/space';
  14. import {Organization} from 'sentry/types';
  15. import EventView, {EventData} from 'sentry/utils/discover/eventView';
  16. import {FieldKey} from 'sentry/views/dashboardsV2/widgetBuilder/issueWidget/fields';
  17. import {Container, FieldShortId, OverflowLink} from '../discover/styles';
  18. /**
  19. * Types, functions and definitions for rendering fields in discover results.
  20. */
  21. type RenderFunctionBaggage = {
  22. location: Location;
  23. organization: Organization;
  24. eventView?: EventView;
  25. };
  26. type FieldFormatterRenderFunctionPartial = (
  27. data: EventData,
  28. baggage: RenderFunctionBaggage
  29. ) => React.ReactNode;
  30. type SpecialFieldRenderFunc = (
  31. data: EventData,
  32. baggage: RenderFunctionBaggage
  33. ) => React.ReactNode;
  34. type SpecialField = {
  35. renderFunc: SpecialFieldRenderFunc;
  36. sortField: string | null;
  37. };
  38. type SpecialFields = {
  39. assignee: SpecialField;
  40. count: SpecialField;
  41. events: SpecialField;
  42. issue: SpecialField;
  43. lifetimeCount: SpecialField;
  44. lifetimeEvents: SpecialField;
  45. lifetimeUserCount: SpecialField;
  46. lifetimeUsers: SpecialField;
  47. links: SpecialField;
  48. userCount: SpecialField;
  49. users: SpecialField;
  50. };
  51. /**
  52. * "Special fields" either do not map 1:1 to an single column in the event database,
  53. * or they require custom UI formatting that can't be handled by the datatype formatters.
  54. */
  55. const SPECIAL_FIELDS: SpecialFields = {
  56. issue: {
  57. sortField: null,
  58. renderFunc: (data, {organization}) => {
  59. const issueID = data['issue.id'];
  60. if (!issueID) {
  61. return (
  62. <Container>
  63. <FieldShortId shortId={`${data.issue}`} />
  64. </Container>
  65. );
  66. }
  67. const target = {
  68. pathname: `/organizations/${organization.slug}/issues/${issueID}/`,
  69. };
  70. return (
  71. <Container>
  72. <OverflowLink to={target} aria-label={issueID}>
  73. <FieldShortId shortId={`${data.issue}`} />
  74. </OverflowLink>
  75. </Container>
  76. );
  77. },
  78. },
  79. assignee: {
  80. sortField: null,
  81. renderFunc: data => {
  82. const memberList = MemberListStore.getAll();
  83. return (
  84. <ActorContainer>
  85. <AssigneeSelector id={data.id} memberList={memberList} noDropdown />
  86. </ActorContainer>
  87. );
  88. },
  89. },
  90. lifetimeEvents: {
  91. sortField: null,
  92. renderFunc: (data, {organization}) =>
  93. issuesCountRenderer(data, organization, 'lifetimeEvents'),
  94. },
  95. lifetimeUsers: {
  96. sortField: null,
  97. renderFunc: (data, {organization}) =>
  98. issuesCountRenderer(data, organization, 'lifetimeUsers'),
  99. },
  100. events: {
  101. sortField: 'freq',
  102. renderFunc: (data, {organization}) =>
  103. issuesCountRenderer(data, organization, 'events'),
  104. },
  105. users: {
  106. sortField: 'user',
  107. renderFunc: (data, {organization}) =>
  108. issuesCountRenderer(data, organization, 'users'),
  109. },
  110. lifetimeCount: {
  111. sortField: null,
  112. renderFunc: (data, {organization}) =>
  113. issuesCountRenderer(data, organization, 'lifetimeEvents'),
  114. },
  115. lifetimeUserCount: {
  116. sortField: null,
  117. renderFunc: (data, {organization}) =>
  118. issuesCountRenderer(data, organization, 'lifetimeUsers'),
  119. },
  120. count: {
  121. sortField: null,
  122. renderFunc: (data, {organization}) =>
  123. issuesCountRenderer(data, organization, 'events'),
  124. },
  125. userCount: {
  126. sortField: null,
  127. renderFunc: (data, {organization}) =>
  128. issuesCountRenderer(data, organization, 'users'),
  129. },
  130. links: {
  131. sortField: null,
  132. renderFunc: ({links}) => <LinksContainer dangerouslySetInnerHTML={{__html: links}} />,
  133. },
  134. };
  135. const issuesCountRenderer = (
  136. data: EventData,
  137. organization: Organization,
  138. field: 'events' | 'users' | 'lifetimeEvents' | 'lifetimeUsers'
  139. ) => {
  140. const {start, end, period} = data;
  141. const isUserField = !!/user/i.exec(field.toLowerCase());
  142. const primaryCount = data[field];
  143. const count = data[isUserField ? 'users' : 'events'];
  144. const lifetimeCount = data[isUserField ? 'lifetimeUsers' : 'lifetimeEvents'];
  145. const filteredCount = data[isUserField ? 'filteredUsers' : 'filteredEvents'];
  146. const discoverLink = getDiscoverUrl(data, organization);
  147. const filteredDiscoverLink = getDiscoverUrl(data, organization, true);
  148. const selectionDateString =
  149. !!start && !!end
  150. ? 'time range'
  151. : getRelativeSummary(period || DEFAULT_STATS_PERIOD).toLowerCase();
  152. return (
  153. <Container>
  154. <Tooltip
  155. isHoverable
  156. skipWrapper
  157. overlayStyle={{padding: 0}}
  158. title={
  159. <div>
  160. {filteredCount ? (
  161. <Fragment>
  162. <StyledLink to={filteredDiscoverLink}>
  163. {t('Matching search filters')}
  164. <WrappedCount value={filteredCount} />
  165. </StyledLink>
  166. <Divider />
  167. </Fragment>
  168. ) : null}
  169. <StyledLink to={discoverLink}>
  170. {t('Total in %s', selectionDateString)}
  171. <WrappedCount value={count} />
  172. </StyledLink>
  173. <Divider />
  174. <StyledContent>
  175. {t('Since issue began')}
  176. <WrappedCount value={lifetimeCount} />
  177. </StyledContent>
  178. </div>
  179. }
  180. >
  181. <span>
  182. {['events', 'users'].includes(field) && filteredCount ? (
  183. <Fragment>
  184. <Count value={filteredCount} />
  185. <SecondaryCount value={primaryCount} />
  186. </Fragment>
  187. ) : (
  188. <Count value={primaryCount} />
  189. )}
  190. </span>
  191. </Tooltip>
  192. </Container>
  193. );
  194. };
  195. const getDiscoverUrl = (
  196. data: EventData,
  197. organization: Organization,
  198. filtered?: boolean
  199. ) => {
  200. const commonQuery = {projects: [Number(data.projectId)]};
  201. const discoverView = EventView.fromSavedQuery({
  202. ...commonQuery,
  203. id: undefined,
  204. start: data.start,
  205. end: data.end,
  206. range: data.period,
  207. name: data.title,
  208. fields: ['title', 'release', 'environment', 'user', 'timestamp'],
  209. orderby: '-timestamp',
  210. query: `issue.id:${data.id}${filtered ? data.discoverSearchQuery : ''}`,
  211. version: 2,
  212. });
  213. return discoverView.getResultsViewUrlTarget(organization.slug);
  214. };
  215. export function getSortField(field: string): string | null {
  216. if (SPECIAL_FIELDS.hasOwnProperty(field)) {
  217. return SPECIAL_FIELDS[field as keyof typeof SPECIAL_FIELDS].sortField;
  218. }
  219. switch (field) {
  220. case FieldKey.LAST_SEEN:
  221. return 'date';
  222. case FieldKey.FIRST_SEEN:
  223. return 'new';
  224. default:
  225. return null;
  226. }
  227. }
  228. const contentStyle = css`
  229. width: 100%;
  230. justify-content: space-between;
  231. display: flex;
  232. padding: 6px 10px;
  233. `;
  234. const StyledContent = styled('div')`
  235. ${contentStyle};
  236. `;
  237. const StyledLink = styled(Link)`
  238. ${contentStyle};
  239. color: ${p => p.theme.gray400};
  240. &:hover {
  241. color: ${p => p.theme.gray400};
  242. background: ${p => p.theme.hover};
  243. }
  244. `;
  245. const SecondaryCount = styled(Count)`
  246. :before {
  247. content: '/';
  248. padding-left: ${space(0.25)};
  249. padding-right: 2px;
  250. }
  251. `;
  252. const WrappedCount = styled(({value, ...p}) => (
  253. <div {...p}>
  254. <Count value={value} />
  255. </div>
  256. ))`
  257. text-align: right;
  258. font-weight: bold;
  259. font-variant-numeric: tabular-nums;
  260. padding-left: ${space(2)};
  261. color: ${p => p.theme.subText};
  262. `;
  263. const Divider = styled('div')`
  264. height: 1px;
  265. overflow: hidden;
  266. background-color: ${p => p.theme.innerBorder};
  267. `;
  268. const ActorContainer = styled('div')`
  269. display: flex;
  270. justify-content: left;
  271. margin-left: 18px;
  272. /* IconUser is the only one with 20px. We are setting 24px here to make the height consistent */
  273. height: 24px;
  274. :hover {
  275. cursor: default;
  276. }
  277. `;
  278. const LinksContainer = styled('span')`
  279. white-space: nowrap;
  280. `;
  281. /**
  282. * Get the field renderer for the named field and metadata
  283. *
  284. * @param {String} field name
  285. * @param {object} metadata mapping.
  286. * @returns {Function}
  287. */
  288. export function getIssueFieldRenderer(
  289. field: string
  290. ): FieldFormatterRenderFunctionPartial | null {
  291. if (SPECIAL_FIELDS.hasOwnProperty(field)) {
  292. return SPECIAL_FIELDS[field].renderFunc;
  293. }
  294. // Return null if there is no field renderer for this field
  295. // Should check the discover field renderer for this field
  296. return null;
  297. }