integrationSetup.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. <DocsWrapper>
  145. <AddIntegrationButton
  146. provider={provider}
  147. onAddIntegration={() => setInstalled(true)}
  148. organization={organization}
  149. priority="primary"
  150. size="sm"
  151. analyticsParams={{view: 'onboarding', already_installed: false}}
  152. modalParams={{projectId: project.id}}
  153. />
  154. </DocsWrapper>
  155. </Fragment>
  156. );
  157. };
  158. const renderPostInstallInstructions = () => {
  159. if (!provider) {
  160. return null;
  161. }
  162. return (
  163. <Fragment>
  164. <PostInstallCodeSnippet
  165. provider={provider}
  166. platform={project.platform}
  167. isOnboarding
  168. />
  169. <ExternalLink
  170. onClick={() => {
  171. trackAnalytics('growth.onboarding_view_full_docs', {
  172. organization,
  173. });
  174. }}
  175. href="https://docs.sentry.io/product/integrations/cloud-monitoring/aws-lambda/"
  176. >
  177. {t('View Full Documentation')}
  178. </ExternalLink>
  179. </Fragment>
  180. );
  181. };
  182. if (isLoading) {
  183. return <LoadingIndicator />;
  184. }
  185. if (!docsConfig || !dsn || !projectKeyId) {
  186. return (
  187. <LoadingError
  188. message={t(
  189. 'The getting started documentation for this platform is currently unavailable.'
  190. )}
  191. onRetry={refetch}
  192. />
  193. );
  194. }
  195. const docParams: DocsParams<any> = {
  196. api,
  197. projectKeyId,
  198. dsn,
  199. organization,
  200. platformKey: platform.id,
  201. projectId: project.id,
  202. projectSlug: project.slug,
  203. isFeedbackSelected: false,
  204. isPerformanceSelected: false,
  205. isProfilingSelected: false,
  206. isReplaySelected: false,
  207. isSelfHosted,
  208. platformOptions: selectedPlatformOptions,
  209. sourcePackageRegistries: {
  210. isLoading: false,
  211. data: undefined,
  212. },
  213. urlPrefix,
  214. };
  215. return (
  216. <Fragment>
  217. <SetupIntroduction
  218. stepHeaderText={t('Automatically instrument %s SDK', platform.name)}
  219. platform={platform.id}
  220. />
  221. <PlatformOptionsControl
  222. platformOptions={platformOptions}
  223. onChange={docsConfig.onboarding.onPlatformOptionsChange?.(docParams)}
  224. />
  225. <Divider />
  226. {installed ? renderPostInstallInstructions() : renderIntegrationInstructions()}
  227. {getDynamicText({
  228. value: !hasError ? null : loadingError,
  229. fixed: testOnlyAlert,
  230. })}
  231. </Fragment>
  232. );
  233. }
  234. const DocsWrapper = styled(motion.div)``;
  235. DocsWrapper.defaultProps = {
  236. initial: {opacity: 0, y: 40},
  237. animate: {opacity: 1, y: 0},
  238. exit: {opacity: 0},
  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;