teamKeyTransactionButton.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {Component} from 'react';
  2. import Button from 'sentry/components/button';
  3. import TeamKeyTransactionComponent, {
  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, tn} from 'sentry/locale';
  10. import {Organization, Project} from 'sentry/types';
  11. import {defined} from 'sentry/utils';
  12. import EventView from 'sentry/utils/discover/eventView';
  13. import useTeams from 'sentry/utils/useTeams';
  14. import withProjects from 'sentry/utils/withProjects';
  15. /**
  16. * This can't be a function component because `TeamKeyTransaction` uses
  17. * `DropdownControl` which in turn uses passes a ref to this component.
  18. */
  19. class TitleButton extends Component<TitleProps> {
  20. render() {
  21. const {isOpen, keyedTeams, ...props} = this.props;
  22. const keyedTeamsCount = keyedTeams?.length ?? 0;
  23. const button = (
  24. <Button
  25. {...props}
  26. icon={keyedTeamsCount ? <IconStar color="yellow300" isSolid /> : <IconStar />}
  27. >
  28. {keyedTeamsCount
  29. ? tn('Starred for Team', 'Starred for Teams', keyedTeamsCount)
  30. : t('Star for Team')}
  31. </Button>
  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. organization: Organization;
  42. transactionName: string;
  43. };
  44. type Props = BaseProps &
  45. TeamKeyTransactionManager.TeamKeyTransactionManagerChildrenProps & {
  46. project: Project;
  47. };
  48. function TeamKeyTransactionButton({
  49. counts,
  50. getKeyedTeams,
  51. project,
  52. transactionName,
  53. ...props
  54. }: Props) {
  55. const keyedTeams = getKeyedTeams(project.id, transactionName);
  56. return (
  57. <TeamKeyTransactionComponent
  58. counts={counts}
  59. keyedTeams={keyedTeams}
  60. title={TitleButton}
  61. project={project}
  62. transactionName={transactionName}
  63. {...props}
  64. />
  65. );
  66. }
  67. type WrapperProps = BaseProps & {
  68. eventView: EventView;
  69. projects: Project[];
  70. };
  71. function TeamKeyTransactionButtonWrapper({
  72. eventView,
  73. organization,
  74. projects,
  75. ...props
  76. }: WrapperProps) {
  77. const {teams, initiallyLoaded} = useTeams({provideUserTeams: true});
  78. if (eventView.project.length !== 1) {
  79. return <TitleButton isOpen={false} disabled keyedTeams={null} />;
  80. }
  81. const projectId = String(eventView.project[0]);
  82. const project = projects.find(proj => proj.id === projectId);
  83. if (!defined(project)) {
  84. return <TitleButton isOpen={false} disabled keyedTeams={null} />;
  85. }
  86. return (
  87. <TeamKeyTransactionManager.Provider
  88. organization={organization}
  89. teams={teams}
  90. selectedTeams={['myteams']}
  91. selectedProjects={[String(projectId)]}
  92. >
  93. <TeamKeyTransactionManager.Consumer>
  94. {({isLoading, ...results}) => (
  95. <TeamKeyTransactionButton
  96. organization={organization}
  97. project={project}
  98. isLoading={isLoading || !initiallyLoaded}
  99. {...props}
  100. {...results}
  101. />
  102. )}
  103. </TeamKeyTransactionManager.Consumer>
  104. </TeamKeyTransactionManager.Provider>
  105. );
  106. }
  107. export default withProjects(TeamKeyTransactionButtonWrapper);