inviteBanner.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.access.includes('org:write') && 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. organization,
  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. organization,
  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. <Fragment>
  154. {/* this is temporary to collect feedback about the banner */}
  155. <FloatingFeedbackWidget />
  156. <StyledCard>
  157. <CardTitleContainer>
  158. <CardTitleContent>
  159. <CardTitle>{t('Bring your full GitHub team on board in Sentry')}</CardTitle>
  160. <Subtitle>
  161. {tct('[missingMemberCount] missing members', {
  162. missingMemberCount: missingMembers.length,
  163. })}
  164. <QuestionTooltip
  165. title={t(
  166. "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"
  167. )}
  168. size="xs"
  169. />
  170. </Subtitle>
  171. </CardTitleContent>
  172. <ButtonBar gap={1}>
  173. <Button
  174. priority="primary"
  175. size="xs"
  176. onClick={openInviteModal}
  177. analyticsEventName="Github Invite Banner: View All"
  178. analyticsEventKey="github_invite_banner.view_all"
  179. >
  180. {t('View All')}
  181. </Button>
  182. <DropdownMenu
  183. items={menuItems}
  184. triggerProps={{
  185. size: 'xs',
  186. showChevron: false,
  187. icon: <IconEllipsis direction="down" size="sm" />,
  188. 'aria-label': t('Actions'),
  189. }}
  190. />
  191. </ButtonBar>
  192. </CardTitleContainer>
  193. <Carousel>
  194. <MemberCards
  195. missingMembers={missingMembers}
  196. handleSendInvite={handleSendInvite}
  197. openInviteModal={openInviteModal}
  198. />
  199. </Carousel>
  200. </StyledCard>
  201. </Fragment>
  202. );
  203. }
  204. export default withOrganization(InviteBanner);
  205. type MemberCardsProps = {
  206. handleSendInvite: (email: string) => void;
  207. missingMembers: MissingMember[];
  208. openInviteModal: () => void;
  209. };
  210. function MemberCards({
  211. missingMembers,
  212. handleSendInvite,
  213. openInviteModal,
  214. }: MemberCardsProps) {
  215. return (
  216. <Fragment>
  217. {missingMembers.slice(0, MAX_MEMBERS_TO_SHOW).map(member => {
  218. const username = member.externalId.split(':').pop();
  219. return (
  220. <MemberCard
  221. key={member.externalId}
  222. data-test-id={`member-card-${member.externalId}`}
  223. >
  224. <MemberCardContent>
  225. <MemberCardContentRow>
  226. <IconGithub size="sm" />
  227. {/* TODO(cathy): create mapping from integration to lambda external link function */}
  228. <StyledExternalLink href={`https://github.com/${username}`}>
  229. @{username}
  230. </StyledExternalLink>
  231. </MemberCardContentRow>
  232. <MemberCardContentRow>
  233. <IconCommit size="xs" />
  234. {tct('[commitCount] Recent Commits', {commitCount: member.commitCount})}
  235. </MemberCardContentRow>
  236. <MemberEmail>{member.email}</MemberEmail>
  237. </MemberCardContent>
  238. <Button
  239. size="sm"
  240. onClick={() => handleSendInvite(member.email)}
  241. data-test-id="invite-missing-member"
  242. icon={<IconMail />}
  243. analyticsEventName="Github Invite Banner: Invite"
  244. analyticsEventKey="github_invite_banner.invite"
  245. >
  246. {t('Invite')}
  247. </Button>
  248. </MemberCard>
  249. );
  250. })}
  251. <MemberCard data-test-id="see-more-card" key="see-more">
  252. <MemberCardContent>
  253. <MemberCardContentRow>
  254. <SeeMore>
  255. {tct('See all [missingMembersCount] missing members', {
  256. missingMembersCount: missingMembers.length,
  257. })}
  258. </SeeMore>
  259. </MemberCardContentRow>
  260. <Subtitle>
  261. {tct('Accounting for [totalCommits] recent commits', {
  262. totalCommits: missingMembers.reduce(
  263. (acc, curr) => acc + curr.commitCount,
  264. 0
  265. ),
  266. })}
  267. </Subtitle>
  268. </MemberCardContent>
  269. <Button
  270. size="sm"
  271. priority="primary"
  272. onClick={openInviteModal}
  273. analyticsEventName="Github Invite Banner: View All"
  274. analyticsEventKey="github_invite_banner.view_all"
  275. >
  276. {t('View All')}
  277. </Button>
  278. </MemberCard>
  279. </Fragment>
  280. );
  281. }
  282. const StyledCard = styled(Card)`
  283. display: flex;
  284. padding: ${space(2)};
  285. padding-bottom: ${space(1.5)};
  286. overflow: hidden;
  287. `;
  288. const CardTitleContainer = styled('div')`
  289. display: flex;
  290. justify-content: space-between;
  291. `;
  292. const CardTitleContent = styled('div')`
  293. display: flex;
  294. flex-direction: column;
  295. `;
  296. const CardTitle = styled('h6')`
  297. margin: 0;
  298. font-size: ${p => p.theme.fontSizeLarge};
  299. font-weight: bold;
  300. color: ${p => p.theme.gray400};
  301. `;
  302. const Subtitle = styled('div')`
  303. display: flex;
  304. align-items: center;
  305. font-size: ${p => p.theme.fontSizeSmall};
  306. font-weight: 400;
  307. color: ${p => p.theme.gray300};
  308. gap: ${space(0.5)};
  309. `;
  310. const MemberEmail = styled('div')`
  311. display: block;
  312. max-width: 70%;
  313. font-size: ${p => p.theme.fontSizeSmall};
  314. font-weight: 400;
  315. color: ${p => p.theme.gray300};
  316. text-overflow: ellipsis;
  317. overflow: hidden;
  318. `;
  319. const MemberCard = styled(Card)`
  320. display: flex;
  321. flex-direction: row;
  322. flex-wrap: wrap;
  323. min-width: 30%;
  324. margin: ${space(1)} ${space(0.5)} 0 0;
  325. padding: ${space(2)} 18px;
  326. justify-content: center;
  327. align-items: center;
  328. `;
  329. const MemberCardContent = styled('div')`
  330. display: flex;
  331. flex-direction: column;
  332. flex: 1 1;
  333. min-width: 50%;
  334. max-width: 75%;
  335. `;
  336. const MemberCardContentRow = styled('div')`
  337. display: flex;
  338. align-items: center;
  339. margin-bottom: ${space(0.25)};
  340. font-size: ${p => p.theme.fontSizeSmall};
  341. gap: ${space(0.75)};
  342. `;
  343. export const StyledExternalLink = styled(ExternalLink)`
  344. font-size: ${p => p.theme.fontSizeMedium};
  345. `;
  346. const SeeMore = styled('div')`
  347. font-size: ${p => p.theme.fontSizeLarge};
  348. `;