Refactor schedules hooks to accept repoId as parameter

Remove repoId from hook closure and pass it explicitly.
Improves reusability and makes hooks more flexible.
This commit is contained in:
Chris Scott committed 2026-03-24 11:39:21 -04:00
1 parent 9975be955e
commit fbf259f22c
8 files changed
+665 -446

No files matched your search

@@ -0,0 +1,137 @@
import { Combobox, type ComboboxOption } from '@/components/ui/combobox'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Switch } from '@/components/ui/switch'
import { TabsContent } from '@/components/ui/tabs'
import { Info } from 'lucide-react'
type GeneralTabProps = {
name: string
onNameChange: (value: string) => void
description: string
onDescriptionChange: (value: string) => void
agentSlug: string
onAgentSlugChange: (value: string) => void
agentOptions: ComboboxOption[]
model: string
onModelChange: (value: string) => void
modelOptions: ComboboxOption[]
enabled: boolean
onEnabledChange: (value: boolean) => void
showRepoSelector?: boolean
isEditing: boolean
repoId?: number
onRepoChange?: (repoId: number | undefined) => void
repoOptions: ComboboxOption[]
}
function InfoHint({ text }: { text: string }) {
return (
<span
title={text}
aria-label={text}
className="inline-flex h-4 w-4 items-center justify-center text-muted-foreground"
>
<Info className="h-3.5 w-3.5" />
</span>
)
}
export function GeneralTab({
name,
onNameChange,
description,
onDescriptionChange,
agentSlug,
onAgentSlugChange,
agentOptions,
model,
onModelChange,
modelOptions,
enabled,
onEnabledChange,
showRepoSelector,
isEditing,
repoId,
onRepoChange,
repoOptions,
}: GeneralTabProps) {
return (
<TabsContent value="basics" className="mt-0 min-h-0 flex-1 overflow-y-auto pt-4 pb-5">
<div className="space-y-4">
{showRepoSelector && !isEditing && (
<div className="space-y-2">
<Label>Repository</Label>
<Combobox
value={repoId?.toString() ?? ''}
onChange={(value) => onRepoChange?.(value ? Number(value) : undefined)}
options={repoOptions}
placeholder="Select a repository"
allowCustomValue={false}
/>
</div>
)}
<div className="grid gap-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="schedule-name">Name</Label>
<Input
id="schedule-name"
value={name}
onChange={(event) => onNameChange(event.target.value)}
placeholder="Nightly repo health check"
/>
</div>
<div className="space-y-2">
<Label htmlFor="schedule-description">Description</Label>
<Input
id="schedule-description"
value={description}
onChange={(event) => onDescriptionChange(event.target.value)}
placeholder="What this job checks or produces"
/>
</div>
</div>
<div className="grid gap-4 sm:grid-cols-[minmax(0,1fr)_220px] sm:items-end">
<div className="space-y-2">
<Label htmlFor="schedule-agent">Agent slug</Label>
<Combobox
value={agentSlug}
onChange={onAgentSlugChange}
options={agentOptions}
placeholder="Select an agent"
allowCustomValue
showClear
/>
</div>
<div className="space-y-2">
<div className="flex items-center gap-2">
<Label htmlFor="schedule-model">Model override</Label>
<InfoHint text="Pick from detected OpenCode models or type a custom provider/model value." />
</div>
<Combobox
value={model}
onChange={onModelChange}
options={modelOptions}
placeholder="Workspace default"
allowCustomValue
showClear
/>
</div>
</div>
<div className="rounded-lg border border-border bg-card p-4">
<div className="flex items-center justify-between gap-4">
<div className="flex items-center gap-2">
<p className="text-sm font-medium">Enabled</p>
<InfoHint text="Auto-run this job on its schedule while still allowing manual runs from the dashboard." />
</div>
<Switch checked={enabled} onCheckedChange={onEnabledChange} />
</div>
</div>
</div>
</TabsContent>
)
}
@@ -0,0 +1,126 @@
import { Button } from '@/components/ui/button'
import { Label } from '@/components/ui/label'
import { TabsContent } from '@/components/ui/tabs'
import { Textarea } from '@/components/ui/textarea'
import { Badge } from '@/components/ui/badge'
import { Check, Pencil, Plus, Trash2 } from 'lucide-react'
import type { PromptTemplate } from '@opencode-manager/shared/types'
type PromptTabProps = {
prompt: string
onPromptChange: (value: string) => void
selectedPromptTemplateId: number | null
onApplyTemplate: (template: PromptTemplate) => void
templates: PromptTemplate[]
onEditTemplate: (template: PromptTemplate) => void
onDeleteTemplate: (templateId: number) => void
onNewTemplate: () => void
}
export function PromptTab({
prompt,
onPromptChange,
selectedPromptTemplateId,
onApplyTemplate,
templates,
onEditTemplate,
onDeleteTemplate,
onNewTemplate,
}: PromptTabProps) {
return (
<TabsContent value="prompt" className="mt-0 min-h-0 flex-1 overflow-y-auto px-3 pt-4 pb-5 sm:px-4">
<div className="space-y-4">
<div className="space-y-2">
<div className="flex items-center justify-center gap-2">
<Label>Prompt templates</Label>
<Button
type="button"
variant="outline"
size="sm"
className="h-7 gap-1 text-xs"
onClick={onNewTemplate}
>
<Plus className="h-3 w-3" />
New
</Button>
</div>
<div className="grid gap-3 lg:grid-cols-2">
{templates.map((template) => {
const isSelected = selectedPromptTemplateId === template.id
return (
<div key={template.id} className="relative group">
<button
type="button"
onClick={() => onApplyTemplate(template)}
className={`w-full rounded-xl border-2 p-4 text-left transition-all ${
isSelected
? 'border-primary bg-primary/10 ring-2 ring-primary/30'
: 'border-border bg-card hover:bg-accent/40'
}`}
>
<div className="flex flex-wrap items-center gap-2">
<Badge className="text-[10px] uppercase tracking-wide border-transparent bg-orange-500 text-white">
{template.category}
</Badge>
<Badge className="text-[10px] uppercase tracking-wide border-transparent bg-slate-600 text-white">
{template.cadenceHint}
</Badge>
</div>
<div className="mt-3">
<p className="text-sm font-semibold">{template.title}</p>
<p className="mt-1 text-xs text-muted-foreground">{template.description}</p>
</div>
<p className="mt-3 text-xs text-muted-foreground line-clamp-3">{template.suggestedDescription}</p>
{isSelected && (
<div className="absolute top-2 right-2 h-5 w-5 rounded-full bg-primary flex items-center justify-center">
<Check className="h-3 w-3 text-primary-foreground" />
</div>
)}
</button>
<div className={`absolute top-2 right-10 flex gap-1 transition-opacity ${isSelected ? 'opacity-100' : 'opacity-0 group-hover:opacity-100'}`}>
<Button
type="button"
variant="ghost"
size="icon"
className="h-6 w-6"
onClick={(e) => { e.stopPropagation(); onEditTemplate(template) }}
>
<Pencil className="h-3 w-3" />
</Button>
<Button
type="button"
variant="ghost"
size="icon"
className="h-6 w-6 text-destructive hover:text-destructive"
onClick={(e) => { e.stopPropagation(); onDeleteTemplate(template.id) }}
>
<Trash2 className="h-3 w-3" />
</Button>
</div>
</div>
)
})}
</div>
</div>
<div className="space-y-2">
<Label htmlFor="schedule-prompt">Prompt</Label>
<Textarea
id="schedule-prompt"
value={prompt}
onChange={(event) => {
onPromptChange(event.target.value)
}}
className="min-h-[320px]"
placeholder="Review the repo, summarize notable risks, and open a session I can inspect later."
/>
</div>
<p className="text-xs text-muted-foreground">
This prompt becomes the first message sent to the agent when the schedule runs.
</p>
</div>
</TabsContent>
)
}
@@ -7,32 +7,25 @@ import { settingsApi } from '@/api/settings'
import { listRepos } from '@/api/repos'
import type { Repo } from '@/api/types'
import { OPENCODE_API_ENDPOINT } from '@/config'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Combobox, type ComboboxOption } from '@/components/ui/combobox'
import type { ComboboxOption } from '@/components/ui/combobox'
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { MultiSelect } from '@/components/ui/multi-select'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Switch } from '@/components/ui/switch'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Textarea } from '@/components/ui/textarea'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import {
buildCronExpressionFromPreset,
cronPresetOptions,
detectSchedulePreset,
formatDraftScheduleSummary,
getLocalTimeZone,
intervalOptions,
schedulePresetOptions,
type SchedulePreset,
weekdayOptions,
} from '@/components/schedules/schedule-utils'
import { getRepoDisplayName } from '@/lib/utils'
import { Check, Info, Loader2, Pencil, Plus, Sparkles, Trash2 } from 'lucide-react'
import { Loader2 } from 'lucide-react'
import { usePromptTemplates, useDeletePromptTemplate } from '@/hooks/usePromptTemplates'
import { PromptTemplateDialog } from './PromptTemplateDialog'
import { DeleteDialog } from '@/components/ui/delete-dialog'
import { GeneralTab } from './GeneralTab'
import { TimingTab } from './TimingTab'
import { PromptTab } from './PromptTab'
import { SkillsTab } from './SkillsTab'
type ScheduleJobDialogProps = {
open: boolean
@@ -43,18 +36,7 @@ type ScheduleJobDialogProps = {
showRepoSelector?: boolean
repoId?: number
onRepoChange?: (repoId: number | undefined) => void
}
function InfoHint({ text }: { text: string }) {
return (
<span
title={text}
aria-label={text}
className="inline-flex h-4 w-4 items-center justify-center text-muted-foreground"
>
<Info className="h-3.5 w-3.5" />
</span>
)
isEditing?: boolean
}
export function ScheduleJobDialog({ open, onOpenChange, job, isSaving, onSubmit, showRepoSelector, repoId: selectedRepoId, onRepoChange }: ScheduleJobDialogProps) {
@@ -291,384 +273,64 @@ export function ScheduleJobDialog({ open, onOpenChange, job, isSaving, onSubmit,
</TabsList>
</div>
<TabsContent value="basics" className="mt-0 min-h-0 flex-1 overflow-y-auto pt-4 pb-5">
<div className="space-y-4">
{showRepoSelector && !job && (
<div className="space-y-2">
<Label>Repository</Label>
<Combobox
value={selectedRepoId?.toString() ?? ''}
onChange={(value) => onRepoChange?.(value ? Number(value) : undefined)}
options={repoOptions}
placeholder="Select a repository"
allowCustomValue={false}
/>
</div>
)}
<div className="grid gap-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="schedule-name">Name</Label>
<Input id="schedule-name" value={name} onChange={(event) => setName(event.target.value)} placeholder="Nightly repo health check" />
</div>
<GeneralTab
name={name}
onNameChange={setName}
description={description}
onDescriptionChange={setDescription}
agentSlug={agentSlug}
onAgentSlugChange={setAgentSlug}
agentOptions={agentOptions}
model={model}
onModelChange={setModel}
modelOptions={modelOptions}
enabled={enabled}
onEnabledChange={setEnabled}
showRepoSelector={showRepoSelector}
isEditing={!!job}
repoId={selectedRepoId}
onRepoChange={onRepoChange}
repoOptions={repoOptions}
/>
<div className="space-y-2">
<Label htmlFor="schedule-description">Description</Label>
<Input id="schedule-description" value={description} onChange={(event) => setDescription(event.target.value)} placeholder="What this job checks or produces" />
</div>
</div>
<TimingTab
schedulePreset={schedulePreset}
onSchedulePresetChange={setSchedulePreset}
intervalMinutes={intervalMinutes}
onIntervalMinutesChange={setIntervalMinutes}
timeOfDay={timeOfDay}
onTimeOfDayChange={setTimeOfDay}
hourlyMinute={hourlyMinute}
onHourlyMinuteChange={setHourlyMinute}
weeklyDays={weeklyDays}
onWeeklyDaysChange={setWeeklyDays}
monthlyDay={monthlyDay}
onMonthlyDayChange={setMonthlyDay}
cronExpression={cronExpression}
onCronExpressionChange={setCronExpression}
timezone={timezone}
onTimezoneChange={setTimezone}
/>
<div className="grid gap-4 sm:grid-cols-[minmax(0,1fr)_220px] sm:items-end">
<div className="space-y-2">
<Label htmlFor="schedule-agent">Agent slug</Label>
<Combobox
value={agentSlug}
onChange={setAgentSlug}
options={agentOptions}
placeholder="Select an agent"
allowCustomValue
showClear
/>
</div>
<PromptTab
prompt={prompt}
onPromptChange={setPrompt}
selectedPromptTemplateId={selectedPromptTemplateId}
onApplyTemplate={applyPromptTemplate}
templates={templates}
onEditTemplate={(template) => { setEditingTemplate(template); setTemplateDialogOpen(true) }}
onDeleteTemplate={setDeletingTemplateId}
onNewTemplate={() => { setEditingTemplate(undefined); setTemplateDialogOpen(true) }}
/>
<div className="space-y-2">
<div className="flex items-center gap-2">
<Label htmlFor="schedule-model">Model override</Label>
<InfoHint text="Pick from detected OpenCode models or type a custom provider/model value." />
</div>
<Combobox
value={model}
onChange={setModel}
options={modelOptions}
placeholder="Workspace default"
allowCustomValue
showClear
/>
</div>
</div>
<div className="rounded-lg border border-border bg-card p-4">
<div className="flex items-center justify-between gap-4">
<div className="flex items-center gap-2">
<p className="text-sm font-medium">Enabled</p>
<InfoHint text="Auto-run this job on its schedule while still allowing manual runs from the dashboard." />
</div>
<Switch checked={enabled} onCheckedChange={setEnabled} />
</div>
</div>
</div>
</TabsContent>
<TabsContent value="timing" className="px-3 mt-0 min-h-0 flex-1 overflow-y-auto sm:px-6 pt-4 pb-5">
<div className="space-y-4">
<div className="space-y-3 rounded-lg border border-border bg-card p-4">
<div>
<Label>Repeat</Label>
<p className="mt-1 text-xs text-muted-foreground">Use a simple scheduler builder by default. Advanced cron is still available if you need it.</p>
</div>
<div className="flex flex-wrap gap-2">
{schedulePresetOptions.map((option) => (
<Button
key={option.value}
type="button"
variant={schedulePreset === option.value ? 'default' : 'outline'}
size="sm"
onClick={() => setSchedulePreset(option.value)}
>
{option.label}
</Button>
))}
</div>
</div>
{schedulePreset === 'interval' ? (
<div className="space-y-3 rounded-lg border border-border bg-card p-4">
<div className="flex items-center justify-between gap-3">
<Label htmlFor="schedule-interval">Run every</Label>
<Input
id="schedule-interval"
type="number"
min={5}
max={10080}
value={intervalMinutes}
onChange={(event) => setIntervalMinutes(event.target.value)}
className="w-28"
/>
</div>
<div className="flex flex-wrap gap-2">
{intervalOptions.map((option) => (
<Button key={option.value} type="button" variant={intervalMinutes === String(option.value) ? 'default' : 'outline'} size="sm" onClick={() => setIntervalMinutes(String(option.value))}>
{option.label}
</Button>
))}
</div>
</div>
) : schedulePreset === 'hourly' ? (
<div className="grid gap-4 rounded-lg border border-border bg-card p-4 sm:grid-cols-[120px_minmax(0,1fr)] sm:items-end">
<div className="space-y-2">
<Label htmlFor="schedule-hourly-minute">Minute</Label>
<Input
id="schedule-hourly-minute"
type="number"
min={0}
max={59}
value={hourlyMinute}
onChange={(event) => setHourlyMinute(event.target.value)}
/>
</div>
<p className="text-sm text-muted-foreground">Run every hour at the selected minute mark.</p>
</div>
) : schedulePreset === 'daily' || schedulePreset === 'weekdays' ? (
<div className="grid gap-4 rounded-lg border border-border bg-card p-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="schedule-time">Time</Label>
<Input id="schedule-time" type="time" value={timeOfDay} onChange={(event) => setTimeOfDay(event.target.value)} />
</div>
<div className="space-y-2">
<Label htmlFor="schedule-timezone">Timezone</Label>
<Input id="schedule-timezone" value={timezone} onChange={(event) => setTimezone(event.target.value)} placeholder="Detected from browser" />
</div>
</div>
) : schedulePreset === 'weekly' ? (
<div className="space-y-4 rounded-lg border border-border bg-card p-4">
<div className="space-y-2">
<Label>Days</Label>
<div className="flex flex-wrap gap-2">
{weekdayOptions.map((option) => {
const selected = weeklyDays.includes(option.value)
return (
<Button
key={option.value}
type="button"
variant={selected ? 'default' : 'outline'}
size="sm"
onClick={() => setWeeklyDays((current) => selected ? current.filter((value) => value !== option.value) : [...current, option.value])}
>
{option.label}
</Button>
)
})}
</div>
</div>
<div className="grid gap-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="schedule-weekly-time">Time</Label>
<Input id="schedule-weekly-time" type="time" value={timeOfDay} onChange={(event) => setTimeOfDay(event.target.value)} />
</div>
<div className="space-y-2">
<Label htmlFor="schedule-weekly-timezone">Timezone</Label>
<Input id="schedule-weekly-timezone" value={timezone} onChange={(event) => setTimezone(event.target.value)} placeholder="Detected from browser" />
</div>
</div>
</div>
) : schedulePreset === 'monthly' ? (
<div className="space-y-4 rounded-lg border border-border bg-card p-4">
<div className="grid gap-4 sm:grid-cols-3">
<div className="space-y-2">
<Label htmlFor="schedule-monthly-day">Day</Label>
<Input
id="schedule-monthly-day"
type="number"
min={1}
max={31}
value={monthlyDay}
onChange={(event) => setMonthlyDay(event.target.value)}
/>
</div>
<div className="space-y-2">
<Label htmlFor="schedule-monthly-time">Time</Label>
<Input id="schedule-monthly-time" type="time" value={timeOfDay} onChange={(event) => setTimeOfDay(event.target.value)} />
</div>
<div className="space-y-2">
<Label htmlFor="schedule-monthly-timezone">Timezone</Label>
<Input id="schedule-monthly-timezone" value={timezone} onChange={(event) => setTimezone(event.target.value)} placeholder="Detected from browser" />
</div>
</div>
</div>
) : (
<div className="space-y-4 rounded-lg border border-border bg-card p-4">
<div className="space-y-2">
<Label htmlFor="schedule-cron">Cron expression</Label>
<Input
id="schedule-cron"
value={cronExpression}
onChange={(event) => setCronExpression(event.target.value)}
placeholder="0 9 * * 1-5"
className="font-mono"
/>
<p className="text-xs text-muted-foreground">Examples: `0 9 * * 1-5` weekdays at 9 AM, `30 6 1 * *` monthly on the 1st at 6:30 AM.</p>
</div>
<div className="space-y-2">
<Label htmlFor="schedule-timezone">Timezone</Label>
<Input
id="schedule-timezone"
value={timezone}
onChange={(event) => setTimezone(event.target.value)}
placeholder="Detected from browser"
/>
</div>
<div className="flex flex-wrap gap-2">
{cronPresetOptions.map((option) => (
<Button key={option.value} type="button" variant="outline" size="sm" onClick={() => setCronExpression(option.value)}>
{option.label}
</Button>
))}
</div>
</div>
)}
<div className="rounded-lg border border-border bg-muted/40 p-4">
<p className="text-xs font-medium uppercase tracking-wide text-muted-foreground">Schedule Preview</p>
<p className="mt-2 text-sm font-medium break-words">{formatDraftScheduleSummary({ preset: schedulePreset, intervalMinutes, timeOfDay, hourlyMinute, weeklyDays, monthlyDay, cronExpression, timezone })}</p>
{schedulePreset !== 'interval' && (
<p className="mt-2 text-xs text-muted-foreground font-mono break-all">
{buildCronExpressionFromPreset({ preset: schedulePreset, intervalMinutes, timeOfDay, hourlyMinute, weeklyDays, monthlyDay, cronExpression })}
</p>
)}
</div>
</div>
</TabsContent>
<TabsContent value="prompt" className="mt-0 min-h-0 flex-1 overflow-y-auto px-3 pt-4 pb-5 sm:px-4">
<div className="space-y-4">
<div className="space-y-2">
<div className="flex items-center justify-center gap-2">
<Label>Prompt templates</Label>
<Button
type="button"
variant="outline"
size="sm"
className="h-7 gap-1 text-xs"
onClick={() => { setEditingTemplate(undefined); setTemplateDialogOpen(true) }}
>
<Plus className="h-3 w-3" />
New
</Button>
</div>
<div className="grid gap-3 lg:grid-cols-2">
{templates.map((template) => {
const isSelected = selectedPromptTemplateId === template.id
return (
<div key={template.id} className="relative group">
<button
type="button"
onClick={() => applyPromptTemplate(template)}
className={`w-full rounded-xl border-2 p-4 text-left transition-all ${
isSelected
? 'border-primary bg-primary/10 ring-2 ring-primary/30'
: 'border-border bg-card hover:bg-accent/40'
}`}
>
<div className="flex flex-wrap items-center gap-2">
<Badge className="text-[10px] uppercase tracking-wide border-transparent bg-orange-500 text-white">
{template.category}
</Badge>
<Badge className="text-[10px] uppercase tracking-wide border-transparent bg-slate-600 text-white">
{template.cadenceHint}
</Badge>
</div>
<div className="mt-3">
<p className="text-sm font-semibold">{template.title}</p>
<p className="mt-1 text-xs text-muted-foreground">{template.description}</p>
</div>
<p className="mt-3 text-xs text-muted-foreground line-clamp-3">{template.suggestedDescription}</p>
{isSelected && (
<div className="absolute top-2 right-2 h-5 w-5 rounded-full bg-primary flex items-center justify-center">
<Check className="h-3 w-3 text-primary-foreground" />
</div>
)}
</button>
<div className={`absolute top-2 right-10 flex gap-1 transition-opacity ${isSelected ? 'opacity-100' : 'opacity-0 group-hover:opacity-100'}`}>
<Button
type="button"
variant="ghost"
size="icon"
className="h-6 w-6"
onClick={(e) => { e.stopPropagation(); setEditingTemplate(template); setTemplateDialogOpen(true) }}
>
<Pencil className="h-3 w-3" />
</Button>
<Button
type="button"
variant="ghost"
size="icon"
className="h-6 w-6 text-destructive hover:text-destructive"
onClick={(e) => { e.stopPropagation(); setDeletingTemplateId(template.id) }}
>
<Trash2 className="h-3 w-3" />
</Button>
</div>
</div>
)
})}
</div>
</div>
<div className="space-y-2">
<Label htmlFor="schedule-prompt">Prompt</Label>
<Textarea
id="schedule-prompt"
value={prompt}
onChange={(event) => {
setPrompt(event.target.value)
setSelectedPromptTemplateId(null)
}}
className="min-h-[320px]"
placeholder="Review the repo, summarize notable risks, and open a session I can inspect later."
/>
</div>
<p className="text-xs text-muted-foreground">
This prompt becomes the first message sent to the agent when the schedule runs.
</p>
</div>
</TabsContent>
<TabsContent value="skills" className="mt-0 min-h-0 flex-1 overflow-y-auto px-6 pt-4 pb-5">
<div className="space-y-4">
<div className="space-y-2">
<Label>Select skills for this schedule</Label>
<p className="text-xs text-muted-foreground">
Skills provide domain-specific instructions to the agent. Select which skills should be available when this schedule runs.
</p>
</div>
{skillsLoading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
) : skills.length === 0 ? (
<div className="rounded-lg border border-dashed border-border bg-card/50 p-6 text-center">
<Sparkles className="h-6 w-6 mx-auto mb-2 text-muted-foreground" />
<p className="text-sm text-muted-foreground">No skills discovered. Configure skill paths in Settings to make skills available here.</p>
</div>
) : (
<MultiSelect
value={skillSlugs}
onChange={setSkillSlugs}
options={skills.map(s => ({ value: s.name, label: s.name, description: s.description }))}
placeholder="Search and select skills..."
/>
)}
<div className="space-y-2">
<Label htmlFor="schedule-skill-notes">Notes</Label>
<Textarea
id="schedule-skill-notes"
value={skillNotes}
onChange={(event) => setSkillNotes(event.target.value)}
placeholder="Optional notes about skill usage for this schedule."
className="min-h-[80px]"
/>
</div>
</div>
</TabsContent>
<SkillsTab
skillSlugs={skillSlugs}
onSkillSlugsChange={setSkillSlugs}
skillNotes={skillNotes}
onSkillNotesChange={setSkillNotes}
skills={skills}
skillsLoading={skillsLoading}
/>
</Tabs>
<div className="mt-0 shrink-0 border-t border-border px-3 sm:px-6 py-4 flex flex-row gap-2 sm:justify-end">
@@ -0,0 +1,65 @@
import { Label } from '@/components/ui/label'
import { TabsContent } from '@/components/ui/tabs'
import { Textarea } from '@/components/ui/textarea'
import { MultiSelect } from '@/components/ui/multi-select'
import { Loader2, Sparkles } from 'lucide-react'
type SkillsTabProps = {
skillSlugs: string[]
onSkillSlugsChange: (value: string[]) => void
skillNotes: string
onSkillNotesChange: (value: string) => void
skills: Array<{ name: string; description: string }>
skillsLoading: boolean
}
export function SkillsTab({
skillSlugs,
onSkillSlugsChange,
skillNotes,
onSkillNotesChange,
skills,
skillsLoading,
}: SkillsTabProps) {
return (
<TabsContent value="skills" className="mt-0 min-h-0 flex-1 overflow-y-auto px-6 pt-4 pb-5">
<div className="space-y-4">
<div className="space-y-2">
<Label>Select skills for this schedule</Label>
<p className="text-xs text-muted-foreground">
Skills provide domain-specific instructions to the agent. Select which skills should be available when this schedule runs.
</p>
</div>
{skillsLoading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
) : skills.length === 0 ? (
<div className="rounded-lg border border-dashed border-border bg-card/50 p-6 text-center">
<Sparkles className="h-6 w-6 mx-auto mb-2 text-muted-foreground" />
<p className="text-sm text-muted-foreground">No skills discovered. Configure skill paths in Settings to make skills available here.</p>
</div>
) : (
<MultiSelect
value={skillSlugs}
onChange={onSkillSlugsChange}
options={skills.map(s => ({ value: s.name, label: s.name, description: s.description }))}
placeholder="Search and select skills..."
/>
)}
<div className="space-y-2">
<Label htmlFor="schedule-skill-notes">Notes</Label>
<Textarea
id="schedule-skill-notes"
value={skillNotes}
onChange={(event) => onSkillNotesChange(event.target.value)}
placeholder="Optional notes about skill usage for this schedule."
className="min-h-[80px]"
/>
</div>
</div>
</TabsContent>
)
}
@@ -0,0 +1,229 @@
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { TabsContent } from '@/components/ui/tabs'
import {
buildCronExpressionFromPreset,
cronPresetOptions,
intervalOptions,
schedulePresetOptions,
weekdayOptions,
type SchedulePreset,
} from '@/components/schedules/schedule-utils'
import { formatDraftScheduleSummary, getLocalTimeZone } from '@/components/schedules/schedule-utils'
type TimingTabProps = {
schedulePreset: SchedulePreset
onSchedulePresetChange: (value: SchedulePreset) => void
intervalMinutes: string
onIntervalMinutesChange: (value: string) => void
timeOfDay: string
onTimeOfDayChange: (value: string) => void
hourlyMinute: string
onHourlyMinuteChange: (value: string) => void
weeklyDays: string[]
onWeeklyDaysChange: (value: string[]) => void
monthlyDay: string
onMonthlyDayChange: (value: string) => void
cronExpression: string
onCronExpressionChange: (value: string) => void
timezone: string
onTimezoneChange: (value: string) => void
}
export function TimingTab({
schedulePreset,
onSchedulePresetChange,
intervalMinutes,
onIntervalMinutesChange,
timeOfDay,
onTimeOfDayChange,
hourlyMinute,
onHourlyMinuteChange,
weeklyDays,
onWeeklyDaysChange,
monthlyDay,
onMonthlyDayChange,
cronExpression,
onCronExpressionChange,
timezone,
onTimezoneChange,
}: TimingTabProps) {
return (
<TabsContent value="timing" className="px-3 mt-0 min-h-0 flex-1 overflow-y-auto sm:px-6 pt-4 pb-5">
<div className="space-y-4">
<div className="space-y-3 rounded-lg border border-border bg-card p-4">
<div>
<Label>Repeat</Label>
<p className="mt-1 text-xs text-muted-foreground">Use a simple scheduler builder by default. Advanced cron is still available if you need it.</p>
</div>
<div className="flex flex-wrap gap-2">
{schedulePresetOptions.map((option) => (
<Button
key={option.value}
type="button"
variant={schedulePreset === option.value ? 'default' : 'outline'}
size="sm"
onClick={() => onSchedulePresetChange(option.value)}
>
{option.label}
</Button>
))}
</div>
</div>
{schedulePreset === 'interval' ? (
<div className="space-y-3 rounded-lg border border-border bg-card p-4">
<div className="flex items-center justify-between gap-3">
<Label htmlFor="schedule-interval">Run every</Label>
<Input
id="schedule-interval"
type="number"
min={5}
max={10080}
value={intervalMinutes}
onChange={(event) => onIntervalMinutesChange(event.target.value)}
className="w-28"
/>
</div>
<div className="flex flex-wrap gap-2">
{intervalOptions.map((option) => (
<Button key={option.value} type="button" variant={intervalMinutes === String(option.value) ? 'default' : 'outline'} size="sm" onClick={() => onIntervalMinutesChange(String(option.value))}>
{option.label}
</Button>
))}
</div>
</div>
) : schedulePreset === 'hourly' ? (
<div className="grid gap-4 rounded-lg border border-border bg-card p-4 sm:grid-cols-[120px_minmax(0,1fr)] sm:items-end">
<div className="space-y-2">
<Label htmlFor="schedule-hourly-minute">Minute</Label>
<Input
id="schedule-hourly-minute"
type="number"
min={0}
max={59}
value={hourlyMinute}
onChange={(event) => onHourlyMinuteChange(event.target.value)}
/>
</div>
<p className="text-sm text-muted-foreground">Run every hour at the selected minute mark.</p>
</div>
) : schedulePreset === 'daily' || schedulePreset === 'weekdays' ? (
<div className="grid gap-4 rounded-lg border border-border bg-card p-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="schedule-time">Time</Label>
<Input id="schedule-time" type="time" value={timeOfDay} onChange={(event) => onTimeOfDayChange(event.target.value)} />
</div>
<div className="space-y-2">
<Label htmlFor="schedule-timezone">Timezone</Label>
<Input id="schedule-timezone" value={timezone} onChange={(event) => onTimezoneChange(event.target.value)} placeholder={getLocalTimeZone()} />
</div>
</div>
) : schedulePreset === 'weekly' ? (
<div className="space-y-4 rounded-lg border border-border bg-card p-4">
<div className="space-y-2">
<Label>Days</Label>
<div className="flex flex-wrap gap-2">
{weekdayOptions.map((option) => {
const selected = weeklyDays.includes(option.value)
return (
<Button
key={option.value}
type="button"
variant={selected ? 'default' : 'outline'}
size="sm"
onClick={() => onWeeklyDaysChange(selected ? weeklyDays.filter((value) => value !== option.value) : [...weeklyDays, option.value])}
>
{option.label}
</Button>
)
})}
</div>
</div>
<div className="grid gap-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="schedule-weekly-time">Time</Label>
<Input id="schedule-weekly-time" type="time" value={timeOfDay} onChange={(event) => onTimeOfDayChange(event.target.value)} />
</div>
<div className="space-y-2">
<Label htmlFor="schedule-weekly-timezone">Timezone</Label>
<Input id="schedule-weekly-timezone" value={timezone} onChange={(event) => onTimezoneChange(event.target.value)} placeholder={getLocalTimeZone()} />
</div>
</div>
</div>
) : schedulePreset === 'monthly' ? (
<div className="space-y-4 rounded-lg border border-border bg-card p-4">
<div className="grid gap-4 sm:grid-cols-3">
<div className="space-y-2">
<Label htmlFor="schedule-monthly-day">Day</Label>
<Input
id="schedule-monthly-day"
type="number"
min={1}
max={31}
value={monthlyDay}
onChange={(event) => onMonthlyDayChange(event.target.value)}
/>
</div>
<div className="space-y-2">
<Label htmlFor="schedule-monthly-time">Time</Label>
<Input id="schedule-monthly-time" type="time" value={timeOfDay} onChange={(event) => onTimeOfDayChange(event.target.value)} />
</div>
<div className="space-y-2">
<Label htmlFor="schedule-monthly-timezone">Timezone</Label>
<Input id="schedule-monthly-timezone" value={timezone} onChange={(event) => onTimezoneChange(event.target.value)} placeholder={getLocalTimeZone()} />
</div>
</div>
</div>
) : (
<div className="space-y-4 rounded-lg border border-border bg-card p-4">
<div className="space-y-2">
<Label htmlFor="schedule-cron">Cron expression</Label>
<Input
id="schedule-cron"
value={cronExpression}
onChange={(event) => onCronExpressionChange(event.target.value)}
placeholder="0 9 * * 1-5"
className="font-mono"
/>
<p className="text-xs text-muted-foreground">Examples: `0 9 * * 1-5` weekdays at 9 AM, `30 6 1 * *` monthly on the 1st at 6:30 AM.</p>
</div>
<div className="space-y-2">
<Label htmlFor="schedule-timezone">Timezone</Label>
<Input
id="schedule-timezone"
value={timezone}
onChange={(event) => onTimezoneChange(event.target.value)}
placeholder={getLocalTimeZone()}
/>
</div>
<div className="flex flex-wrap gap-2">
{cronPresetOptions.map((option) => (
<Button key={option.value} type="button" variant="outline" size="sm" onClick={() => onCronExpressionChange(option.value)}>
{option.label}
</Button>
))}
</div>
</div>
)}
<div className="rounded-lg border border-border bg-muted/40 p-4">
<p className="text-xs font-medium uppercase tracking-wide text-muted-foreground">Schedule Preview</p>
<p className="mt-2 text-sm font-medium break-words">{formatDraftScheduleSummary({ preset: schedulePreset, intervalMinutes, timeOfDay, hourlyMinute, weeklyDays, monthlyDay, cronExpression, timezone })}</p>
{schedulePreset !== 'interval' && (
<p className="mt-2 text-xs text-muted-foreground font-mono break-all">
{buildCronExpressionFromPreset({ preset: schedulePreset, intervalMinutes, timeOfDay, hourlyMinute, weeklyDays, monthlyDay, cronExpression })}
</p>
)}
</div>
</div>
</TabsContent>
)
}
+30 -33
View File
@@ -73,17 +73,16 @@ export function useRepoScheduleRun(repoId: number | undefined, jobId: number | n
})
}
export function useCreateRepoSchedule(repoId: number | undefined) {
export function useCreateRepoSchedule() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ repoId: callRepoId, data }: { repoId?: number; data: CreateScheduleJobRequest }) => {
const resolvedRepoId = callRepoId ?? repoId
const response = await createRepoSchedule(resolvedRepoId!, data)
mutationFn: async ({ repoId, data }: { repoId: number; data: CreateScheduleJobRequest }) => {
const response = await createRepoSchedule(repoId, data)
return response.job
},
onSuccess: (__variables) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedules', __variables.repoId ?? repoId] })
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
showToast.success('Schedule created')
},
@@ -93,18 +92,17 @@ export function useCreateRepoSchedule(repoId: number | undefined) {
})
}
export function useUpdateRepoSchedule(repoId: number | undefined) {
export function useUpdateRepoSchedule() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ repoId: callRepoId, jobId, data }: { repoId?: number; jobId: number; data: UpdateScheduleJobRequest }) => {
const resolvedRepoId = callRepoId ?? repoId
const response = await updateRepoSchedule(resolvedRepoId!, jobId, data)
mutationFn: async ({ repoId, jobId, data }: { repoId: number; jobId: number; data: UpdateScheduleJobRequest }) => {
const response = await updateRepoSchedule(repoId, jobId, data)
return response.job
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId ?? repoId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule', variables.repoId ?? repoId, variables.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule', variables.repoId, variables.jobId] })
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
showToast.success('Schedule updated')
},
@@ -114,16 +112,15 @@ export function useUpdateRepoSchedule(repoId: number | undefined) {
})
}
export function useDeleteRepoSchedule(repoId: number | undefined) {
export function useDeleteRepoSchedule() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ repoId: callRepoId, jobId }: { repoId?: number; jobId: number }) => {
const resolvedRepoId = callRepoId ?? repoId
return deleteRepoSchedule(resolvedRepoId!, jobId)
mutationFn: ({ repoId, jobId }: { repoId: number; jobId: number }) => {
return deleteRepoSchedule(repoId, jobId)
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId ?? repoId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
showToast.success('Schedule deleted')
},
@@ -133,20 +130,19 @@ export function useDeleteRepoSchedule(repoId: number | undefined) {
})
}
export function useRunRepoSchedule(repoId: number | undefined) {
export function useRunRepoSchedule() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ repoId: callRepoId, jobId }: { repoId?: number; jobId: number }) => {
const resolvedRepoId = callRepoId ?? repoId
const response = await runRepoSchedule(resolvedRepoId!, jobId)
mutationFn: async ({ repoId, jobId }: { repoId: number; jobId: number }) => {
const response = await runRepoSchedule(repoId, jobId)
return response.run
},
onSuccess: (run, variables) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId ?? repoId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId ?? repoId, run.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule', variables.repoId ?? repoId, run.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-run', variables.repoId ?? repoId, run.jobId, run.id] })
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, run.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule', variables.repoId, run.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-run', variables.repoId, run.jobId, run.id] })
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
showToast.success(run.status === 'running' ? 'Schedule started' : 'Schedule run completed')
},
@@ -156,19 +152,20 @@ export function useRunRepoSchedule(repoId: number | undefined) {
})
}
export function useCancelRepoScheduleRun(repoId: number | undefined) {
export function useCancelRepoScheduleRun() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ jobId, runId }: { jobId: number; runId: number }) => {
const response = await cancelRepoScheduleRun(repoId!, jobId, runId)
mutationFn: async ({ repoId, jobId, runId }: { repoId: number; jobId: number; runId: number }) => {
const response = await cancelRepoScheduleRun(repoId, jobId, runId)
return response.run
},
onSuccess: (run) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedules', repoId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', repoId, run.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule', repoId, run.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-run', repoId, run.jobId, run.id] })
onSuccess: (run, variables) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, run.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule', variables.repoId, run.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-run', variables.repoId, run.jobId, run.id] })
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
showToast.success('Schedule run cancelled')
},
onError: (error) => {
+4 -4
View File
@@ -33,10 +33,10 @@ export function GlobalSchedules() {
const { data: jobs = [], isLoading, error } = useAllSchedules()
const createMutation = useCreateRepoSchedule(undefined)
const deleteMutation = useDeleteRepoSchedule(undefined)
const runMutation = useRunRepoSchedule(undefined)
const updateMutation = useUpdateRepoSchedule(undefined)
const createMutation = useCreateRepoSchedule()
const deleteMutation = useDeleteRepoSchedule()
const runMutation = useRunRepoSchedule()
const updateMutation = useUpdateRepoSchedule()
const uniqueRepos = useMemo(() => {
const repoMap = new Map<string, { name: string; url: string }>()
+11 -8
View File
@@ -44,11 +44,11 @@ export function Schedules() {
const { data: runs, isLoading: runsLoading } = useRepoScheduleRuns(repoId, selectedJobId, 30)
const { data: selectedRunDetails, isLoading: selectedRunLoading } = useRepoScheduleRun(repoId, selectedJobId, selectedRunId)
const createMutation = useCreateRepoSchedule(repoId)
const updateMutation = useUpdateRepoSchedule(repoId)
const deleteMutation = useDeleteRepoSchedule(repoId)
const runMutation = useRunRepoSchedule(repoId)
const cancelRunMutation = useCancelRepoScheduleRun(repoId)
const createMutation = useCreateRepoSchedule()
const updateMutation = useUpdateRepoSchedule()
const deleteMutation = useDeleteRepoSchedule()
const runMutation = useRunRepoSchedule()
const cancelRunMutation = useCancelRepoScheduleRun()
useEffect(() => {
if (!jobs?.length) {
@@ -103,7 +103,7 @@ export function Schedules() {
const hasJobs = (jobs?.length ?? 0) > 0
const handleCreate = (data: CreateScheduleJobRequest) => {
createMutation.mutate({ data }, {
createMutation.mutate({ repoId: repoId!, data }, {
onSuccess: (job) => {
setSelectedJobId(job.id)
setDialogOpen(false)
@@ -118,6 +118,7 @@ export function Schedules() {
}
updateMutation.mutate({
repoId: repoId!,
jobId: editingJob.id,
data: toUpdateScheduleRequest(data),
}, {
@@ -133,7 +134,7 @@ export function Schedules() {
return
}
deleteMutation.mutate({ jobId: deleteJobId }, {
deleteMutation.mutate({ repoId: repoId!, jobId: deleteJobId }, {
onSuccess: () => {
if (selectedJobId === deleteJobId) {
setSelectedJobId(null)
@@ -149,6 +150,7 @@ export function Schedules() {
}
updateMutation.mutate({
repoId: repoId!,
jobId: selectedJob.id,
data: { enabled: !selectedJob.enabled },
})
@@ -159,7 +161,7 @@ export function Schedules() {
return
}
runMutation.mutate({ jobId: selectedJob.id }, {
runMutation.mutate({ repoId: repoId!, jobId: selectedJob.id }, {
onSuccess: (run) => {
setSelectedRunId(run.id)
},
@@ -172,6 +174,7 @@ export function Schedules() {
}
cancelRunMutation.mutate({
repoId: repoId!,
jobId: activeRun.jobId,
runId: activeRun.id,
}, {