deadRageSelectorCards.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import {ComponentProps, ReactNode, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {LinkButton} from 'sentry/components/button';
  4. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import QuestionTooltip from 'sentry/components/questionTooltip';
  7. import TextOverflow from 'sentry/components/textOverflow';
  8. import {IconCursorArrow} from 'sentry/icons';
  9. import {t, tct} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import useDeadRageSelectors from 'sentry/utils/replays/hooks/useDeadRageSelectors';
  12. import {ColorOrAlias} from 'sentry/utils/theme';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  16. import Accordion from 'sentry/views/performance/landing/widgets/components/accordion';
  17. import {
  18. ContentContainer,
  19. HeaderContainer,
  20. HeaderTitleLegend,
  21. StatusContainer,
  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. {t('Most Dead Clicks')}
  40. <QuestionTooltip
  41. size="xs"
  42. position="top"
  43. title={t('The top selectors your users have dead clicked on.')}
  44. isHoverable
  45. />
  46. </StyledWidgetHeader>
  47. <Subtitle>{t('Suggested replays to watch')}</Subtitle>
  48. </div>
  49. }
  50. deadOrRage="dead"
  51. />
  52. <AccordionWidget
  53. clickType="count_rage_clicks"
  54. header={
  55. <div>
  56. <StyledWidgetHeader>
  57. {t('Most Rage Clicks')}
  58. <QuestionTooltip
  59. size="xs"
  60. position="top"
  61. title={t('The top selectors your users have rage clicked on.')}
  62. isHoverable
  63. />
  64. </StyledWidgetHeader>
  65. <Subtitle>{t('Suggested replays to watch')}</Subtitle>
  66. </div>
  67. }
  68. deadOrRage="rage"
  69. />
  70. </SplitCardContainer>
  71. );
  72. }
  73. function AccordionWidget({
  74. clickType,
  75. deadOrRage,
  76. header,
  77. }: {
  78. clickType: 'count_dead_clicks' | 'count_rage_clicks';
  79. deadOrRage: 'dead' | 'rage';
  80. header: ReactNode;
  81. }) {
  82. const [selectedListIndex, setSelectListIndex] = useState(-1);
  83. const {isLoading, isError, data} = useDeadRageSelectors({
  84. per_page: 3,
  85. sort: `-${clickType}`,
  86. cursor: undefined,
  87. prefix: 'selector_',
  88. isWidgetData: true,
  89. });
  90. const location = useLocation();
  91. const filteredData = data.filter(d => (d[clickType] ?? 0) > 0);
  92. const clickColor = deadOrRage === 'dead' ? 'yellow300' : 'red300';
  93. return (
  94. <StyledWidgetContainer>
  95. <StyledHeaderContainer>
  96. <ClickColor color={clickColor}>
  97. <IconCursorArrow />
  98. </ClickColor>
  99. {header}
  100. </StyledHeaderContainer>
  101. {isLoading && (
  102. <StatusContainer>
  103. <LoadingIndicator />
  104. </StatusContainer>
  105. )}
  106. {isError || (!isLoading && filteredData.length === 0) ? (
  107. <CenteredContentContainer>
  108. <EmptyStateWarning>
  109. <div>{t('No results found')}</div>
  110. <EmptySubtitle>
  111. {tct(
  112. "Once your users start clicking around, you'll see the top selectors that were [type] clicked here.",
  113. {type: deadOrRage}
  114. )}
  115. </EmptySubtitle>
  116. </EmptyStateWarning>
  117. </CenteredContentContainer>
  118. ) : (
  119. <LeftAlignedContentContainer>
  120. <Accordion
  121. buttonOnLeft
  122. expandedIndex={selectedListIndex}
  123. setExpandedIndex={setSelectListIndex}
  124. items={filteredData.map(d => {
  125. const selectorQuery = `${deadOrRage}.selector:"${transformSelectorQuery(
  126. d.dom_element
  127. )}"`;
  128. return {
  129. header: () => (
  130. <AccordionItemHeader
  131. count={d[clickType] ?? 0}
  132. selector={d.dom_element}
  133. clickColor={clickColor}
  134. selectorQuery={selectorQuery}
  135. id={d.project_id}
  136. />
  137. ),
  138. content: () => (
  139. <ExampleReplaysList
  140. location={location}
  141. clickType={clickType}
  142. selectorQuery={selectorQuery}
  143. projectId={d.project_id}
  144. />
  145. ),
  146. };
  147. })}
  148. />
  149. </LeftAlignedContentContainer>
  150. )}
  151. <SearchButton
  152. label={t('See all selectors')}
  153. path="selectors"
  154. sort={`-${clickType}`}
  155. />
  156. </StyledWidgetContainer>
  157. );
  158. }
  159. function AccordionItemHeader({
  160. count,
  161. clickColor,
  162. selector,
  163. selectorQuery,
  164. id,
  165. }: {
  166. clickColor: ColorOrAlias;
  167. count: number;
  168. id: number;
  169. selector: string;
  170. selectorQuery: string;
  171. }) {
  172. const clickCount = (
  173. <ClickColor color={clickColor}>
  174. <IconCursorArrow size="xs" />
  175. {count}
  176. </ClickColor>
  177. );
  178. return (
  179. <StyledAccordionHeader>
  180. <SelectorLink value={selector} selectorQuery={selectorQuery} />
  181. <RightAlignedCell>
  182. {clickCount}
  183. <ProjectInfo id={id} isWidget />
  184. </RightAlignedCell>
  185. </StyledAccordionHeader>
  186. );
  187. }
  188. function SearchButton({
  189. label,
  190. sort,
  191. path,
  192. ...props
  193. }: {
  194. label: ReactNode;
  195. path: string;
  196. sort: string;
  197. } & Omit<ComponentProps<typeof LinkButton>, 'size' | 'to'>) {
  198. const location = useLocation();
  199. const organization = useOrganization();
  200. return (
  201. <StyledButton
  202. {...props}
  203. size="xs"
  204. to={{
  205. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${path}/`),
  206. query: {
  207. ...location.query,
  208. sort,
  209. query: undefined,
  210. cursor: undefined,
  211. },
  212. }}
  213. >
  214. {label}
  215. </StyledButton>
  216. );
  217. }
  218. const SplitCardContainer = styled('div')`
  219. display: grid;
  220. grid-template-columns: 1fr 1fr;
  221. grid-template-rows: max-content;
  222. grid-auto-flow: column;
  223. gap: 0 ${space(2)};
  224. align-items: stretch;
  225. `;
  226. const ClickColor = styled(TextOverflow)<{color: ColorOrAlias}>`
  227. color: ${p => p.theme[p.color]};
  228. display: grid;
  229. grid-template-columns: auto auto;
  230. gap: ${space(0.75)};
  231. align-items: center;
  232. `;
  233. const StyledHeaderContainer = styled(HeaderContainer)`
  234. grid-auto-flow: row;
  235. align-items: center;
  236. grid-template-rows: auto;
  237. grid-template-columns: 30px auto;
  238. `;
  239. const LeftAlignedContentContainer = styled(ContentContainer)`
  240. justify-content: flex-start;
  241. `;
  242. const CenteredContentContainer = styled(ContentContainer)`
  243. justify-content: center;
  244. `;
  245. const StyledButton = styled(LinkButton)`
  246. width: 100%;
  247. border-radius: ${p => p.theme.borderRadiusBottom};
  248. padding: ${space(3)};
  249. border-bottom: none;
  250. border-left: none;
  251. border-right: none;
  252. font-size: ${p => p.theme.fontSizeMedium};
  253. `;
  254. const StyledAccordionHeader = styled('div')`
  255. display: grid;
  256. grid-template-columns: 1fr max-content;
  257. flex: 1;
  258. `;
  259. const StyledWidgetHeader = styled(HeaderTitleLegend)`
  260. display: grid;
  261. gap: ${space(1)};
  262. justify-content: start;
  263. align-items: center;
  264. `;
  265. const StyledWidgetContainer = styled(WidgetContainer)`
  266. margin-bottom: 0;
  267. padding-top: ${space(1.5)};
  268. `;
  269. export const RightAlignedCell = styled('div')`
  270. text-align: right;
  271. display: flex;
  272. align-items: center;
  273. justify-content: center;
  274. gap: ${space(1)};
  275. `;
  276. const EmptySubtitle = styled('div')`
  277. font-size: ${p => p.theme.fontSizeMedium};
  278. line-height: 1.8em;
  279. padding-left: ${space(1)};
  280. padding-right: ${space(1)};
  281. `;
  282. export default DeadRageSelectorCards;