teamKeyTransactionButton.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import Button from 'app/components/button';
  4. import TeamKeyTransactionComponent, {
  5. TitleProps,
  6. } from 'app/components/performance/teamKeyTransaction';
  7. import * as TeamKeyTransactionManager from 'app/components/performance/teamKeyTransactionsManager';
  8. import {IconStar} from 'app/icons';
  9. import {t, tn} from 'app/locale';
  10. import {Organization, Project, Team} from 'app/types';
  11. import {defined} from 'app/utils';
  12. import EventView from 'app/utils/discover/eventView';
  13. import withProjects from 'app/utils/withProjects';
  14. import withTeams from 'app/utils/withTeams';
  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 {keyedTeamsCount, ...props} = this.props;
  22. return (
  23. <StyledButton
  24. {...props}
  25. icon={keyedTeamsCount ? <IconStar color="yellow300" isSolid /> : <IconStar />}
  26. >
  27. {keyedTeamsCount
  28. ? tn('Starred for Team', 'Starred for Teams', keyedTeamsCount)
  29. : t('Star for Team')}
  30. </StyledButton>
  31. );
  32. }
  33. }
  34. type BaseProps = {
  35. organization: Organization;
  36. transactionName: string;
  37. teams: Team[];
  38. };
  39. type Props = BaseProps &
  40. TeamKeyTransactionManager.TeamKeyTransactionManagerChildrenProps & {
  41. project: Project;
  42. };
  43. function TeamKeyTransactionButton({
  44. counts,
  45. getKeyedTeams,
  46. project,
  47. transactionName,
  48. ...props
  49. }: Props) {
  50. const keyedTeams = getKeyedTeams(project.id, transactionName);
  51. return (
  52. <TeamKeyTransactionComponent
  53. counts={counts}
  54. keyedTeams={keyedTeams}
  55. title={TitleButton}
  56. project={project}
  57. transactionName={transactionName}
  58. {...props}
  59. />
  60. );
  61. }
  62. type WrapperProps = BaseProps & {
  63. eventView: EventView;
  64. projects: Project[];
  65. };
  66. function TeamKeyTransactionButtonWrapper({
  67. eventView,
  68. organization,
  69. teams,
  70. projects,
  71. ...props
  72. }: WrapperProps) {
  73. if (eventView.project.length !== 1) {
  74. return <TitleButton disabled keyedTeamsCount={0} />;
  75. }
  76. const projectId = String(eventView.project[0]);
  77. const project = projects.find(proj => proj.id === projectId);
  78. if (!defined(project)) {
  79. return <TitleButton disabled keyedTeamsCount={0} />;
  80. }
  81. const userTeams = teams.filter(({isMember}) => isMember);
  82. return (
  83. <TeamKeyTransactionManager.Provider
  84. organization={organization}
  85. teams={userTeams}
  86. selectedTeams={['myteams']}
  87. selectedProjects={[String(projectId)]}
  88. >
  89. <TeamKeyTransactionManager.Consumer>
  90. {results => (
  91. <TeamKeyTransactionButton
  92. organization={organization}
  93. project={project}
  94. {...props}
  95. {...results}
  96. />
  97. )}
  98. </TeamKeyTransactionManager.Consumer>
  99. </TeamKeyTransactionManager.Provider>
  100. );
  101. }
  102. const StyledButton = styled(Button)`
  103. width: 180px;
  104. `;
  105. export default withTeams(withProjects(TeamKeyTransactionButtonWrapper));