tableCell.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. import {browserHistory} from 'react-router';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import Avatar from 'sentry/components/avatar';
  6. import {Button} from 'sentry/components/button';
  7. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  8. import UserBadge from 'sentry/components/idBadge/userBadge';
  9. import Link from 'sentry/components/links/link';
  10. import ContextIcon from 'sentry/components/replays/contextIcon';
  11. import {formatTime} from 'sentry/components/replays/utils';
  12. import ScoreBar from 'sentry/components/scoreBar';
  13. import TimeSince from 'sentry/components/timeSince';
  14. import {Tooltip} from 'sentry/components/tooltip';
  15. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  16. import {
  17. IconCalendar,
  18. IconCursorArrow,
  19. IconDelete,
  20. IconEllipsis,
  21. IconFire,
  22. } from 'sentry/icons';
  23. import {t, tct} from 'sentry/locale';
  24. import {space, ValidSize} from 'sentry/styles/space';
  25. import type {Organization} from 'sentry/types';
  26. import {trackAnalytics} from 'sentry/utils/analytics';
  27. import EventView from 'sentry/utils/discover/eventView';
  28. import {spanOperationRelativeBreakdownRenderer} from 'sentry/utils/discover/fieldRenderers';
  29. import {getShortEventId} from 'sentry/utils/events';
  30. import {decodeScalar} from 'sentry/utils/queryString';
  31. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  32. import {useLocation} from 'sentry/utils/useLocation';
  33. import useMedia from 'sentry/utils/useMedia';
  34. import useProjects from 'sentry/utils/useProjects';
  35. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  36. import type {ReplayListRecordWithTx} from 'sentry/views/performance/transactionSummary/transactionReplays/useReplaysWithTxData';
  37. import type {ReplayListLocationQuery, ReplayListRecord} from 'sentry/views/replays/types';
  38. type Props = {
  39. replay: ReplayListRecord | ReplayListRecordWithTx;
  40. showDropdownFilters?: boolean;
  41. };
  42. export type ReferrerTableType =
  43. | 'main'
  44. | 'dead-table'
  45. | 'errors-table'
  46. | 'rage-table'
  47. | 'selector-widget';
  48. type EditType = 'set' | 'remove';
  49. function generateAction({
  50. key,
  51. value,
  52. edit,
  53. location,
  54. }: {
  55. edit: EditType;
  56. key: string;
  57. location: Location<ReplayListLocationQuery>;
  58. value: string;
  59. }) {
  60. const search = new MutableSearch(decodeScalar(location.query.query) || '');
  61. const modifiedQuery =
  62. edit === 'set' ? search.setFilterValues(key, [value]) : search.removeFilter(key);
  63. const onAction = () => {
  64. browserHistory.push({
  65. pathname: location.pathname,
  66. query: {
  67. ...location.query,
  68. query: modifiedQuery.formatString(),
  69. },
  70. });
  71. };
  72. return onAction;
  73. }
  74. function OSBrowserDropdownFilter({
  75. type,
  76. name,
  77. version,
  78. }: {
  79. name: string | null;
  80. type: string;
  81. version: string | null;
  82. }) {
  83. const location = useLocation<ReplayListLocationQuery>();
  84. return (
  85. <DropdownMenu
  86. items={[
  87. ...(name
  88. ? [
  89. {
  90. key: 'name',
  91. label: tct('[type] name: [name]', {
  92. type: <b>{type}</b>,
  93. name: <b>{name}</b>,
  94. }),
  95. children: [
  96. {
  97. key: 'name_add',
  98. label: t('Add to filter'),
  99. onAction: generateAction({
  100. key: `${type}.name`,
  101. value: name ?? '',
  102. edit: 'set',
  103. location,
  104. }),
  105. },
  106. {
  107. key: 'name_exclude',
  108. label: t('Exclude from filter'),
  109. onAction: generateAction({
  110. key: `${type}.name`,
  111. value: name ?? '',
  112. edit: 'remove',
  113. location,
  114. }),
  115. },
  116. ],
  117. },
  118. ]
  119. : []),
  120. ...(version
  121. ? [
  122. {
  123. key: 'version',
  124. label: tct('[type] version: [version]', {
  125. type: <b>{type}</b>,
  126. version: <b>{version}</b>,
  127. }),
  128. children: [
  129. {
  130. key: 'version_add',
  131. label: t('Add to filter'),
  132. onAction: generateAction({
  133. key: `${type}.version`,
  134. value: version ?? '',
  135. edit: 'set',
  136. location,
  137. }),
  138. },
  139. {
  140. key: 'version_exclude',
  141. label: t('Exclude from filter'),
  142. onAction: generateAction({
  143. key: `${type}.version`,
  144. value: version ?? '',
  145. edit: 'remove',
  146. location,
  147. }),
  148. },
  149. ],
  150. },
  151. ]
  152. : []),
  153. ]}
  154. usePortal
  155. size="xs"
  156. offset={4}
  157. position="bottom"
  158. preventOverflowOptions={{padding: 4}}
  159. flipOptions={{
  160. fallbackPlacements: ['top', 'right-start', 'right-end', 'left-start', 'left-end'],
  161. }}
  162. trigger={triggerProps => (
  163. <ActionMenuTrigger
  164. {...triggerProps}
  165. translucentBorder
  166. aria-label={t('Actions')}
  167. icon={<IconEllipsis size="xs" />}
  168. size="zero"
  169. />
  170. )}
  171. />
  172. );
  173. }
  174. function NumericDropdownFilter({
  175. type,
  176. val,
  177. triggerOverlay,
  178. }: {
  179. type: string;
  180. val: number;
  181. triggerOverlay?: boolean;
  182. }) {
  183. const location = useLocation<ReplayListLocationQuery>();
  184. return (
  185. <DropdownMenu
  186. items={[
  187. {
  188. key: 'add',
  189. label: 'Add to filter',
  190. onAction: generateAction({
  191. key: type,
  192. value: val.toString(),
  193. edit: 'set',
  194. location,
  195. }),
  196. },
  197. {
  198. key: 'greater',
  199. label: 'Show values greater than',
  200. onAction: generateAction({
  201. key: type,
  202. value: '>' + val.toString(),
  203. edit: 'set',
  204. location,
  205. }),
  206. },
  207. {
  208. key: 'less',
  209. label: 'Show values less than',
  210. onAction: generateAction({
  211. key: type,
  212. value: '<' + val.toString(),
  213. edit: 'set',
  214. location,
  215. }),
  216. },
  217. {
  218. key: 'exclude',
  219. label: t('Exclude from filter'),
  220. onAction: generateAction({
  221. key: type,
  222. value: val.toString(),
  223. edit: 'remove',
  224. location,
  225. }),
  226. },
  227. ]}
  228. usePortal
  229. size="xs"
  230. offset={4}
  231. position="bottom"
  232. preventOverflowOptions={{padding: 4}}
  233. flipOptions={{
  234. fallbackPlacements: ['top', 'right-start', 'right-end', 'left-start', 'left-end'],
  235. }}
  236. trigger={triggerProps =>
  237. triggerOverlay ? (
  238. <OverlayActionMenuTrigger
  239. {...triggerProps}
  240. translucentBorder
  241. aria-label={t('Actions')}
  242. icon={<IconEllipsis size="xs" />}
  243. size="zero"
  244. />
  245. ) : (
  246. <NumericActionMenuTrigger
  247. {...triggerProps}
  248. translucentBorder
  249. aria-label={t('Actions')}
  250. icon={<IconEllipsis size="xs" />}
  251. size="zero"
  252. />
  253. )
  254. }
  255. />
  256. );
  257. }
  258. function getUserBadgeUser(replay: Props['replay']) {
  259. return replay.is_archived
  260. ? {
  261. username: '',
  262. email: '',
  263. id: '',
  264. ip_address: '',
  265. name: '',
  266. }
  267. : {
  268. username: replay.user?.display_name || '',
  269. email: replay.user?.email || '',
  270. id: replay.user?.id || '',
  271. ip_address: replay.user?.ip || '',
  272. name: replay.user?.username || '',
  273. };
  274. }
  275. export function ReplayCell({
  276. eventView,
  277. organization,
  278. referrer,
  279. replay,
  280. referrer_table,
  281. isWidget,
  282. }: Props & {
  283. eventView: EventView;
  284. organization: Organization;
  285. referrer: string;
  286. referrer_table: ReferrerTableType;
  287. isWidget?: boolean;
  288. }) {
  289. const {projects} = useProjects();
  290. const project = projects.find(p => p.id === replay.project_id);
  291. const replayDetails = {
  292. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${replay.id}/`),
  293. query: {
  294. referrer,
  295. ...eventView.generateQueryStringObject(),
  296. },
  297. };
  298. const replayDetailsErrorTab = {
  299. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${replay.id}/`),
  300. query: {
  301. referrer,
  302. ...eventView.generateQueryStringObject(),
  303. t_main: 'errors',
  304. },
  305. };
  306. const replayDetailsDeadRage = {
  307. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${replay.id}/`),
  308. query: {
  309. referrer,
  310. ...eventView.generateQueryStringObject(),
  311. f_b_type: 'rageOrDead',
  312. },
  313. };
  314. const detailsTab = () => {
  315. switch (referrer_table) {
  316. case 'errors-table':
  317. return replayDetailsErrorTab;
  318. case 'dead-table':
  319. case 'rage-table':
  320. case 'selector-widget':
  321. return replayDetailsDeadRage;
  322. default:
  323. return replayDetails;
  324. }
  325. };
  326. const trackNavigationEvent = () =>
  327. trackAnalytics('replay.list-navigate-to-details', {
  328. project_id: project?.id,
  329. platform: project?.platform,
  330. organization,
  331. referrer,
  332. referrer_table,
  333. });
  334. if (replay.is_archived) {
  335. return (
  336. <Item isArchived={replay.is_archived}>
  337. <Row gap={1}>
  338. <StyledIconDelete color="gray500" size="md" />
  339. <div>
  340. <Row gap={0.5}>{t('Deleted Replay')}</Row>
  341. <Row gap={0.5}>
  342. {project ? <Avatar size={12} project={project} /> : null}
  343. {getShortEventId(replay.id)}
  344. </Row>
  345. </div>
  346. </Row>
  347. </Item>
  348. );
  349. }
  350. const subText = (
  351. <Cols>
  352. <Row gap={1}>
  353. <Row gap={0.5}>
  354. {/* Avatar is used instead of ProjectBadge because using ProjectBadge increases spacing, which doesn't look as good */}
  355. {project ? <Avatar size={12} project={project} /> : null}
  356. {project ? project.slug : null}
  357. <Link to={detailsTab} onClick={trackNavigationEvent}>
  358. {getShortEventId(replay.id)}
  359. </Link>
  360. </Row>
  361. <Row gap={0.5}>
  362. <IconCalendar color="gray300" size="xs" />
  363. <TimeSince date={replay.started_at} />
  364. </Row>
  365. </Row>
  366. </Cols>
  367. );
  368. return (
  369. <Item isWidget={isWidget}>
  370. <UserBadge
  371. avatarSize={24}
  372. displayName={
  373. replay.is_archived ? (
  374. replay.user.display_name || t('Anonymous User')
  375. ) : (
  376. <MainLink to={detailsTab} onClick={trackNavigationEvent}>
  377. {replay.user.display_name || t('Anonymous User')}
  378. </MainLink>
  379. )
  380. }
  381. user={getUserBadgeUser(replay)}
  382. // this is the subheading for the avatar, so displayEmail in this case is a misnomer
  383. displayEmail={subText}
  384. />
  385. </Item>
  386. );
  387. }
  388. const StyledIconDelete = styled(IconDelete)`
  389. margin: ${space(0.25)};
  390. `;
  391. const Cols = styled('div')`
  392. display: flex;
  393. flex-direction: column;
  394. gap: ${space(0.5)};
  395. width: 100%;
  396. `;
  397. const Row = styled('div')<{gap: ValidSize; minWidth?: number}>`
  398. display: flex;
  399. gap: ${p => space(p.gap)};
  400. align-items: center;
  401. ${p => (p.minWidth ? `min-width: ${p.minWidth}px;` : '')}
  402. `;
  403. const MainLink = styled(Link)`
  404. font-size: ${p => p.theme.fontSizeLarge};
  405. `;
  406. export function TransactionCell({
  407. organization,
  408. replay,
  409. }: Props & {organization: Organization}) {
  410. const location = useLocation();
  411. if (replay.is_archived) {
  412. return <Item isArchived />;
  413. }
  414. const hasTxEvent = 'txEvent' in replay;
  415. const txDuration = hasTxEvent ? replay.txEvent?.['transaction.duration'] : undefined;
  416. return hasTxEvent ? (
  417. <Item>
  418. <SpanOperationBreakdown>
  419. {txDuration ? <div>{txDuration}ms</div> : null}
  420. {spanOperationRelativeBreakdownRenderer(
  421. replay.txEvent,
  422. {organization, location},
  423. {enableOnClick: false}
  424. )}
  425. </SpanOperationBreakdown>
  426. </Item>
  427. ) : null;
  428. }
  429. export function OSCell({replay, showDropdownFilters}: Props) {
  430. const {name, version} = replay.os ?? {};
  431. const theme = useTheme();
  432. const hasRoomForColumns = useMedia(`(min-width: ${theme.breakpoints.large})`);
  433. if (replay.is_archived) {
  434. return <Item isArchived />;
  435. }
  436. return (
  437. <Item>
  438. <Container>
  439. <Tooltip title={`${name ?? ''} ${version ?? ''}`}>
  440. <ContextIcon
  441. name={name ?? ''}
  442. version={version && hasRoomForColumns ? version : undefined}
  443. showVersion={false}
  444. showTooltip={false}
  445. />
  446. {showDropdownFilters ? (
  447. <OSBrowserDropdownFilter type="os" name={name} version={version} />
  448. ) : null}
  449. </Tooltip>
  450. </Container>
  451. </Item>
  452. );
  453. }
  454. export function BrowserCell({replay, showDropdownFilters}: Props) {
  455. const {name, version} = replay.browser ?? {};
  456. const theme = useTheme();
  457. const hasRoomForColumns = useMedia(`(min-width: ${theme.breakpoints.large})`);
  458. if (replay.is_archived) {
  459. return <Item isArchived />;
  460. }
  461. return (
  462. <Item>
  463. <Container>
  464. <Tooltip title={`${name} ${version}`}>
  465. <ContextIcon
  466. name={name ?? ''}
  467. version={version && hasRoomForColumns ? version : undefined}
  468. showVersion={false}
  469. showTooltip={false}
  470. />
  471. {showDropdownFilters ? (
  472. <OSBrowserDropdownFilter type="browser" name={name} version={version} />
  473. ) : null}
  474. </Tooltip>
  475. </Container>
  476. </Item>
  477. );
  478. }
  479. export function DurationCell({replay, showDropdownFilters}: Props) {
  480. if (replay.is_archived) {
  481. return <Item isArchived />;
  482. }
  483. return (
  484. <Item>
  485. <Container>
  486. <Time>{formatTime(replay.duration.asMilliseconds())}</Time>
  487. {showDropdownFilters ? (
  488. <NumericDropdownFilter type="duration" val={replay.duration.asSeconds()} />
  489. ) : null}
  490. </Container>
  491. </Item>
  492. );
  493. }
  494. export function RageClickCountCell({replay, showDropdownFilters}: Props) {
  495. if (replay.is_archived) {
  496. return <Item isArchived />;
  497. }
  498. return (
  499. <Item data-test-id="replay-table-count-rage-clicks">
  500. <Container>
  501. {replay.count_rage_clicks ? (
  502. <RageClickCount>
  503. <IconCursorArrow size="sm" />
  504. {replay.count_rage_clicks}
  505. </RageClickCount>
  506. ) : (
  507. <Count>0</Count>
  508. )}
  509. {showDropdownFilters ? (
  510. <NumericDropdownFilter
  511. type="count_rage_clicks"
  512. val={replay.count_rage_clicks ?? 0}
  513. />
  514. ) : null}
  515. </Container>
  516. </Item>
  517. );
  518. }
  519. export function DeadClickCountCell({replay, showDropdownFilters}: Props) {
  520. if (replay.is_archived) {
  521. return <Item isArchived />;
  522. }
  523. return (
  524. <Item data-test-id="replay-table-count-dead-clicks">
  525. <Container>
  526. {replay.count_dead_clicks ? (
  527. <DeadClickCount>
  528. <IconCursorArrow size="sm" />
  529. {replay.count_dead_clicks}
  530. </DeadClickCount>
  531. ) : (
  532. <Count>0</Count>
  533. )}
  534. {showDropdownFilters ? (
  535. <NumericDropdownFilter
  536. type="count_dead_clicks"
  537. val={replay.count_dead_clicks ?? 0}
  538. />
  539. ) : null}
  540. </Container>
  541. </Item>
  542. );
  543. }
  544. export function ErrorCountCell({replay, showDropdownFilters}: Props) {
  545. if (replay.is_archived) {
  546. return <Item isArchived />;
  547. }
  548. return (
  549. <Item data-test-id="replay-table-count-errors">
  550. <Container>
  551. {replay.count_errors ? (
  552. <ErrorCount>
  553. <IconFire />
  554. {replay.count_errors}
  555. </ErrorCount>
  556. ) : (
  557. <Count>0</Count>
  558. )}
  559. {showDropdownFilters ? (
  560. <NumericDropdownFilter type="count_errors" val={replay.count_errors ?? 0} />
  561. ) : null}
  562. </Container>
  563. </Item>
  564. );
  565. }
  566. export function ActivityCell({replay, showDropdownFilters}: Props) {
  567. if (replay.is_archived) {
  568. return <Item isArchived />;
  569. }
  570. const scoreBarPalette = new Array(10).fill([CHART_PALETTE[0][0]]);
  571. return (
  572. <Item>
  573. <Container>
  574. <ScoreBar
  575. size={20}
  576. score={replay?.activity ?? 1}
  577. palette={scoreBarPalette}
  578. radius={0}
  579. />
  580. {showDropdownFilters ? (
  581. <NumericDropdownFilter
  582. type="activity"
  583. val={replay?.activity ?? 0}
  584. triggerOverlay
  585. />
  586. ) : null}
  587. </Container>
  588. </Item>
  589. );
  590. }
  591. const Item = styled('div')<{isArchived?: boolean; isWidget?: boolean}>`
  592. display: flex;
  593. align-items: center;
  594. gap: ${space(1)};
  595. ${p =>
  596. p.isWidget
  597. ? `padding: ${space(0.75)} ${space(1.5)} ${space(1.5)} ${space(1.5)};`
  598. : `padding: ${space(1.5)};`};
  599. ${p => (p.isArchived ? 'opacity: 0.5;' : '')};
  600. `;
  601. const Count = styled('span')`
  602. font-variant-numeric: tabular-nums;
  603. `;
  604. const DeadClickCount = styled(Count)`
  605. display: flex;
  606. width: 40px;
  607. gap: ${space(0.5)};
  608. color: ${p => p.theme.yellow300};
  609. `;
  610. const RageClickCount = styled(Count)`
  611. display: flex;
  612. width: 40px;
  613. gap: ${space(0.5)};
  614. color: ${p => p.theme.red300};
  615. `;
  616. const ErrorCount = styled(Count)`
  617. display: flex;
  618. align-items: center;
  619. gap: ${space(0.5)};
  620. color: ${p => p.theme.red400};
  621. `;
  622. const Time = styled('span')`
  623. font-variant-numeric: tabular-nums;
  624. `;
  625. const SpanOperationBreakdown = styled('div')`
  626. width: 100%;
  627. display: flex;
  628. flex-direction: column;
  629. gap: ${space(0.5)};
  630. color: ${p => p.theme.gray500};
  631. font-size: ${p => p.theme.fontSizeMedium};
  632. text-align: right;
  633. `;
  634. const Container = styled('div')`
  635. position: relative;
  636. display: flex;
  637. flex-direction: column;
  638. justify-content: center;
  639. `;
  640. const ActionMenuTrigger = styled(Button)`
  641. position: absolute;
  642. top: 50%;
  643. transform: translateY(-50%);
  644. padding: ${space(0.75)};
  645. left: -${space(0.75)};
  646. display: flex;
  647. align-items: center;
  648. opacity: 0;
  649. transition: opacity 0.1s;
  650. &.focus-visible,
  651. &[aria-expanded='true'],
  652. ${Container}:hover & {
  653. opacity: 1;
  654. }
  655. `;
  656. const NumericActionMenuTrigger = styled(ActionMenuTrigger)`
  657. left: 100%;
  658. margin-left: ${space(0.75)};
  659. z-index: 1;
  660. `;
  661. const OverlayActionMenuTrigger = styled(NumericActionMenuTrigger)`
  662. right: 0%;
  663. left: unset;
  664. `;