teamKeyTransactionField.tsx 2.9 KB

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