deadRageSelectorCards.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import {ComponentProps, ReactNode, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import Accordion from 'sentry/components/accordion/accordion';
  4. import {LinkButton} from 'sentry/components/button';
  5. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  6. import FeatureBadge from 'sentry/components/featureBadge';
  7. import Placeholder from 'sentry/components/placeholder';
  8. import QuestionTooltip from 'sentry/components/questionTooltip';
  9. import TextOverflow from 'sentry/components/textOverflow';
  10. import {IconCursorArrow} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import useDeadRageSelectors from 'sentry/utils/replays/hooks/useDeadRageSelectors';
  14. import {ColorOrAlias} from 'sentry/utils/theme';
  15. import {useLocation} from 'sentry/utils/useLocation';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  18. import {
  19. ContentContainer,
  20. HeaderContainer,
  21. HeaderTitleLegend,
  22. Subtitle,
  23. WidgetContainer,
  24. } from 'sentry/views/profiling/landing/styles';
  25. import ExampleReplaysList from 'sentry/views/replays/deadRageClick/exampleReplaysList';
  26. import {
  27. ProjectInfo,
  28. SelectorLink,
  29. transformSelectorQuery,
  30. } from 'sentry/views/replays/deadRageClick/selectorTable';
  31. function DeadRageSelectorCards() {
  32. return (
  33. <SplitCardContainer>
  34. <AccordionWidget
  35. clickType="count_dead_clicks"
  36. header={
  37. <div>
  38. <StyledWidgetHeader>
  39. <TitleTooltipContainer>
  40. {t('Most Dead Clicks')}
  41. <QuestionTooltip
  42. size="xs"
  43. position="top"
  44. title={t(
  45. '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).'
  46. )}
  47. isHoverable
  48. />
  49. </TitleTooltipContainer>
  50. <FeatureBadge type="beta" />
  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. <FeatureBadge type="beta" />
  74. </StyledWidgetHeader>
  75. <Subtitle>{t('Suggested replays to watch')}</Subtitle>
  76. </div>
  77. }
  78. deadOrRage="rage"
  79. />
  80. </SplitCardContainer>
  81. );
  82. }
  83. function AccordionWidget({
  84. clickType,
  85. deadOrRage,
  86. header,
  87. }: {
  88. clickType: 'count_dead_clicks' | 'count_rage_clicks';
  89. deadOrRage: 'dead' | 'rage';
  90. header: ReactNode;
  91. }) {
  92. const [selectedListIndex, setSelectListIndex] = useState(-1);
  93. const {isLoading, isError, data} = useDeadRageSelectors({
  94. per_page: 3,
  95. sort: `-${clickType}`,
  96. cursor: undefined,
  97. prefix: 'selector_',
  98. isWidgetData: true,
  99. });
  100. const location = useLocation();
  101. const filteredData = data.filter(d => (d[clickType] ?? 0) > 0);
  102. const clickColor = deadOrRage === 'dead' ? 'yellow300' : 'red300';
  103. return (
  104. <StyledWidgetContainer>
  105. <StyledHeaderContainer>
  106. <ClickColor color={clickColor}>
  107. <IconCursorArrow />
  108. </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>
  120. <div>{t('No results found')}</div>
  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. <ClickColor color={clickColor}>
  186. <IconCursorArrow size="xs" />
  187. {count}
  188. </ClickColor>
  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 ClickColor = styled(TextOverflow)<{color: ColorOrAlias}>`
  243. color: ${p => p.theme[p.color]};
  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. `;
  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. export default DeadRageSelectorCards;