monitorCreateForm.tsx 9.6 KB

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