mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
refactor(onboarding): extract useCreateAgent and useDeleteAgent hooks
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
808e60557b
commit
8c1025ece9
@@ -0,0 +1,41 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useMutation } from "@tanstack/react-query";
|
||||||
|
import { useOnboarding } from "@onboardjs/react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { createAgentAction } from "@/features/agents/agents.action";
|
||||||
|
import type { OnboardingAgent, OnboardingDefaultsData } from "@/features/onboarding/types";
|
||||||
|
|
||||||
|
export const useCreateAgent = () => {
|
||||||
|
const { state, updateContext } = useOnboarding();
|
||||||
|
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async (name: string) => {
|
||||||
|
const orgId = (state?.context.flowData.org as any)?.id as string | undefined;
|
||||||
|
if (!orgId) throw new Error("Missing org ID — cannot create agent");
|
||||||
|
|
||||||
|
const defaults = (state?.context.flowData.defaults ?? {}) as OnboardingDefaultsData;
|
||||||
|
const result = await createAgentAction({
|
||||||
|
organizationId: orgId,
|
||||||
|
data: { name, description: "" },
|
||||||
|
});
|
||||||
|
if (!result?.data?.data) {
|
||||||
|
throw new Error(result?.serverError ?? `Failed to create agent "${name}"`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const newAgent: OnboardingAgent = {
|
||||||
|
id: result.data.data.id,
|
||||||
|
name: result.data.data.name,
|
||||||
|
notifierId: defaults.notifierId,
|
||||||
|
storageId: defaults.storageId,
|
||||||
|
};
|
||||||
|
const agents = [
|
||||||
|
...((state?.context.flowData.agents ?? []) as OnboardingAgent[]),
|
||||||
|
newAgent,
|
||||||
|
];
|
||||||
|
await updateContext({ flowData: { ...state?.context.flowData, agents } });
|
||||||
|
return newAgent;
|
||||||
|
},
|
||||||
|
onError: (err: Error) => toast.error(err.message),
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useMutation } from "@tanstack/react-query";
|
||||||
|
import { useOnboarding } from "@onboardjs/react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { deleteAgentAction } from "@/features/agents/agent-delete.action";
|
||||||
|
import type { OnboardingAgent } from "@/features/onboarding/types";
|
||||||
|
|
||||||
|
export const useDeleteAgent = () => {
|
||||||
|
const { state, updateContext } = useOnboarding();
|
||||||
|
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async (agentId: string) => {
|
||||||
|
const orgId = (state?.context.flowData.org as any)?.id as string | undefined;
|
||||||
|
const result = await deleteAgentAction({ agentId, organizationId: orgId });
|
||||||
|
if (result?.data?.success === false) throw new Error("Failed to delete agent");
|
||||||
|
|
||||||
|
const agents = (
|
||||||
|
(state?.context.flowData.agents ?? []) as OnboardingAgent[]
|
||||||
|
).filter((a) => a.id !== agentId);
|
||||||
|
await updateContext({ flowData: { ...state?.context.flowData, agents } });
|
||||||
|
},
|
||||||
|
onError: (err: Error) => toast.error(err.message),
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -2,118 +2,77 @@
|
|||||||
|
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useOnboarding } from "@onboardjs/react";
|
import { useOnboarding } from "@onboardjs/react";
|
||||||
import { toast } from "sonner";
|
import { X } from "lucide-react";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { X } from "lucide-react";
|
import { useCreateAgent } from "@/features/onboarding/hooks/use-create-agent";
|
||||||
import { createAgentAction } from "@/features/agents/agents.action";
|
import { useDeleteAgent } from "@/features/onboarding/hooks/use-delete-agent";
|
||||||
import { deleteAgentAction } from "@/features/agents/agent-delete.action";
|
import type { OnboardingAgent } from "@/features/onboarding/types";
|
||||||
import type { OnboardingAgent, OnboardingDefaultsData } from "@/features/onboarding/types";
|
|
||||||
|
|
||||||
export const StepAgentCreate = () => {
|
export const StepAgentCreate = () => {
|
||||||
const { next, updateContext, state } = useOnboarding();
|
const { next, state } = useOnboarding();
|
||||||
const defaults = (state?.context.flowData.defaults ?? {}) as OnboardingDefaultsData;
|
const agents = (state?.context.flowData.agents ?? []) as OnboardingAgent[];
|
||||||
const agents = (state?.context.flowData.agents ?? []) as OnboardingAgent[];
|
|
||||||
const orgId = (state?.context.flowData.org as any)?.id as string | undefined;
|
|
||||||
|
|
||||||
|
const [name, setName] = useState("");
|
||||||
|
const createAgent = useCreateAgent();
|
||||||
|
const deleteAgent = useDeleteAgent();
|
||||||
|
|
||||||
const [name, setName] = useState("");
|
const onAdd = () => {
|
||||||
const [adding, setAdding] = useState(false);
|
if (!name.trim()) return;
|
||||||
|
createAgent.mutate(name.trim(), { onSuccess: () => setName("") });
|
||||||
|
};
|
||||||
|
|
||||||
const addAgent = async () => {
|
return (
|
||||||
if (!name.trim()) return;
|
<div className="flex flex-col gap-4">
|
||||||
if (!orgId) {
|
<div>
|
||||||
toast.error("Missing org ID — cannot create agent");
|
<h1 className="text-2xl font-semibold">Create an agent</h1>
|
||||||
return;
|
<p className="text-sm text-muted-foreground mt-1">
|
||||||
}
|
Optional — agents will use your default notifier and storage.
|
||||||
setAdding(true);
|
</p>
|
||||||
try {
|
</div>
|
||||||
const result = await createAgentAction({
|
|
||||||
organizationId: orgId,
|
|
||||||
data: { name: name.trim(), description: "" },
|
|
||||||
});
|
|
||||||
if (!result?.data?.data) {
|
|
||||||
toast.error(result?.serverError ?? `Failed to create agent "${name.trim()}"`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const newAgent: OnboardingAgent = {
|
|
||||||
id: result.data.data.id,
|
|
||||||
name: result.data.data.name,
|
|
||||||
notifierId: defaults.notifierId,
|
|
||||||
storageId: defaults.storageId,
|
|
||||||
};
|
|
||||||
const updated = [...agents, newAgent];
|
|
||||||
setName("");
|
|
||||||
await updateContext({ flowData: { ...state?.context.flowData, agents: updated } });
|
|
||||||
} finally {
|
|
||||||
setAdding(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const removeAgent = async (id: string) => {
|
{agents.length > 0 && (
|
||||||
const updated = agents.filter((a) => a.id !== id);
|
<div className="flex flex-col gap-1">
|
||||||
const result = await deleteAgentAction({ agentId: id, organizationId: orgId });
|
{agents.map((agent) => (
|
||||||
if (result?.data?.success === false) {
|
<div
|
||||||
toast.error("Failed to delete agent");
|
key={agent.id}
|
||||||
} else {
|
className="flex items-center gap-2 rounded-lg border border-primary/20 bg-primary/10 p-2 text-sm text-primary"
|
||||||
await updateContext({ flowData: { ...state?.context.flowData, agents: updated } });
|
>
|
||||||
}
|
<span className="flex-1 truncate">{agent.name}</span>
|
||||||
};
|
<button
|
||||||
|
type="button"
|
||||||
const onContinue = async () => {
|
onClick={() => deleteAgent.mutate(agent.id)}
|
||||||
await next();
|
disabled={deleteAgent.isPending}
|
||||||
};
|
className="opacity-50 hover:opacity-100 transition-opacity"
|
||||||
|
>
|
||||||
return (
|
<X className="size-4" />
|
||||||
<div className="flex flex-col gap-4">
|
</button>
|
||||||
<div>
|
|
||||||
<h1 className="text-2xl font-semibold">Create an agent</h1>
|
|
||||||
<p className="text-sm text-muted-foreground mt-1">
|
|
||||||
Optional — agents will use your default notifier and storage.
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
))}
|
||||||
{agents.length > 0 && (
|
|
||||||
<div className="flex flex-col gap-1">
|
|
||||||
{agents.map((agent) => (
|
|
||||||
<div
|
|
||||||
key={agent.id}
|
|
||||||
className="flex items-center gap-2 rounded-lg border border-primary/20 bg-primary/10 p-2 text-sm text-primary"
|
|
||||||
>
|
|
||||||
<span className="flex-1 truncate">{agent.name}</span>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={() => removeAgent(agent.id)}
|
|
||||||
className="opacity-50 hover:opacity-100 transition-opacity"
|
|
||||||
>
|
|
||||||
<X className="size-4" />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<div className="flex gap-2">
|
|
||||||
<Input
|
|
||||||
value={name}
|
|
||||||
onChange={(e) => setName(e.target.value)}
|
|
||||||
placeholder="agent-prod"
|
|
||||||
onKeyDown={(e) => e.key === "Enter" && addAgent()}
|
|
||||||
disabled={adding}
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
onClick={addAgent}
|
|
||||||
disabled={adding || !name.trim()}
|
|
||||||
>
|
|
||||||
{adding ? "Adding…" : "Add"}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Button type="button" onClick={onContinue}>
|
|
||||||
Continue
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
)}
|
||||||
|
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Input
|
||||||
|
value={name}
|
||||||
|
onChange={(e) => setName(e.target.value)}
|
||||||
|
placeholder="agent-prod"
|
||||||
|
onKeyDown={(e) => e.key === "Enter" && onAdd()}
|
||||||
|
disabled={createAgent.isPending}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
onClick={onAdd}
|
||||||
|
disabled={createAgent.isPending || !name.trim()}
|
||||||
|
>
|
||||||
|
{createAgent.isPending ? "Adding…" : "Add"}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Button type="button" onClick={() => next()}>
|
||||||
|
Continue
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user