swadhinbiswas ✨ feat: improve InlineDiffReview.tsx · 7/24/2026
8a5ae62
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { DiffView } from "@/components/diff/DiffView";
import { useCallback, useState, useEffect } from "react";
import { toast } from "sonner";
interface InlineDiffReviewProps {
rawDiff: string;
owner: string;
repo: string;
pullNumber: number;
repoUrl: string;
currentUser?: any;
baseSha?: string;
headSha?: string;
}
/**
* Wrapper around DiffView that adds inline comment functionality for PR reviews.
* Shows a comment form when user clicks the "+" button on a diff line.
*/
export function InlineDiffReview({
rawDiff,
owner,
repo,
pullNumber,
repoUrl,
currentUser,
headSha,
}: InlineDiffReviewProps) {
const [commentForm, setCommentForm] = useState<{
filePath: string;
line: number;
side: "LEFT" | "RIGHT";
} | null>(null);
const [commentBody, setCommentBody] = useState("");
const [isSubmitting, setIsSubmitting] = useState(false);
const [suggestionMode, setSuggestionMode] = useState(false);
// Partial File Approvals state
const [fileApprovals, setFileApprovals] = useState<Record<string, boolean>>({});
// Code Quality state
const [codeQualityIssues, setCodeQualityIssues] = useState<Array<{
filePath: string;
line: number;
message: string;
severity: string;
provider: string;
}>>([]);
const fetchApprovals = useCallback(async () => {
if (!currentUser) return;
try {
const res = await fetch(`/api/repos/${owner}/${repo}/pulls/${pullNumber}/file-approvals`);
if (res.ok) {
const data = await res.json();
const approvalMap: Record<string, boolean> = {};
for (const approval of data.approvals || []) {
if (approval.approvers?.includes(currentUser.username) || approval.approvedById === currentUser.id) {
approvalMap[approval.path] = true;
}
}
setFileApprovals(approvalMap);
}
} catch {}
}, [owner, repo, pullNumber, currentUser]);
const fetchCodeQuality = useCallback(async () => {
try {
const res = await fetch(`/api/repos/${owner}/${repo}/pulls/${pullNumber}/code-quality`);
if (res.ok) {
const data = await res.json();
setCodeQualityIssues(data);
}
} catch {}
}, [owner, repo, pullNumber]);
useEffect(() => {
fetchApprovals();
fetchCodeQuality();
}, [fetchApprovals, fetchCodeQuality]);
const handleApproveFile = useCallback(async (filePath: string, approved: boolean) => {
try {
if (approved) {
// Create approval
await fetch(`/api/repos/${owner}/${repo}/pulls/${pullNumber}/file-approvals`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ path: filePath, commitSha: headSha || "unknown" })
});
setFileApprovals(prev => ({ ...prev, [filePath]: true }));
} else {
await fetch(`/api/repos/${owner}/${repo}/pulls/${pullNumber}/file-approvals?path=${encodeURIComponent(filePath)}`, {
method: "DELETE"
});
setFileApprovals(prev => ({ ...prev, [filePath]: false }));
}
} catch {
toast.error("Failed to toggle approval");
}
}, [owner, repo, pullNumber, headSha, currentUser]);
const handleAddComment = useCallback(
(filePath: string, line: number, side: "LEFT" | "RIGHT") => {
setCommentForm({ filePath, line, side });
setCommentBody("");
setSuggestionMode(false);
},
[],
);
const handleCancel = useCallback(() => {
setCommentForm(null);
setCommentBody("");
setSuggestionMode(false);
}, []);
const handleSubmit = useCallback(async () => {
if (!commentForm || !commentBody.trim()) return;
setIsSubmitting(true);
try {
const payload: any = {
body: commentBody,
path: commentForm.filePath,
line: commentForm.line,
side: commentForm.side,
};
if (headSha) {
payload.commitSha = headSha;
}
// Check for suggestion syntax
if (
commentBody.includes("```suggestion") ||
commentBody.includes("```suggestion\n")
) {
const match = commentBody.match(/```suggestion\n([\s\S]*?)```/);
if (match) {
payload.suggestionContent = match[1];
}
}
const res = await fetch(
`/api/repos/${owner}/${repo}/pulls/${pullNumber}/comments`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
},
);
if (res.ok) {
toast.success("Comment added");
setCommentForm(null);
setCommentBody("");
} else {
const err = await res.json();
toast.error(err.error || "Failed to add comment");
}
} catch {
toast.error("Failed to add comment");
} finally {
setIsSubmitting(false);
}
}, [commentForm, commentBody, owner, repo, pullNumber, headSha]);
const insertSuggestion = useCallback(() => {
setSuggestionMode(true);
setCommentBody(
(prev) =>
prev +
(prev ? "\n" : "") +
"```suggestion\n// Replace with your suggested code\n```",
);
}, []);
return (
<div className="space-y-4">
<DiffView
rawDiff={rawDiff}
repoUrl={repoUrl}
enableComments={!!currentUser}
onAddComment={handleAddComment}
fileApprovals={fileApprovals}
onApproveFile={handleApproveFile}
codeQualityIssues={codeQualityIssues}
/>
{/* Floating inline comment form */}
{commentForm && (
<div className="fixed bottom-4 right-4 z-50 w-[480px] max-w-[90vw] rounded-lg border bg-card shadow-2xl">
<div className="flex items-center justify-between px-4 py-2 border-b bg-muted/30 rounded-t-lg">
<div className="text-sm font-medium">
Add comment on{" "}
<code className="bg-muted px-1.5 py-0.5 rounded text-xs">
{commentForm.filePath}
</code>
<span className="text-muted-foreground ml-1">
line {commentForm.line} ({commentForm.side})
</span>
</div>
<button
onClick={handleCancel}
className="text-muted-foreground hover:text-foreground text-lg leading-none"
>
×
</button>
</div>
<div className="p-4 space-y-3">
<textarea
className="w-full rounded-md border bg-background px-3 py-2 text-sm font-mono min-h-[120px] focus:outline-none focus:ring-2 focus:ring-primary resize-y"
placeholder="Write a comment... Use ```suggestion blocks for code suggestions."
value={commentBody}
onChange={(e) => setCommentBody(e.target.value)}
autoFocus
/>
<div className="flex items-center justify-between">
<button
onClick={insertSuggestion}
className="text-xs text-muted-foreground hover:text-foreground transition-colors"
title="Insert a code suggestion"
>
+ Suggestion
</button>
<div className="flex items-center gap-2">
<button
onClick={handleCancel}
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent transition-colors"
>
Cancel
</button>
<button
onClick={handleSubmit}
disabled={!commentBody.trim() || isSubmitting}
className="rounded-md bg-primary px-3 py-1.5 text-sm font-medium text-primary-foreground hover:bg-primary/90 transition-colors disabled:opacity-50"
>
{isSubmitting ? "Submitting..." : "Comment"}
</button>
</div>
</div>
</div>
</div>
)}
</div>
);
}
export default InlineDiffReview;