integrationSetup.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import {Fragment, useCallback, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {motion} from 'framer-motion';
  4. import {openInviteMembersModal} from 'sentry/actionCreators/modal';
  5. import {Alert} from 'sentry/components/alert';
  6. import {Button} from 'sentry/components/button';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import LoadingError from 'sentry/components/loadingError';
  10. import platforms from 'sentry/data/platforms';
  11. import {t, tct} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {IntegrationProvider, Project} from 'sentry/types';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import getDynamicText from 'sentry/utils/getDynamicText';
  16. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  17. import useApi from 'sentry/utils/useApi';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import {AddIntegrationButton} from 'sentry/views/settings/organizationIntegrations/addIntegrationButton';
  20. import AddInstallationInstructions from './components/integrations/addInstallationInstructions';
  21. import PostInstallCodeSnippet from './components/integrations/postInstallCodeSnippet';
  22. import SetupIntroduction from './components/setupIntroduction';
  23. type Props = {
  24. integrationSlug: string;
  25. project: Project | null;
  26. onClickManualSetup?: () => void;
  27. };
  28. function IntegrationSetup(props: Props) {
  29. const [hasError, setHasError] = useState(false);
  30. const [installed, setInstalled] = useState(false);
  31. const [provider, setProvider] = useState<IntegrationProvider | null>(null);
  32. const organization = useOrganization();
  33. const {project, integrationSlug} = props;
  34. const api = useApi();
  35. const fetchData = useCallback(() => {
  36. if (!integrationSlug) {
  37. return Promise.resolve();
  38. }
  39. const endpoint = `/organizations/${organization.slug}/config/integrations/?provider_key=${integrationSlug}`;
  40. return api
  41. .requestPromise(endpoint)
  42. .then(integrations => {
  43. setProvider(integrations.providers[0]);
  44. setHasError(false);
  45. })
  46. .catch(error => {
  47. setHasError(true);
  48. throw error;
  49. });
  50. }, [integrationSlug, api, organization.slug]);
  51. useEffect(() => {
  52. fetchData();
  53. }, [fetchData]);
  54. const loadingError = (
  55. <LoadingError
  56. message={t(
  57. 'Failed to load the integration for the %s platform.',
  58. project?.platform ?? 'other'
  59. )}
  60. onRetry={fetchData}
  61. />
  62. );
  63. const testOnlyAlert = (
  64. <Alert type="warning">
  65. Platform documentation is not rendered in for tests in CI
  66. </Alert>
  67. );
  68. const renderSetupInstructions = () => {
  69. const currentPlatform = project?.platform ?? 'other';
  70. return (
  71. <SetupIntroduction
  72. stepHeaderText={t(
  73. 'Automatically instrument %s',
  74. platforms.find(p => p.id === currentPlatform)?.name ?? ''
  75. )}
  76. platform={currentPlatform}
  77. />
  78. );
  79. };
  80. const renderIntegrationInstructions = () => {
  81. if (!provider || !project) {
  82. return null;
  83. }
  84. return (
  85. <Fragment>
  86. {renderSetupInstructions()}
  87. <motion.p
  88. variants={{
  89. initial: {opacity: 0},
  90. animate: {opacity: 1},
  91. exit: {opacity: 0},
  92. }}
  93. >
  94. {tct(
  95. "Don't have have permissions to create a Cloudformation stack? [link:Invite your team instead].",
  96. {
  97. link: (
  98. <Button
  99. priority="link"
  100. onClick={() => {
  101. openInviteMembersModal();
  102. }}
  103. aria-label={t('Invite your team instead')}
  104. />
  105. ),
  106. }
  107. )}
  108. </motion.p>
  109. <motion.div
  110. variants={{
  111. initial: {opacity: 0},
  112. animate: {opacity: 1},
  113. exit: {opacity: 0},
  114. }}
  115. >
  116. <AddInstallationInstructions />
  117. </motion.div>
  118. <DocsWrapper>
  119. <StyledButtonBar gap={1}>
  120. <AddIntegrationButton
  121. provider={provider}
  122. onAddIntegration={() => setInstalled(true)}
  123. organization={organization}
  124. priority="primary"
  125. size="sm"
  126. analyticsParams={{view: 'onboarding', already_installed: false}}
  127. modalParams={{projectId: project.id}}
  128. />
  129. <Button
  130. size="sm"
  131. to={{
  132. pathname: window.location.pathname,
  133. query: {manual: '1'},
  134. }}
  135. onClick={() => {
  136. props.onClickManualSetup?.();
  137. trackIntegrationAnalytics('integrations.switch_manual_sdk_setup', {
  138. integration_type: 'first_party',
  139. integration: integrationSlug,
  140. view: 'onboarding',
  141. organization,
  142. });
  143. }}
  144. >
  145. {t('Manual Setup')}
  146. </Button>
  147. </StyledButtonBar>
  148. </DocsWrapper>
  149. </Fragment>
  150. );
  151. };
  152. const renderPostInstallInstructions = () => {
  153. if (!project || !provider) {
  154. return null;
  155. }
  156. return (
  157. <Fragment>
  158. {renderSetupInstructions()}
  159. <PostInstallCodeSnippet
  160. provider={provider}
  161. platform={project.platform}
  162. isOnboarding
  163. />
  164. <ExternalLink
  165. onClick={() => {
  166. trackAnalytics('growth.onboarding_view_full_docs', {
  167. organization,
  168. });
  169. }}
  170. href="https://docs.sentry.io/product/integrations/cloud-monitoring/aws-lambda/"
  171. >
  172. {t('View Full Documentation')}
  173. </ExternalLink>
  174. </Fragment>
  175. );
  176. };
  177. return (
  178. <Fragment>
  179. {installed ? renderPostInstallInstructions() : renderIntegrationInstructions()}
  180. {getDynamicText({
  181. value: !hasError ? null : loadingError,
  182. fixed: testOnlyAlert,
  183. })}
  184. </Fragment>
  185. );
  186. }
  187. const DocsWrapper = styled(motion.div)``;
  188. DocsWrapper.defaultProps = {
  189. initial: {opacity: 0, y: 40},
  190. animate: {opacity: 1, y: 0},
  191. exit: {opacity: 0},
  192. };
  193. const StyledButtonBar = styled(ButtonBar)`
  194. margin-top: ${space(3)};
  195. width: max-content;
  196. @media (max-width: ${p => p.theme.breakpoints.small}) {
  197. width: auto;
  198. grid-row-gap: ${space(1)};
  199. grid-auto-flow: row;
  200. }
  201. `;
  202. export default IntegrationSetup;