monitorQuickStartGuide.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import partition from 'lodash/partition';
  4. import {CompactSelect} from 'sentry/components/compactSelect';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import type {PlatformKey} from 'sentry/types';
  8. import {ProjectKey} from 'sentry/types';
  9. import {useApiQuery} from 'sentry/utils/queryClient';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import {
  12. CLICronQuickStart,
  13. CurlCronQuickStart,
  14. GoCronQuickStart,
  15. NodeJSCronQuickStart,
  16. PHPCronQuickStart,
  17. PHPLaravelCronQuickStart,
  18. PythonCeleryCronQuickStart,
  19. PythonCronQuickStart,
  20. QuickStartProps,
  21. } from 'sentry/views/monitors/components/quickStartEntries';
  22. import {Monitor} from '../types';
  23. interface Props {
  24. monitor: Monitor;
  25. }
  26. interface OnboardingGuide {
  27. Guide: React.ComponentType<QuickStartProps>;
  28. label: string;
  29. platforms?: Set<PlatformKey>;
  30. }
  31. const onboardingGuides: Record<string, OnboardingGuide> = {
  32. cli: {
  33. label: 'Sentry CLI',
  34. Guide: CLICronQuickStart,
  35. },
  36. curl: {
  37. label: 'cURL',
  38. Guide: CurlCronQuickStart,
  39. },
  40. python: {
  41. label: 'Python',
  42. Guide: PythonCronQuickStart,
  43. platforms: new Set([
  44. 'python',
  45. 'python-django',
  46. 'python-flask',
  47. 'python-fastapi',
  48. 'python-starlette',
  49. 'python-sanic',
  50. 'python-bottle',
  51. 'python-pylons',
  52. 'python-pyramid',
  53. 'python-tornado',
  54. 'python-rq',
  55. ]),
  56. },
  57. pythonCelery: {
  58. label: 'Celery',
  59. Guide: PythonCeleryCronQuickStart,
  60. platforms: new Set(['python-celery']),
  61. },
  62. php: {
  63. label: 'PHP',
  64. Guide: PHPCronQuickStart,
  65. platforms: new Set(['php', 'php-monolog', 'php-symfony']),
  66. },
  67. phpLaravel: {
  68. label: 'Laravel',
  69. Guide: PHPLaravelCronQuickStart,
  70. platforms: new Set(['php-laravel']),
  71. },
  72. nodeJs: {
  73. label: 'Node',
  74. Guide: NodeJSCronQuickStart,
  75. platforms: new Set(['node']),
  76. },
  77. go: {
  78. label: 'Go',
  79. Guide: GoCronQuickStart,
  80. platforms: new Set(['go']),
  81. },
  82. };
  83. const guideToSelectOption = ({key, label}) => ({label, value: key});
  84. export default function MonitorQuickStartGuide({monitor}: Props) {
  85. const org = useOrganization();
  86. const {data: projectKeys} = useApiQuery<Array<ProjectKey>>(
  87. [`/projects/${org.slug}/${monitor.project.slug}/keys/`],
  88. {staleTime: Infinity}
  89. );
  90. const guideList = Object.entries(onboardingGuides).map(([key, guide]) => ({
  91. ...guide,
  92. key,
  93. }));
  94. const [genericGuides, platformGuides] = partition(
  95. guideList,
  96. guide => guide.platforms === undefined
  97. );
  98. const exampleOptions = [
  99. {label: t('Platform Specfiic'), options: platformGuides.map(guideToSelectOption)},
  100. {label: t('Generic'), options: genericGuides.map(guideToSelectOption)},
  101. ];
  102. const platformSpecific = platformGuides.filter(
  103. guide => guide.platforms?.has(monitor.project.platform ?? 'other')
  104. );
  105. const defaultExample = platformSpecific.length > 0 ? platformSpecific[0].key : 'cli';
  106. const [selectedGuide, setSelectedGuide] = useState(defaultExample);
  107. const {Guide} = onboardingGuides[selectedGuide];
  108. return (
  109. <Container>
  110. <CompactSelect
  111. options={exampleOptions}
  112. value={selectedGuide}
  113. onChange={({value}) => setSelectedGuide(value)}
  114. />
  115. <Guide
  116. slug={monitor.slug}
  117. orgSlug={org.slug}
  118. orgId={org.id}
  119. projectId={monitor.project.id}
  120. publicKey={projectKeys?.[0].public}
  121. dsnKey={projectKeys?.[0].dsn.public}
  122. />
  123. </Container>
  124. );
  125. }
  126. const Container = styled('div')`
  127. display: flex;
  128. flex-direction: column;
  129. gap: ${space(2)};
  130. `;