deadRageSelectorCards.tsx 9.3 KB

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