tableCell.tsx 18 KB

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