import { useState } from "react"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { Check, Loader2, Copy } from "lucide-react"; import { toast } from "sonner"; interface CommentItemProps { comment: any; // Type strictly if possible, but any is fine for MVP owner: string; repo: string; pullNumber: number; currentUser: any; onReply?: (commentId: string) => void; } export function CommentItem({ comment, owner, repo, pullNumber, currentUser, onReply }: CommentItemProps) { const [isApplying, setIsApplying] = useState(false); // Parse suggestion const suggestionRegex = /```suggestion\n([\s\S]*?)```/; const match = comment.body.match(suggestionRegex); const hasSuggestion = !!match; const suggestionContent = match ? match[1] : null; // Text parts (before and after suggestion) const parts = comment.body.split(suggestionRegex); const beforeText = parts[0]; const afterText = parts[2] || ""; // parts[1] is the capturing group const handleApply = async () => { if (!confirm("Are you sure you want to apply this suggestion?")) return; setIsApplying(true); try { const res = await fetch(`/api/repos/${owner}/${repo}/pulls/${pullNumber}/comments/apply-suggestion`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ commentId: comment.id, commitMessage: `Apply suggestion from @${comment.author.username}` }) }); if (!res.ok) { const data = await res.json(); throw new Error(data.message || "Failed to apply suggestion"); } toast.success("Suggestion applied!"); // Reload to show changes setTimeout(() => window.location.reload(), 1000); } catch (e: any) { toast.error(e.message); } finally { setIsApplying(false); } }; return (
{beforeText}
} {/* Suggestion Block */} {hasSuggestion && suggestionContent && ({suggestionContent}
{afterText}
}