capacitor.tsx 15 KB

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