cronsLandingPanel.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {Fragment, useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import HookOrDefault from 'sentry/components/hookOrDefault';
  5. import Panel from 'sentry/components/panels/panel';
  6. import PanelBody from 'sentry/components/panels/panelBody';
  7. import {TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  8. import {IconChevron} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import {trackAnalytics} from 'sentry/utils/analytics';
  12. import {browserHistory} from 'sentry/utils/browserHistory';
  13. import {decodeScalar} from 'sentry/utils/queryString';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import useOrganization from 'sentry/utils/useOrganization';
  16. import MonitorCreateForm from 'sentry/views/monitors/components/monitorCreateForm';
  17. import type {SupportedPlatform} from './platformPickerPanel';
  18. import {CRON_SDK_PLATFORMS, PlatformPickerPanel} from './platformPickerPanel';
  19. import type {QuickStartProps} from './quickStartEntries';
  20. import {
  21. CeleryBeatAutoDiscovery,
  22. GoUpsertPlatformGuide,
  23. JavaUpsertPlatformGuide,
  24. LaravelUpsertPlatformGuide,
  25. NodeJsUpsertPlatformGuide,
  26. PHPUpsertPlatformGuide,
  27. RubyRailsMixinPlatformGuide,
  28. RubySidekiqAutoPlatformGuide,
  29. RubyUpsertPlatformGuide,
  30. } from './quickStartEntries';
  31. enum GuideKey {
  32. BEAT_AUTO = 'beat_auto',
  33. UPSERT = 'upsert',
  34. MANUAL = 'manual',
  35. MIXIN = 'mixin',
  36. SIDEKIQ_AUTO = 'sidekiq_auto',
  37. }
  38. interface PlatformGuide {
  39. Guide: React.ComponentType<QuickStartProps>;
  40. key: GuideKey;
  41. title: string;
  42. }
  43. const platformGuides: Record<SupportedPlatform, PlatformGuide[]> = {
  44. 'python-celery': [
  45. {
  46. Guide: CeleryBeatAutoDiscovery,
  47. title: 'Beat Auto Discovery',
  48. key: GuideKey.BEAT_AUTO,
  49. },
  50. ],
  51. php: [
  52. {
  53. Guide: PHPUpsertPlatformGuide,
  54. title: 'Upsert',
  55. key: GuideKey.UPSERT,
  56. },
  57. ],
  58. 'php-laravel': [
  59. {
  60. Guide: LaravelUpsertPlatformGuide,
  61. title: 'Upsert',
  62. key: GuideKey.UPSERT,
  63. },
  64. ],
  65. python: [],
  66. node: [
  67. {
  68. Guide: NodeJsUpsertPlatformGuide,
  69. title: 'Upsert',
  70. key: GuideKey.UPSERT,
  71. },
  72. ],
  73. go: [
  74. {
  75. Guide: GoUpsertPlatformGuide,
  76. title: 'Upsert',
  77. key: GuideKey.UPSERT,
  78. },
  79. ],
  80. java: [
  81. {
  82. Guide: JavaUpsertPlatformGuide,
  83. title: 'Upsert',
  84. key: GuideKey.UPSERT,
  85. },
  86. ],
  87. 'java-spring-boot': [],
  88. ruby: [
  89. {
  90. Guide: RubyUpsertPlatformGuide,
  91. title: 'Upsert',
  92. key: GuideKey.UPSERT,
  93. },
  94. ],
  95. 'ruby-rails': [
  96. {
  97. Guide: RubySidekiqAutoPlatformGuide,
  98. title: 'Sidekiq Auto Discovery',
  99. key: GuideKey.SIDEKIQ_AUTO,
  100. },
  101. {
  102. Guide: RubyRailsMixinPlatformGuide,
  103. title: 'Mixin',
  104. key: GuideKey.MIXIN,
  105. },
  106. ],
  107. };
  108. export function isValidPlatform(platform?: string | null): platform is SupportedPlatform {
  109. return !!(platform && platform in platformGuides);
  110. }
  111. export function isValidGuide(guide?: string): guide is GuideKey {
  112. return !!(guide && Object.values<string>(GuideKey).includes(guide));
  113. }
  114. export function CronsLandingPanel() {
  115. const organization = useOrganization();
  116. const location = useLocation();
  117. const platform = decodeScalar(location.query?.platform) ?? null;
  118. const guide = decodeScalar(location.query?.guide);
  119. const OnboardingPanelHook = HookOrDefault({
  120. hookName: 'component:crons-onboarding-panel',
  121. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  122. });
  123. useEffect(() => {
  124. if (!platform || !guide) {
  125. return;
  126. }
  127. trackAnalytics('landing_page.platform_guide.viewed', {
  128. organization,
  129. platform,
  130. guide,
  131. });
  132. }, [organization, platform, guide]);
  133. const navigateToPlatformGuide = (
  134. selectedPlatform: SupportedPlatform | null,
  135. selectedGuide?: string
  136. ) => {
  137. if (!selectedPlatform) {
  138. browserHistory.push({
  139. pathname: location.pathname,
  140. query: {...location.query, platform: undefined, guide: undefined},
  141. });
  142. return;
  143. }
  144. if (!selectedGuide) {
  145. selectedGuide = platformGuides[selectedPlatform][0]?.key ?? GuideKey.MANUAL;
  146. }
  147. browserHistory.push({
  148. pathname: location.pathname,
  149. query: {...location.query, platform: selectedPlatform, guide: selectedGuide},
  150. });
  151. };
  152. if (!isValidPlatform(platform) || !isValidGuide(guide)) {
  153. return (
  154. <OnboardingPanelHook>
  155. <PlatformPickerPanel onSelect={navigateToPlatformGuide} />
  156. </OnboardingPanelHook>
  157. );
  158. }
  159. const platformText = CRON_SDK_PLATFORMS.find(
  160. ({platform: sdkPlatform}) => sdkPlatform === platform
  161. )?.label;
  162. const guides = platformGuides[platform];
  163. return (
  164. <OnboardingPanelHook>
  165. <Panel>
  166. <BackButton
  167. icon={<IconChevron direction="left" />}
  168. onClick={() => navigateToPlatformGuide(null)}
  169. borderless
  170. >
  171. {t('Back to Platforms')}
  172. </BackButton>
  173. <PanelBody withPadding>
  174. <h3>{t('Get Started with %s', platformText)}</h3>
  175. <Tabs
  176. onChange={guideKey => navigateToPlatformGuide(platform, guideKey)}
  177. value={guide}
  178. >
  179. <TabList>
  180. {[
  181. ...guides.map(({key, title}) => (
  182. <TabList.Item key={key}>{title}</TabList.Item>
  183. )),
  184. <TabList.Item key={GuideKey.MANUAL}>{t('Manual')}</TabList.Item>,
  185. ]}
  186. </TabList>
  187. <TabPanels>
  188. {[
  189. ...guides.map(({key, Guide}) => (
  190. <TabPanels.Item key={key}>
  191. <GuideContainer>
  192. <Guide />
  193. </GuideContainer>
  194. </TabPanels.Item>
  195. )),
  196. <TabPanels.Item key={GuideKey.MANUAL}>
  197. <GuideContainer>
  198. <MonitorCreateForm />
  199. </GuideContainer>
  200. </TabPanels.Item>,
  201. ]}
  202. </TabPanels>
  203. </Tabs>
  204. </PanelBody>
  205. </Panel>
  206. </OnboardingPanelHook>
  207. );
  208. }
  209. const BackButton = styled(Button)`
  210. font-weight: ${p => p.theme.fontWeightNormal};
  211. color: ${p => p.theme.subText};
  212. margin: ${space(1)} 0 0 ${space(1)};
  213. padding-left: ${space(0.5)};
  214. padding-right: ${space(0.5)};
  215. `;
  216. const GuideContainer = styled('div')`
  217. display: flex;
  218. flex-direction: column;
  219. gap: ${space(2)};
  220. padding-top: ${space(2)};
  221. `;