Home / Function/ CronJobScheduleSection() — supabase Function Reference

CronJobScheduleSection() — supabase Function Reference

Architecture documentation for the CronJobScheduleSection() function in CronJobScheduleSection.tsx from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  449db5e2_4110_a6b4_0fbd_4a79211a5b8f["CronJobScheduleSection()"]
  922453d5_7a37_7b75_101a_ce1bb8b9b79f["formatScheduleString()"]
  449db5e2_4110_a6b4_0fbd_4a79211a5b8f -->|calls| 922453d5_7a37_7b75_101a_ce1bb8b9b79f
  db635c30_5fb4_0763_08b7_100d0df1469e["getScheduleMessage()"]
  449db5e2_4110_a6b4_0fbd_4a79211a5b8f -->|calls| db635c30_5fb4_0763_08b7_100d0df1469e
  style 449db5e2_4110_a6b4_0fbd_4a79211a5b8f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet/CronJobScheduleSection.tsx lines 35–196

export const CronJobScheduleSection = ({ form, supportsSeconds }: CronJobScheduleSectionProps) => {
  const { data: project } = useSelectedProjectQuery()

  const [inputValue, setInputValue] = useState('')
  const [debouncedValue] = useDebounce(inputValue, 750)
  const [useNaturalLanguage, setUseNaturalLanguage] = useState(false)

  const PRESETS = [
    ...(supportsSeconds ? [{ name: 'Every 30 seconds', expression: '30 seconds' }] : []),
    { name: 'Every minute', expression: '* * * * *' },
    { name: 'Every 5 minutes', expression: '*/5 * * * *' },
    { name: 'Every first of the month, at 00:00', expression: '0 0 1 * *' },
    { name: 'Every night at midnight', expression: '0 0 * * *' },
    { name: 'Every Monday at 2 AM', expression: '0 2 * * 1' },
  ] as const

  const { mutate: generateCronSyntax, isPending: isGeneratingCron } = useSqlCronGenerateMutation({
    onSuccess: (expression) => {
      form.setValue('schedule', expression, {
        shouldValidate: true,
        shouldDirty: true,
        shouldTouch: true,
      })
    },
  })

  const { data: timezone } = useCronTimezoneQuery({
    projectRef: project?.ref,
    connectionString: project?.connectionString,
  })

  useEffect(() => {
    if (useNaturalLanguage && debouncedValue) {
      generateCronSyntax({ prompt: debouncedValue })
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [debouncedValue, useNaturalLanguage])

  const schedule = form.watch('schedule')
  const scheduleString = formatScheduleString(schedule)

  return (
    <SheetSection>
      <FormField_Shadcn_
        control={form.control}
        name="schedule"
        render={({ field }) => {
          return (
            <FormItem_Shadcn_ className="flex flex-col gap-1">
              <div className="flex flex-row justify-between">
                <FormLabel_Shadcn_>Schedule</FormLabel_Shadcn_>
                <span className="text-foreground-lighter text-xs">
                  {useNaturalLanguage
                    ? 'Describe your schedule in words'
                    : 'Enter a cron expression'}
                </span>
              </div>

              <FormControl_Shadcn_>
                {useNaturalLanguage ? (
                  <Input
                    value={inputValue}
                    placeholder="E.g. every 5 minutes"
                    onKeyDown={(e) => {
                      if (e.key === 'Enter') {
                        e.preventDefault()
                      }
                    }}
                    onChange={(e) => setInputValue(e.target.value)}
                  />
                ) : (
                  <Input_Shadcn_
                    {...field}
                    autoComplete="off"
                    placeholder="* * * * *"
                    onKeyDown={(e) => {
                      if (e.key === 'Enter') {
                        e.preventDefault()
                      }
                    }}
                  />
                )}
              </FormControl_Shadcn_>
              <FormMessage_Shadcn_ />
              <div className="flex flex-col gap-y-4 mt-3 mb-2">
                <div className="flex items-center gap-2">
                  <Switch
                    checked={useNaturalLanguage}
                    onCheckedChange={() => {
                      setUseNaturalLanguage(!useNaturalLanguage)
                      setInputValue('')
                    }}
                  />
                  <p className="text-sm text-foreground-light">Use natural language</p>
                </div>

                <ul className="flex gap-2 flex-wrap mt-2">
                  {PRESETS.map((preset) => (
                    <li key={preset.name}>
                      <Button
                        type="outline"
                        onClick={() => {
                          if (useNaturalLanguage) {
                            setUseNaturalLanguage(false)
                          }
                          form.setValue('schedule', preset.expression, {
                            shouldValidate: true,
                            shouldDirty: true,
                            shouldTouch: true,
                          })
                        }}
                      >
                        {preset.name}
                      </Button>
                    </li>
                  ))}
                </ul>
                <Accordion_Shadcn_ type="single" collapsible>
                  <AccordionItem_Shadcn_ value="item-1" className="border-none">
                    <AccordionTrigger_Shadcn_ className="text-xs text-foreground-light font-normal gap-2 justify-start py-1 ">
                      View syntax chart
                    </AccordionTrigger_Shadcn_>
                    <AccordionContent_Shadcn_ asChild className="!pb-0">
                      <CronSyntaxChart />
                    </AccordionContent_Shadcn_>
                  </AccordionItem_Shadcn_>
                </Accordion_Shadcn_>
              </div>
              <div className="bg-surface-100 p-4 rounded grid gap-y-4 border">
                <h4 className="text-sm text-foreground">
                  Schedule {timezone ? `(${timezone})` : ''}
                </h4>
                <span
                  className={cn(
                    'text-xl font-mono',
                    scheduleString
                      ? isGeneratingCron
                        ? 'animate-pulse text-foreground-lighter'
                        : 'text-foreground'
                      : 'text-foreground-lighter'
                  )}
                >
                  {isGeneratingCron ? <CronSyntaxLoader /> : schedule || '* * * * * *'}
                </span>

                {!inputValue && !isGeneratingCron && !scheduleString ? (
                  <span className="text-sm text-foreground-light">
                    Describe your schedule above
                  </span>
                ) : (
                  <span className="text-sm text-foreground-light flex items-center gap-2">
                    {isGeneratingCron ? <LoadingDots /> : getScheduleMessage(scheduleString)}
                  </span>
                )}
              </div>
            </FormItem_Shadcn_>
          )
        }}
      />
    </SheetSection>
  )
}

Subdomains

Frequently Asked Questions

What does CronJobScheduleSection() do?
CronJobScheduleSection() is a function in the supabase codebase.
What does CronJobScheduleSection() call?
CronJobScheduleSection() calls 2 function(s): formatScheduleString, getScheduleMessage.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free