-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from tylerslaton/overhaul-agent-edit
feat: overhaul agent edit UX
- Loading branch information
Showing
14 changed files
with
459 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { useEffect } from "react"; | ||
import { useForm } from "react-hook-form"; | ||
import { z } from "zod"; | ||
|
||
import { Agent } from "~/lib/model/agents"; | ||
|
||
import { ControlledTextarea } from "~/components/form/controlledInputs"; | ||
import { Form } from "~/components/ui/form"; | ||
|
||
const formSchema = z.object({ | ||
prompt: z.string().optional(), | ||
}); | ||
|
||
export type AdvancedFormValues = z.infer<typeof formSchema>; | ||
|
||
type AdvancedFormProps = { | ||
agent: Agent; | ||
onSubmit?: (values: AdvancedFormValues) => void; | ||
onChange?: (values: AdvancedFormValues) => void; | ||
}; | ||
|
||
export function AdvancedForm({ agent, onSubmit, onChange }: AdvancedFormProps) { | ||
const form = useForm<AdvancedFormValues>({ | ||
resolver: zodResolver(formSchema), | ||
mode: "onChange", | ||
defaultValues: { | ||
prompt: agent.prompt || "", | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (agent) form.reset({ prompt: agent.prompt || "" }); | ||
}, [agent, form]); | ||
|
||
useEffect(() => { | ||
return form.watch((values) => { | ||
if (!onChange) return; | ||
|
||
const { data, success } = formSchema.safeParse(values); | ||
|
||
if (!success) return; | ||
|
||
onChange(data); | ||
}).unsubscribe; | ||
}, [onChange, form]); | ||
|
||
const handleSubmit = form.handleSubmit((values: AdvancedFormValues) => | ||
onSubmit?.({ ...values }) | ||
); | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={handleSubmit} className="space-y-8"> | ||
<ControlledTextarea | ||
control={form.control} | ||
name="prompt" | ||
label="Additional Instructions" | ||
description="Give the agent additional instructions on how it should behave." | ||
placeholder="Talk like a pirate." | ||
/> | ||
</form> | ||
</Form> | ||
); | ||
} |
Oops, something went wrong.