|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useState } from "react" |
| 4 | +import { useRouter } from "next/navigation" |
| 5 | +import { useForm, FormProvider } from "react-hook-form" |
| 6 | +import { zodResolver } from "@hookform/resolvers/zod" |
| 7 | +import { z } from "zod" |
| 8 | + |
| 9 | +import { useOpenRouterModels } from "@/hooks/use-open-router-models" |
| 10 | +import { |
| 11 | + Button, |
| 12 | + FormControl, |
| 13 | + FormDescription, |
| 14 | + FormField, |
| 15 | + FormItem, |
| 16 | + FormLabel, |
| 17 | + FormMessage, |
| 18 | + Select, |
| 19 | + SelectContent, |
| 20 | + SelectItem, |
| 21 | + SelectTrigger, |
| 22 | + SelectValue, |
| 23 | + Textarea, |
| 24 | +} from "@/components/ui" |
| 25 | + |
| 26 | +import { createRun } from "./actions" |
| 27 | + |
| 28 | +const formSchema = z.object({ |
| 29 | + model: z.string({ |
| 30 | + required_error: "Please select a model", |
| 31 | + }), |
| 32 | + description: z.string().optional(), |
| 33 | +}) |
| 34 | + |
| 35 | +type FormValues = z.infer<typeof formSchema> |
| 36 | + |
| 37 | +export function NewRun() { |
| 38 | + const router = useRouter() |
| 39 | + const { data: models, isLoading, error } = useOpenRouterModels() |
| 40 | + const [isSubmitting, setIsSubmitting] = useState(false) |
| 41 | + |
| 42 | + const form = useForm<FormValues>({ |
| 43 | + resolver: zodResolver(formSchema), |
| 44 | + }) |
| 45 | + |
| 46 | + async function onSubmit(data: FormValues) { |
| 47 | + setIsSubmitting(true) |
| 48 | + |
| 49 | + try { |
| 50 | + const run = await createRun(data) |
| 51 | + router.push(`/runs/${run.id}`) |
| 52 | + } catch (error) { |
| 53 | + console.error("Error creating run:", error) |
| 54 | + setIsSubmitting(false) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className="space-y-6 max-w-2xl mx-auto p-6"> |
| 60 | + <div className="space-y-2"> |
| 61 | + <h1 className="text-2xl font-bold">Create New Run</h1> |
| 62 | + <p className="text-muted-foreground"> |
| 63 | + Create a new run by selecting a model and providing an optional description. |
| 64 | + </p> |
| 65 | + </div> |
| 66 | + <div> |
| 67 | + <FormProvider {...form}> |
| 68 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6"> |
| 69 | + <FormField |
| 70 | + control={form.control} |
| 71 | + name="model" |
| 72 | + render={({ field }) => ( |
| 73 | + <FormItem> |
| 74 | + <FormLabel>Model</FormLabel> |
| 75 | + <Select onValueChange={field.onChange} defaultValue={field.value}> |
| 76 | + <FormControl> |
| 77 | + <SelectTrigger> |
| 78 | + <SelectValue placeholder="Select a model" /> |
| 79 | + </SelectTrigger> |
| 80 | + </FormControl> |
| 81 | + <SelectContent> |
| 82 | + {isLoading ? ( |
| 83 | + <div className="p-2 text-center text-muted-foreground"> |
| 84 | + Loading models... |
| 85 | + </div> |
| 86 | + ) : error ? ( |
| 87 | + <div className="p-2 text-center text-destructive"> |
| 88 | + Error loading models. Using fallback options. |
| 89 | + </div> |
| 90 | + ) : ( |
| 91 | + models?.map((model) => ( |
| 92 | + <SelectItem key={model.id} value={model.id}> |
| 93 | + {model.name} |
| 94 | + </SelectItem> |
| 95 | + )) |
| 96 | + )} |
| 97 | + </SelectContent> |
| 98 | + </Select> |
| 99 | + <FormDescription>Select the model to use for this run.</FormDescription> |
| 100 | + <FormMessage /> |
| 101 | + </FormItem> |
| 102 | + )} |
| 103 | + /> |
| 104 | + <FormField |
| 105 | + control={form.control} |
| 106 | + name="description" |
| 107 | + render={({ field }) => ( |
| 108 | + <FormItem> |
| 109 | + <FormLabel>Description</FormLabel> |
| 110 | + <FormControl> |
| 111 | + <Textarea |
| 112 | + placeholder="Enter a description for this run (optional)" |
| 113 | + className="resize-none" |
| 114 | + {...field} |
| 115 | + /> |
| 116 | + </FormControl> |
| 117 | + <FormDescription> |
| 118 | + Provide an optional description to help identify this run. |
| 119 | + </FormDescription> |
| 120 | + <FormMessage /> |
| 121 | + </FormItem> |
| 122 | + )} |
| 123 | + /> |
| 124 | + <Button type="submit" disabled={isSubmitting}> |
| 125 | + {isSubmitting ? "Creating..." : "Create Run"} |
| 126 | + </Button> |
| 127 | + </form> |
| 128 | + </FormProvider> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ) |
| 132 | +} |
0 commit comments