deadRageSelectorCards.tsx 8.7 KB

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