|
| 1 | +import { toString as CronToString } from 'cronstrue' |
| 2 | +import { Edit3, List } from 'lucide-react' |
| 3 | +import Link from 'next/link' |
| 4 | +import { useRouter } from 'next/router' |
| 5 | +import { useState } from 'react' |
| 6 | + |
| 7 | +import { useParams } from 'common' |
| 8 | +import { NavigationItem, PageLayout } from 'components/layouts/PageLayout/PageLayout' |
| 9 | +import { useCronJobQuery } from 'data/database-cron-jobs/database-cron-job-query' |
| 10 | +import { useEdgeFunctionsQuery } from 'data/edge-functions/edge-functions-query' |
| 11 | +import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject' |
| 12 | +import { |
| 13 | + Button, |
| 14 | + cn, |
| 15 | + CodeBlock, |
| 16 | + Sheet, |
| 17 | + SheetContent, |
| 18 | + Tooltip, |
| 19 | + TooltipContent, |
| 20 | + TooltipTrigger, |
| 21 | +} from 'ui' |
| 22 | +import ShimmeringLoader from 'ui-patterns/ShimmeringLoader' |
| 23 | +import { CreateCronJobSheet } from './CreateCronJobSheet' |
| 24 | +import { isSecondsFormat, parseCronJobCommand } from './CronJobs.utils' |
| 25 | +import { PreviousRunsTab } from './PreviousRunsTab' |
| 26 | + |
| 27 | +export const CronJobPage = () => { |
| 28 | + const router = useRouter() |
| 29 | + const { ref, id, pageId, childId } = useParams() |
| 30 | + const childLabel = router?.query?.['child-label'] as string |
| 31 | + const { data: project } = useSelectedProjectQuery() |
| 32 | + |
| 33 | + const [isEditSheetOpen, setIsEditSheetOpen] = useState(false) |
| 34 | + const [isClosing, setIsClosing] = useState(false) |
| 35 | + |
| 36 | + const jobId = Number(childId) |
| 37 | + |
| 38 | + const { data: job, isLoading } = useCronJobQuery({ |
| 39 | + projectRef: project?.ref, |
| 40 | + connectionString: project?.connectionString, |
| 41 | + id: jobId, |
| 42 | + }) |
| 43 | + |
| 44 | + const { data: edgeFunctions = [] } = useEdgeFunctionsQuery({ projectRef: project?.ref }) |
| 45 | + |
| 46 | + // Parse the cron job command to check if it's an edge function |
| 47 | + const cronJobValues = parseCronJobCommand(job?.command || '', project?.ref!) |
| 48 | + const edgeFunction = |
| 49 | + cronJobValues.type === 'edge_function' ? cronJobValues.edgeFunctionName : undefined |
| 50 | + const edgeFunctionSlug = edgeFunction?.split('/functions/v1/').pop() |
| 51 | + const isValidEdgeFunction = edgeFunctions.some((x) => x.slug === edgeFunctionSlug) |
| 52 | + |
| 53 | + const breadcrumbItems = [ |
| 54 | + { |
| 55 | + label: 'Integrations', |
| 56 | + href: `/project/${ref}/integrations`, |
| 57 | + }, |
| 58 | + { |
| 59 | + label: 'Cron', |
| 60 | + href: pageId |
| 61 | + ? `/project/${ref}/integrations/${id}/${pageId}` |
| 62 | + : `/project/${ref}/integrations/${id}`, |
| 63 | + }, |
| 64 | + { |
| 65 | + label: childLabel ?? job?.jobname ?? '', |
| 66 | + }, |
| 67 | + ] |
| 68 | + |
| 69 | + const navigationItems: NavigationItem[] = [] |
| 70 | + |
| 71 | + const pageTitle = childLabel || childId || 'Cron Job' |
| 72 | + |
| 73 | + const pageSubtitle = job ? ( |
| 74 | + <div className="text-sm text-foreground-light"> |
| 75 | + Running{' '} |
| 76 | + <Tooltip> |
| 77 | + <TooltipTrigger asChild> |
| 78 | + <span className="cursor-pointer underline decoration-dotted lowercase"> |
| 79 | + {isSecondsFormat(job.schedule) |
| 80 | + ? job.schedule.toLowerCase() |
| 81 | + : CronToString(job.schedule.toLowerCase())} |
| 82 | + </span> |
| 83 | + </TooltipTrigger> |
| 84 | + <TooltipContent side="bottom" align="center"> |
| 85 | + <div className="text-xs"> |
| 86 | + <p className="font-mono mb-1">{job.schedule.toLowerCase()}</p> |
| 87 | + {!isSecondsFormat(job.schedule) && ( |
| 88 | + <p className="text-foreground-light">{CronToString(job.schedule.toLowerCase())}</p> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + </TooltipContent> |
| 92 | + </Tooltip>{' '} |
| 93 | + with command{' '} |
| 94 | + <Tooltip> |
| 95 | + <TooltipTrigger asChild> |
| 96 | + <code className="text-xs font-mono bg-surface-200 px-1 py-0.5 rounded max-w-[200px] inline-block truncate align-bottom cursor-pointer"> |
| 97 | + {job.command} |
| 98 | + </code> |
| 99 | + </TooltipTrigger> |
| 100 | + <TooltipContent |
| 101 | + side="bottom" |
| 102 | + align="center" |
| 103 | + className="min-w-[200px] max-w-[400px] text-wrap p-0" |
| 104 | + > |
| 105 | + <p className="text-xs font-mono px-2 py-1 border-b bg-surface-100">Command</p> |
| 106 | + <CodeBlock |
| 107 | + hideLineNumbers |
| 108 | + language="sql" |
| 109 | + value={job.command.trim()} |
| 110 | + className={cn( |
| 111 | + 'py-0 px-3.5 max-w-full prose dark:prose-dark border-0 rounded-t-none', |
| 112 | + '[&>code]:m-0 [&>code>span]:flex [&>code>span]:flex-wrap min-h-11', |
| 113 | + '[&>code]:text-xs' |
| 114 | + )} |
| 115 | + /> |
| 116 | + </TooltipContent> |
| 117 | + </Tooltip> |
| 118 | + </div> |
| 119 | + ) : null |
| 120 | + |
| 121 | + // Secondary actions |
| 122 | + const secondaryActions = [ |
| 123 | + <Button |
| 124 | + key="edit" |
| 125 | + type="outline" |
| 126 | + icon={<Edit3 strokeWidth={1.5} size="14" />} |
| 127 | + onClick={() => setIsEditSheetOpen(true)} |
| 128 | + > |
| 129 | + Edit |
| 130 | + </Button>, |
| 131 | + <Button key="view-logs" asChild type="outline" icon={<List strokeWidth={1.5} size="14" />}> |
| 132 | + <Link |
| 133 | + target="_blank" |
| 134 | + rel="noopener noreferrer" |
| 135 | + href={`/project/${project?.ref}/logs/pgcron-logs/`} |
| 136 | + > |
| 137 | + View Cron logs |
| 138 | + </Link> |
| 139 | + </Button>, |
| 140 | + ...(isValidEdgeFunction |
| 141 | + ? [ |
| 142 | + <Button key="view-edge-logs" asChild type="outline"> |
| 143 | + <Link |
| 144 | + target="_blank" |
| 145 | + rel="noopener noreferrer" |
| 146 | + href={`/project/${project?.ref}/functions/${edgeFunctionSlug}/logs`} |
| 147 | + > |
| 148 | + View Edge Function logs |
| 149 | + </Link> |
| 150 | + </Button>, |
| 151 | + ] |
| 152 | + : []), |
| 153 | + ] |
| 154 | + |
| 155 | + return ( |
| 156 | + <> |
| 157 | + <PageLayout |
| 158 | + title={pageTitle} |
| 159 | + size="full" |
| 160 | + breadcrumbs={breadcrumbItems} |
| 161 | + navigationItems={navigationItems} |
| 162 | + secondaryActions={secondaryActions} |
| 163 | + subtitle={isLoading ? <ShimmeringLoader className="py-0 h-[20px] w-96" /> : pageSubtitle} |
| 164 | + className="border-b-0" |
| 165 | + > |
| 166 | + <PreviousRunsTab /> |
| 167 | + </PageLayout> |
| 168 | + |
| 169 | + <Sheet open={isEditSheetOpen} onOpenChange={setIsEditSheetOpen}> |
| 170 | + <SheetContent size="lg"> |
| 171 | + {job && ( |
| 172 | + <CreateCronJobSheet |
| 173 | + selectedCronJob={{ |
| 174 | + jobname: job.jobname, |
| 175 | + schedule: job.schedule, |
| 176 | + active: job.active, |
| 177 | + command: job.command, |
| 178 | + }} |
| 179 | + supportsSeconds={true} |
| 180 | + isClosing={isClosing} |
| 181 | + setIsClosing={setIsClosing} |
| 182 | + onClose={() => { |
| 183 | + setIsEditSheetOpen(false) |
| 184 | + setIsClosing(false) |
| 185 | + }} |
| 186 | + /> |
| 187 | + )} |
| 188 | + </SheetContent> |
| 189 | + </Sheet> |
| 190 | + </> |
| 191 | + ) |
| 192 | +} |
0 commit comments