teamKeyTransaction.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import {Component, Fragment} from 'react';
  2. import {createPortal} from 'react-dom';
  3. import {Manager, Popper, Reference} from 'react-popper';
  4. import styled from '@emotion/styled';
  5. import MenuHeader from 'sentry/components/actions/menuHeader';
  6. import CheckboxFancy from 'sentry/components/checkboxFancy/checkboxFancy';
  7. import {GetActorPropsFn} from 'sentry/components/deprecatedDropdownMenu';
  8. import MenuItem from 'sentry/components/menuItem';
  9. import {TeamSelection} from 'sentry/components/performance/teamKeyTransactionsManager';
  10. import {t} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {Organization, Project, Team} from 'sentry/types';
  13. import {defined} from 'sentry/utils';
  14. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  15. import {MAX_TEAM_KEY_TRANSACTIONS} from 'sentry/utils/performance/constants';
  16. export type TitleProps = Partial<ReturnType<GetActorPropsFn>> & {
  17. isOpen: boolean;
  18. keyedTeams: Team[] | null;
  19. disabled?: boolean;
  20. initialValue?: number;
  21. };
  22. type Props = {
  23. counts: Map<string, number> | null;
  24. error: string | null;
  25. handleToggleKeyTransaction: (selection: TeamSelection) => void;
  26. isLoading: boolean;
  27. keyedTeams: Set<string> | null;
  28. organization: Organization;
  29. project: Project;
  30. teams: Team[];
  31. title: React.ComponentType<TitleProps>;
  32. transactionName: string;
  33. initialValue?: number;
  34. };
  35. type State = {
  36. isOpen: boolean;
  37. };
  38. class TeamKeyTransaction extends Component<Props, State> {
  39. state: State = {
  40. isOpen: false,
  41. };
  42. componentDidUpdate(_props: Props, prevState: State) {
  43. if (this.state.isOpen && prevState.isOpen === false) {
  44. document.addEventListener('click', this.handleClickOutside, true);
  45. }
  46. if (this.state.isOpen === false && prevState.isOpen) {
  47. document.removeEventListener('click', this.handleClickOutside, true);
  48. }
  49. }
  50. componentWillUnmount() {
  51. document.removeEventListener('click', this.handleClickOutside, true);
  52. }
  53. private menuEl: Element | null = null;
  54. handleClickOutside = (event: MouseEvent) => {
  55. if (!this.menuEl) {
  56. return;
  57. }
  58. if (!(event.target instanceof Element)) {
  59. return;
  60. }
  61. if (this.menuEl.contains(event.target)) {
  62. return;
  63. }
  64. this.setState({isOpen: false});
  65. };
  66. toggleOpen = () => {
  67. this.setState(({isOpen}) => ({isOpen: !isOpen}));
  68. };
  69. toggleSelection = (enabled: boolean, selection: TeamSelection) => () => {
  70. const {handleToggleKeyTransaction, organization} = this.props;
  71. const {action} = selection;
  72. trackAdvancedAnalyticsEvent('performance_views.team_key_transaction.set', {
  73. organization,
  74. action,
  75. });
  76. return enabled ? handleToggleKeyTransaction(selection) : undefined;
  77. };
  78. partitionTeams(counts: Map<string, number>, keyedTeams: Set<string>) {
  79. const {teams, project} = this.props;
  80. const enabledTeams: Team[] = [];
  81. const disabledTeams: Team[] = [];
  82. const noAccessTeams: Team[] = [];
  83. const projectTeams = new Set(project.teams.map(({id}) => id));
  84. for (const team of teams) {
  85. if (!projectTeams.has(team.id)) {
  86. noAccessTeams.push(team);
  87. } else if (
  88. keyedTeams.has(team.id) ||
  89. (counts.get(team.id) ?? 0) < MAX_TEAM_KEY_TRANSACTIONS
  90. ) {
  91. enabledTeams.push(team);
  92. } else {
  93. disabledTeams.push(team);
  94. }
  95. }
  96. return {
  97. enabledTeams,
  98. disabledTeams,
  99. noAccessTeams,
  100. };
  101. }
  102. renderMenuContent(counts: Map<string, number>, keyedTeams: Set<string>) {
  103. const {teams, project, transactionName} = this.props;
  104. const {enabledTeams, disabledTeams, noAccessTeams} = this.partitionTeams(
  105. counts,
  106. keyedTeams
  107. );
  108. const isMyTeamsEnabled = enabledTeams.length > 0;
  109. const myTeamsHandler = this.toggleSelection(isMyTeamsEnabled, {
  110. action: enabledTeams.length === keyedTeams.size ? 'unkey' : 'key',
  111. teamIds: enabledTeams.map(({id}) => id),
  112. project,
  113. transactionName,
  114. });
  115. const hasTeamsWithAccess = enabledTeams.length + disabledTeams.length > 0;
  116. return (
  117. <DropdownContent>
  118. {hasTeamsWithAccess && (
  119. <Fragment>
  120. <DropdownMenuHeader first>
  121. {t('My Teams with Access')}
  122. <ActionItem>
  123. <CheckboxFancy
  124. isDisabled={!isMyTeamsEnabled}
  125. isChecked={teams.length === keyedTeams.size}
  126. isIndeterminate={teams.length > keyedTeams.size && keyedTeams.size > 0}
  127. onClick={myTeamsHandler}
  128. />
  129. </ActionItem>
  130. </DropdownMenuHeader>
  131. {enabledTeams.map(team => (
  132. <TeamKeyTransactionItem
  133. key={team.slug}
  134. team={team}
  135. isKeyed={keyedTeams.has(team.id)}
  136. disabled={false}
  137. onSelect={this.toggleSelection(true, {
  138. action: keyedTeams.has(team.id) ? 'unkey' : 'key',
  139. teamIds: [team.id],
  140. project,
  141. transactionName,
  142. })}
  143. />
  144. ))}
  145. {disabledTeams.map(team => (
  146. <TeamKeyTransactionItem
  147. key={team.slug}
  148. team={team}
  149. isKeyed={keyedTeams.has(team.id)}
  150. disabled
  151. onSelect={this.toggleSelection(true, {
  152. action: keyedTeams.has(team.id) ? 'unkey' : 'key',
  153. teamIds: [team.id],
  154. project,
  155. transactionName,
  156. })}
  157. />
  158. ))}
  159. </Fragment>
  160. )}
  161. {noAccessTeams.length > 0 && (
  162. <Fragment>
  163. <DropdownMenuHeader first={!hasTeamsWithAccess}>
  164. {t('My Teams without Access')}
  165. </DropdownMenuHeader>
  166. {noAccessTeams.map(team => (
  167. <TeamKeyTransactionItem key={team.slug} team={team} disabled />
  168. ))}
  169. </Fragment>
  170. )}
  171. </DropdownContent>
  172. );
  173. }
  174. renderMenu(): React.ReactPortal | null {
  175. const {isLoading, counts, keyedTeams} = this.props;
  176. if (isLoading || !defined(counts) || !defined(keyedTeams)) {
  177. return null;
  178. }
  179. const modifiers = [
  180. {
  181. name: 'hide',
  182. enabled: false,
  183. },
  184. {
  185. name: 'preventOverflow',
  186. enabled: true,
  187. options: {padding: 10},
  188. },
  189. ];
  190. return createPortal(
  191. <Popper placement="top" modifiers={modifiers}>
  192. {({ref: popperRef, style, placement}) => (
  193. <DropdownWrapper
  194. ref={ref => {
  195. (popperRef as Function)(ref);
  196. this.menuEl = ref;
  197. }}
  198. style={style}
  199. data-placement={placement}
  200. >
  201. {this.renderMenuContent(counts, keyedTeams)}
  202. </DropdownWrapper>
  203. )}
  204. </Popper>,
  205. document.body
  206. );
  207. }
  208. render() {
  209. const {isLoading, error, title: Title, keyedTeams, initialValue, teams} = this.props;
  210. const {isOpen} = this.state;
  211. const menu: React.ReactPortal | null = isOpen ? this.renderMenu() : null;
  212. return (
  213. <Manager>
  214. <Reference>
  215. {({ref}) => (
  216. <StarWrapper ref={ref}>
  217. <Title
  218. isOpen={isOpen}
  219. disabled={isLoading || Boolean(error)}
  220. keyedTeams={
  221. keyedTeams ? teams.filter(({id}) => keyedTeams.has(id)) : null
  222. }
  223. initialValue={initialValue}
  224. onClick={this.toggleOpen}
  225. />
  226. </StarWrapper>
  227. )}
  228. </Reference>
  229. {menu}
  230. </Manager>
  231. );
  232. }
  233. }
  234. type ItemProps = {
  235. disabled: boolean;
  236. team: Team;
  237. isKeyed?: boolean;
  238. onSelect?: () => void;
  239. };
  240. function TeamKeyTransactionItem({team, isKeyed, disabled, onSelect}: ItemProps) {
  241. return (
  242. <DropdownMenuItem
  243. key={team.slug}
  244. disabled={disabled}
  245. onSelect={onSelect}
  246. stopPropagation
  247. >
  248. <MenuItemContent>
  249. {team.slug}
  250. <ActionItem>
  251. {!defined(isKeyed) ? null : disabled ? (
  252. t('Max %s', MAX_TEAM_KEY_TRANSACTIONS)
  253. ) : (
  254. <CheckboxFancy isChecked={isKeyed} />
  255. )}
  256. </ActionItem>
  257. </MenuItemContent>
  258. </DropdownMenuItem>
  259. );
  260. }
  261. const StarWrapper = styled('div')`
  262. display: flex;
  263. /* Fixes Star when it’s filled and is wrapped around Tooltip */
  264. & > span {
  265. display: flex;
  266. }
  267. `;
  268. const DropdownWrapper = styled('div')`
  269. /* Adapted from the dropdown-menu class */
  270. border: none;
  271. border-radius: 2px;
  272. box-shadow: 0 0 0 1px rgba(52, 60, 69, 0.2), 0 1px 3px rgba(70, 82, 98, 0.25);
  273. background-clip: padding-box;
  274. background-color: ${p => p.theme.background};
  275. width: 220px;
  276. overflow: visible;
  277. z-index: ${p => p.theme.zIndex.tooltip};
  278. &:before,
  279. &:after {
  280. width: 0;
  281. height: 0;
  282. content: '';
  283. display: block;
  284. position: absolute;
  285. right: auto;
  286. }
  287. &:before {
  288. border-left: 9px solid transparent;
  289. border-right: 9px solid transparent;
  290. left: calc(50% - 9px);
  291. z-index: -2;
  292. }
  293. &:after {
  294. border-left: 8px solid transparent;
  295. border-right: 8px solid transparent;
  296. left: calc(50% - 8px);
  297. z-index: -1;
  298. }
  299. &[data-placement*='bottom'] {
  300. margin-top: 9px;
  301. &:before {
  302. border-bottom: 9px solid ${p => p.theme.border};
  303. top: -9px;
  304. }
  305. &:after {
  306. border-bottom: 8px solid ${p => p.theme.background};
  307. top: -8px;
  308. }
  309. }
  310. &[data-placement*='top'] {
  311. margin-bottom: 9px;
  312. &:before {
  313. border-top: 9px solid ${p => p.theme.border};
  314. bottom: -9px;
  315. }
  316. &:after {
  317. border-top: 8px solid ${p => p.theme.background};
  318. bottom: -8px;
  319. }
  320. }
  321. `;
  322. const DropdownContent = styled('div')`
  323. max-height: 250px;
  324. pointer-events: auto;
  325. overflow-y: auto;
  326. `;
  327. const DropdownMenuHeader = styled(MenuHeader)<{first?: boolean}>`
  328. display: flex;
  329. flex-direction: row;
  330. justify-content: space-between;
  331. align-items: center;
  332. padding: ${space(1)} ${space(2)};
  333. background: ${p => p.theme.backgroundSecondary};
  334. ${p => p.first && 'border-radius: 2px'};
  335. `;
  336. const DropdownMenuItem = styled(MenuItem)`
  337. font-size: ${p => p.theme.fontSizeMedium};
  338. &:not(:last-child) {
  339. border-bottom: 1px solid ${p => p.theme.innerBorder};
  340. }
  341. `;
  342. const MenuItemContent = styled('div')`
  343. display: flex;
  344. flex-direction: row;
  345. justify-content: space-between;
  346. align-items: center;
  347. width: 100%;
  348. `;
  349. const ActionItem = styled('span')`
  350. min-width: ${space(2)};
  351. margin-left: ${space(1)};
  352. `;
  353. export default TeamKeyTransaction;