deadRageSelectorCards.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import type {ComponentProps, ReactNode} from 'react';
  2. import {useState} from 'react';
  3. import styled from '@emotion/styled';
  4. import Accordion from 'sentry/components/accordion/accordion';
  5. import {LinkButton} from 'sentry/components/button';
  6. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  7. import Placeholder from 'sentry/components/placeholder';
  8. import {Flex} from 'sentry/components/profiling/flex';
  9. import QuestionTooltip from 'sentry/components/questionTooltip';
  10. import TextOverflow from 'sentry/components/textOverflow';
  11. import {IconCursorArrow, IconSearch} from 'sentry/icons';
  12. import {t, tct} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import useDeadRageSelectors from 'sentry/utils/replays/hooks/useDeadRageSelectors';
  15. import type {ColorOrAlias} from 'sentry/utils/theme';
  16. import {useLocation} from 'sentry/utils/useLocation';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  19. import {
  20. ContentContainer,
  21. HeaderContainer,
  22. HeaderTitleLegend,
  23. Subtitle,
  24. WidgetContainer,
  25. } from 'sentry/views/profiling/landing/styles';
  26. import ExampleReplaysList from 'sentry/views/replays/deadRageClick/exampleReplaysList';
  27. import {
  28. ProjectInfo,
  29. SelectorLink,
  30. transformSelectorQuery,
  31. } from 'sentry/views/replays/deadRageClick/selectorTable';
  32. function DeadRageSelectorCards() {
  33. return (
  34. <SplitCardContainer>
  35. <AccordionWidget
  36. clickType="count_dead_clicks"
  37. header={
  38. <div>
  39. <StyledWidgetHeader>
  40. <TitleTooltipContainer>
  41. {t('Most Dead Clicks')}
  42. <QuestionTooltip
  43. size="xs"
  44. position="top"
  45. title={t(
  46. 'The top selectors your users have dead clicked on (i.e., a user click that does not result in any page activity after 7 seconds).'
  47. )}
  48. isHoverable
  49. />
  50. </TitleTooltipContainer>
  51. </StyledWidgetHeader>
  52. <Subtitle>{t('Suggested replays to watch')}</Subtitle>
  53. </div>
  54. }
  55. deadOrRage="dead"
  56. />
  57. <AccordionWidget
  58. clickType="count_rage_clicks"
  59. header={
  60. <div>
  61. <StyledWidgetHeader>
  62. <TitleTooltipContainer>
  63. {t('Most Rage Clicks')}
  64. <QuestionTooltip
  65. size="xs"
  66. position="top"
  67. title={t(
  68. 'The top selectors your users have rage clicked on (i.e., 5 or more clicks on a dead element, which exhibits no page activity after 7 seconds).'
  69. )}
  70. isHoverable
  71. />
  72. </TitleTooltipContainer>
  73. </StyledWidgetHeader>
  74. <Subtitle>{t('Suggested replays to watch')}</Subtitle>
  75. </div>
  76. }
  77. deadOrRage="rage"
  78. />
  79. </SplitCardContainer>
  80. );
  81. }
  82. function AccordionWidget({
  83. clickType,
  84. deadOrRage,
  85. header,
  86. }: {
  87. clickType: 'count_dead_clicks' | 'count_rage_clicks';
  88. deadOrRage: 'dead' | 'rage';
  89. header: ReactNode;
  90. }) {
  91. const [selectedListIndex, setSelectListIndex] = useState(-1);
  92. const {isLoading, isError, data} = useDeadRageSelectors({
  93. per_page: 3,
  94. sort: `-${clickType}`,
  95. cursor: undefined,
  96. prefix: 'selector_',
  97. isWidgetData: true,
  98. });
  99. const location = useLocation();
  100. const filteredData = data.filter(d => (d[clickType] ?? 0) > 0);
  101. const clickColor = deadOrRage === 'dead' ? 'yellow300' : 'red300';
  102. return (
  103. <StyledWidgetContainer data-test-id="selector-widget">
  104. <StyledHeaderContainer>
  105. <IconCursorArrow color={clickColor} />
  106. {header}
  107. </StyledHeaderContainer>
  108. {isLoading ? (
  109. <LoadingContainer>
  110. <StyledPlaceholder />
  111. <StyledPlaceholder />
  112. <StyledPlaceholder />
  113. </LoadingContainer>
  114. ) : isError || (!isLoading && filteredData.length === 0) ? (
  115. <CenteredContentContainer>
  116. <EmptyStateWarning withIcon={false}>
  117. <EmptyHeader>
  118. <IconSearch size="sm" />
  119. {t('No results found')}
  120. </EmptyHeader>
  121. <EmptySubtitle>
  122. {tct(
  123. 'There were no [type] clicks within this timeframe. Expand your timeframe, or increase your replay sample rate to see more data.',
  124. {type: deadOrRage}
  125. )}
  126. </EmptySubtitle>
  127. </EmptyStateWarning>
  128. </CenteredContentContainer>
  129. ) : (
  130. <LeftAlignedContentContainer>
  131. <Accordion
  132. buttonOnLeft
  133. collapsible
  134. expandedIndex={selectedListIndex}
  135. setExpandedIndex={setSelectListIndex}
  136. items={filteredData.map(d => {
  137. const selectorQuery = `${deadOrRage}.selector:"${transformSelectorQuery(
  138. d.dom_element.fullSelector
  139. )}"`;
  140. return {
  141. header: () => (
  142. <AccordionItemHeader
  143. count={d[clickType] ?? 0}
  144. selector={d.dom_element.selector}
  145. clickColor={clickColor}
  146. selectorQuery={selectorQuery}
  147. id={d.project_id}
  148. />
  149. ),
  150. content: () => (
  151. <ExampleReplaysList
  152. location={location}
  153. clickType={clickType}
  154. selectorQuery={selectorQuery}
  155. projectId={d.project_id}
  156. />
  157. ),
  158. };
  159. })}
  160. />
  161. </LeftAlignedContentContainer>
  162. )}
  163. <SearchButton
  164. label={t('See all selectors')}
  165. path="selectors"
  166. sort={`-${clickType}`}
  167. />
  168. </StyledWidgetContainer>
  169. );
  170. }
  171. function AccordionItemHeader({
  172. count,
  173. clickColor,
  174. selector,
  175. selectorQuery,
  176. id,
  177. }: {
  178. clickColor: ColorOrAlias;
  179. count: number;
  180. id: number;
  181. selector: string;
  182. selectorQuery: string;
  183. }) {
  184. const clickCount = (
  185. <ClickCount>
  186. <IconCursorArrow size="xs" color={clickColor} />
  187. {count}
  188. </ClickCount>
  189. );
  190. return (
  191. <StyledAccordionHeader>
  192. <SelectorLink
  193. value={selector}
  194. selectorQuery={selectorQuery}
  195. projectId={id.toString()}
  196. />
  197. <RightAlignedCell>
  198. {clickCount}
  199. <ProjectInfo id={id} isWidget />
  200. </RightAlignedCell>
  201. </StyledAccordionHeader>
  202. );
  203. }
  204. function SearchButton({
  205. label,
  206. sort,
  207. path,
  208. ...props
  209. }: {
  210. label: ReactNode;
  211. path: string;
  212. sort: string;
  213. } & Omit<ComponentProps<typeof LinkButton>, 'size' | 'to'>) {
  214. const location = useLocation();
  215. const organization = useOrganization();
  216. return (
  217. <StyledButton
  218. {...props}
  219. size="xs"
  220. to={{
  221. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${path}/`),
  222. query: {
  223. ...location.query,
  224. sort,
  225. query: undefined,
  226. cursor: undefined,
  227. },
  228. }}
  229. >
  230. {label}
  231. </StyledButton>
  232. );
  233. }
  234. const SplitCardContainer = styled('div')`
  235. display: grid;
  236. grid-template-columns: 1fr 1fr;
  237. grid-template-rows: max-content;
  238. grid-auto-flow: column;
  239. gap: 0 ${space(2)};
  240. align-items: stretch;
  241. `;
  242. const ClickCount = styled(TextOverflow)`
  243. color: ${p => p.theme.gray400};
  244. display: grid;
  245. grid-template-columns: auto auto;
  246. gap: ${space(0.75)};
  247. align-items: center;
  248. `;
  249. const StyledHeaderContainer = styled(HeaderContainer)`
  250. grid-auto-flow: row;
  251. align-items: center;
  252. grid-template-rows: auto;
  253. grid-template-columns: 30px auto;
  254. `;
  255. const LeftAlignedContentContainer = styled(ContentContainer)`
  256. justify-content: flex-start;
  257. `;
  258. const CenteredContentContainer = styled(ContentContainer)`
  259. justify-content: center;
  260. `;
  261. const StyledButton = styled(LinkButton)`
  262. width: 100%;
  263. border-radius: ${p => p.theme.borderRadiusBottom};
  264. padding: ${space(3)};
  265. border-bottom: none;
  266. border-left: none;
  267. border-right: none;
  268. font-size: ${p => p.theme.fontSizeMedium};
  269. background-color: transparent;
  270. `;
  271. const StyledAccordionHeader = styled('div')`
  272. display: grid;
  273. grid-template-columns: 1fr max-content;
  274. flex: 1;
  275. `;
  276. const TitleTooltipContainer = styled('div')`
  277. display: flex;
  278. gap: ${space(1)};
  279. align-items: center;
  280. `;
  281. const StyledWidgetHeader = styled(HeaderTitleLegend)`
  282. display: grid;
  283. justify-content: space-between;
  284. align-items: center;
  285. `;
  286. const StyledWidgetContainer = styled(WidgetContainer)`
  287. margin-bottom: 0;
  288. padding-top: ${space(1.5)};
  289. `;
  290. export const RightAlignedCell = styled('div')`
  291. text-align: right;
  292. display: flex;
  293. align-items: center;
  294. justify-content: center;
  295. gap: ${space(1)};
  296. padding-left: ${space(1)};
  297. `;
  298. const EmptySubtitle = styled('div')`
  299. font-size: ${p => p.theme.fontSizeMedium};
  300. line-height: 1.6em;
  301. padding-left: ${space(1)};
  302. padding-right: ${space(1)};
  303. `;
  304. const LoadingContainer = styled(ContentContainer)`
  305. gap: ${space(0.25)};
  306. padding: ${space(1)} ${space(0.5)} 3px ${space(0.5)};
  307. `;
  308. const StyledPlaceholder = styled(Placeholder)`
  309. height: 34px;
  310. `;
  311. const EmptyHeader = styled(Flex)`
  312. justify-content: center;
  313. align-items: center;
  314. gap: ${space(1.5)};
  315. color: ${p => p.theme.gray300};
  316. `;
  317. export default DeadRageSelectorCards;