capacitor.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  2. import tracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import type {
  5. Docs,
  6. DocsParams,
  7. OnboardingConfig,
  8. PlatformOption,
  9. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  10. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  11. import {getFeedbackConfigureDescription} from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  12. import {
  13. getReplayConfigOptions,
  14. getReplayConfigureDescription,
  15. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  16. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  17. import {t, tct} from 'sentry/locale';
  18. export enum SiblingOption {
  19. ANGULARV10 = 'angularV10',
  20. ANGULARV12 = 'angularV12',
  21. REACT = 'react',
  22. VUE3 = 'vue3',
  23. VUE2 = 'vue2',
  24. }
  25. type PlatformOptionKey = 'siblingOption';
  26. const platformOptions: Record<PlatformOptionKey, PlatformOption> = {
  27. siblingOption: {
  28. label: t('Sibling Package'),
  29. items: [
  30. {
  31. label: t('Angular 12+'),
  32. value: SiblingOption.ANGULARV12,
  33. },
  34. {
  35. label: t('Angular 10 & 11'),
  36. value: SiblingOption.ANGULARV10,
  37. },
  38. {
  39. label: t('React'),
  40. value: SiblingOption.REACT,
  41. },
  42. {
  43. label: t('Vue 3'),
  44. value: SiblingOption.VUE3,
  45. },
  46. {
  47. label: t('Vue 2'),
  48. value: SiblingOption.VUE2,
  49. },
  50. ],
  51. },
  52. };
  53. type PlatformOptions = typeof platformOptions;
  54. type Params = DocsParams<PlatformOptions>;
  55. const getSentryInitLayout = (params: Params, siblingOption: string): string => {
  56. return `${
  57. siblingOption === SiblingOption.VUE2
  58. ? `Vue,`
  59. : siblingOption === SiblingOption.VUE3
  60. ? 'app,'
  61. : ''
  62. }dsn: "${params.dsn}",
  63. integrations: [${
  64. params.isPerformanceSelected
  65. ? `
  66. new ${getSiblingImportName(siblingOption)}.BrowserTracing({
  67. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  68. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],
  69. ${
  70. params.isPerformanceSelected ? getPerformanceIntegration(siblingOption) : ''
  71. }})`
  72. : ''
  73. }${
  74. params.isFeedbackSelected
  75. ? `
  76. Sentry.feedbackIntegration({
  77. // Additional SDK configuration goes in here, for example:
  78. colorScheme: "light",
  79. }),`
  80. : ''
  81. }${
  82. params.isReplaySelected
  83. ? `
  84. new ${getSiblingImportName(siblingOption)}.Replay(${getReplayConfigOptions(
  85. params.replayOptions
  86. )}),`
  87. : ''
  88. }
  89. ],${
  90. params.isPerformanceSelected
  91. ? `
  92. // Performance Monitoring
  93. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  94. : ''
  95. }${
  96. params.isReplaySelected
  97. ? `
  98. // Session Replay
  99. replaysSessionSampleRate: 0.1, // This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production.
  100. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  101. : ''
  102. }`;
  103. };
  104. const getNextStep = (
  105. params: Params
  106. ): {
  107. description: string;
  108. id: string;
  109. link: string;
  110. name: string;
  111. }[] => {
  112. let nextStepDocs = [...nextSteps];
  113. if (params.isPerformanceSelected) {
  114. nextStepDocs = nextStepDocs.filter(
  115. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  116. );
  117. }
  118. if (params.isReplaySelected) {
  119. nextStepDocs = nextStepDocs.filter(
  120. step => step.id !== ProductSolution.SESSION_REPLAY
  121. );
  122. }
  123. return nextStepDocs;
  124. };
  125. const isAngular = (siblingOption: string): boolean =>
  126. siblingOption === SiblingOption.ANGULARV10 ||
  127. siblingOption === SiblingOption.ANGULARV12;
  128. const isVue = (siblingOption: string): boolean =>
  129. siblingOption === SiblingOption.VUE2 || siblingOption === SiblingOption.VUE3;
  130. function getPerformanceIntegration(siblingOption: string): string {
  131. return `${
  132. isVue(siblingOption)
  133. ? `routingInstrumentation: SentryVue.vueRouterInstrumentation(router),`
  134. : isAngular(siblingOption)
  135. ? `routingInstrumentation: SentryAngular.routingInstrumentation,`
  136. : ''
  137. }`;
  138. }
  139. const performanceAngularErrorHandler = `,
  140. {
  141. provide: SentryAngular.TraceService,
  142. deps: [Router],
  143. },
  144. {
  145. provide: APP_INITIALIZER,
  146. useFactory: () => () => {},
  147. deps: [SentryAngular.TraceService],
  148. multi: true,
  149. },`;
  150. const getInstallStep = (params: Params) => [
  151. {
  152. type: StepType.INSTALL,
  153. description: (
  154. <p>
  155. {tct(
  156. `Install the Sentry Capacitor SDK as a dependency using [codeNpm:npm] or [codeYarn:yarn], alongside the Sentry [siblingName:] SDK:`,
  157. {
  158. codeYarn: <code />,
  159. codeNpm: <code />,
  160. siblingName: getSiblingName(params.platformOptions.siblingOption),
  161. }
  162. )}
  163. </p>
  164. ),
  165. configurations: [
  166. {
  167. language: 'bash',
  168. code: [
  169. {
  170. label: 'npm',
  171. value: 'npm',
  172. language: 'bash',
  173. code: `npm install --save @sentry/capacitor ${getNpmPackage(
  174. params.platformOptions.siblingOption
  175. )}`,
  176. },
  177. {
  178. label: 'yarn',
  179. value: 'yarn',
  180. language: 'bash',
  181. code: `yarn add @sentry/capacitor ${getNpmPackage(
  182. params.platformOptions.siblingOption
  183. )} --exact`,
  184. },
  185. ],
  186. },
  187. {
  188. additionalInfo: (
  189. <p>
  190. {tct(
  191. `The version of the Sentry [siblingName:] SDK must match with the version referred by Sentry Capacitor. To check which version of the Sentry [siblingName:] SDK is installed, use the following command: [code:npm info @sentry/capacitor peerDependencies]`,
  192. {
  193. code: <code />,
  194. siblingName: getSiblingName(params.platformOptions.siblingOption),
  195. }
  196. )}
  197. </p>
  198. ),
  199. },
  200. ],
  201. },
  202. ];
  203. const onboarding: OnboardingConfig<PlatformOptions> = {
  204. install: params => getInstallStep(params),
  205. configure: params => [
  206. {
  207. type: StepType.CONFIGURE,
  208. configurations: getSetupConfiguration({params, showExtraStep: true}),
  209. },
  210. getUploadSourceMapsStep({
  211. guideLink:
  212. 'https://docs.sentry.io/platforms/javascript/guides/capacitor/sourcemaps/',
  213. ...params,
  214. }),
  215. ],
  216. verify: _ => [
  217. {
  218. type: StepType.VERIFY,
  219. description: t(
  220. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  221. ),
  222. configurations: [
  223. {
  224. language: 'javascript',
  225. code: `myUndefinedFunction();`,
  226. },
  227. ],
  228. },
  229. ],
  230. nextSteps: params => getNextStep(params),
  231. };
  232. export const nextSteps = [
  233. {
  234. id: 'capacitor-android-setup',
  235. name: t('Capacitor 2 Setup'),
  236. description: t(
  237. 'If you are using Capacitor 2 or older, follow this step to add required changes in order to initialize the Capacitor SDK on Android.'
  238. ),
  239. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/?#capacitor-2---android-specifics',
  240. },
  241. {
  242. id: 'performance-monitoring',
  243. name: t('Performance Monitoring'),
  244. description: t(
  245. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  246. ),
  247. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/performance/',
  248. },
  249. {
  250. id: 'session-replay',
  251. name: t('Session Replay'),
  252. description: t(
  253. 'Get to the root cause of an error or latency issue faster by seeing all the technical details related to that issue in one visual replay on your web application.'
  254. ),
  255. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/session-replay/',
  256. },
  257. ];
  258. function getSiblingImportsSetupConfiguration(siblingOption: string): string {
  259. switch (siblingOption) {
  260. case SiblingOption.VUE3:
  261. return `import {createApp} from "vue";
  262. import {createRouter} from "vue-router";`;
  263. case SiblingOption.VUE2:
  264. return `import Vue from "vue";
  265. import Router from "vue-router";`;
  266. default:
  267. return '';
  268. }
  269. }
  270. function getVueConstSetup(siblingOption: string): string {
  271. switch (siblingOption) {
  272. case SiblingOption.VUE3:
  273. return `
  274. const app = createApp({
  275. // ...
  276. });
  277. const router = createRouter({
  278. // ...
  279. });
  280. `;
  281. case SiblingOption.VUE2:
  282. return `
  283. Vue.use(Router);
  284. const router = new Router({
  285. // ...
  286. });
  287. `;
  288. default:
  289. return '';
  290. }
  291. }
  292. function getSetupConfiguration({
  293. params,
  294. showExtraStep,
  295. showDescription,
  296. }: {
  297. params: Params;
  298. showExtraStep: boolean;
  299. showDescription?: boolean;
  300. }) {
  301. const siblingOption = params.platformOptions.siblingOption;
  302. const sentryInitLayout = getSentryInitLayout(params, siblingOption);
  303. const configuration = [
  304. {
  305. description: showDescription
  306. ? tct(
  307. `You should init the Sentry capacitor SDK in your [code:main.ts] file as soon as possible during application load up, before initializing Sentry [siblingName:]:`,
  308. {
  309. siblingName: getSiblingName(siblingOption),
  310. code: <code />,
  311. }
  312. )
  313. : null,
  314. language: 'javascript',
  315. code: `${getSiblingImportsSetupConfiguration(siblingOption)}
  316. import * as Sentry from '@sentry/capacitor';
  317. import * as ${getSiblingImportName(siblingOption)} from '${getNpmPackage(
  318. siblingOption
  319. )}';
  320. ${getVueConstSetup(siblingOption)}
  321. Sentry.init({
  322. ${sentryInitLayout}
  323. },
  324. // Forward the init method from ${getNpmPackage(params.platformOptions.siblingOption)}
  325. ${getSiblingImportName(siblingOption)}.init
  326. );`,
  327. },
  328. ];
  329. if (isAngular(siblingOption) && showExtraStep) {
  330. configuration.push({
  331. description: tct(
  332. "The Sentry Angular SDK exports a function to instantiate ErrorHandler provider that will automatically send JavaScript errors captured by the Angular's error handler.",
  333. {}
  334. ),
  335. language: 'javascript',
  336. code: `
  337. import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
  338. import { Router } from "@angular/router";
  339. import * as SentryAngular from "${getNpmPackage(siblingOption)}";
  340. @NgModule({
  341. // ...
  342. providers: [
  343. {
  344. provide: ErrorHandler,
  345. useValue: SentryAngular.createErrorHandler(),
  346. }${params.isPerformanceSelected ? performanceAngularErrorHandler : ' '}
  347. ],
  348. // ...
  349. })
  350. export class AppModule {}`,
  351. });
  352. }
  353. return configuration;
  354. }
  355. function getNpmPackage(siblingOption: string): string {
  356. const packages: Record<SiblingOption, string> = {
  357. [SiblingOption.ANGULARV10]: '@sentry/angular',
  358. [SiblingOption.ANGULARV12]: '@sentry/angular-ivy',
  359. [SiblingOption.REACT]: '@sentry/react',
  360. [SiblingOption.VUE3]: '@sentry/vue',
  361. [SiblingOption.VUE2]: '@sentry/vue',
  362. };
  363. return packages[siblingOption];
  364. }
  365. function getSiblingName(siblingOption: string): string {
  366. siblingOption;
  367. switch (siblingOption) {
  368. case SiblingOption.ANGULARV10:
  369. case SiblingOption.ANGULARV12:
  370. return 'Angular';
  371. case SiblingOption.REACT:
  372. return 'React';
  373. case SiblingOption.VUE2:
  374. case SiblingOption.VUE3:
  375. return 'Vue';
  376. default:
  377. return '';
  378. }
  379. }
  380. function getSiblingImportName(siblingOption: string): string {
  381. siblingOption;
  382. switch (siblingOption) {
  383. case SiblingOption.ANGULARV10:
  384. case SiblingOption.ANGULARV12:
  385. return 'SentryAngular';
  386. case SiblingOption.REACT:
  387. return 'SentryReact';
  388. case SiblingOption.VUE2:
  389. case SiblingOption.VUE3:
  390. return 'SentryVue';
  391. default:
  392. return '';
  393. }
  394. }
  395. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  396. install: params => getInstallStep(params),
  397. configure: params => [
  398. {
  399. type: StepType.CONFIGURE,
  400. description: getReplayConfigureDescription({
  401. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/session-replay/',
  402. }),
  403. configurations: getSetupConfiguration({
  404. params,
  405. showExtraStep: false,
  406. showDescription: false,
  407. }),
  408. additionalInfo: tracePropagationMessage,
  409. },
  410. ],
  411. verify: () => [],
  412. nextSteps: () => [],
  413. };
  414. const feedbackOnboarding: OnboardingConfig<PlatformOptions> = {
  415. install: (params: Params) => getInstallStep(params),
  416. configure: (params: Params) => [
  417. {
  418. type: StepType.CONFIGURE,
  419. description: getFeedbackConfigureDescription({
  420. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/',
  421. }),
  422. configurations: getSetupConfiguration({
  423. params,
  424. showExtraStep: false,
  425. showDescription: false,
  426. }),
  427. additionalInfo: crashReportCallout({
  428. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/#crash-report-modal',
  429. }),
  430. },
  431. ],
  432. verify: () => [],
  433. nextSteps: () => [],
  434. };
  435. const docs: Docs<PlatformOptions> = {
  436. onboarding,
  437. platformOptions,
  438. feedbackOnboardingNpm: feedbackOnboarding,
  439. replayOnboardingNpm: replayOnboarding,
  440. };
  441. export default docs;