integrationSetup.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 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 type="warning">
  104. Platform documentation is not rendered in for tests in CI
  105. </Alert>
  106. );
  107. const renderIntegrationInstructions = () => {
  108. if (!provider) {
  109. return null;
  110. }
  111. return (
  112. <Fragment>
  113. <motion.p
  114. variants={{
  115. initial: {opacity: 0},
  116. animate: {opacity: 1},
  117. exit: {opacity: 0},
  118. }}
  119. >
  120. {tct(
  121. "Don't have have permissions to create a Cloudformation stack? [link:Invite your team instead].",
  122. {
  123. link: (
  124. <Button
  125. priority="link"
  126. onClick={() => {
  127. openInviteMembersModal();
  128. }}
  129. aria-label={t('Invite your team instead')}
  130. />
  131. ),
  132. }
  133. )}
  134. </motion.p>
  135. <motion.div
  136. variants={{
  137. initial: {opacity: 0},
  138. animate: {opacity: 1},
  139. exit: {opacity: 0},
  140. }}
  141. >
  142. <AddInstallationInstructions />
  143. </motion.div>
  144. <motion.div
  145. initial={{opacity: 0, y: 40}}
  146. animate={{opacity: 1, y: 0}}
  147. exit={{opacity: 0, y: 40}}
  148. >
  149. <AddIntegrationButton
  150. provider={provider}
  151. onAddIntegration={() => setInstalled(true)}
  152. organization={organization}
  153. priority="primary"
  154. size="sm"
  155. analyticsParams={{view: 'onboarding', already_installed: false}}
  156. modalParams={{projectId: project.id}}
  157. />
  158. </motion.div>
  159. </Fragment>
  160. );
  161. };
  162. const renderPostInstallInstructions = () => {
  163. if (!provider) {
  164. return null;
  165. }
  166. return (
  167. <Fragment>
  168. <PostInstallCodeSnippet
  169. provider={provider}
  170. platform={project.platform}
  171. isOnboarding
  172. />
  173. <ExternalLink
  174. onClick={() => {
  175. trackAnalytics('growth.onboarding_view_full_docs', {
  176. organization,
  177. });
  178. }}
  179. href="https://docs.sentry.io/product/integrations/cloud-monitoring/aws-lambda/"
  180. >
  181. {t('View Full Documentation')}
  182. </ExternalLink>
  183. </Fragment>
  184. );
  185. };
  186. if (isLoading) {
  187. return <LoadingIndicator />;
  188. }
  189. if (!docsConfig || !dsn || !projectKeyId) {
  190. return (
  191. <LoadingError
  192. message={t(
  193. 'The getting started documentation for this platform is currently unavailable.'
  194. )}
  195. onRetry={refetch}
  196. />
  197. );
  198. }
  199. const docParams: DocsParams<any> = {
  200. api,
  201. projectKeyId,
  202. dsn,
  203. organization,
  204. platformKey: platform.id,
  205. projectId: project.id,
  206. projectSlug: project.slug,
  207. isFeedbackSelected: false,
  208. isPerformanceSelected: false,
  209. isProfilingSelected: false,
  210. isReplaySelected: false,
  211. isSelfHosted,
  212. platformOptions: selectedPlatformOptions,
  213. sourcePackageRegistries: {
  214. isLoading: false,
  215. data: undefined,
  216. },
  217. urlPrefix,
  218. };
  219. return (
  220. <Fragment>
  221. <SetupIntroduction
  222. stepHeaderText={t('Automatically instrument %s SDK', platform.name)}
  223. platform={platform.id}
  224. />
  225. <PlatformOptionsControl
  226. platformOptions={platformOptions}
  227. onChange={docsConfig.onboarding.onPlatformOptionsChange?.(docParams)}
  228. />
  229. <Divider />
  230. {installed ? renderPostInstallInstructions() : renderIntegrationInstructions()}
  231. {getDynamicText({
  232. value: !hasError ? null : loadingError,
  233. fixed: testOnlyAlert,
  234. })}
  235. </Fragment>
  236. );
  237. }
  238. const Divider = styled('hr')`
  239. height: 1px;
  240. width: 100%;
  241. background: ${p => p.theme.border};
  242. border: none;
  243. margin-bottom: ${space(3)};
  244. `;
  245. export default IntegrationSetup;