CronJobScheduleSection.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { motion } from 'framer-motion'
  2. import { useEffect, useState } from 'react'
  3. import { UseFormReturn } from 'react-hook-form'
  4. import {
  5. Accordion,
  6. AccordionContent,
  7. AccordionItem,
  8. AccordionTrigger,
  9. Button,
  10. cn,
  11. FormControl,
  12. FormField,
  13. FormItem,
  14. FormLabel,
  15. FormMessage,
  16. Input,
  17. SheetSection,
  18. Switch,
  19. } from 'ui'
  20. import { useDebounce } from 'use-debounce'
  21. import { formatScheduleString, getScheduleMessage } from '../CronJobs.utils'
  22. import CronSyntaxChart from '../CronSyntaxChart'
  23. import { type CreateCronJobForm } from './CreateCronJobSheet.constants'
  24. import { useSqlCronGenerateMutation } from '@/data/ai/sql-cron-mutation'
  25. import { useCronTimezoneQuery } from '@/data/database-cron-jobs/database-cron-timezone-query'
  26. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  27. interface CronJobScheduleSectionProps {
  28. form: UseFormReturn<CreateCronJobForm>
  29. supportsSeconds: boolean
  30. }
  31. export const CronJobScheduleSection = ({ form, supportsSeconds }: CronJobScheduleSectionProps) => {
  32. const { data: project } = useSelectedProjectQuery()
  33. const [inputValue, setInputValue] = useState('')
  34. const [debouncedValue] = useDebounce(inputValue, 750)
  35. const [useNaturalLanguage, setUseNaturalLanguage] = useState(false)
  36. const PRESETS = [
  37. ...(supportsSeconds ? [{ name: 'Every 30 seconds', expression: '30 seconds' }] : []),
  38. { name: 'Every minute', expression: '* * * * *' },
  39. { name: 'Every 5 minutes', expression: '*/5 * * * *' },
  40. { name: 'Every first of the month, at 00:00', expression: '0 0 1 * *' },
  41. { name: 'Every night at midnight', expression: '0 0 * * *' },
  42. { name: 'Every Monday at 2 AM', expression: '0 2 * * 1' },
  43. ] as const
  44. const { mutate: generateCronSyntax, isPending: isGeneratingCron } = useSqlCronGenerateMutation({
  45. onSuccess: (expression) => {
  46. form.setValue('schedule', expression, {
  47. shouldValidate: true,
  48. shouldDirty: true,
  49. shouldTouch: true,
  50. })
  51. },
  52. })
  53. const { data: timezone } = useCronTimezoneQuery({
  54. projectRef: project?.ref,
  55. connectionString: project?.connectionString,
  56. })
  57. useEffect(() => {
  58. if (useNaturalLanguage && debouncedValue) {
  59. generateCronSyntax({ prompt: debouncedValue })
  60. }
  61. // eslint-disable-next-line react-hooks/exhaustive-deps
  62. }, [debouncedValue, useNaturalLanguage])
  63. const schedule = form.watch('schedule')
  64. const scheduleString = formatScheduleString(schedule)
  65. return (
  66. <SheetSection>
  67. <FormField
  68. control={form.control}
  69. name="schedule"
  70. render={({ field }) => {
  71. return (
  72. <FormItem className="flex flex-col gap-1">
  73. <div className="flex flex-row justify-between">
  74. <FormLabel>Schedule</FormLabel>
  75. <span className="text-foreground-lighter text-xs">
  76. {useNaturalLanguage
  77. ? 'Describe your schedule in words'
  78. : 'Enter a cron expression'}
  79. </span>
  80. </div>
  81. <FormControl>
  82. {useNaturalLanguage ? (
  83. <Input
  84. value={inputValue}
  85. placeholder="E.g. every 5 minutes"
  86. onKeyDown={(e) => {
  87. if (e.key === 'Enter') {
  88. e.preventDefault()
  89. }
  90. }}
  91. onChange={(e) => setInputValue(e.target.value)}
  92. />
  93. ) : (
  94. <Input
  95. {...field}
  96. autoComplete="off"
  97. placeholder="* * * * *"
  98. onKeyDown={(e) => {
  99. if (e.key === 'Enter') {
  100. e.preventDefault()
  101. }
  102. }}
  103. />
  104. )}
  105. </FormControl>
  106. <FormMessage />
  107. <div className="flex flex-col gap-y-4 mt-3 mb-2">
  108. <div className="flex items-center gap-2">
  109. <Switch
  110. checked={useNaturalLanguage}
  111. onCheckedChange={() => {
  112. setUseNaturalLanguage(!useNaturalLanguage)
  113. setInputValue('')
  114. }}
  115. />
  116. <p className="text-sm text-foreground-light">Use natural language</p>
  117. </div>
  118. <ul className="flex gap-2 flex-wrap mt-2">
  119. {PRESETS.map((preset) => (
  120. <li key={preset.name}>
  121. <Button
  122. type="outline"
  123. onClick={() => {
  124. if (useNaturalLanguage) {
  125. setUseNaturalLanguage(false)
  126. }
  127. form.setValue('schedule', preset.expression, {
  128. shouldValidate: true,
  129. shouldDirty: true,
  130. shouldTouch: true,
  131. })
  132. }}
  133. >
  134. {preset.name}
  135. </Button>
  136. </li>
  137. ))}
  138. </ul>
  139. <Accordion type="single" collapsible>
  140. <AccordionItem value="item-1" className="border-none">
  141. <AccordionTrigger className="text-xs text-foreground-light font-normal gap-2 justify-start py-1 ">
  142. View syntax chart
  143. </AccordionTrigger>
  144. <AccordionContent asChild className="pb-0!">
  145. <CronSyntaxChart />
  146. </AccordionContent>
  147. </AccordionItem>
  148. </Accordion>
  149. </div>
  150. <div className="bg-surface-100 p-4 rounded-sm grid gap-y-4 border">
  151. <h4 className="text-sm text-foreground">
  152. Schedule {timezone ? `(${timezone})` : ''}
  153. </h4>
  154. <span
  155. className={cn(
  156. 'text-xl font-mono',
  157. scheduleString
  158. ? isGeneratingCron
  159. ? 'animate-pulse text-foreground-lighter'
  160. : 'text-foreground'
  161. : 'text-foreground-lighter'
  162. )}
  163. >
  164. {isGeneratingCron ? <CronSyntaxLoader /> : schedule || '* * * * * *'}
  165. </span>
  166. {!inputValue && !isGeneratingCron && !scheduleString ? (
  167. <span className="text-sm text-foreground-light">
  168. Describe your schedule above
  169. </span>
  170. ) : (
  171. <span className="text-sm text-foreground-light flex items-center gap-2">
  172. {isGeneratingCron ? <LoadingDots /> : getScheduleMessage(scheduleString)}
  173. </span>
  174. )}
  175. </div>
  176. </FormItem>
  177. )
  178. }}
  179. />
  180. </SheetSection>
  181. )
  182. }
  183. const CronSyntaxLoader = () => {
  184. return (
  185. <div className="flex gap-2">
  186. {['*', '*', '*', '*', '*'].map((char, i) => (
  187. <motion.span
  188. key={i}
  189. initial={{ opacity: 0.3 }}
  190. animate={{ opacity: 1 }}
  191. transition={{
  192. duration: 0.6,
  193. repeat: Infinity,
  194. repeatType: 'reverse',
  195. delay: i * 0.15,
  196. }}
  197. >
  198. {char}
  199. </motion.span>
  200. ))}
  201. </div>
  202. )
  203. }
  204. const LoadingDots = () => {
  205. return (
  206. <span className="inline-flex items-center">
  207. {[0, 1, 2].map((i) => (
  208. <motion.span
  209. key={i}
  210. initial={{ opacity: 0 }}
  211. animate={{ opacity: 1 }}
  212. transition={{
  213. duration: 0.5,
  214. repeat: Infinity,
  215. repeatType: 'reverse',
  216. delay: i * 0.2,
  217. }}
  218. >
  219. .
  220. </motion.span>
  221. ))}
  222. </span>
  223. )
  224. }