inviteBanner.tsx 11 KB

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