import {
ActorBadge,
Badge,
BodyDiff,
Button,
Checkbox,
InsetPanel,
SkeletonLine,
Text,
Tooltip,
} from "@cloudhal/ui";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useState } from "react";
import { type TFunction, useT } from "../i18n/index.tsx";
import { api, type ProposalItem, type ProposalStep } from "../lib/api.ts";
import { apiErrorText } from "../lib/api-errors.ts";
import { useBodyDiffLabels } from "./BodyChanges.tsx";
import { RelativeTime } from "./RelativeTime.tsx";
import { useToast } from "./Toast.ts";
/**
* Agents' suggestions for a body (RFC-0014 §5, DESIGN.md §2y): each open proposal as the change it
* would make now (`BodyDiff`), with Accept and Reject. Accepting writes the body at once, so it
* waits while the form has unsaved changes; a suggestion whose blocks changed since is out of date
* and can only be rejected. A suggestion of several operations lists them with checkboxes: the
* diff follows the choice (`POST …/preview`) and Accept applies only the chosen ones.
*/
export function BodyProposals({
siteId,
nodeId,
sectionId,
dirty,
readOnly = false,
onDecided,
}: {
siteId: string;
/** The article, or the page whose rich-text section `sectionId` is shown. */
nodeId: string;
sectionId?: string;
/** The form has unsaved changes: accepting would overwrite them. */
dirty: boolean;
readOnly?: boolean;
/** After an accept: reload the article or page. */
onDecided: () => void;
}) {
const t = useT();
const toast = useToast();
const labels = useBodyDiffLabels();
const queryClient = useQueryClient();
const key = ["proposals", siteId, nodeId];
const list = useQuery({ queryKey: key, queryFn: () => api.proposals.list(siteId, nodeId) });
const decide = useMutation({
mutationFn: ({ id, accept, ops }: { id: string; accept: boolean; ops?: number[] }) =>
accept ? api.proposals.accept(siteId, id, ops) : api.proposals.reject(siteId, id),
onSuccess: (_r, { accept }) => {
toast.success(accept ? t("proposals.accepted") : t("proposals.rejected"));
if (accept) onDecided();
},
onError: (e) => {
toast.error(t("proposals.failed"), apiErrorText(t, e));
onDecided();
},
onSettled: () => queryClient.invalidateQueries({ queryKey: key }),
});
const items = (list.data ?? []).filter((i) => i.proposal.sectionId === sectionId);
if (items.length === 0) return null;
return (