tableCell.tsx 18 KB

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