diff --git a/web/src/app/(main)/system/agents/_state/create-agent-dialog.reducer.ts b/web/src/app/(main)/system/agents/_state/create-agent-dialog.reducer.ts
new file mode 100644
index 00000000..4d554cc2
--- /dev/null
+++ b/web/src/app/(main)/system/agents/_state/create-agent-dialog.reducer.ts
@@ -0,0 +1,53 @@
+import {
+ createRequests,
+ isRequestAction,
+ type RequestAction,
+ type Requests,
+ requestReducer,
+} from "@/lib/reducers/request";
+import { setSlice } from "@/lib/reducers/slice";
+
+export type CreateAgentForm = {
+ key: string;
+ name: string;
+ description: string;
+};
+
+type CreateAgentRequest = "create";
+
+export type CreateAgentDialogState = {
+ open: boolean;
+ form: CreateAgentForm;
+ requests: Requests;
+};
+
+export type CreateAgentDialogAction =
+ | RequestAction
+ | { type: "openChanged"; open: boolean }
+ | { type: "formFieldChanged"; patch: Partial }
+ | { type: "created" };
+
+const EMPTY_FORM: CreateAgentForm = { key: "", name: "", description: "" };
+
+export const initialCreateAgentDialogState: CreateAgentDialogState = {
+ open: false,
+ form: EMPTY_FORM,
+ requests: createRequests(["create"]),
+};
+
+export function createAgentDialogReducer(
+ state: CreateAgentDialogState,
+ action: CreateAgentDialogAction,
+): CreateAgentDialogState {
+ if (isRequestAction(action)) {
+ return setSlice(state, "requests", requestReducer(state.requests, action));
+ }
+ switch (action.type) {
+ case "openChanged":
+ return action.open === state.open ? state : { ...state, open: action.open };
+ case "formFieldChanged":
+ return { ...state, form: { ...state.form, ...action.patch } };
+ case "created":
+ return { ...state, open: false, form: EMPTY_FORM };
+ }
+}
diff --git a/web/src/app/(main)/system/agents/page.tsx b/web/src/app/(main)/system/agents/page.tsx
index c16e4042..50d93aad 100644
--- a/web/src/app/(main)/system/agents/page.tsx
+++ b/web/src/app/(main)/system/agents/page.tsx
@@ -37,6 +37,8 @@ import { t } from "@/i18n";
import { api } from "@/lib/api";
import type { Agent } from "@/lib/types";
+import { createAgentDialogReducer, initialCreateAgentDialogState } from "./_state/create-agent-dialog.reducer";
+
// AgentGridCard is one clickable tile opening the agent's editor drawer. Custom
// (non-builtin) agents get a delete button.
function AgentGridCard({ agent, onOpen, onDeleted }: { agent: Agent; onOpen: () => void; onDeleted: () => void }) {
@@ -113,26 +115,22 @@ function AgentGridCard({ agent, onOpen, onDeleted }: { agent: Agent; onOpen: ()
}
function CreateAgentDialog({ onCreated }: { onCreated: (key: string) => void }) {
- const [open, setOpen] = React.useState(false);
- const [key, setKey] = React.useState("");
- const [name, setName] = React.useState("");
- const [description, setDescription] = React.useState("");
- const [busy, setBusy] = React.useState(false);
+ const [state, dispatch] = React.useReducer(createAgentDialogReducer, initialCreateAgentDialogState);
+ const { open, form } = state;
+ const { key, name, description } = form;
+ const busy = state.requests.create.pending;
async function create() {
- setBusy(true);
+ dispatch({ type: "request/started", key: "create" });
try {
const a = await api.createAgent(key.trim(), name.trim(), description.trim());
toast.success(t('Agent "{name}" created', { name: a.name }));
- setOpen(false);
- setKey("");
- setName("");
- setDescription("");
+ dispatch({ type: "created" });
onCreated(a.key);
} catch (e) {
toast.error(t("Create failed: {error}", { error: (e as Error).message }));
} finally {
- setBusy(false);
+ dispatch({ type: "request/finished", key: "create" });
}
}
@@ -140,7 +138,7 @@ function CreateAgentDialog({ onCreated }: { onCreated: (key: string) => void })
const canCreate = keyOk && name.trim().length > 0 && !busy;
return (
-
-