teamKeyTransactionField.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {Component} from 'react';
  2. import Button from 'sentry/components/button';
  3. import TeamKeyTransaction, {
  4. TitleProps,
  5. } from 'sentry/components/performance/teamKeyTransaction';
  6. import * as TeamKeyTransactionManager from 'sentry/components/performance/teamKeyTransactionsManager';
  7. import Tooltip from 'sentry/components/tooltip';
  8. import {IconStar} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import {Organization, Project} from 'sentry/types';
  11. import {defined} from 'sentry/utils';
  12. import withProjects from 'sentry/utils/withProjects';
  13. class TitleStar extends Component<TitleProps> {
  14. render() {
  15. const {isOpen, keyedTeams, initialValue, ...props} = this.props;
  16. const keyedTeamsCount = keyedTeams?.length ?? initialValue ?? 0;
  17. const star = (
  18. <IconStar
  19. color={keyedTeamsCount ? 'yellow300' : 'gray200'}
  20. isSolid={keyedTeamsCount > 0}
  21. data-test-id="team-key-transaction-column"
  22. />
  23. );
  24. const button = (
  25. <Button
  26. {...props}
  27. icon={star}
  28. borderless
  29. size="zero"
  30. aria-label={t('Toggle star for team')}
  31. />
  32. );
  33. if (!isOpen && keyedTeams?.length) {
  34. const teamSlugs = keyedTeams.map(({slug}) => slug).join(', ');
  35. return <Tooltip title={teamSlugs}>{button}</Tooltip>;
  36. }
  37. return button;
  38. }
  39. }
  40. type BaseProps = {
  41. isKeyTransaction: boolean;
  42. organization: Organization;
  43. };
  44. type Props = BaseProps &
  45. TeamKeyTransactionManager.TeamKeyTransactionManagerChildrenProps & {
  46. project: Project;
  47. transactionName: string;
  48. };
  49. function TeamKeyTransactionField({
  50. isKeyTransaction,
  51. counts,
  52. getKeyedTeams,
  53. project,
  54. transactionName,
  55. ...props
  56. }: Props) {
  57. const keyedTeams = getKeyedTeams(project.id, transactionName);
  58. return (
  59. <TeamKeyTransaction
  60. counts={counts}
  61. keyedTeams={keyedTeams}
  62. title={TitleStar}
  63. project={project}
  64. transactionName={transactionName}
  65. initialValue={Number(isKeyTransaction)}
  66. {...props}
  67. />
  68. );
  69. }
  70. type WrapperProps = BaseProps & {
  71. projectSlug: string | undefined;
  72. projects: Project[];
  73. transactionName: string | undefined;
  74. };
  75. function TeamKeyTransactionFieldWrapper({
  76. isKeyTransaction,
  77. projects,
  78. projectSlug,
  79. transactionName,
  80. ...props
  81. }: WrapperProps) {
  82. const project = projects.find(proj => proj.slug === projectSlug);
  83. // All these fields need to be defined in order to toggle a team key
  84. // transaction. Since they are not defined, just render a plain star
  85. // with no interactions.
  86. if (!defined(project) || !defined(transactionName)) {
  87. return (
  88. <TitleStar
  89. isOpen={false}
  90. disabled
  91. keyedTeams={null}
  92. initialValue={Number(isKeyTransaction)}
  93. />
  94. );
  95. }
  96. return (
  97. <TeamKeyTransactionManager.Consumer>
  98. {results => (
  99. <TeamKeyTransactionField
  100. isKeyTransaction={isKeyTransaction}
  101. project={project}
  102. transactionName={transactionName}
  103. {...props}
  104. {...results}
  105. />
  106. )}
  107. </TeamKeyTransactionManager.Consumer>
  108. );
  109. }
  110. export default withProjects(TeamKeyTransactionFieldWrapper);