monitorCreateForm.tsx 9.6 KB

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