File size: 8,576 Bytes
eeb9404 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | 'use client';
import React, { useState, useEffect } from 'react';
import { ScheduledFunction, EdgeFunction } from '@/lib/vfs/types';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Loader2, AlertCircle, Info } from 'lucide-react';
interface ScheduledFunctionEditorProps {
scheduledFunction: ScheduledFunction | null;
edgeFunctions: EdgeFunction[];
isOpen: boolean;
onClose: () => void;
onSave: (data: Partial<ScheduledFunction>) => Promise<void>;
}
export function ScheduledFunctionEditor({
scheduledFunction: fn,
edgeFunctions,
isOpen,
onClose,
onSave,
}: ScheduledFunctionEditorProps) {
const [name, setName] = useState(fn?.name || '');
const [functionId, setFunctionId] = useState(fn?.functionId || '');
const [cronExpression, setCronExpression] = useState(fn?.cronExpression || '');
const [timezone, setTimezone] = useState(fn?.timezone || 'UTC');
const [description, setDescription] = useState(fn?.description || '');
const [config, setConfig] = useState(fn?.config ? JSON.stringify(fn.config, null, 2) : '{}');
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (isOpen) {
setName(fn?.name || '');
setFunctionId(fn?.functionId || '');
setCronExpression(fn?.cronExpression || '');
setTimezone(fn?.timezone || 'UTC');
setDescription(fn?.description || '');
setConfig(fn?.config ? JSON.stringify(fn.config, null, 2) : '{}');
setError(null);
}
}, [fn, isOpen]);
const handleSave = async () => {
setError(null);
if (!name.trim()) {
setError('Name is required');
return;
}
if (!/^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$/.test(name)) {
setError('Name must be lowercase letters, numbers, and hyphens only');
return;
}
if (!functionId) {
setError('Edge function selection is required');
return;
}
if (!cronExpression.trim()) {
setError('Cron expression is required');
return;
}
let parsedConfig: Record<string, unknown> = {};
if (config.trim()) {
try {
const parsed = JSON.parse(config);
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
setError('Config must be a JSON object');
return;
}
parsedConfig = parsed;
} catch {
setError('Config must be valid JSON');
return;
}
}
setSaving(true);
try {
await onSave({
name: name.trim(),
functionId,
cronExpression: cronExpression.trim(),
timezone: timezone.trim() || 'UTC',
description: description.trim() || undefined,
config: parsedConfig,
enabled: fn?.enabled ?? true,
});
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to save scheduled function');
} finally {
setSaving(false);
}
};
return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent className="sm:max-w-lg max-h-[85vh] flex flex-col">
<DialogHeader>
<DialogTitle>
{fn ? 'Edit Schedule' : 'Create Schedule'}
</DialogTitle>
<DialogDescription>
Run an edge function on a cron schedule.
</DialogDescription>
</DialogHeader>
<div className="flex-1 overflow-auto space-y-4">
{/* Name */}
<div className="space-y-2">
<Label htmlFor="sched-name">Name</Label>
<Input
id="sched-name"
value={name}
onChange={e => setName(e.target.value.toLowerCase())}
placeholder="daily-report"
disabled={!!fn}
/>
</div>
{/* Edge Function */}
<div className="space-y-2">
<Label htmlFor="sched-function">Edge Function</Label>
<Select value={functionId} onValueChange={setFunctionId}>
<SelectTrigger>
<SelectValue placeholder="Select a function..." />
</SelectTrigger>
<SelectContent>
{edgeFunctions.map(ef => (
<SelectItem key={ef.id} value={ef.id}>
{ef.name}
</SelectItem>
))}
</SelectContent>
</Select>
{edgeFunctions.length === 0 && (
<p className="text-xs text-muted-foreground">
No edge functions available. Create one in the Functions tab first.
</p>
)}
</div>
{/* Cron Expression */}
<div className="space-y-2">
<Label htmlFor="sched-cron">Cron Expression</Label>
<Input
id="sched-cron"
value={cronExpression}
onChange={e => setCronExpression(e.target.value)}
placeholder="0 8 * * *"
className="font-mono"
/>
</div>
{/* Timezone */}
<div className="space-y-2">
<Label htmlFor="sched-tz">Timezone</Label>
<Input
id="sched-tz"
value={timezone}
onChange={e => setTimezone(e.target.value)}
placeholder="UTC"
/>
<p className="text-xs text-muted-foreground">
e.g. UTC, America/New_York, Europe/London
</p>
</div>
{/* Description */}
<div className="space-y-2">
<Label htmlFor="sched-desc">Description (optional)</Label>
<Input
id="sched-desc"
value={description}
onChange={e => setDescription(e.target.value)}
placeholder="What does this schedule do?"
/>
</div>
{/* Config */}
<div className="space-y-2">
<Label htmlFor="sched-config">Config JSON (optional)</Label>
<Textarea
id="sched-config"
value={config}
onChange={e => setConfig(e.target.value)}
placeholder="{}"
className="font-mono text-sm h-20"
/>
<p className="text-xs text-muted-foreground">
Custom data passed as the request body to the edge function.
</p>
</div>
{/* Cron Reference */}
<div className="bg-muted/30 border rounded-lg p-4 space-y-2">
<div className="flex items-center gap-2 text-sm font-medium">
<Info className="h-4 w-4" />
Cron Patterns <span className="font-normal text-muted-foreground">(minimum 5 min interval)</span>
</div>
<div className="grid gap-1 text-xs font-mono">
<div><span className="text-muted-foreground">*/5 * * * *</span> Every 5 minutes</div>
<div><span className="text-muted-foreground">0 * * * *</span> Every hour</div>
<div><span className="text-muted-foreground">0 8 * * *</span> Daily at 8am</div>
<div><span className="text-muted-foreground">0 0 * * 1</span> Every Monday at midnight</div>
<div><span className="text-muted-foreground">0 0 1 * *</span> First of every month</div>
</div>
</div>
{/* Error */}
{error && (
<div className="flex items-center gap-2 text-sm text-destructive bg-destructive/10 p-3 rounded-lg">
<AlertCircle className="h-4 w-4 shrink-0" />
{error}
</div>
)}
</div>
{/* Footer */}
<div className="flex items-center justify-end gap-2 pt-4 border-t">
<Button variant="outline" onClick={onClose} disabled={saving}>
Cancel
</Button>
<Button onClick={handleSave} disabled={saving}>
{saving ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Saving...
</>
) : (
fn ? 'Save Changes' : 'Create Schedule'
)}
</Button>
</div>
</DialogContent>
</Dialog>
);
}
|