monitorCreateForm.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import {Fragment, useRef} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {css} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import {Observer} from 'mobx-react';
  6. import NumberField from 'sentry/components/forms/fields/numberField';
  7. import SelectField from 'sentry/components/forms/fields/selectField';
  8. import SentryProjectSelectorField from 'sentry/components/forms/fields/sentryProjectSelectorField';
  9. import TextField from 'sentry/components/forms/fields/textField';
  10. import Form from 'sentry/components/forms/form';
  11. import FormModel from 'sentry/components/forms/model';
  12. import Panel from 'sentry/components/panels/panel';
  13. import PanelBody from 'sentry/components/panels/panelBody';
  14. import {timezoneOptions} from 'sentry/data/timezones';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
  18. import commonTheme from 'sentry/utils/theme';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import usePageFilters from 'sentry/utils/usePageFilters';
  21. import useProjects from 'sentry/utils/useProjects';
  22. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  23. import {MockTimelineVisualization} from 'sentry/views/monitors/components/mockTimelineVisualization';
  24. import {
  25. DEFAULT_CRONTAB,
  26. DEFAULT_MONITOR_TYPE,
  27. mapMonitorFormErrors,
  28. transformMonitorFormData,
  29. } from 'sentry/views/monitors/components/monitorForm';
  30. import {Monitor, ScheduleType} from 'sentry/views/monitors/types';
  31. import {crontabAsText, getScheduleIntervals} from 'sentry/views/monitors/utils';
  32. const DEFAULT_SCHEDULE_CONFIG = {
  33. scheduleType: 'crontab',
  34. cronSchedule: DEFAULT_CRONTAB,
  35. intervalFrequency: '1',
  36. intervalUnit: 'day',
  37. };
  38. export default function MonitorCreateForm() {
  39. const organization = useOrganization();
  40. const {projects} = useProjects();
  41. const {selection} = usePageFilters();
  42. const form = useRef(
  43. new FormModel({
  44. transformData: transformMonitorFormData,
  45. mapFormErrors: mapMonitorFormErrors,
  46. })
  47. );
  48. const selectedProjectId = selection.projects[0];
  49. const selectedProject = selectedProjectId
  50. ? projects.find(p => p.id === selectedProjectId + '')
  51. : null;
  52. const isSuperuser = isActiveSuperuser();
  53. const filteredProjects = projects.filter(project => isSuperuser || project.isMember);
  54. function onCreateMonitor(data: Monitor) {
  55. const endpointOptions = {
  56. query: {
  57. project: selection.projects,
  58. environment: selection.environments,
  59. },
  60. };
  61. browserHistory.push(
  62. normalizeUrl({
  63. pathname: `/organizations/${organization.slug}/crons/${data.slug}/`,
  64. query: endpointOptions.query,
  65. })
  66. );
  67. }
  68. function changeScheduleType(type: ScheduleType) {
  69. form.current.setValue('config.schedule_type', type);
  70. }
  71. return (
  72. <Form
  73. allowUndo
  74. requireChanges
  75. apiEndpoint={`/organizations/${organization.slug}/monitors/`}
  76. apiMethod="POST"
  77. model={form.current}
  78. initialData={{
  79. project: selectedProject ? selectedProject.slug : null,
  80. type: DEFAULT_MONITOR_TYPE,
  81. 'config.schedule_type': DEFAULT_SCHEDULE_CONFIG.scheduleType,
  82. }}
  83. onSubmitSuccess={onCreateMonitor}
  84. submitLabel={t('Next')}
  85. >
  86. <FieldContainer>
  87. <MultiColumnInput columns="250px 1fr">
  88. <StyledSentryProjectSelectorField
  89. name="project"
  90. projects={filteredProjects}
  91. placeholder={t('Choose Project')}
  92. disabledReason={t('Existing monitors cannot be moved between projects')}
  93. valueIsSlug
  94. required
  95. stacked
  96. inline={false}
  97. />
  98. <StyledTextField
  99. name="name"
  100. placeholder={t('My Cron Job')}
  101. required
  102. stacked
  103. inline={false}
  104. />
  105. </MultiColumnInput>
  106. <LabelText>{t('SCHEDULE')}</LabelText>
  107. <ScheduleOptions>
  108. <Observer>
  109. {() => {
  110. const currScheduleType = form.current.getValue('config.schedule_type');
  111. const selectedCrontab = currScheduleType === ScheduleType.CRONTAB;
  112. const parsedSchedule = form.current.getError('config.schedule')
  113. ? ''
  114. : crontabAsText(
  115. form.current.getValue('config.schedule')?.toString() ?? ''
  116. );
  117. return (
  118. <Fragment>
  119. <SchedulePanel
  120. highlighted={selectedCrontab}
  121. onClick={() => changeScheduleType(ScheduleType.CRONTAB)}
  122. >
  123. <PanelBody withPadding>
  124. <ScheduleLabel>{t('Crontab Schedule')}</ScheduleLabel>
  125. <MultiColumnInput columns="1fr 1fr">
  126. <StyledTextField
  127. name="config.schedule"
  128. placeholder="* * * * *"
  129. defaultValue={DEFAULT_SCHEDULE_CONFIG.cronSchedule}
  130. css={{input: {fontFamily: commonTheme.text.familyMono}}}
  131. required={selectedCrontab}
  132. stacked
  133. inline={false}
  134. hideControlState={!selectedCrontab}
  135. />
  136. <StyledSelectField
  137. name="config.timezone"
  138. defaultValue="UTC"
  139. options={timezoneOptions}
  140. required={selectedCrontab}
  141. stacked
  142. inline={false}
  143. />
  144. <CronstrueText>{parsedSchedule}</CronstrueText>
  145. </MultiColumnInput>
  146. </PanelBody>
  147. </SchedulePanel>
  148. <SchedulePanel
  149. highlighted={!selectedCrontab}
  150. onClick={() => changeScheduleType(ScheduleType.INTERVAL)}
  151. >
  152. <PanelBody withPadding>
  153. <ScheduleLabel>{t('Interval Schedule')}</ScheduleLabel>
  154. <MultiColumnInput columns="auto 1fr 2fr">
  155. <Label>{t('Every')}</Label>
  156. <StyledNumberField
  157. name="config.schedule.frequency"
  158. placeholder="e.g. 1"
  159. defaultValue={DEFAULT_SCHEDULE_CONFIG.intervalFrequency}
  160. required={!selectedCrontab}
  161. stacked
  162. inline={false}
  163. hideControlState={selectedCrontab}
  164. />
  165. <StyledSelectField
  166. name="config.schedule.interval"
  167. options={getScheduleIntervals(
  168. Number(
  169. form.current.getValue('config.schedule.frequency') ?? 1
  170. )
  171. )}
  172. defaultValue={DEFAULT_SCHEDULE_CONFIG.intervalUnit}
  173. required={!selectedCrontab}
  174. stacked
  175. inline={false}
  176. />
  177. </MultiColumnInput>
  178. </PanelBody>
  179. </SchedulePanel>
  180. </Fragment>
  181. );
  182. }}
  183. </Observer>
  184. </ScheduleOptions>
  185. <Observer>
  186. {() => {
  187. const scheduleType = form.current.getValue('config.schedule_type');
  188. const cronSchedule = form.current.getValue('config.schedule');
  189. const intervalFrequency = form.current.getValue('config.schedule.frequency');
  190. const intervalUnit = form.current.getValue('config.schedule.interval');
  191. const schedule = {
  192. scheduleType,
  193. cronSchedule,
  194. intervalFrequency,
  195. intervalUnit,
  196. };
  197. return <MockTimelineVisualization schedule={schedule} />;
  198. }}
  199. </Observer>
  200. </FieldContainer>
  201. </Form>
  202. );
  203. }
  204. const FieldContainer = styled('div')`
  205. width: 800px;
  206. `;
  207. const SchedulePanel = styled(Panel)<{highlighted: boolean}>`
  208. border-radius: 0 ${space(0.75)} ${space(0.75)} 0;
  209. ${p =>
  210. p.highlighted
  211. ? css`
  212. border: 2px solid ${p.theme.purple300};
  213. `
  214. : css`
  215. padding: 1px;
  216. `};
  217. &:first-child {
  218. border-radius: ${space(0.75)} 0 0 ${space(0.75)};
  219. }
  220. `;
  221. const ScheduleLabel = styled('div')`
  222. font-weight: bold;
  223. margin-bottom: ${space(2)};
  224. `;
  225. const Label = styled('div')`
  226. font-weight: bold;
  227. color: ${p => p.theme.subText};
  228. `;
  229. const LabelText = styled(Label)`
  230. margin-top: ${space(2)};
  231. margin-bottom: ${space(1)};
  232. `;
  233. const ScheduleOptions = styled('div')`
  234. display: grid;
  235. grid-template-columns: 1fr 1fr;
  236. `;
  237. const MultiColumnInput = styled('div')<{columns?: string}>`
  238. display: grid;
  239. align-items: center;
  240. gap: ${space(1)};
  241. grid-template-columns: ${p => p.columns};
  242. `;
  243. const CronstrueText = styled(LabelText)`
  244. font-weight: normal;
  245. font-size: ${p => p.theme.fontSizeExtraSmall};
  246. font-family: ${p => p.theme.text.familyMono};
  247. grid-column: auto / span 2;
  248. `;
  249. const StyledNumberField = styled(NumberField)`
  250. padding: 0;
  251. `;
  252. const StyledSelectField = styled(SelectField)`
  253. padding: 0;
  254. `;
  255. const StyledTextField = styled(TextField)`
  256. padding: 0;
  257. `;
  258. const StyledSentryProjectSelectorField = styled(SentryProjectSelectorField)`
  259. padding: 0;
  260. `;