replaysErroneousDeadRageCards.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import {Fragment, useMemo} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {Button} from 'sentry/components/button';
  6. import {IconClose, IconSearch} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {Organization} from 'sentry/types';
  10. import EventView from 'sentry/utils/discover/eventView';
  11. import useReplayList from 'sentry/utils/replays/hooks/useReplayList';
  12. import {useHaveSelectedProjectsSentAnyReplayEvents} from 'sentry/utils/replays/hooks/useReplayOnboarding';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import ReplayTable from 'sentry/views/replays/replayTable';
  16. import {ReplayColumn} from 'sentry/views/replays/replayTable/types';
  17. import {ReplayListLocationQuery} from 'sentry/views/replays/types';
  18. function ReplaysErroneousDeadRageCards() {
  19. const organization = useOrganization();
  20. const location = useLocation<ReplayListLocationQuery>();
  21. const newLocation = useMemo(() => {
  22. return {
  23. pathname: '',
  24. search: '',
  25. hash: '',
  26. state: '',
  27. action: 'PUSH' as const,
  28. key: '',
  29. query: {
  30. project: location.query.project,
  31. environment: location.query.environment,
  32. start: location.query.start,
  33. statsPeriod: location.query.statsPeriod,
  34. utc: location.query.utc,
  35. end: location.query.end,
  36. },
  37. };
  38. }, [
  39. location.query.project,
  40. location.query.environment,
  41. location.query.start,
  42. location.query.statsPeriod,
  43. location.query.utc,
  44. location.query.end,
  45. ]);
  46. const eventViewDead = useMemo(() => {
  47. return EventView.fromNewQueryWithLocation(
  48. {
  49. id: '',
  50. name: '',
  51. version: 2,
  52. fields: [
  53. 'activity',
  54. 'duration',
  55. 'count_dead_clicks',
  56. 'id',
  57. 'project_id',
  58. 'user',
  59. 'finished_at',
  60. 'is_archived',
  61. 'started_at',
  62. ],
  63. projects: [],
  64. query: 'count_dead_clicks:>0',
  65. orderby: '-count_dead_clicks',
  66. },
  67. newLocation
  68. );
  69. }, [newLocation]);
  70. const eventViewRage = useMemo(() => {
  71. return EventView.fromNewQueryWithLocation(
  72. {
  73. id: '',
  74. name: '',
  75. version: 2,
  76. fields: [
  77. 'activity',
  78. 'duration',
  79. 'count_rage_clicks',
  80. 'id',
  81. 'project_id',
  82. 'user',
  83. 'finished_at',
  84. 'is_archived',
  85. 'started_at',
  86. ],
  87. projects: [],
  88. query: 'count_rage_clicks:>0',
  89. orderby: '-count_rage_clicks',
  90. },
  91. newLocation
  92. );
  93. }, [newLocation]);
  94. const hasSessionReplay = organization.features.includes('session-replay');
  95. const hasDeadRageCards = organization.features.includes('replay-error-click-cards');
  96. const {hasSentOneReplay, fetching} = useHaveSelectedProjectsSentAnyReplayEvents();
  97. const deadCols = [ReplayColumn.MOST_DEAD_CLICKS, ReplayColumn.COUNT_DEAD_CLICKS];
  98. const rageCols = [ReplayColumn.MOST_RAGE_CLICKS, ReplayColumn.COUNT_RAGE_CLICKS];
  99. return hasSessionReplay && hasDeadRageCards && hasSentOneReplay && !fetching ? (
  100. <SplitCardContainer>
  101. <CardTable
  102. eventView={eventViewDead}
  103. location={newLocation}
  104. organization={organization}
  105. visibleColumns={deadCols}
  106. searchQuery={{
  107. ...location.query,
  108. cursor: undefined,
  109. query: 'count_dead_clicks:>0',
  110. sort: '-count_dead_clicks',
  111. }}
  112. buttonLabel={t('Show all replays with dead clicks')}
  113. />
  114. <CardTable
  115. eventView={eventViewRage}
  116. location={newLocation}
  117. organization={organization}
  118. visibleColumns={rageCols}
  119. searchQuery={{
  120. ...location.query,
  121. cursor: undefined,
  122. query: 'count_rage_clicks:>0',
  123. sort: '-count_rage_clicks',
  124. }}
  125. buttonLabel={t('Show all replays with rage clicks')}
  126. />
  127. </SplitCardContainer>
  128. ) : null;
  129. }
  130. function CardTable({
  131. buttonLabel,
  132. eventView,
  133. location,
  134. organization,
  135. searchQuery,
  136. visibleColumns,
  137. }: {
  138. buttonLabel: string;
  139. eventView: EventView;
  140. location: Location;
  141. organization: Organization;
  142. searchQuery: {
  143. cursor: undefined;
  144. query: string;
  145. sort: string;
  146. };
  147. visibleColumns: ReplayColumn[];
  148. }) {
  149. const {replays, isFetching, fetchError} = useReplayList({
  150. eventView,
  151. location,
  152. organization,
  153. perPage: 3,
  154. });
  155. const gridRows = new Array(replays ? (replays.length > 0 ? 3 : 1) : 1)
  156. .fill(' ')
  157. .map(_ => '1fr')
  158. .join(' ');
  159. const emptyLocation = useLocation();
  160. const emptySearchQuery = {
  161. ...emptyLocation.query,
  162. cursor: undefined,
  163. query: '',
  164. sort: '',
  165. };
  166. return (
  167. <Fragment>
  168. <ReplayTable
  169. fetchError={fetchError}
  170. isFetching={isFetching}
  171. replays={replays}
  172. sort={undefined}
  173. visibleColumns={visibleColumns}
  174. saveLocation
  175. gridRows={'auto ' + gridRows}
  176. />
  177. <StyledButton
  178. size="sm"
  179. onClick={() => {
  180. const newQuery =
  181. emptyLocation.query.query === searchQuery.query
  182. ? emptySearchQuery
  183. : searchQuery;
  184. browserHistory.push({
  185. pathname: emptyLocation.pathname,
  186. query: newQuery,
  187. });
  188. }}
  189. icon={
  190. emptyLocation.query.query === searchQuery.query ? (
  191. <IconClose size="xs" />
  192. ) : (
  193. <IconSearch size="xs" />
  194. )
  195. }
  196. >
  197. {emptyLocation.query.query === searchQuery.query
  198. ? t('Clear filter')
  199. : buttonLabel}
  200. </StyledButton>
  201. </Fragment>
  202. );
  203. }
  204. const SplitCardContainer = styled('div')`
  205. display: grid;
  206. grid-template-columns: 1fr 1fr;
  207. grid-template-rows: max-content max-content;
  208. grid-auto-flow: column;
  209. gap: 0 ${space(2)};
  210. align-items: stretch;
  211. `;
  212. const StyledButton = styled(Button)`
  213. width: 100%;
  214. border-top: none;
  215. border-radius: ${p => p.theme.borderRadiusBottom};
  216. padding: ${space(3)};
  217. `;
  218. export default ReplaysErroneousDeadRageCards;