deadRageSelectorCards.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import type {ComponentProps, ReactNode} from 'react';
  2. import {useState} from 'react';
  3. import styled from '@emotion/styled';
  4. import {LinkButton} from 'sentry/components/button';
  5. import {Flex} from 'sentry/components/container/flex';
  6. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  7. import Placeholder from 'sentry/components/placeholder';
  8. import QuestionTooltip from 'sentry/components/questionTooltip';
  9. import Accordion from 'sentry/components/replays/accordion';
  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. collapsible
  133. expandedIndex={selectedListIndex}
  134. setExpandedIndex={setSelectListIndex}
  135. items={filteredData.map(d => {
  136. const selectorQuery = `${deadOrRage}.selector:"${transformSelectorQuery(
  137. d.dom_element.fullSelector
  138. )}"`;
  139. return {
  140. header: (
  141. <AccordionItemHeader
  142. count={d[clickType] ?? 0}
  143. selector={d.dom_element.selector}
  144. clickColor={clickColor}
  145. selectorQuery={selectorQuery}
  146. id={d.project_id}
  147. />
  148. ),
  149. content: (
  150. <ExampleReplaysList
  151. location={location}
  152. clickType={clickType}
  153. selectorQuery={selectorQuery}
  154. projectId={d.project_id}
  155. />
  156. ),
  157. };
  158. })}
  159. />
  160. </LeftAlignedContentContainer>
  161. )}
  162. <SearchButton
  163. label={t('See all selectors')}
  164. path="selectors"
  165. sort={`-${clickType}`}
  166. />
  167. </StyledWidgetContainer>
  168. );
  169. }
  170. function AccordionItemHeader({
  171. count,
  172. clickColor,
  173. selector,
  174. selectorQuery,
  175. id,
  176. }: {
  177. clickColor: ColorOrAlias;
  178. count: number;
  179. id: number;
  180. selector: string;
  181. selectorQuery: string;
  182. }) {
  183. const clickCount = (
  184. <ClickCount>
  185. <IconCursorArrow size="xs" color={clickColor} />
  186. {count}
  187. </ClickCount>
  188. );
  189. return (
  190. <StyledAccordionHeader>
  191. <SelectorLink
  192. value={selector}
  193. selectorQuery={selectorQuery}
  194. projectId={id.toString()}
  195. />
  196. <RightAlignedCell>
  197. {clickCount}
  198. <ProjectInfo id={id} isWidget />
  199. </RightAlignedCell>
  200. </StyledAccordionHeader>
  201. );
  202. }
  203. function SearchButton({
  204. label,
  205. sort,
  206. path,
  207. ...props
  208. }: {
  209. label: ReactNode;
  210. path: string;
  211. sort: string;
  212. } & Omit<ComponentProps<typeof LinkButton>, 'size' | 'to'>) {
  213. const location = useLocation();
  214. const organization = useOrganization();
  215. return (
  216. <StyledButton
  217. {...props}
  218. size="xs"
  219. to={{
  220. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${path}/`),
  221. query: {
  222. ...location.query,
  223. sort,
  224. query: undefined,
  225. cursor: undefined,
  226. },
  227. }}
  228. >
  229. {label}
  230. </StyledButton>
  231. );
  232. }
  233. const SplitCardContainer = styled('div')`
  234. display: grid;
  235. grid-template-columns: 1fr 1fr;
  236. grid-template-rows: max-content;
  237. grid-auto-flow: column;
  238. gap: 0 ${space(2)};
  239. align-items: stretch;
  240. `;
  241. const ClickCount = styled(TextOverflow)`
  242. color: ${p => p.theme.gray400};
  243. display: grid;
  244. grid-template-columns: auto auto;
  245. gap: ${space(0.75)};
  246. align-items: center;
  247. `;
  248. const StyledHeaderContainer = styled(HeaderContainer)`
  249. grid-auto-flow: row;
  250. align-items: center;
  251. grid-template-rows: auto;
  252. grid-template-columns: 30px auto;
  253. `;
  254. const LeftAlignedContentContainer = styled(ContentContainer)`
  255. justify-content: flex-start;
  256. `;
  257. const CenteredContentContainer = styled(ContentContainer)`
  258. justify-content: center;
  259. `;
  260. const StyledButton = styled(LinkButton)`
  261. width: 100%;
  262. border-radius: ${p => p.theme.borderRadiusBottom};
  263. padding: ${space(3)};
  264. border-bottom: none;
  265. border-left: none;
  266. border-right: none;
  267. font-size: ${p => p.theme.fontSizeMedium};
  268. background-color: transparent;
  269. `;
  270. const StyledAccordionHeader = styled('div')`
  271. display: grid;
  272. grid-template-columns: 1fr max-content;
  273. flex: 1;
  274. `;
  275. const TitleTooltipContainer = styled('div')`
  276. display: flex;
  277. gap: ${space(1)};
  278. align-items: center;
  279. `;
  280. const StyledWidgetHeader = styled(HeaderTitleLegend)`
  281. display: grid;
  282. justify-content: space-between;
  283. align-items: center;
  284. `;
  285. const StyledWidgetContainer = styled(WidgetContainer)`
  286. margin-bottom: 0;
  287. padding-top: ${space(1.5)};
  288. `;
  289. export const RightAlignedCell = styled('div')`
  290. text-align: right;
  291. display: flex;
  292. align-items: center;
  293. justify-content: center;
  294. gap: ${space(1)};
  295. padding-left: ${space(1)};
  296. `;
  297. const EmptySubtitle = styled('div')`
  298. font-size: ${p => p.theme.fontSizeMedium};
  299. line-height: 1.6em;
  300. padding-left: ${space(1)};
  301. padding-right: ${space(1)};
  302. `;
  303. const LoadingContainer = styled(ContentContainer)`
  304. gap: ${space(0.25)};
  305. padding: ${space(1)} ${space(0.5)} 3px ${space(0.5)};
  306. `;
  307. const StyledPlaceholder = styled(Placeholder)`
  308. height: 34px;
  309. `;
  310. const EmptyHeader = styled(Flex)`
  311. justify-content: center;
  312. align-items: center;
  313. gap: ${space(1.5)};
  314. color: ${p => p.theme.gray300};
  315. `;
  316. export default DeadRageSelectorCards;