tableCell.tsx 19 KB

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