import { useEffect, useState } from "react"; import Form from "@moduletrace/twui/form/Form"; import Select from "@moduletrace/twui/form/Select"; import Textarea from "@moduletrace/twui/form/Textarea"; import Span from "@moduletrace/twui/layout/Span"; import Stack from "@moduletrace/twui/layout/Stack"; import postAdminApi from "@/src/layouts/bots/(functions)/post-admin-api"; import AddFormActions from "@/src/layouts/bots/(partials)/add-form-actions"; import type { HandOverTarget } from "@/src/types"; type Props = { /** * What is handed over, for the confirm ("the task "Draft the post""). */ what: string; /** * Under `/api/admin/`: who it can go to, and the hand-over itself. * Both get `body`; the hand-over also the picked `bot_id`. */ targets_path: string; submit_path: string; body: Record; /** * Offer a note added to the brief (a task's hand-over). */ with_note?: boolean; onHandedOver: (params: { saved: T; msg: string }) => void; onCancel: () => void; }; /** * Picks the bot a task or routine goes to (the ones already where it runs, * loaded when the form opens) and hands it over. Shared by the task panel * and both routine lists. */ export default function HandOverForm({ what, targets_path, submit_path, body, with_note, onHandedOver, onCancel, }: Props) { const [targets, setTargets] = useState(); const [bot_id, setBotId] = useState(); const [note, setNote] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(); useEffect(() => { postAdminApi({ path: targets_path, body }).then((res) => { if (!res.success) { setError(res.msg || "Couldn't load the bots."); setTargets([]); return; } const list = (res.payload || []) as HandOverTarget[]; setTargets(list); setBotId(list[0]?.id); }); }, [targets_path]); const submit = async () => { if (loading) return; const target = targets?.find((it) => it.id == bot_id); if (!target) { setError("Pick a bot."); return; } if (!window.confirm(`Hand ${what} over to ${target.name}?`)) return; setLoading(true); setError(undefined); const res = await postAdminApi({ path: submit_path, body: { ...body, bot_id: target.id, instructions: with_note && note.trim() ? note : undefined }, }); setLoading(false); if (!res.success || !res.singleRes) { setError(res.msg || "Couldn't hand it over."); return; } onHandedOver({ saved: res.singleRes, msg: res.msg || `Handed over to ${target.name}.` }); }; return (
{!targets ? ( Loading the bots... ) : targets.length ? (