tableCell.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. import {useTheme} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import Avatar from 'sentry/components/avatar';
  5. import UserAvatar from 'sentry/components/avatar/userAvatar';
  6. import {Button} from 'sentry/components/button';
  7. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  8. import Duration from 'sentry/components/duration/duration';
  9. import Link from 'sentry/components/links/link';
  10. import PlatformIcon from 'sentry/components/replays/platformIcon';
  11. import ReplayPlayPauseButton from 'sentry/components/replays/replayPlayPauseButton';
  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. IconPlay,
  23. } from 'sentry/icons';
  24. import {t, tct} from 'sentry/locale';
  25. import type {ValidSize} from 'sentry/styles/space';
  26. import {space} from 'sentry/styles/space';
  27. import type {Organization} from 'sentry/types/organization';
  28. import {trackAnalytics} from 'sentry/utils/analytics';
  29. import {browserHistory} from 'sentry/utils/browserHistory';
  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 normalizeUrl from 'sentry/utils/url/normalizeUrl';
  36. import {useLocation} from 'sentry/utils/useLocation';
  37. import useMedia from 'sentry/utils/useMedia';
  38. import useProjects from 'sentry/utils/useProjects';
  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. const DEFAULT_NUMERIC_DROPDOWN_FORMATTER = (val: number) => val.toString();
  173. function NumericDropdownFilter({
  174. type,
  175. val,
  176. triggerOverlay,
  177. formatter = DEFAULT_NUMERIC_DROPDOWN_FORMATTER,
  178. }: {
  179. type: string;
  180. val: number;
  181. formatter?: (val: number) => string;
  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: formatter(val),
  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: '>' + formatter(val),
  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: '<' + formatter(val),
  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: formatter(val),
  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. className,
  284. }: Props & {
  285. eventView: EventView;
  286. organization: Organization;
  287. referrer: string;
  288. className?: string;
  289. isWidget?: boolean;
  290. referrer_table?: ReferrerTableType;
  291. }) {
  292. const {projects} = useProjects();
  293. const project = projects.find(p => p.id === replay.project_id);
  294. const replayDetails = {
  295. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${replay.id}/`),
  296. query: {
  297. referrer,
  298. ...eventView.generateQueryStringObject(),
  299. },
  300. };
  301. const replayDetailsDeadRage = {
  302. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${replay.id}/`),
  303. query: {
  304. referrer,
  305. ...eventView.generateQueryStringObject(),
  306. f_b_type: 'rageOrDead',
  307. },
  308. };
  309. const detailsTab = () => {
  310. switch (referrer_table) {
  311. case 'selector-widget':
  312. return replayDetailsDeadRage;
  313. default:
  314. return replayDetails;
  315. }
  316. };
  317. const trackNavigationEvent = () =>
  318. trackAnalytics('replay.list-navigate-to-details', {
  319. project_id: project?.id,
  320. platform: project?.platform,
  321. organization,
  322. referrer,
  323. referrer_table,
  324. });
  325. if (replay.is_archived) {
  326. return (
  327. <Item isArchived={replay.is_archived} isReplayCell>
  328. <Row gap={1}>
  329. <StyledIconDelete color="gray500" size="md" />
  330. <div>
  331. <Row gap={0.5}>{t('Deleted Replay')}</Row>
  332. <Row gap={0.5}>
  333. {project ? <Avatar size={12} project={project} /> : null}
  334. <ArchivedId>{getShortEventId(replay.id)}</ArchivedId>
  335. </Row>
  336. </div>
  337. </Row>
  338. </Item>
  339. );
  340. }
  341. const subText = (
  342. <Cols>
  343. <Row gap={1}>
  344. <Row gap={0.5}>
  345. {/* Avatar is used instead of ProjectBadge because using ProjectBadge increases spacing, which doesn't look as good */}
  346. {project ? <Avatar size={12} project={project} /> : null}
  347. {project ? project.slug : null}
  348. <Link to={detailsTab()} onClick={trackNavigationEvent}>
  349. {getShortEventId(replay.id)}
  350. </Link>
  351. <Row gap={0.5}>
  352. <IconCalendar color="gray300" size="xs" />
  353. <TimeSince date={replay.started_at} />
  354. </Row>
  355. </Row>
  356. </Row>
  357. </Cols>
  358. );
  359. return (
  360. <Item isWidget={isWidget} isReplayCell className={className}>
  361. <Row gap={1}>
  362. <UserAvatar user={getUserBadgeUser(replay)} size={24} />
  363. <SubText>
  364. <Row gap={0.5}>
  365. {replay.is_archived ? (
  366. replay.user.display_name || t('Anonymous User')
  367. ) : (
  368. <MainLink
  369. to={detailsTab()}
  370. onClick={trackNavigationEvent}
  371. data-has-viewed={replay.has_viewed}
  372. >
  373. {replay.user.display_name || t('Anonymous User')}
  374. </MainLink>
  375. )}
  376. </Row>
  377. <Row gap={0.5}>{subText}</Row>
  378. </SubText>
  379. </Row>
  380. </Item>
  381. );
  382. }
  383. const ArchivedId = styled('div')`
  384. font-size: ${p => p.theme.fontSizeSmall};
  385. `;
  386. const StyledIconDelete = styled(IconDelete)`
  387. margin: ${space(0.25)};
  388. `;
  389. const Cols = styled('div')`
  390. display: flex;
  391. flex-direction: column;
  392. gap: ${space(0.5)};
  393. width: 100%;
  394. `;
  395. const Row = styled('div')<{gap: ValidSize; minWidth?: number}>`
  396. display: flex;
  397. gap: ${p => space(p.gap)};
  398. align-items: center;
  399. ${p => (p.minWidth ? `min-width: ${p.minWidth}px;` : '')}
  400. `;
  401. const MainLink = styled(Link)`
  402. font-size: ${p => p.theme.fontSizeLarge};
  403. line-height: normal;
  404. ${p => p.theme.overflowEllipsis};
  405. font-weight: ${p => p.theme.fontWeightBold};
  406. &[data-has-viewed='true'] {
  407. font-weight: ${p => p.theme.fontWeightNormal};
  408. }
  409. `;
  410. const SubText = styled('div')`
  411. font-size: 0.875em;
  412. line-height: normal;
  413. color: ${p => p.theme.gray300};
  414. ${p => p.theme.overflowEllipsis};
  415. display: flex;
  416. flex-direction: column;
  417. gap: ${space(0.25)};
  418. `;
  419. export function TransactionCell({
  420. organization,
  421. replay,
  422. }: Props & {organization: Organization}) {
  423. const location = useLocation();
  424. if (replay.is_archived) {
  425. return <Item isArchived />;
  426. }
  427. const hasTxEvent = 'txEvent' in replay;
  428. const txDuration = hasTxEvent ? replay.txEvent?.['transaction.duration'] : undefined;
  429. return hasTxEvent ? (
  430. <Item>
  431. <SpanOperationBreakdown>
  432. {txDuration ? <div>{txDuration}ms</div> : null}
  433. {spanOperationRelativeBreakdownRenderer(
  434. replay.txEvent,
  435. {organization, location},
  436. {enableOnClick: false}
  437. )}
  438. </SpanOperationBreakdown>
  439. </Item>
  440. ) : null;
  441. }
  442. export function OSCell({replay, showDropdownFilters}: Props) {
  443. const {name, version} = replay.os ?? {};
  444. const theme = useTheme();
  445. const hasRoomForColumns = useMedia(`(min-width: ${theme.breakpoints.large})`);
  446. if (replay.is_archived) {
  447. return <Item isArchived />;
  448. }
  449. return (
  450. <Item>
  451. <Container>
  452. <Tooltip title={`${name ?? ''} ${version ?? ''}`}>
  453. <PlatformIcon
  454. name={name ?? ''}
  455. version={version && hasRoomForColumns ? version : undefined}
  456. showVersion={false}
  457. showTooltip={false}
  458. />
  459. {showDropdownFilters ? (
  460. <OSBrowserDropdownFilter type="os" name={name} version={version} />
  461. ) : null}
  462. </Tooltip>
  463. </Container>
  464. </Item>
  465. );
  466. }
  467. export function BrowserCell({replay, showDropdownFilters}: Props) {
  468. const {name, version} = replay.browser ?? {};
  469. const theme = useTheme();
  470. const hasRoomForColumns = useMedia(`(min-width: ${theme.breakpoints.large})`);
  471. if (replay.is_archived) {
  472. return <Item isArchived />;
  473. }
  474. return (
  475. <Item>
  476. <Container>
  477. <Tooltip title={`${name} ${version}`}>
  478. <PlatformIcon
  479. name={name ?? ''}
  480. version={version && hasRoomForColumns ? version : undefined}
  481. showVersion={false}
  482. showTooltip={false}
  483. />
  484. {showDropdownFilters ? (
  485. <OSBrowserDropdownFilter type="browser" name={name} version={version} />
  486. ) : null}
  487. </Tooltip>
  488. </Container>
  489. </Item>
  490. );
  491. }
  492. export function DurationCell({replay, showDropdownFilters}: Props) {
  493. if (replay.is_archived) {
  494. return <Item isArchived />;
  495. }
  496. return (
  497. <Item>
  498. <Container>
  499. <Duration duration={[replay.duration.asMilliseconds(), 'ms']} precision="sec" />
  500. {showDropdownFilters ? (
  501. <NumericDropdownFilter
  502. type="duration"
  503. val={replay.duration.asSeconds()}
  504. formatter={(val: number) => `${val}s`}
  505. />
  506. ) : null}
  507. </Container>
  508. </Item>
  509. );
  510. }
  511. export function RageClickCountCell({replay, showDropdownFilters}: Props) {
  512. if (replay.is_archived) {
  513. return <Item isArchived />;
  514. }
  515. return (
  516. <Item data-test-id="replay-table-count-rage-clicks">
  517. <Container>
  518. {replay.count_rage_clicks ? (
  519. <RageClickCount>
  520. <IconCursorArrow size="sm" color="red300" />
  521. {replay.count_rage_clicks}
  522. </RageClickCount>
  523. ) : (
  524. <Count>0</Count>
  525. )}
  526. {showDropdownFilters ? (
  527. <NumericDropdownFilter
  528. type="count_rage_clicks"
  529. val={replay.count_rage_clicks ?? 0}
  530. />
  531. ) : null}
  532. </Container>
  533. </Item>
  534. );
  535. }
  536. export function DeadClickCountCell({replay, showDropdownFilters}: Props) {
  537. if (replay.is_archived) {
  538. return <Item isArchived />;
  539. }
  540. return (
  541. <Item data-test-id="replay-table-count-dead-clicks">
  542. <Container>
  543. {replay.count_dead_clicks ? (
  544. <DeadClickCount>
  545. <IconCursorArrow size="sm" color="yellow300" />
  546. {replay.count_dead_clicks}
  547. </DeadClickCount>
  548. ) : (
  549. <Count>0</Count>
  550. )}
  551. {showDropdownFilters ? (
  552. <NumericDropdownFilter
  553. type="count_dead_clicks"
  554. val={replay.count_dead_clicks ?? 0}
  555. />
  556. ) : null}
  557. </Container>
  558. </Item>
  559. );
  560. }
  561. export function ErrorCountCell({replay, showDropdownFilters}: Props) {
  562. if (replay.is_archived) {
  563. return <Item isArchived />;
  564. }
  565. return (
  566. <Item data-test-id="replay-table-count-errors">
  567. <Container>
  568. {replay.count_errors ? (
  569. <ErrorCount>
  570. <IconFire color="red300" />
  571. {replay.count_errors}
  572. </ErrorCount>
  573. ) : (
  574. <Count>0</Count>
  575. )}
  576. {showDropdownFilters ? (
  577. <NumericDropdownFilter type="count_errors" val={replay.count_errors ?? 0} />
  578. ) : null}
  579. </Container>
  580. </Item>
  581. );
  582. }
  583. export function ActivityCell({replay, showDropdownFilters}: Props) {
  584. if (replay.is_archived) {
  585. return <Item isArchived />;
  586. }
  587. const scoreBarPalette = new Array(10).fill([CHART_PALETTE[0][0]]);
  588. return (
  589. <Item>
  590. <Container>
  591. <ScoreBar
  592. size={20}
  593. score={replay?.activity ?? 1}
  594. palette={scoreBarPalette}
  595. radius={0}
  596. />
  597. {showDropdownFilters ? (
  598. <NumericDropdownFilter
  599. type="activity"
  600. val={replay?.activity ?? 0}
  601. triggerOverlay
  602. />
  603. ) : null}
  604. </Container>
  605. </Item>
  606. );
  607. }
  608. export function PlayPauseCell({
  609. isSelected,
  610. handleClick,
  611. }: {
  612. handleClick: () => void;
  613. isSelected: boolean;
  614. }) {
  615. const inner = isSelected ? (
  616. <ReplayPlayPauseButton size="sm" priority="default" borderless />
  617. ) : (
  618. <Button
  619. title={t('Play')}
  620. aria-label={t('Play')}
  621. icon={<IconPlay size="sm" />}
  622. onClick={handleClick}
  623. data-test-id="replay-table-play-button"
  624. borderless
  625. size="sm"
  626. priority="default"
  627. />
  628. );
  629. return <Item>{inner}</Item>;
  630. }
  631. const Item = styled('div')<{
  632. isArchived?: boolean;
  633. isReplayCell?: boolean;
  634. isWidget?: boolean;
  635. }>`
  636. display: flex;
  637. align-items: center;
  638. gap: ${space(1)};
  639. ${p =>
  640. p.isWidget
  641. ? `padding: ${space(0.75)} ${space(1.5)} ${space(1.5)} ${space(1.5)};`
  642. : `padding: ${space(1.5)};`};
  643. ${p => (p.isArchived ? 'opacity: 0.5;' : '')};
  644. ${p => (p.isReplayCell ? 'overflow: auto;' : '')};
  645. `;
  646. const Count = styled('span')`
  647. font-variant-numeric: tabular-nums;
  648. `;
  649. const DeadClickCount = styled(Count)`
  650. display: flex;
  651. width: 40px;
  652. gap: ${space(0.5)};
  653. `;
  654. const RageClickCount = styled(Count)`
  655. display: flex;
  656. width: 40px;
  657. gap: ${space(0.5)};
  658. `;
  659. const ErrorCount = styled(Count)`
  660. display: flex;
  661. align-items: center;
  662. gap: ${space(0.5)};
  663. `;
  664. const SpanOperationBreakdown = styled('div')`
  665. width: 100%;
  666. display: flex;
  667. flex-direction: column;
  668. gap: ${space(0.5)};
  669. color: ${p => p.theme.gray500};
  670. font-size: ${p => p.theme.fontSizeMedium};
  671. text-align: right;
  672. `;
  673. const Container = styled('div')`
  674. position: relative;
  675. display: flex;
  676. flex-direction: column;
  677. justify-content: center;
  678. `;
  679. const ActionMenuTrigger = styled(Button)`
  680. position: absolute;
  681. top: 50%;
  682. transform: translateY(-50%);
  683. padding: ${space(0.75)};
  684. left: -${space(0.75)};
  685. display: flex;
  686. align-items: center;
  687. opacity: 0;
  688. transition: opacity 0.1s;
  689. &:focus-visible,
  690. &[aria-expanded='true'],
  691. ${Container}:hover & {
  692. opacity: 1;
  693. }
  694. `;
  695. const NumericActionMenuTrigger = styled(ActionMenuTrigger)`
  696. left: 100%;
  697. margin-left: ${space(0.75)};
  698. z-index: 1;
  699. `;
  700. const OverlayActionMenuTrigger = styled(NumericActionMenuTrigger)`
  701. right: 0%;
  702. left: unset;
  703. `;