inviteBanner.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import {useCallback, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as qs from 'query-string';
  4. import {openInviteMissingMembersModal} from 'sentry/actionCreators/modal';
  5. import {promptsCheck, promptsUpdate} from 'sentry/actionCreators/prompts';
  6. import {Button} from 'sentry/components/button';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import Card from 'sentry/components/card';
  9. import Carousel from 'sentry/components/carousel';
  10. import {openConfirmModal} from 'sentry/components/confirm';
  11. import {DropdownMenu, MenuItemProps} from 'sentry/components/dropdownMenu';
  12. import ExternalLink from 'sentry/components/links/externalLink';
  13. import QuestionTooltip from 'sentry/components/questionTooltip';
  14. import {IconCommit, IconEllipsis, IconGithub, IconMail} from 'sentry/icons';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {MissingMember, Organization, OrgRole} from 'sentry/types';
  18. import {trackAnalytics} from 'sentry/utils/analytics';
  19. import {promptIsDismissed} from 'sentry/utils/promptIsDismissed';
  20. import useApi from 'sentry/utils/useApi';
  21. import {useLocation} from 'sentry/utils/useLocation';
  22. import withOrganization from 'sentry/utils/withOrganization';
  23. const MAX_MEMBERS_TO_SHOW = 5;
  24. type Props = {
  25. allowedRoles: OrgRole[];
  26. missingMembers: {integration: string; users: MissingMember[]};
  27. onModalClose: () => void;
  28. onSendInvite: (email: string) => void;
  29. organization: Organization;
  30. };
  31. export function InviteBanner({
  32. missingMembers,
  33. onSendInvite,
  34. organization,
  35. allowedRoles,
  36. onModalClose,
  37. }: Props) {
  38. // NOTE: this is currently used for Github only
  39. const isEligibleForBanner =
  40. organization.features.includes('integrations-gh-invite') &&
  41. organization.access.includes('org:write') &&
  42. organization.githubNudgeInvite &&
  43. missingMembers?.users?.length > 0;
  44. const [sendingInvite, setSendingInvite] = useState<boolean>(false);
  45. const [showBanner, setShowBanner] = useState<boolean>(false);
  46. const api = useApi();
  47. const integrationName = missingMembers?.integration;
  48. const promptsFeature = `${integrationName}_missing_members`;
  49. const location = useLocation();
  50. const snoozePrompt = useCallback(async () => {
  51. trackAnalytics('github_invite_banner.snoozed', {
  52. organization,
  53. });
  54. setShowBanner(false);
  55. await promptsUpdate(api, {
  56. organizationId: organization.id,
  57. feature: promptsFeature,
  58. status: 'snoozed',
  59. });
  60. }, [api, organization, promptsFeature]);
  61. const openInviteModal = useCallback(() => {
  62. openInviteMissingMembersModal({
  63. allowedRoles,
  64. missingMembers,
  65. organization,
  66. onClose: onModalClose,
  67. });
  68. }, [allowedRoles, missingMembers, organization, onModalClose]);
  69. useEffect(() => {
  70. if (!isEligibleForBanner) {
  71. return;
  72. }
  73. promptsCheck(api, {
  74. organizationId: organization.id,
  75. feature: promptsFeature,
  76. }).then(prompt => {
  77. setShowBanner(!promptIsDismissed(prompt));
  78. });
  79. }, [api, organization, promptsFeature, isEligibleForBanner]);
  80. useEffect(() => {
  81. const {inviteMissingMembers} = qs.parse(location.search);
  82. if (isEligibleForBanner && inviteMissingMembers) {
  83. openInviteModal();
  84. }
  85. }, [openInviteModal, location, isEligibleForBanner]);
  86. if (isEligibleForBanner && showBanner) {
  87. trackAnalytics('github_invite_banner.viewed', {
  88. organization,
  89. members_shown: missingMembers.users.slice(0, MAX_MEMBERS_TO_SHOW).length,
  90. });
  91. }
  92. if (!isEligibleForBanner || !showBanner) {
  93. return null;
  94. }
  95. // TODO(cathy): include docs link
  96. const menuItems: MenuItemProps[] = [
  97. {
  98. key: 'invite-banner-snooze',
  99. label: t('Hide Missing Members'),
  100. onAction: () => {
  101. openConfirmModal({
  102. message: t('Are you sure you want to snooze this banner?'),
  103. onConfirm: snoozePrompt,
  104. });
  105. },
  106. },
  107. ];
  108. const handleSendInvite = async (email: string) => {
  109. if (sendingInvite) {
  110. return;
  111. }
  112. setSendingInvite(true);
  113. await onSendInvite(email);
  114. setSendingInvite(false);
  115. };
  116. const users = missingMembers.users;
  117. const cards = users.slice(0, MAX_MEMBERS_TO_SHOW).map(member => {
  118. const username = member.externalId.split(':').pop();
  119. return (
  120. <MemberCard
  121. key={member.externalId}
  122. data-test-id={`member-card-${member.externalId}`}
  123. >
  124. <MemberCardContent>
  125. <MemberCardContentRow>
  126. <IconGithub size="sm" />
  127. {/* TODO(cathy): create mapping from integration to lambda external link function */}
  128. <StyledExternalLink href={`https://github.com/${username}`}>
  129. @{username}
  130. </StyledExternalLink>
  131. </MemberCardContentRow>
  132. <MemberCardContentRow>
  133. <IconCommit size="xs" />
  134. {tct('[commitCount] Recent Commits', {commitCount: member.commitCount})}
  135. </MemberCardContentRow>
  136. <MemberEmail>{member.email}</MemberEmail>
  137. </MemberCardContent>
  138. <Button
  139. size="sm"
  140. onClick={() => handleSendInvite(member.email)}
  141. data-test-id="invite-missing-member"
  142. icon={<IconMail />}
  143. analyticsEventName="Github Invite Banner: Invite"
  144. analyticsEventKey="github_invite_banner.invite"
  145. >
  146. {t('Invite')}
  147. </Button>
  148. </MemberCard>
  149. );
  150. });
  151. cards.push(
  152. <SeeMoreCard
  153. key="see-more"
  154. missingMembers={missingMembers}
  155. openInviteModal={openInviteModal}
  156. />
  157. );
  158. return (
  159. <StyledCard>
  160. <CardTitleContainer>
  161. <CardTitleContent>
  162. <CardTitle>{t('Bring your full GitHub team on board in Sentry')}</CardTitle>
  163. <Subtitle>
  164. {tct('[missingMemberCount] missing members', {
  165. missingMemberCount: users.length,
  166. })}
  167. <QuestionTooltip
  168. title={t(
  169. "Based on the last 30 days of GitHub commit data, there are team members committing code to Sentry projects that aren't in your Sentry organization"
  170. )}
  171. size="xs"
  172. />
  173. </Subtitle>
  174. </CardTitleContent>
  175. <ButtonBar gap={1}>
  176. <Button
  177. priority="primary"
  178. size="xs"
  179. onClick={openInviteModal}
  180. analyticsEventName="Github Invite Banner: View All"
  181. analyticsEventKey="github_invite_banner.view_all"
  182. >
  183. {t('View All')}
  184. </Button>
  185. <DropdownMenu
  186. items={menuItems}
  187. triggerProps={{
  188. size: 'xs',
  189. showChevron: false,
  190. icon: <IconEllipsis direction="down" size="sm" />,
  191. 'aria-label': t('Actions'),
  192. }}
  193. />
  194. </ButtonBar>
  195. </CardTitleContainer>
  196. <Carousel>{cards}</Carousel>
  197. </StyledCard>
  198. );
  199. }
  200. export default withOrganization(InviteBanner);
  201. type SeeMoreCardProps = {
  202. missingMembers: {integration: string; users: MissingMember[]};
  203. openInviteModal: () => void;
  204. };
  205. function SeeMoreCard({missingMembers, openInviteModal}: SeeMoreCardProps) {
  206. const {users} = missingMembers;
  207. return (
  208. <MemberCard data-test-id="see-more-card">
  209. <MemberCardContent>
  210. <MemberCardContentRow>
  211. <SeeMore>
  212. {tct('See all [missingMembersCount] missing members', {
  213. missingMembersCount: users.length,
  214. })}
  215. </SeeMore>
  216. </MemberCardContentRow>
  217. <Subtitle>
  218. {tct('Accounting for [totalCommits] recent commits', {
  219. totalCommits: users.reduce((acc, curr) => acc + curr.commitCount, 0),
  220. })}
  221. </Subtitle>
  222. </MemberCardContent>
  223. <Button
  224. size="sm"
  225. priority="primary"
  226. onClick={openInviteModal}
  227. analyticsEventName="Github Invite Banner: View All"
  228. analyticsEventKey="github_invite_banner.view_all"
  229. >
  230. {t('View All')}
  231. </Button>
  232. </MemberCard>
  233. );
  234. }
  235. const StyledCard = styled(Card)`
  236. display: flex;
  237. padding: ${space(2)};
  238. padding-bottom: ${space(1.5)};
  239. overflow: hidden;
  240. `;
  241. const CardTitleContainer = styled('div')`
  242. display: flex;
  243. justify-content: space-between;
  244. `;
  245. const CardTitleContent = styled('div')`
  246. display: flex;
  247. flex-direction: column;
  248. `;
  249. const CardTitle = styled('h6')`
  250. margin: 0;
  251. font-size: ${p => p.theme.fontSizeLarge};
  252. font-weight: bold;
  253. color: ${p => p.theme.gray400};
  254. `;
  255. const Subtitle = styled('div')`
  256. display: flex;
  257. align-items: center;
  258. font-size: ${p => p.theme.fontSizeSmall};
  259. font-weight: 400;
  260. color: ${p => p.theme.gray300};
  261. gap: ${space(0.5)};
  262. `;
  263. const MemberEmail = styled('div')`
  264. display: block;
  265. max-width: 70%;
  266. font-size: ${p => p.theme.fontSizeSmall};
  267. font-weight: 400;
  268. color: ${p => p.theme.gray300};
  269. text-overflow: ellipsis;
  270. overflow: hidden;
  271. `;
  272. const MemberCard = styled(Card)`
  273. display: flex;
  274. flex-direction: row;
  275. flex-wrap: wrap;
  276. min-width: 30%;
  277. margin: ${space(1)} ${space(0.5)} 0 0;
  278. padding: ${space(2)} 18px;
  279. justify-content: center;
  280. align-items: center;
  281. `;
  282. const MemberCardContent = styled('div')`
  283. display: flex;
  284. flex-direction: column;
  285. flex: 1 1;
  286. min-width: 50%;
  287. max-width: 75%;
  288. `;
  289. const MemberCardContentRow = styled('div')`
  290. display: flex;
  291. align-items: center;
  292. margin-bottom: ${space(0.25)};
  293. font-size: ${p => p.theme.fontSizeSmall};
  294. gap: ${space(0.75)};
  295. `;
  296. export const StyledExternalLink = styled(ExternalLink)`
  297. font-size: ${p => p.theme.fontSizeMedium};
  298. `;
  299. const SeeMore = styled('div')`
  300. font-size: ${p => p.theme.fontSizeLarge};
  301. `;