cronsLandingPanel.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. RubyUpsertPlatformGuide,
  32. } from './quickStartEntries';
  33. enum GuideKey {
  34. BEAT_AUTO = 'beat_auto',
  35. UPSERT = 'upsert',
  36. MANUAL = 'manual',
  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. export function isValidPlatform(platform?: string | null): platform is SupportedPlatform {
  98. return !!(platform && platform in platformGuides);
  99. }
  100. export function isValidGuide(guide?: string): guide is GuideKey {
  101. return !!(guide && Object.values<string>(GuideKey).includes(guide));
  102. }
  103. export function CronsLandingPanel() {
  104. const organization = useOrganization();
  105. const location = useLocation();
  106. const platform = decodeScalar(location.query?.platform) ?? null;
  107. const guide = decodeScalar(location.query?.guide);
  108. useEffect(() => {
  109. if (!platform || !guide) {
  110. return;
  111. }
  112. trackAnalytics('landing_page.platform_guide.viewed', {
  113. organization,
  114. platform,
  115. guide,
  116. });
  117. }, [organization, platform, guide]);
  118. const navigateToPlatformGuide = (
  119. selectedPlatform: SupportedPlatform | null,
  120. selectedGuide?: string
  121. ) => {
  122. if (!selectedPlatform) {
  123. browserHistory.push({
  124. pathname: location.pathname,
  125. query: {...location.query, platform: undefined, guide: undefined},
  126. });
  127. return;
  128. }
  129. if (!selectedGuide) {
  130. selectedGuide = platformGuides[selectedPlatform][0]?.key ?? GuideKey.MANUAL;
  131. }
  132. browserHistory.push({
  133. pathname: location.pathname,
  134. query: {...location.query, platform: selectedPlatform, guide: selectedGuide},
  135. });
  136. };
  137. if (!isValidPlatform(platform) || !isValidGuide(guide)) {
  138. return <PlatformPickerPanel onSelect={navigateToPlatformGuide} />;
  139. }
  140. const platformText = CRON_SDK_PLATFORMS.find(
  141. ({platform: sdkPlatform}) => sdkPlatform === platform
  142. )?.label;
  143. const guides = platformGuides[platform];
  144. function onCreateMonitor(data: Monitor) {
  145. const url = normalizeUrl(`/organizations/${organization.slug}/crons/${data.slug}/`);
  146. browserHistory.push(url);
  147. }
  148. return (
  149. <Panel>
  150. <BackButton
  151. icon={<IconChevron size="sm" direction="left" />}
  152. onClick={() => navigateToPlatformGuide(null)}
  153. borderless
  154. >
  155. {t('Back to Platforms')}
  156. </BackButton>
  157. <PanelBody withPadding>
  158. <h3>{t('Get Started with %s', platformText)}</h3>
  159. <Tabs
  160. onChange={guideKey => navigateToPlatformGuide(platform, guideKey)}
  161. value={guide}
  162. >
  163. <TabList>
  164. {[
  165. ...guides.map(({key, title}) => (
  166. <TabList.Item key={key}>{title}</TabList.Item>
  167. )),
  168. <TabList.Item key={GuideKey.MANUAL}>{t('Manual')}</TabList.Item>,
  169. ]}
  170. </TabList>
  171. <TabPanels>
  172. {[
  173. ...guides.map(({key, Guide}) => (
  174. <TabPanels.Item key={key}>
  175. <GuideContainer>
  176. <Guide />
  177. </GuideContainer>
  178. </TabPanels.Item>
  179. )),
  180. <TabPanels.Item key={GuideKey.MANUAL}>
  181. <GuideContainer>
  182. <MonitorForm
  183. apiMethod="POST"
  184. apiEndpoint={`/organizations/${organization.slug}/monitors/`}
  185. onSubmitSuccess={onCreateMonitor}
  186. submitLabel={t('Next')}
  187. />
  188. </GuideContainer>
  189. </TabPanels.Item>,
  190. ]}
  191. </TabPanels>
  192. </Tabs>
  193. </PanelBody>
  194. </Panel>
  195. );
  196. }
  197. const BackButton = styled(Button)`
  198. font-weight: normal;
  199. color: ${p => p.theme.subText};
  200. margin: ${space(1)} 0 0 ${space(1)};
  201. padding-left: ${space(0.5)};
  202. padding-right: ${space(0.5)};
  203. `;
  204. const GuideContainer = styled('div')`
  205. display: flex;
  206. flex-direction: column;
  207. gap: ${space(2)};
  208. padding-top: ${space(2)};
  209. `;