shareModal.tsx 6.1 KB

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