"use client"; import { useCallback, useState } from "react"; import { useTranslation } from "react-i18next"; import { useToast } from "@/components/toast-provider"; import { useTaskCIAutomationOptions } from "@/hooks/domains/github/use-task-ci-options"; import { findCIAutomationStateForPR, findPRAutomationOptionsForPR, deriveCIAutomationQueueState, } from "@/lib/github/ci-automation"; import type { TaskCIAutomationPatch, TaskPR } from "@/lib/types/github"; import { CIAutomationPromptDialog } from "@/components/github/pr-ci-automation-prompt-dialog"; import { CIAutomationErrorRow, CIAutomationHeader, CIAutomationOptionRows, } from "@/components/github/pr-ci-automation-rows"; export function PRCIAutomationControls({ pr }: { pr: TaskPR }) { const { options, loading, saving, error, refresh, update, resetPrompt, retryMerge } = useTaskCIAutomationOptions(pr.task_id); const { toast } = useToast(); const { t } = useTranslation(); const [promptOpen, setPromptOpen] = useState(false); const [promptDraft, setPromptDraft] = useState(""); const automationState = findCIAutomationStateForPR(options?.pr_states, pr); const prOptions = findPRAutomationOptionsForPR(options?.pr_options, pr); const queueState = deriveCIAutomationQueueState(pr, prOptions, automationState); const openPromptEditor = useCallback(() => { setPromptDraft(options?.auto_fix_prompt_override ?? options?.effective_auto_fix_prompt ?? ""); setPromptOpen(true); }, [options]); const reportError = useCallback( (description: string) => { toast({ description, variant: "error" }); }, [toast], ); // The five automation switches are scoped to this PR; every patch from the // switch rows below carries this PR's identity so the backend applies it // to this PR only instead of fanning out to every linked PR. const patchOption = useCallback( (patch: TaskCIAutomationPatch) => { const scoped: TaskCIAutomationPatch = { ...patch, repository_id: pr.repository_id ?? "", pr_number: pr.pr_number, }; Promise.resolve(update(scoped)).catch(() => reportError(t("github:failedToUpdateCiAutomation")), ); }, [pr, reportError, t, update], ); // The auto-fix prompt override stays task-level (applies to every linked // PR), so this patch intentionally omits PR identity. const savePrompt = useCallback(() => { const value = promptDraft.trim(); if (!value) return; Promise.resolve(update({ auto_fix_prompt_override: value })) .then(() => setPromptOpen(false)) .catch(() => reportError(t("github:failedToSaveAutoFixPrompt"))); }, [promptDraft, reportError, t, update]); const useDefaultPrompt = useCallback(() => { Promise.resolve(resetPrompt()) .then(() => setPromptOpen(false)) .catch(() => reportError(t("github:failedToResetAutoFixPrompt"))); }, [reportError, resetPrompt, t]); const retryLoad = useCallback(() => { Promise.resolve(refresh()).catch(() => reportError(t("github:failedToLoadCiAutomationOptions")), ); }, [refresh, reportError, t]); const retryAutoMerge = useCallback(() => { Promise.resolve(retryMerge(pr.repository_id ?? "", pr.pr_number)) .then(() => toast({ description: t("github:autoMergeRetryAccepted") })) .catch(() => reportError(t("github:failedToRetryAutoMerge"))); }, [pr.pr_number, pr.repository_id, reportError, retryMerge, t, toast]); const disabled = loading || saving || !options; return (