"use client";
import { useEffect, useState } from "react";
import { IconGitMerge } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { useToast } from "@/components/toast-provider";
import { mergePR } from "@/lib/api/domains/github-api";
import type { TaskPR } from "@/lib/types/github";
import { isPRReadyToMerge } from "./pr-task-icon";
// Renders nothing unless the PR is fully green (CI success + mergeable +
// approval or no-review-needed). Shared by the PR detail panel header and
// the topbar hover popover so a "ready" PR can be merged from either
// surface. `compact` switches to the smaller pill variant used inside the
// dense popover.
export function PRMergeButton({
taskPR,
onMerged,
compact = false,
}: {
taskPR: TaskPR;
onMerged?: () => void;
compact?: boolean;
}) {
const { toast } = useToast();
const [merging, setMerging] = useState(false);
// After a successful merge we stay hidden until the store catches up to
// state="merged" — otherwise the button briefly re-enables during the async
// refresh window and a double-click would hit the GitHub API again.
const [merged, setMerged] = useState(false);
// If the same component instance ever renders a different PR (e.g. the user
// switches the active task while the panel/popover stays mounted), the
// sticky `merged` flag from a previous merge would hide the button for an
// unrelated, still-mergeable PR. Reset it whenever the underlying PR id
// changes.
useEffect(() => {
setMerged(false);
}, [taskPR.id]);
if (merged || !isPRReadyToMerge(taskPR)) return null;
const handleMerge = async (e: React.MouseEvent) => {
e.stopPropagation();
setMerging(true);
try {
await mergePR(taskPR.owner, taskPR.repo, taskPR.pr_number);
setMerged(true);
toast({ description: "PR merged", variant: "success" });
onMerged?.();
} catch (err) {
toast({
title: "Failed to merge",
description: err instanceof Error ? err.message : "An error occurred",
variant: "error",
});
} finally {
setMerging(false);
}
};
if (compact) {
return (
);
}
return (
);
}