capacitor.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  2. import widgetCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/widgetCallout';
  3. import TracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import type {
  6. Docs,
  7. DocsParams,
  8. OnboardingConfig,
  9. PlatformOption,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  12. import {
  13. getCrashReportJavaScriptInstallStep,
  14. getCrashReportModalConfigDescription,
  15. getCrashReportModalIntroduction,
  16. getFeedbackConfigOptions,
  17. getFeedbackConfigureDescription,
  18. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  19. import {
  20. getReplayConfigOptions,
  21. getReplayConfigureDescription,
  22. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  23. import {t, tct} from 'sentry/locale';
  24. export enum SiblingOption {
  25. ANGULARV10 = 'angularV10',
  26. ANGULARV12 = 'angularV12',
  27. REACT = 'react',
  28. VUE3 = 'vue3',
  29. VUE2 = 'vue2',
  30. }
  31. type PlatformOptionKey = 'siblingOption';
  32. const platformOptions: Record<PlatformOptionKey, PlatformOption> = {
  33. siblingOption: {
  34. label: t('Sibling Package'),
  35. items: [
  36. {
  37. label: t('Angular 12+'),
  38. value: SiblingOption.ANGULARV12,
  39. },
  40. {
  41. label: t('Angular 10 & 11'),
  42. value: SiblingOption.ANGULARV10,
  43. },
  44. {
  45. label: t('React'),
  46. value: SiblingOption.REACT,
  47. },
  48. {
  49. label: t('Vue 3'),
  50. value: SiblingOption.VUE3,
  51. },
  52. {
  53. label: t('Vue 2'),
  54. value: SiblingOption.VUE2,
  55. },
  56. ],
  57. },
  58. };
  59. type PlatformOptions = typeof platformOptions;
  60. type Params = DocsParams<PlatformOptions>;
  61. function getIntegrations(params: Params, siblingOption: string) {
  62. const integrations: string[] = ['SentrySibling.browserTracingIntegration()'];
  63. if (params.isPerformanceSelected) {
  64. integrations.push(`
  65. new ${getSiblingImportName(siblingOption)}.BrowserTracing({
  66. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  67. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],
  68. ${
  69. params.isPerformanceSelected ? getPerformanceIntegration(siblingOption) : ''
  70. }})`);
  71. }
  72. if (params.isFeedbackSelected) {
  73. const feedbackIntegration: string[] = [
  74. `// Additional SDK configuration goes in here, for example:
  75. colorScheme: "system"`,
  76. ];
  77. const feedbackConfigOptions = getFeedbackConfigOptions(params.feedbackOptions);
  78. if (feedbackConfigOptions) {
  79. feedbackIntegration.push(feedbackConfigOptions);
  80. }
  81. integrations.push(
  82. `
  83. Sentry.feedbackIntegration({
  84. ${feedbackIntegration.join(',')}
  85. }),`
  86. );
  87. }
  88. if (params.isReplaySelected) {
  89. integrations.push(
  90. `
  91. new ${getSiblingImportName(siblingOption)}.Replay(${getReplayConfigOptions(
  92. params.replayOptions
  93. )}),`
  94. );
  95. }
  96. return integrations.join(',');
  97. }
  98. const getSentryInitLayout = (params: Params, siblingOption: string): string => {
  99. return `${
  100. siblingOption === SiblingOption.VUE2
  101. ? `Vue,`
  102. : siblingOption === SiblingOption.VUE3
  103. ? 'app,'
  104. : ''
  105. }dsn: "${params.dsn.public}",
  106. integrations: [
  107. ${getIntegrations(params, siblingOption)}
  108. ],
  109. ${
  110. params.isPerformanceSelected
  111. ? `
  112. // Tracing
  113. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  114. : ''
  115. }${
  116. params.isReplaySelected
  117. ? `
  118. // Session Replay
  119. 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.
  120. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  121. : ''
  122. }`;
  123. };
  124. const isAngular = (siblingOption: string): boolean =>
  125. siblingOption === SiblingOption.ANGULARV10 ||
  126. siblingOption === SiblingOption.ANGULARV12;
  127. const isVue = (siblingOption: string): boolean =>
  128. siblingOption === SiblingOption.VUE2 || siblingOption === SiblingOption.VUE3;
  129. function getPerformanceIntegration(siblingOption: string): string {
  130. return `${
  131. isVue(siblingOption)
  132. ? `routingInstrumentation: SentryVue.vueRouterInstrumentation(router),`
  133. : isAngular(siblingOption)
  134. ? `routingInstrumentation: SentryAngular.routingInstrumentation,`
  135. : ''
  136. }`;
  137. }
  138. const performanceAngularErrorHandler = `,
  139. {
  140. provide: SentryAngular.TraceService,
  141. deps: [Router],
  142. },
  143. {
  144. provide: APP_INITIALIZER,
  145. useFactory: () => () => {},
  146. deps: [SentryAngular.TraceService],
  147. multi: true,
  148. },`;
  149. const onboarding: OnboardingConfig<PlatformOptions> = {
  150. install: (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. )}@^7`,
  176. },
  177. {
  178. label: 'yarn',
  179. value: 'yarn',
  180. language: 'bash',
  181. code: `yarn add @sentry/capacitor ${getNpmPackage(
  182. params.platformOptions.siblingOption
  183. )}@^7 --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. configure: params => [
  204. {
  205. type: StepType.CONFIGURE,
  206. configurations: getSetupConfiguration({params, showExtraStep: true}),
  207. },
  208. getUploadSourceMapsStep({
  209. guideLink:
  210. 'https://docs.sentry.io/platforms/javascript/guides/capacitor/sourcemaps/',
  211. ...params,
  212. }),
  213. ],
  214. verify: _ => [
  215. {
  216. type: StepType.VERIFY,
  217. description: t(
  218. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  219. ),
  220. configurations: [
  221. {
  222. language: 'javascript',
  223. code: `myUndefinedFunction();`,
  224. },
  225. ],
  226. },
  227. ],
  228. nextSteps: params => [
  229. {
  230. id: 'capacitor-android-setup',
  231. name: t('Capacitor 2 Setup'),
  232. description: t(
  233. 'If you are using Capacitor 2 or older, follow this step to add required changes in order to initialize the Capacitor SDK on Android.'
  234. ),
  235. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/?#capacitor-2---android-specifics',
  236. },
  237. params.isPerformanceSelected
  238. ? null
  239. : {
  240. id: 'performance-monitoring',
  241. name: t('Tracing'),
  242. description: t(
  243. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  244. ),
  245. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/tracing/',
  246. },
  247. params.isReplaySelected
  248. ? null
  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. };
  259. function getSiblingImportsSetupConfiguration(siblingOption: string): string {
  260. switch (siblingOption) {
  261. case SiblingOption.VUE3:
  262. return `import {createApp} from "vue";
  263. import {createRouter} from "vue-router";`;
  264. case SiblingOption.VUE2:
  265. return `import Vue from "vue";
  266. import Router from "vue-router";`;
  267. default:
  268. return '';
  269. }
  270. }
  271. function getVueConstSetup(siblingOption: string): string {
  272. switch (siblingOption) {
  273. case SiblingOption.VUE3:
  274. return `
  275. const app = createApp({
  276. // ...
  277. });
  278. const router = createRouter({
  279. // ...
  280. });
  281. `;
  282. case SiblingOption.VUE2:
  283. return `
  284. Vue.use(Router);
  285. const router = new Router({
  286. // ...
  287. });
  288. `;
  289. default:
  290. return '';
  291. }
  292. }
  293. function getSetupConfiguration({
  294. params,
  295. showExtraStep,
  296. showDescription,
  297. }: {
  298. params: Params;
  299. showExtraStep: boolean;
  300. showDescription?: boolean;
  301. }) {
  302. const siblingOption = params.platformOptions.siblingOption;
  303. const sentryInitLayout = getSentryInitLayout(params, siblingOption);
  304. const configuration = [
  305. {
  306. description: showDescription
  307. ? tct(
  308. `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:]:`,
  309. {
  310. siblingName: getSiblingName(siblingOption),
  311. code: <code />,
  312. }
  313. )
  314. : null,
  315. language: 'javascript',
  316. code: `${getSiblingImportsSetupConfiguration(siblingOption)}
  317. import * as Sentry from '@sentry/capacitor';
  318. import * as ${getSiblingImportName(siblingOption)} from '${getNpmPackage(
  319. siblingOption
  320. )}';
  321. ${getVueConstSetup(siblingOption)}
  322. Sentry.init({
  323. ${sentryInitLayout}
  324. },
  325. // Forward the init method from ${getNpmPackage(params.platformOptions.siblingOption)}
  326. ${getSiblingImportName(siblingOption)}.init
  327. );`,
  328. },
  329. ];
  330. if (isAngular(siblingOption) && showExtraStep) {
  331. configuration.push({
  332. description: tct(
  333. "The Sentry Angular SDK exports a function to instantiate ErrorHandler provider that will automatically send JavaScript errors captured by the Angular's error handler.",
  334. {}
  335. ),
  336. language: 'javascript',
  337. code: `
  338. import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
  339. import { Router } from "@angular/router";
  340. import * as SentryAngular from "${getNpmPackage(siblingOption)}";
  341. @NgModule({
  342. // ...
  343. providers: [
  344. {
  345. provide: ErrorHandler,
  346. useValue: SentryAngular.createErrorHandler(),
  347. }${params.isPerformanceSelected ? performanceAngularErrorHandler : ' '}
  348. ],
  349. // ...
  350. })
  351. export class AppModule {}`,
  352. });
  353. }
  354. return configuration;
  355. }
  356. function getNpmPackage(siblingOption: string): string {
  357. const packages: Record<SiblingOption, string> = {
  358. [SiblingOption.ANGULARV10]: '@sentry/angular',
  359. [SiblingOption.ANGULARV12]: '@sentry/angular-ivy',
  360. [SiblingOption.REACT]: '@sentry/react',
  361. [SiblingOption.VUE3]: '@sentry/vue',
  362. [SiblingOption.VUE2]: '@sentry/vue',
  363. };
  364. return packages[siblingOption];
  365. }
  366. function getSiblingName(siblingOption: string): string {
  367. siblingOption;
  368. switch (siblingOption) {
  369. case SiblingOption.ANGULARV10:
  370. case SiblingOption.ANGULARV12:
  371. return 'Angular';
  372. case SiblingOption.REACT:
  373. return 'React';
  374. case SiblingOption.VUE2:
  375. case SiblingOption.VUE3:
  376. return 'Vue';
  377. default:
  378. return '';
  379. }
  380. }
  381. function getSiblingImportName(siblingOption: string): string {
  382. siblingOption;
  383. switch (siblingOption) {
  384. case SiblingOption.ANGULARV10:
  385. case SiblingOption.ANGULARV12:
  386. return 'SentryAngular';
  387. case SiblingOption.REACT:
  388. return 'SentryReact';
  389. case SiblingOption.VUE2:
  390. case SiblingOption.VUE3:
  391. return 'SentryVue';
  392. default:
  393. return '';
  394. }
  395. }
  396. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  397. install: params => onboarding.install(params),
  398. configure: params => [
  399. {
  400. type: StepType.CONFIGURE,
  401. description: getReplayConfigureDescription({
  402. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/session-replay/',
  403. }),
  404. configurations: getSetupConfiguration({
  405. params,
  406. showExtraStep: false,
  407. showDescription: false,
  408. }),
  409. additionalInfo: <TracePropagationMessage />,
  410. },
  411. ],
  412. verify: () => [],
  413. nextSteps: () => [],
  414. };
  415. const feedbackOnboarding: OnboardingConfig<PlatformOptions> = {
  416. install: (params: Params) => onboarding.install(params),
  417. configure: (params: Params) => [
  418. {
  419. type: StepType.CONFIGURE,
  420. description: getFeedbackConfigureDescription({
  421. linkConfig:
  422. 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/configuration/',
  423. linkButton:
  424. 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/configuration/#bring-your-own-button',
  425. }),
  426. configurations: getSetupConfiguration({
  427. params,
  428. showExtraStep: false,
  429. showDescription: false,
  430. }),
  431. additionalInfo: crashReportCallout({
  432. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/#crash-report-modal',
  433. }),
  434. },
  435. ],
  436. verify: () => [],
  437. nextSteps: () => [],
  438. };
  439. const crashReportOnboarding: OnboardingConfig<PlatformOptions> = {
  440. introduction: () => getCrashReportModalIntroduction(),
  441. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  442. configure: () => [
  443. {
  444. type: StepType.CONFIGURE,
  445. description: getCrashReportModalConfigDescription({
  446. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/configuration/#crash-report-modal',
  447. }),
  448. additionalInfo: widgetCallout({
  449. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/#user-feedback-widget',
  450. }),
  451. },
  452. ],
  453. verify: () => [],
  454. nextSteps: () => [],
  455. };
  456. const docs: Docs<PlatformOptions> = {
  457. onboarding,
  458. platformOptions,
  459. feedbackOnboardingNpm: feedbackOnboarding,
  460. replayOnboardingNpm: replayOnboarding,
  461. crashReportOnboarding,
  462. };
  463. export default docs;