integrationSetup.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 {Button} from 'sentry/components/button';
  6. import {Alert} from 'sentry/components/core/alert';
  7. import ExternalLink from 'sentry/components/links/externalLink';
  8. import LoadingError from 'sentry/components/loadingError';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import type {
  11. BasePlatformOptions,
  12. DocsParams,
  13. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  14. import {useLoadGettingStarted} from 'sentry/components/onboarding/gettingStartedDoc/utils/useLoadGettingStarted';
  15. import {
  16. PlatformOptionsControl,
  17. useUrlPlatformOptions,
  18. } from 'sentry/components/onboarding/platformOptionsControl';
  19. import {t, tct} from 'sentry/locale';
  20. import ConfigStore from 'sentry/stores/configStore';
  21. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  22. import {space} from 'sentry/styles/space';
  23. import type {IntegrationProvider} from 'sentry/types/integrations';
  24. import type {PlatformIntegration, Project} from 'sentry/types/project';
  25. import {trackAnalytics} from 'sentry/utils/analytics';
  26. import getDynamicText from 'sentry/utils/getDynamicText';
  27. import useApi from 'sentry/utils/useApi';
  28. import useOrganization from 'sentry/utils/useOrganization';
  29. import SetupIntroduction from 'sentry/views/onboarding/components/setupIntroduction';
  30. import {AddIntegrationButton} from 'sentry/views/settings/organizationIntegrations/addIntegrationButton';
  31. import AddInstallationInstructions from './components/integrations/addInstallationInstructions';
  32. import PostInstallCodeSnippet from './components/integrations/postInstallCodeSnippet';
  33. export enum InstallationMode {
  34. AUTO = 'auto',
  35. MANUAL = 'manual',
  36. }
  37. export const platformOptions = {
  38. installationMode: {
  39. label: t('Installation Mode'),
  40. items: [
  41. {
  42. label: t('Auto'),
  43. value: InstallationMode.AUTO,
  44. },
  45. {
  46. label: t('Manual'),
  47. value: InstallationMode.MANUAL,
  48. },
  49. ],
  50. defaultValue: InstallationMode.AUTO,
  51. },
  52. } satisfies BasePlatformOptions;
  53. type Props = {
  54. integrationSlug: string;
  55. platform: PlatformIntegration;
  56. project: Project;
  57. };
  58. function IntegrationSetup({project, integrationSlug, platform}: Props) {
  59. const [hasError, setHasError] = useState(false);
  60. const [installed, setInstalled] = useState(false);
  61. const [provider, setProvider] = useState<IntegrationProvider | null>(null);
  62. const organization = useOrganization();
  63. const {isSelfHosted, urlPrefix} = useLegacyStore(ConfigStore);
  64. const {
  65. isLoading,
  66. docs: docsConfig,
  67. dsn,
  68. projectKeyId,
  69. refetch,
  70. } = useLoadGettingStarted({
  71. orgSlug: organization.slug,
  72. projSlug: project.slug,
  73. platform,
  74. });
  75. const selectedPlatformOptions = useUrlPlatformOptions(docsConfig?.platformOptions);
  76. const api = useApi();
  77. const fetchData = useCallback(() => {
  78. if (!integrationSlug) {
  79. return Promise.resolve();
  80. }
  81. const endpoint = `/organizations/${organization.slug}/config/integrations/?provider_key=${integrationSlug}`;
  82. return api
  83. .requestPromise(endpoint)
  84. .then(integrations => {
  85. setProvider(integrations.providers[0]);
  86. setHasError(false);
  87. })
  88. .catch(error => {
  89. setHasError(true);
  90. throw error;
  91. });
  92. }, [integrationSlug, api, organization.slug]);
  93. useEffect(() => {
  94. fetchData();
  95. }, [fetchData]);
  96. const loadingError = (
  97. <LoadingError
  98. message={t('Failed to load the integration for the %s platform.', platform.name)}
  99. onRetry={fetchData}
  100. />
  101. );
  102. const testOnlyAlert = (
  103. <Alert.Container>
  104. <Alert type="warning">
  105. Platform documentation is not rendered in for tests in CI
  106. </Alert>
  107. </Alert.Container>
  108. );
  109. const renderIntegrationInstructions = () => {
  110. if (!provider) {
  111. return null;
  112. }
  113. return (
  114. <Fragment>
  115. <motion.p
  116. variants={{
  117. initial: {opacity: 0},
  118. animate: {opacity: 1},
  119. exit: {opacity: 0},
  120. }}
  121. >
  122. {tct(
  123. "Don't have have permissions to create a Cloudformation stack? [link:Invite your team instead].",
  124. {
  125. link: (
  126. <Button
  127. priority="link"
  128. onClick={() => {
  129. openInviteMembersModal();
  130. }}
  131. aria-label={t('Invite your team instead')}
  132. />
  133. ),
  134. }
  135. )}
  136. </motion.p>
  137. <motion.div
  138. variants={{
  139. initial: {opacity: 0},
  140. animate: {opacity: 1},
  141. exit: {opacity: 0},
  142. }}
  143. >
  144. <AddInstallationInstructions />
  145. </motion.div>
  146. <motion.div
  147. initial={{opacity: 0, y: 40}}
  148. animate={{opacity: 1, y: 0}}
  149. exit={{opacity: 0, y: 40}}
  150. >
  151. <AddIntegrationButton
  152. provider={provider}
  153. onAddIntegration={() => setInstalled(true)}
  154. organization={organization}
  155. priority="primary"
  156. size="sm"
  157. analyticsParams={{view: 'onboarding', already_installed: false}}
  158. modalParams={{projectId: project.id}}
  159. />
  160. </motion.div>
  161. </Fragment>
  162. );
  163. };
  164. const renderPostInstallInstructions = () => {
  165. if (!provider) {
  166. return null;
  167. }
  168. return (
  169. <Fragment>
  170. <PostInstallCodeSnippet
  171. provider={provider}
  172. platform={project.platform}
  173. isOnboarding
  174. />
  175. <ExternalLink
  176. onClick={() => {
  177. trackAnalytics('growth.onboarding_view_full_docs', {
  178. organization,
  179. });
  180. }}
  181. href="https://docs.sentry.io/product/integrations/cloud-monitoring/aws-lambda/"
  182. >
  183. {t('View Full Documentation')}
  184. </ExternalLink>
  185. </Fragment>
  186. );
  187. };
  188. if (isLoading) {
  189. return <LoadingIndicator />;
  190. }
  191. if (!docsConfig || !dsn || !projectKeyId) {
  192. return (
  193. <LoadingError
  194. message={t(
  195. 'The getting started documentation for this platform is currently unavailable.'
  196. )}
  197. onRetry={refetch}
  198. />
  199. );
  200. }
  201. const docParams: DocsParams<any> = {
  202. api,
  203. projectKeyId,
  204. dsn,
  205. organization,
  206. platformKey: platform.id,
  207. projectId: project.id,
  208. projectSlug: project.slug,
  209. isFeedbackSelected: false,
  210. isPerformanceSelected: false,
  211. isProfilingSelected: false,
  212. isReplaySelected: false,
  213. isSelfHosted,
  214. platformOptions: selectedPlatformOptions,
  215. sourcePackageRegistries: {
  216. isLoading: false,
  217. data: undefined,
  218. },
  219. urlPrefix,
  220. };
  221. return (
  222. <Fragment>
  223. <SetupIntroduction
  224. stepHeaderText={t('Automatically instrument %s SDK', platform.name)}
  225. platform={platform.id}
  226. />
  227. <PlatformOptionsControl
  228. platformOptions={platformOptions}
  229. onChange={docsConfig.onboarding.onPlatformOptionsChange?.(docParams)}
  230. />
  231. <Divider />
  232. {installed ? renderPostInstallInstructions() : renderIntegrationInstructions()}
  233. {getDynamicText({
  234. value: !hasError ? null : loadingError,
  235. fixed: testOnlyAlert,
  236. })}
  237. </Fragment>
  238. );
  239. }
  240. const Divider = styled('hr')`
  241. height: 1px;
  242. width: 100%;
  243. background: ${p => p.theme.border};
  244. border: none;
  245. margin-bottom: ${space(3)};
  246. `;
  247. export default IntegrationSetup;