shareModal.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import {Fragment, useCallback, useEffect, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {bulkUpdate} from 'sentry/actionCreators/group';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import {ModalRenderProps} from 'sentry/actionCreators/modal';
  6. import AutoSelectText from 'sentry/components/autoSelectText';
  7. import {Button} from 'sentry/components/button';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import Switch from 'sentry/components/switchButton';
  10. import {IconCopy, IconRefresh} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import GroupStore from 'sentry/stores/groupStore';
  13. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  14. import space from 'sentry/styles/space';
  15. import type {Group, Organization} from 'sentry/types';
  16. import useApi from 'sentry/utils/useApi';
  17. interface ShareIssueModalProps extends ModalRenderProps {
  18. groupId: string;
  19. onToggle: () => void;
  20. organization: Organization;
  21. projectSlug: string;
  22. disabled?: boolean;
  23. disabledReason?: string;
  24. }
  25. type UrlRef = React.ElementRef<typeof AutoSelectText>;
  26. function ShareIssueModal({
  27. Header,
  28. Body,
  29. Footer,
  30. organization,
  31. projectSlug,
  32. groupId,
  33. onToggle,
  34. closeModal,
  35. }: ShareIssueModalProps) {
  36. const api = useApi({persistInFlight: true});
  37. const [loading, setLoading] = useState(false);
  38. const urlRef = useRef<UrlRef>(null);
  39. const groups = useLegacyStore(GroupStore);
  40. const group = (groups as Group[]).find(item => item.id === groupId);
  41. const isShared = group?.isPublic;
  42. const handleShare = useCallback(
  43. (e: React.MouseEvent<HTMLButtonElement> | null, reshare?: boolean) => {
  44. e?.preventDefault();
  45. setLoading(true);
  46. onToggle();
  47. bulkUpdate(
  48. api,
  49. {
  50. orgId: organization.slug,
  51. projectId: projectSlug,
  52. itemIds: [groupId],
  53. data: {
  54. isPublic: reshare ?? !isShared,
  55. },
  56. },
  57. {
  58. error: () => {
  59. addErrorMessage(t('Error sharing'));
  60. },
  61. complete: () => {
  62. setLoading(false);
  63. },
  64. }
  65. );
  66. },
  67. [api, setLoading, onToggle, isShared, organization.slug, projectSlug, groupId]
  68. );
  69. /**
  70. * Share as soon as modal is opened
  71. */
  72. useEffect(() => {
  73. if (isShared) {
  74. return;
  75. }
  76. handleShare(null, true);
  77. // eslint-disable-next-line react-hooks/exhaustive-deps -- we only want to run this on open
  78. }, []);
  79. function getShareUrl() {
  80. const path = `/organizations/${organization.slug}/share/issue/${group!.shareId}/`;
  81. const {host, protocol} = window.location;
  82. return `${protocol}//${host}${path}`;
  83. }
  84. const shareUrl = group?.shareId ? getShareUrl() : null;
  85. return (
  86. <Fragment>
  87. <Header closeButton>
  88. <h4>{t('Share Issue')}</h4>
  89. </Header>
  90. <Body>
  91. <ModalContent>
  92. <SwitchWrapper>
  93. <div>
  94. <Title>{t('Create a public link')}</Title>
  95. <SubText>{t('Share a link with anyone outside your organization')}</SubText>
  96. </div>
  97. <Switch
  98. aria-label={isShared ? t('Unshare') : t('Share')}
  99. isActive={isShared}
  100. size="lg"
  101. toggle={handleShare}
  102. />
  103. </SwitchWrapper>
  104. {(!group || loading) && (
  105. <LoadingContainer>
  106. <LoadingIndicator mini />
  107. </LoadingContainer>
  108. )}
  109. {group && !loading && isShared && shareUrl && (
  110. <UrlContainer>
  111. <TextContainer>
  112. <StyledAutoSelectText ref={urlRef}>{shareUrl}</StyledAutoSelectText>
  113. </TextContainer>
  114. <ClipboardButton
  115. title={t('Copy to clipboard')}
  116. borderless
  117. size="sm"
  118. onClick={() => {
  119. navigator.clipboard.writeText(shareUrl);
  120. urlRef.current?.selectText();
  121. }}
  122. icon={<IconCopy />}
  123. aria-label={t('Copy to clipboard')}
  124. />
  125. <ReshareButton
  126. title={t('Generate new URL. Invalidates previous URL')}
  127. aria-label={t('Generate new URL')}
  128. borderless
  129. size="sm"
  130. icon={<IconRefresh />}
  131. onClick={() => handleShare(null, true)}
  132. />
  133. </UrlContainer>
  134. )}
  135. </ModalContent>
  136. </Body>
  137. <Footer>
  138. {!loading && isShared && shareUrl ? (
  139. <Button
  140. priority="primary"
  141. onClick={() => {
  142. navigator.clipboard.writeText(shareUrl);
  143. closeModal();
  144. }}
  145. >
  146. {t('Copy Link')}
  147. </Button>
  148. ) : (
  149. <Button
  150. priority="primary"
  151. onClick={() => {
  152. closeModal();
  153. }}
  154. >
  155. {t('Close')}
  156. </Button>
  157. )}
  158. </Footer>
  159. </Fragment>
  160. );
  161. }
  162. export default ShareIssueModal;
  163. /**
  164. * min-height reduces layout shift when switching on and off
  165. */
  166. const ModalContent = styled('div')`
  167. display: flex;
  168. gap: ${space(2)};
  169. flex-direction: column;
  170. min-height: 100px;
  171. `;
  172. const SwitchWrapper = styled('div')`
  173. display: flex;
  174. justify-content: space-between;
  175. align-items: center;
  176. gap: ${space(2)};
  177. `;
  178. const Title = styled('div')`
  179. padding-right: ${space(4)};
  180. white-space: nowrap;
  181. `;
  182. const SubText = styled('p')`
  183. color: ${p => p.theme.subText};
  184. font-size: ${p => p.theme.fontSizeSmall};
  185. `;
  186. const LoadingContainer = styled('div')`
  187. display: flex;
  188. justify-content: center;
  189. `;
  190. const UrlContainer = styled('div')`
  191. display: flex;
  192. align-items: stretch;
  193. border: 1px solid ${p => p.theme.border};
  194. border-radius: ${space(0.5)};
  195. `;
  196. const StyledAutoSelectText = styled(AutoSelectText)`
  197. padding: ${space(1)} ${space(1)};
  198. ${p => p.theme.overflowEllipsis}
  199. `;
  200. const TextContainer = styled('div')`
  201. position: relative;
  202. display: flex;
  203. flex-grow: 1;
  204. background-color: transparent;
  205. border-right: 1px solid ${p => p.theme.border};
  206. min-width: 0;
  207. `;
  208. const ClipboardButton = styled(Button)`
  209. border-radius: 0;
  210. border-right: 1px solid ${p => p.theme.border};
  211. height: 100%;
  212. flex-shrink: 0;
  213. &:hover {
  214. border-right: 1px solid ${p => p.theme.border};
  215. }
  216. `;
  217. const ReshareButton = styled(Button)`
  218. height: 100%;
  219. flex-shrink: 0;
  220. `;