teamKeyTransactionButton.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. size="sm"
  27. icon={keyedTeamsCount ? <IconStar color="yellow400" isSolid /> : <IconStar />}
  28. >
  29. {keyedTeamsCount
  30. ? tn('Starred for Team', 'Starred for Teams', keyedTeamsCount)
  31. : t('Star for Team')}
  32. </Button>
  33. );
  34. if (!isOpen && keyedTeams?.length) {
  35. const teamSlugs = keyedTeams.map(({slug}) => slug).join(', ');
  36. return <Tooltip title={teamSlugs}>{button}</Tooltip>;
  37. }
  38. return button;
  39. }
  40. }
  41. type BaseProps = {
  42. organization: Organization;
  43. transactionName: string;
  44. };
  45. type Props = BaseProps &
  46. TeamKeyTransactionManager.TeamKeyTransactionManagerChildrenProps & {
  47. project: Project;
  48. };
  49. function TeamKeyTransactionButton({
  50. counts,
  51. getKeyedTeams,
  52. project,
  53. transactionName,
  54. ...props
  55. }: Props) {
  56. const keyedTeams = getKeyedTeams(project.id, transactionName);
  57. return (
  58. <TeamKeyTransactionComponent
  59. counts={counts}
  60. keyedTeams={keyedTeams}
  61. title={TitleButton}
  62. project={project}
  63. transactionName={transactionName}
  64. {...props}
  65. />
  66. );
  67. }
  68. type WrapperProps = BaseProps & {
  69. eventView: EventView;
  70. projects: Project[];
  71. };
  72. function TeamKeyTransactionButtonWrapper({
  73. eventView,
  74. organization,
  75. projects,
  76. ...props
  77. }: WrapperProps) {
  78. const {teams, initiallyLoaded} = useTeams({provideUserTeams: true});
  79. if (eventView.project.length !== 1) {
  80. return <TitleButton isOpen={false} disabled keyedTeams={null} />;
  81. }
  82. const projectId = String(eventView.project[0]);
  83. const project = projects.find(proj => proj.id === projectId);
  84. if (!defined(project)) {
  85. return <TitleButton isOpen={false} disabled keyedTeams={null} />;
  86. }
  87. return (
  88. <TeamKeyTransactionManager.Provider
  89. organization={organization}
  90. teams={teams}
  91. selectedTeams={['myteams']}
  92. selectedProjects={[String(projectId)]}
  93. >
  94. <TeamKeyTransactionManager.Consumer>
  95. {({isLoading, ...results}) => (
  96. <TeamKeyTransactionButton
  97. organization={organization}
  98. project={project}
  99. isLoading={isLoading || !initiallyLoaded}
  100. {...props}
  101. {...results}
  102. />
  103. )}
  104. </TeamKeyTransactionManager.Consumer>
  105. </TeamKeyTransactionManager.Provider>
  106. );
  107. }
  108. export default withProjects(TeamKeyTransactionButtonWrapper);