replaysErroneousDeadRageCards.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 {hasSentOneReplay, fetching} = useHaveSelectedProjectsSentAnyReplayEvents();
  96. const deadCols = [
  97. ReplayColumn.MOST_DEAD_CLICKS,
  98. ReplayColumn.COUNT_DEAD_CLICKS_NO_HEADER,
  99. ];
  100. const rageCols = [
  101. ReplayColumn.MOST_RAGE_CLICKS,
  102. ReplayColumn.COUNT_RAGE_CLICKS_NO_HEADER,
  103. ];
  104. return hasSessionReplay && hasSentOneReplay && !fetching ? (
  105. <SplitCardContainer>
  106. <CardTable
  107. eventView={eventViewDead}
  108. location={newLocation}
  109. organization={organization}
  110. visibleColumns={deadCols}
  111. searchQuery={{
  112. ...location.query,
  113. cursor: undefined,
  114. query: 'count_dead_clicks:>0',
  115. sort: '-count_dead_clicks',
  116. }}
  117. buttonLabel={t('Show all replays with dead clicks')}
  118. />
  119. <CardTable
  120. eventView={eventViewRage}
  121. location={newLocation}
  122. organization={organization}
  123. visibleColumns={rageCols}
  124. searchQuery={{
  125. ...location.query,
  126. cursor: undefined,
  127. query: 'count_rage_clicks:>0',
  128. sort: '-count_rage_clicks',
  129. }}
  130. buttonLabel={t('Show all replays with rage clicks')}
  131. />
  132. </SplitCardContainer>
  133. ) : null;
  134. }
  135. function CardTable({
  136. buttonLabel,
  137. eventView,
  138. location,
  139. organization,
  140. searchQuery,
  141. visibleColumns,
  142. }: {
  143. buttonLabel: string;
  144. eventView: EventView;
  145. location: Location;
  146. organization: Organization;
  147. searchQuery: {
  148. cursor: undefined;
  149. query: string;
  150. sort: string;
  151. };
  152. visibleColumns: ReplayColumn[];
  153. }) {
  154. const {replays, isFetching, fetchError} = useReplayList({
  155. eventView,
  156. location,
  157. organization,
  158. perPage: 3,
  159. });
  160. const gridRows = new Array(replays ? (replays.length > 0 ? 3 : 1) : 1)
  161. .fill(' ')
  162. .map(_ => '1fr')
  163. .join(' ');
  164. const emptyLocation = useLocation();
  165. const emptySearchQuery = {
  166. ...emptyLocation.query,
  167. cursor: undefined,
  168. query: '',
  169. sort: '',
  170. };
  171. return (
  172. <Fragment>
  173. <ReplayTable
  174. fetchError={fetchError}
  175. isFetching={isFetching}
  176. replays={replays}
  177. sort={undefined}
  178. visibleColumns={visibleColumns}
  179. saveLocation
  180. gridRows={'auto ' + gridRows}
  181. showDropdownFilters={false}
  182. />
  183. <StyledButton
  184. size="sm"
  185. onClick={() => {
  186. const newQuery =
  187. emptyLocation.query.query === searchQuery.query
  188. ? emptySearchQuery
  189. : searchQuery;
  190. browserHistory.push({
  191. pathname: emptyLocation.pathname,
  192. query: newQuery,
  193. });
  194. }}
  195. icon={
  196. emptyLocation.query.query === searchQuery.query ? (
  197. <IconClose size="xs" />
  198. ) : (
  199. <IconSearch size="xs" />
  200. )
  201. }
  202. >
  203. {emptyLocation.query.query === searchQuery.query
  204. ? t('Clear filter')
  205. : buttonLabel}
  206. </StyledButton>
  207. </Fragment>
  208. );
  209. }
  210. const SplitCardContainer = styled('div')`
  211. display: grid;
  212. grid-template-columns: 1fr 1fr;
  213. grid-template-rows: max-content max-content;
  214. grid-auto-flow: column;
  215. gap: 0 ${space(2)};
  216. align-items: stretch;
  217. `;
  218. const StyledButton = styled(Button)`
  219. width: 100%;
  220. border-top: none;
  221. border-radius: ${p => p.theme.borderRadiusBottom};
  222. padding: ${space(3)};
  223. `;
  224. export default ReplaysErroneousDeadRageCards;