inviteBanner.tsx 11 KB

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