inviteBanner.tsx 11 KB

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