integrationSetup.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import 'prism-sentry/index.css';
  2. import {Fragment, useCallback, useEffect, useState} from 'react';
  3. import styled from '@emotion/styled';
  4. import {motion} from 'framer-motion';
  5. import {openInviteMembersModal} from 'sentry/actionCreators/modal';
  6. import Alert from 'sentry/components/alert';
  7. import Button from 'sentry/components/button';
  8. import ButtonBar from 'sentry/components/buttonBar';
  9. import ExternalLink from 'sentry/components/links/externalLink';
  10. import LoadingError from 'sentry/components/loadingError';
  11. import platforms from 'sentry/data/platforms';
  12. import {t, tct} from 'sentry/locale';
  13. import space from 'sentry/styles/space';
  14. import {IntegrationProvider, Project} from 'sentry/types';
  15. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  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/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. to={{
  133. pathname: window.location.pathname,
  134. query: {manual: '1'},
  135. }}
  136. onClick={() => {
  137. props.onClickManualSetup?.();
  138. trackIntegrationAnalytics('integrations.switch_manual_sdk_setup', {
  139. integration_type: 'first_party',
  140. integration: integrationSlug,
  141. view: 'onboarding',
  142. organization,
  143. });
  144. }}
  145. >
  146. {t('Manual Setup')}
  147. </Button>
  148. </StyledButtonBar>
  149. </DocsWrapper>
  150. </Fragment>
  151. );
  152. };
  153. const renderPostInstallInstructions = () => {
  154. if (!project || !provider) {
  155. return null;
  156. }
  157. return (
  158. <Fragment>
  159. {renderSetupInstructions()}
  160. <PostInstallCodeSnippet
  161. provider={provider}
  162. platform={project.platform}
  163. isOnboarding
  164. />
  165. <ExternalLink
  166. onClick={() => {
  167. trackAdvancedAnalyticsEvent('growth.onboarding_view_full_docs', {
  168. organization,
  169. });
  170. }}
  171. href="https://docs.sentry.io/product/integrations/cloud-monitoring/aws-lambda/"
  172. >
  173. {t('View Full Documentation')}
  174. </ExternalLink>
  175. </Fragment>
  176. );
  177. };
  178. return (
  179. <Fragment>
  180. {installed ? renderPostInstallInstructions() : renderIntegrationInstructions()}
  181. {getDynamicText({
  182. value: !hasError ? null : loadingError,
  183. fixed: testOnlyAlert,
  184. })}
  185. </Fragment>
  186. );
  187. }
  188. const DocsWrapper = styled(motion.div)``;
  189. DocsWrapper.defaultProps = {
  190. initial: {opacity: 0, y: 40},
  191. animate: {opacity: 1, y: 0},
  192. exit: {opacity: 0},
  193. };
  194. const StyledButtonBar = styled(ButtonBar)`
  195. margin-top: ${space(3)};
  196. width: max-content;
  197. @media (max-width: ${p => p.theme.breakpoints.small}) {
  198. width: auto;
  199. grid-row-gap: ${space(1)};
  200. grid-auto-flow: row;
  201. }
  202. `;
  203. export default IntegrationSetup;