onboarding.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import styled from '@emotion/styled';
  2. import Alert from 'sentry/components/alert';
  3. import {CodeSnippet} from 'sentry/components/codeSnippet';
  4. import ExternalLink from 'sentry/components/links/externalLink';
  5. import {useReplayContext} from 'sentry/components/replays/replayContext';
  6. import TextCopyInput from 'sentry/components/textCopyInput';
  7. import {t, tct} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {SpanFrame} from 'sentry/utils/replays/types';
  10. import useDismissAlert from 'sentry/utils/useDismissAlert';
  11. import useOrganization from 'sentry/utils/useOrganization';
  12. import useProjectSdkNeedsUpdate from 'sentry/utils/useProjectSdkNeedsUpdate';
  13. import {Output} from 'sentry/views/replays/detail/network/details/getOutputType';
  14. import type {TabKey} from 'sentry/views/replays/detail/network/details/tabs';
  15. export const useDismissReqRespBodiesAlert = () => {
  16. const organization = useOrganization();
  17. return useDismissAlert({
  18. key: `${organization.id}:replay-network-bodies-alert-dismissed`,
  19. });
  20. };
  21. export function UnsupportedOp({type}: {type: 'headers' | 'bodies'}) {
  22. const title =
  23. type === 'bodies'
  24. ? t('Capture Request and Response Bodies')
  25. : t('Capture Request and Response Headers');
  26. return (
  27. <StyledInstructions data-test-id="network-op-unsupported">
  28. <h1>{title}</h1>
  29. <p>
  30. {tct(
  31. `This feature is only compatible with [fetch] and [xhr] request types. [link].`,
  32. {
  33. fetch: <code>fetch</code>,
  34. xhr: <code>xhr</code>,
  35. link: (
  36. <ExternalLink href="https://docs.sentry.io/platforms/javascript/session-replay/configuration/#network-details">
  37. {t('Learn more')}
  38. </ExternalLink>
  39. ),
  40. }
  41. )}
  42. </p>
  43. </StyledInstructions>
  44. );
  45. }
  46. export function Setup({
  47. item,
  48. projectId,
  49. showSnippet,
  50. visibleTab,
  51. }: {
  52. item: SpanFrame;
  53. projectId: string;
  54. showSnippet: Output;
  55. visibleTab: TabKey;
  56. }) {
  57. const organization = useOrganization();
  58. const {isFetching, needsUpdate} = useProjectSdkNeedsUpdate({
  59. // Only show update instructions if not >= 7.50.0, but our instructions
  60. // will show a different min version as there are known bugs in 7.50 ->
  61. // 7.53
  62. minVersion: '7.50.0',
  63. organization,
  64. projectId: [projectId],
  65. });
  66. const sdkNeedsUpdate = !isFetching && Boolean(needsUpdate);
  67. const {replay} = useReplayContext();
  68. const isVideoReplay = replay?.isVideoReplay();
  69. const url = item.description || 'http://example.com';
  70. return isVideoReplay ? null : (
  71. <SetupInstructions
  72. minVersion="7.53.1"
  73. sdkNeedsUpdate={sdkNeedsUpdate}
  74. showSnippet={showSnippet}
  75. url={url}
  76. visibleTab={visibleTab}
  77. />
  78. );
  79. }
  80. function SetupInstructions({
  81. minVersion,
  82. sdkNeedsUpdate,
  83. showSnippet,
  84. url,
  85. visibleTab,
  86. }: {
  87. minVersion: string;
  88. sdkNeedsUpdate: boolean;
  89. showSnippet: Output;
  90. url: string;
  91. visibleTab: TabKey;
  92. }) {
  93. if (showSnippet === Output.DATA && visibleTab === 'details') {
  94. return (
  95. <NoMarginAlert type="muted" system data-test-id="network-setup-steps">
  96. {tct(
  97. 'You can capture additional headers by adding them to the [requestConfig] and [responseConfig] lists in your SDK config.',
  98. {
  99. requestConfig: <code>networkRequestHeaders</code>,
  100. responseConfig: <code>networkResponseHeaders</code>,
  101. }
  102. )}
  103. </NoMarginAlert>
  104. );
  105. }
  106. function trimUrl(oldUrl: string): string {
  107. const end = oldUrl.indexOf('?') > 0 ? oldUrl.indexOf('?') : oldUrl.length;
  108. return oldUrl.substring(0, end);
  109. }
  110. const urlSnippet = `
  111. networkDetailAllowUrls: ['${trimUrl(url)}'],`;
  112. const headersSnippet = `
  113. networkRequestHeaders: ['X-Custom-Header'],
  114. networkResponseHeaders: ['X-Custom-Header'],`;
  115. const includeHeadersSnippet =
  116. showSnippet === Output.SETUP ||
  117. ([Output.URL_SKIPPED, Output.DATA].includes(showSnippet) && visibleTab === 'details');
  118. const code = `Sentry.init({
  119. integrations: [
  120. Sentry.replayIntegration({${urlSnippet + (includeHeadersSnippet ? headersSnippet : '')}
  121. }),
  122. ],
  123. })`;
  124. const title =
  125. showSnippet === Output.SETUP
  126. ? t('Capture Request and Response Headers and Bodies')
  127. : visibleTab === 'details'
  128. ? t('Capture Request and Response Headers')
  129. : t('Capture Request and Response Bodies');
  130. return (
  131. <StyledInstructions data-test-id="network-setup-steps">
  132. <h1>{title}</h1>
  133. <p>
  134. {tct(
  135. `To protect user privacy, Session Replay defaults to not capturing the request or response headers. However, we provide the option to do so, if it’s critical to your debugging process. [link].`,
  136. {
  137. link: (
  138. <ExternalLink href="https://docs.sentry.io/platforms/javascript/session-replay/configuration/#network-details">
  139. {t('Learn More')}
  140. </ExternalLink>
  141. ),
  142. }
  143. )}
  144. </p>
  145. <NetworkUrlWrapper>
  146. {showSnippet === Output.URL_SKIPPED &&
  147. url !== '[Filtered]' &&
  148. tct(
  149. 'Add the following to your [field] list to start capturing data: [alert] ',
  150. {
  151. field: <code>networkDetailAllowUrls</code>,
  152. alert: <StyledTextCopyInput>{trimUrl(url)}</StyledTextCopyInput>,
  153. }
  154. )}
  155. </NetworkUrlWrapper>
  156. {showSnippet === Output.BODY_SKIPPED && (
  157. <Alert type="warning">
  158. {tct('Enable [field] to capture both Request and Response bodies.', {
  159. field: <code>networkCaptureBodies: true</code>,
  160. })}
  161. </Alert>
  162. )}
  163. <h1>{t('Prerequisites')}</h1>
  164. <ol>
  165. {sdkNeedsUpdate ? (
  166. <li>
  167. {tct('Update your SDK version to >= [minVersion]', {
  168. minVersion,
  169. })}
  170. </li>
  171. ) : null}
  172. <li>{t('Edit the Replay integration configuration to allow this URL.')}</li>
  173. <li>{t('That’s it!')}</li>
  174. </ol>
  175. {url !== '[Filtered]' && (
  176. <CodeSnippet filename="JavaScript" language="javascript">
  177. {code}
  178. </CodeSnippet>
  179. )}
  180. </StyledInstructions>
  181. );
  182. }
  183. const StyledTextCopyInput = styled(TextCopyInput)`
  184. margin-top: ${space(0.5)};
  185. `;
  186. const NetworkUrlWrapper = styled('div')`
  187. margin: ${space(1)} ${space(0)} ${space(1.5)} ${space(0)};
  188. `;
  189. const NoMarginAlert = styled(Alert)`
  190. margin: 0;
  191. border-width: 1px 0 0 0;
  192. `;
  193. const StyledInstructions = styled('div')`
  194. font-size: ${p => p.theme.fontSizeSmall};
  195. margin-top: ${space(1)};
  196. border-top: 1px solid ${p => p.theme.border};
  197. padding: ${space(2)};
  198. &:first-child {
  199. margin-top: 0;
  200. border-top: none;
  201. }
  202. h1 {
  203. font-size: inherit;
  204. margin-bottom: ${space(1)};
  205. }
  206. p {
  207. margin-bottom: ${space(2)};
  208. }
  209. p:last-child {
  210. margin-bottom: 0;
  211. }
  212. `;