step.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import beautify from 'js-beautify';
  4. import {CodeSnippet} from 'sentry/components/codeSnippet';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. export enum StepType {
  8. INSTALL = 'install',
  9. CONFIGURE = 'configure',
  10. VERIFY = 'verify',
  11. }
  12. export const StepTitle = {
  13. [StepType.INSTALL]: t('Install'),
  14. [StepType.CONFIGURE]: t('Configure SDK'),
  15. [StepType.VERIFY]: t('Verify'),
  16. };
  17. interface CodeSnippetTab {
  18. code: string;
  19. label: string;
  20. language: string;
  21. value: string;
  22. }
  23. interface TabbedCodeSnippetProps {
  24. /**
  25. * An array of tabs to be displayed
  26. */
  27. tabs: CodeSnippetTab[];
  28. /**
  29. * A callback to be invoked when the configuration is copied to the clipboard
  30. */
  31. onCopy?: () => void;
  32. /**
  33. * A callback to be invoked when the configuration is selected and copied to the clipboard
  34. */
  35. onSelectAndCopy?: () => void;
  36. /**
  37. * Whether or not the configuration or parts of it are currently being loaded
  38. */
  39. partialLoading?: boolean;
  40. }
  41. function TabbedCodeSnippet({
  42. tabs,
  43. onCopy,
  44. onSelectAndCopy,
  45. partialLoading,
  46. }: TabbedCodeSnippetProps) {
  47. const [selectedTabValue, setSelectedTabValue] = useState(tabs[0].value);
  48. const selectedTab = tabs.find(tab => tab.value === selectedTabValue) ?? tabs[0];
  49. const {code, language} = selectedTab;
  50. return (
  51. <CodeSnippet
  52. dark
  53. language={language}
  54. onCopy={onCopy}
  55. onSelectAndCopy={onSelectAndCopy}
  56. hideCopyButton={partialLoading}
  57. disableUserSelection={partialLoading}
  58. tabs={tabs}
  59. selectedTab={selectedTabValue}
  60. onTabClick={value => setSelectedTabValue(value)}
  61. >
  62. {language === 'javascript'
  63. ? beautify.js(code, {
  64. indent_size: 2,
  65. e4x: true,
  66. brace_style: 'preserve-inline',
  67. })
  68. : code.trim()}
  69. </CodeSnippet>
  70. );
  71. }
  72. type ConfigurationType = {
  73. /**
  74. * Additional information to be displayed below the code snippet
  75. */
  76. additionalInfo?: React.ReactNode;
  77. /**
  78. * The code snippet to display
  79. */
  80. code?: string | CodeSnippetTab[];
  81. /**
  82. * Nested configurations provide a convenient way to accommodate diverse layout styles, like the Spring Boot configuration.
  83. */
  84. configurations?: ConfigurationType[];
  85. /**
  86. * A brief description of the configuration
  87. */
  88. description?: React.ReactNode;
  89. /**
  90. * The language of the code to be rendered (python, javascript, etc)
  91. */
  92. language?: string;
  93. /**
  94. * A callback to be invoked when the configuration is copied to the clipboard
  95. */
  96. onCopy?: () => void;
  97. /**
  98. * A callback to be invoked when the configuration is selected and copied to the clipboard
  99. */
  100. onSelectAndCopy?: () => void;
  101. /**
  102. * Whether or not the configuration or parts of it are currently being loaded
  103. */
  104. partialLoading?: boolean;
  105. };
  106. interface BaseStepProps {
  107. /**
  108. * Additional information to be displayed below the configurations
  109. */
  110. additionalInfo?: React.ReactNode;
  111. configurations?: ConfigurationType[];
  112. /**
  113. * A brief description of the step
  114. */
  115. description?: React.ReactNode;
  116. }
  117. interface StepPropsWithTitle extends BaseStepProps {
  118. title: string;
  119. type?: undefined;
  120. }
  121. interface StepPropsWithoutTitle extends BaseStepProps {
  122. type: StepType;
  123. title?: undefined;
  124. }
  125. export type StepProps = StepPropsWithTitle | StepPropsWithoutTitle;
  126. function getConfiguration({
  127. description,
  128. code,
  129. language,
  130. additionalInfo,
  131. onCopy,
  132. onSelectAndCopy,
  133. partialLoading,
  134. }: ConfigurationType) {
  135. return (
  136. <Configuration>
  137. {description && <Description>{description}</Description>}
  138. {Array.isArray(code) ? (
  139. <TabbedCodeSnippet
  140. tabs={code}
  141. onCopy={onCopy}
  142. onSelectAndCopy={onSelectAndCopy}
  143. partialLoading={partialLoading}
  144. />
  145. ) : (
  146. language &&
  147. code && (
  148. <CodeSnippet
  149. dark
  150. language={language}
  151. onCopy={onCopy}
  152. onSelectAndCopy={onSelectAndCopy}
  153. hideCopyButton={partialLoading}
  154. disableUserSelection={partialLoading}
  155. >
  156. {language === 'javascript'
  157. ? beautify.js(code, {
  158. indent_size: 2,
  159. e4x: true,
  160. brace_style: 'preserve-inline',
  161. })
  162. : code.trim()}
  163. </CodeSnippet>
  164. )
  165. )}
  166. {additionalInfo && <AdditionalInfo>{additionalInfo}</AdditionalInfo>}
  167. </Configuration>
  168. );
  169. }
  170. export function Step({
  171. title,
  172. type,
  173. configurations,
  174. additionalInfo,
  175. description,
  176. }: StepProps) {
  177. return (
  178. <div>
  179. <h4>{title ?? StepTitle[type]}</h4>
  180. {description && <Description>{description}</Description>}
  181. {!!configurations?.length && (
  182. <Configurations>
  183. {configurations.map((configuration, index) => {
  184. if (configuration.configurations) {
  185. return (
  186. <Fragment key={index}>
  187. {getConfiguration(configuration)}
  188. {configuration.configurations.map(
  189. (nestedConfiguration, nestedConfigurationIndex) => (
  190. <Fragment key={nestedConfigurationIndex}>
  191. {getConfiguration(nestedConfiguration)}
  192. </Fragment>
  193. )
  194. )}
  195. </Fragment>
  196. );
  197. }
  198. return <Fragment key={index}>{getConfiguration(configuration)}</Fragment>;
  199. })}
  200. </Configurations>
  201. )}
  202. {additionalInfo && <GeneralAdditionalInfo>{additionalInfo}</GeneralAdditionalInfo>}
  203. </div>
  204. );
  205. }
  206. const Configuration = styled('div')`
  207. display: flex;
  208. flex-direction: column;
  209. gap: 1rem;
  210. `;
  211. const Configurations = styled(Configuration)`
  212. margin-top: ${space(2)};
  213. `;
  214. const Description = styled(Configuration)`
  215. code {
  216. color: ${p => p.theme.pink400};
  217. }
  218. `;
  219. const AdditionalInfo = styled(Description)``;
  220. const GeneralAdditionalInfo = styled(Description)`
  221. margin-top: ${space(2)};
  222. `;