import { suggestPullDescription } from "~/fns/pull-suggest";
import { useAction } from "./use-action";
type Fields = { title: string; body: string };
type Options = {
owner: string;
repo: string;
base: string;
head: string;
current: Fields;
/** Pre-filled values (first commit subject, PR template) that are safe to replace without asking. */
defaults: Fields;
onFill: (fields: Fields) => void;
};
const OVERWRITE_PROMPT = "Replace the title and description you have written with the AI suggestion?";
/** Asks the server for a drafted title and body, confirming before it overwrites text the user typed. */
export function usePullSuggestion(options: Options) {
const action = useAction(suggestPullDescription, { invalidate: false });
const edited =
(options.current.title.trim() !== "" && options.current.title !== options.defaults.title) ||
(options.current.body.trim() !== "" && options.current.body !== options.defaults.body);
async function suggest(): Promise<void> {
if (edited && !window.confirm(OVERWRITE_PROMPT)) return;
const result = await action.run({
data: { owner: options.owner, repo: options.repo, base: options.base, head: options.head },
});
if (result) options.onFill(result);
}
return { suggest, pending: action.pending, error: action.error };
}