shareModal.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, addSuccessMessage} 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 = `/share/issue/${group!.shareId}/`;
  81. const {host, protocol} = window.location;
  82. return `${protocol}//${host}${path}`;
  83. }
  84. const shareUrl = group?.shareId ? getShareUrl() : null;
  85. const handleCopy = () => {
  86. navigator.clipboard
  87. .writeText(shareUrl!)
  88. .then(() => {
  89. addSuccessMessage(t('Copied to clipboard'));
  90. })
  91. .catch(() => {
  92. addErrorMessage(t('Error copying to clipboard'));
  93. });
  94. };
  95. return (
  96. <Fragment>
  97. <Header closeButton>
  98. <h4>{t('Share Issue')}</h4>
  99. </Header>
  100. <Body>
  101. <ModalContent>
  102. <SwitchWrapper>
  103. <div>
  104. <Title>{t('Create a public link')}</Title>
  105. <SubText>{t('Share a link with anyone outside your organization')}</SubText>
  106. </div>
  107. <Switch
  108. aria-label={isShared ? t('Unshare') : t('Share')}
  109. isActive={isShared}
  110. size="lg"
  111. toggle={handleShare}
  112. />
  113. </SwitchWrapper>
  114. {(!group || loading) && (
  115. <LoadingContainer>
  116. <LoadingIndicator mini />
  117. </LoadingContainer>
  118. )}
  119. {group && !loading && isShared && shareUrl && (
  120. <UrlContainer>
  121. <TextContainer>
  122. <StyledAutoSelectText ref={urlRef}>{shareUrl}</StyledAutoSelectText>
  123. </TextContainer>
  124. <ClipboardButton
  125. title={t('Copy to clipboard')}
  126. borderless
  127. size="sm"
  128. onClick={() => {
  129. urlRef.current?.selectText();
  130. handleCopy();
  131. }}
  132. icon={<IconCopy />}
  133. aria-label={t('Copy to clipboard')}
  134. />
  135. <ReshareButton
  136. title={t('Generate new URL. Invalidates previous URL')}
  137. aria-label={t('Generate new URL')}
  138. borderless
  139. size="sm"
  140. icon={<IconRefresh />}
  141. onClick={() => handleShare(null, true)}
  142. />
  143. </UrlContainer>
  144. )}
  145. </ModalContent>
  146. </Body>
  147. <Footer>
  148. {!loading && isShared && shareUrl ? (
  149. <Button
  150. priority="primary"
  151. onClick={() => {
  152. handleCopy();
  153. closeModal();
  154. }}
  155. >
  156. {t('Copy Link')}
  157. </Button>
  158. ) : (
  159. <Button
  160. priority="primary"
  161. onClick={() => {
  162. closeModal();
  163. }}
  164. >
  165. {t('Close')}
  166. </Button>
  167. )}
  168. </Footer>
  169. </Fragment>
  170. );
  171. }
  172. export default ShareIssueModal;
  173. /**
  174. * min-height reduces layout shift when switching on and off
  175. */
  176. const ModalContent = styled('div')`
  177. display: flex;
  178. gap: ${space(2)};
  179. flex-direction: column;
  180. min-height: 100px;
  181. `;
  182. const SwitchWrapper = styled('div')`
  183. display: flex;
  184. justify-content: space-between;
  185. align-items: center;
  186. gap: ${space(2)};
  187. `;
  188. const Title = styled('div')`
  189. padding-right: ${space(4)};
  190. white-space: nowrap;
  191. `;
  192. const SubText = styled('p')`
  193. color: ${p => p.theme.subText};
  194. font-size: ${p => p.theme.fontSizeSmall};
  195. `;
  196. const LoadingContainer = styled('div')`
  197. display: flex;
  198. justify-content: center;
  199. `;
  200. const UrlContainer = styled('div')`
  201. display: flex;
  202. align-items: stretch;
  203. border: 1px solid ${p => p.theme.border};
  204. border-radius: ${space(0.5)};
  205. `;
  206. const StyledAutoSelectText = styled(AutoSelectText)`
  207. padding: ${space(1)} ${space(1)};
  208. ${p => p.theme.overflowEllipsis}
  209. `;
  210. const TextContainer = styled('div')`
  211. position: relative;
  212. display: flex;
  213. flex-grow: 1;
  214. background-color: transparent;
  215. border-right: 1px solid ${p => p.theme.border};
  216. min-width: 0;
  217. `;
  218. const ClipboardButton = styled(Button)`
  219. border-radius: 0;
  220. border-right: 1px solid ${p => p.theme.border};
  221. height: 100%;
  222. flex-shrink: 0;
  223. &:hover {
  224. border-right: 1px solid ${p => p.theme.border};
  225. }
  226. `;
  227. const ReshareButton = styled(Button)`
  228. height: 100%;
  229. flex-shrink: 0;
  230. `;