inviteBanner.tsx 8.3 KB

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