integrationSetup.tsx 6.3 KB

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