cronsLandingPanel.tsx 5.8 KB

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