cronsLandingPanel.tsx 6.2 KB

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