teamKeyTransactionButton.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {Button} from 'sentry/components/button';
  2. import TeamKeyTransactionComponent from 'sentry/components/performance/teamKeyTransaction';
  3. import * as TeamKeyTransactionManager from 'sentry/components/performance/teamKeyTransactionsManager';
  4. import {Tooltip} from 'sentry/components/tooltip';
  5. import {IconStar} from 'sentry/icons';
  6. import {t, tn} from 'sentry/locale';
  7. import type {Organization} from 'sentry/types/organization';
  8. import type {Project} from 'sentry/types/project';
  9. import {defined} from 'sentry/utils';
  10. import type EventView from 'sentry/utils/discover/eventView';
  11. import {useTeams} from 'sentry/utils/useTeams';
  12. import withProjects from 'sentry/utils/withProjects';
  13. type BaseProps = {
  14. organization: Organization;
  15. transactionName: string;
  16. };
  17. type Props = BaseProps &
  18. TeamKeyTransactionManager.TeamKeyTransactionManagerChildrenProps & {
  19. project: Project;
  20. };
  21. function TeamKeyTransactionButton({
  22. counts,
  23. getKeyedTeams,
  24. project,
  25. transactionName,
  26. isLoading,
  27. error,
  28. ...props
  29. }: Props) {
  30. const keyedTeams = getKeyedTeams(project.id, transactionName);
  31. const keyedTeamsCount = keyedTeams?.size ?? 0;
  32. const disabled = isLoading || !!error;
  33. return (
  34. <TeamKeyTransactionComponent
  35. counts={counts}
  36. keyedTeams={keyedTeams}
  37. project={project}
  38. transactionName={transactionName}
  39. offset={8}
  40. size="md"
  41. trigger={(triggerProps, isOpen) => (
  42. <Tooltip
  43. disabled={disabled || isOpen}
  44. title={
  45. keyedTeams?.size
  46. ? project.teams
  47. .filter(team => keyedTeams.has(team.id))
  48. .map(({slug}) => slug)
  49. .join(', ')
  50. : null
  51. }
  52. >
  53. <Button
  54. {...triggerProps}
  55. disabled={disabled}
  56. size="sm"
  57. icon={
  58. <IconStar
  59. isSolid={!!keyedTeamsCount}
  60. color={keyedTeamsCount ? 'yellow300' : 'subText'}
  61. />
  62. }
  63. >
  64. {keyedTeamsCount
  65. ? tn('Starred for Team', 'Starred for Teams', keyedTeamsCount)
  66. : t('Star for Team')}
  67. </Button>
  68. </Tooltip>
  69. )}
  70. {...props}
  71. />
  72. );
  73. }
  74. type WrapperProps = BaseProps & {
  75. eventView: EventView;
  76. projects: Project[];
  77. };
  78. function TeamKeyTransactionButtonWrapper({
  79. eventView,
  80. organization,
  81. projects,
  82. ...props
  83. }: WrapperProps) {
  84. const {teams, initiallyLoaded} = useTeams({provideUserTeams: true});
  85. if (eventView.project.length !== 1) {
  86. return (
  87. <Button disabled size="sm" icon={<IconStar />}>
  88. {t('Star for Team')}
  89. </Button>
  90. );
  91. }
  92. const projectId = String(eventView.project[0]);
  93. const project = projects.find(proj => proj.id === projectId);
  94. if (!defined(project)) {
  95. return (
  96. <Button disabled size="sm" icon={<IconStar />}>
  97. {t('Star for Team')}
  98. </Button>
  99. );
  100. }
  101. return (
  102. <TeamKeyTransactionManager.Provider
  103. organization={organization}
  104. teams={teams}
  105. selectedTeams={['myteams']}
  106. selectedProjects={[String(projectId)]}
  107. >
  108. <TeamKeyTransactionManager.Consumer>
  109. {({isLoading, ...results}) => (
  110. <TeamKeyTransactionButton
  111. organization={organization}
  112. project={project}
  113. isLoading={isLoading || !initiallyLoaded}
  114. {...props}
  115. {...results}
  116. />
  117. )}
  118. </TeamKeyTransactionManager.Consumer>
  119. </TeamKeyTransactionManager.Provider>
  120. );
  121. }
  122. export default withProjects(TeamKeyTransactionButtonWrapper);