/ opencodehub / src / components / queue / ConflictResolver.tsx
swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
181 lines · 7.1 KB
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

import React, { useState } from 'react';
import { Button } from "@/components/ui/button";
import {
    Dialog,
    DialogContent,
    DialogDescription,
    DialogHeader,
    DialogTitle,
    DialogTrigger,
    DialogFooter,
} from "@/components/ui/dialog";
import { AlertCircle, Check, FileCode, Loader2, Wrench } from 'lucide-react';
import { toast } from 'sonner';

interface ConflictFile {
    path: string;
    content: string;
}

interface ConflictResolverProps {
    owner: string;
    repo: string;
    queueItemId: string;
    triggerButton?: React.ReactNode;
    variant?: "default" | "icon";
}

export function ConflictResolver({ owner, repo, queueItemId, triggerButton, variant = "default" }: ConflictResolverProps) {
    const [isOpen, setIsOpen] = useState(false);
    const [loading, setLoading] = useState(false);
    const [conflicts, setConflicts] = useState<ConflictFile[]>([]);
    const [currentIndex, setCurrentIndex] = useState(0);
    const [resolutions, setResolutions] = useState<{ path: string, content: string }[]>([]);
    const [currentContent, setCurrentContent] = useState("");

    const fetchConflicts = async () => {
        setLoading(true);
        try {
            const res = await fetch(`/api/repos/${owner}/${repo}/queue/${queueItemId}/conflicts`);
            if (!res.ok) throw new Error("Failed to check conflicts");
            const data = await res.json();
            if (data.error) throw new Error(data.error);

            setConflicts(data);
            if (data.length > 0) {
                setCurrentContent(data[0].content);
            } else {
                toast.success("No conflicts detected via check.");
                setIsOpen(false);
            }
        } catch (e: any) {
            toast.error(e.message);
            setIsOpen(false);
        } finally {
            setLoading(false);
        }
    };

    const handleOpenChange = (open: boolean) => {
        setIsOpen(open);
        if (open) {
            setConflicts([]);
            setResolutions([]);
            setCurrentIndex(0);
            fetchConflicts();
        }
    };

    const handleNext = () => {
        // Save current resolution
        const currentFile = conflicts[currentIndex];
        const newResolutions = [...resolutions];
        const existingIndex = newResolutions.findIndex(r => r.path === currentFile.path);

        if (existingIndex >= 0) {
            newResolutions[existingIndex] = { path: currentFile.path, content: currentContent };
        } else {
            newResolutions.push({ path: currentFile.path, content: currentContent });
        }
        setResolutions(newResolutions);

        if (currentIndex < conflicts.length - 1) {
            setCurrentIndex(currentIndex + 1);
            setCurrentContent(conflicts[currentIndex + 1].content);
        } else {
            // All done, submit
            submitResolutions(newResolutions);
        }
    };

    const submitResolutions = async (finalResolutions: { path: string, content: string }[]) => {
        setLoading(true);
        try {
            const res = await fetch(`/api/repos/${owner}/${repo}/queue/${queueItemId}/conflicts`, {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ resolutions: finalResolutions })
            });

            if (!res.ok) {
                const data = await res.json();
                throw new Error(data.error || "Failed to submit resolutions");
            }

            toast.success("Conflicts resolved and merge committed!");
            setIsOpen(false);
            window.location.reload(); // Refresh to see updated queue status
        } catch (e: any) {
            toast.error(e.message);
        } finally {
            setLoading(false);
        }
    };

    const currentFile = conflicts[currentIndex];

    return (
        <Dialog open={isOpen} onOpenChange={handleOpenChange}>
            <DialogTrigger asChild>
                {triggerButton ? triggerButton : variant === "icon" ? (
                    <button
                        className="p-1.5 rounded hover:bg-muted transition-colors text-orange-600 hover:text-orange-700 hover:bg-orange-50"
                        title="Resolve Conflicts"
                    >
                        <Wrench className="h-4 w-4" />
                    </button>
                ) : (
                    <Button variant="destructive" size="sm">Resolve Conflicts</Button>
                )}
            </DialogTrigger>
            <DialogContent className="sm:max-w-[800px] h-[80vh] flex flex-col">
                <DialogHeader>
                    <DialogTitle>Resolve Conflicts</DialogTitle>
                    <DialogDescription>
                        Fix merge conflicts manually. {conflicts.length > 0 && `File ${currentIndex + 1} of ${conflicts.length}`}
                    </DialogDescription>
                </DialogHeader>

                <div className="flex-1 min-h-0 flex flex-col gap-4 py-4">
                    {loading && conflicts.length === 0 ? (
                        <div className="flex items-center justify-center h-full">
                            <Loader2 className="h-8 w-8 animate-spin text-primary" />
                            <span className="ml-2">Checking for conflicts...</span>
                        </div>
                    ) : conflicts.length > 0 && currentFile ? (
                        <>
                            <div className="flex items-center gap-2 font-mono text-sm bg-muted p-2 rounded">
                                <FileCode className="h-4 w-4" />
                                {currentFile.path}
                            </div>
                            <textarea
                                className="flex-1 w-full p-4 font-mono text-xs border rounded resize-none bg-slate-950 text-slate-50 focus:outline-none focus:ring-2 focus:ring-primary"
                                value={currentContent}
                                onChange={(e) => setCurrentContent(e.target.value)}
                                spellCheck={false}
                            />
                            <div className="text-xs text-muted-foreground">
                                Make sure to remove <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code>, <code>=======</code>, and <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;</code> markers.
                            </div>
                        </>
                    ) : (
                        <div className="flex items-center justify-center h-full text-muted-foreground">
                            No conflicts loaded.
                        </div>
                    )}
                </div>

                <DialogFooter>
                    <Button variant="outline" onClick={() => setIsOpen(false)} disabled={loading}>
                        Cancel
                    </Button>
                    <Button onClick={handleNext} disabled={loading || conflicts.length === 0}>
                        {currentIndex < conflicts.length - 1 ? "Next File" : "Complete Resolution"}
                    </Button>
                </DialogFooter>
            </DialogContent>
        </Dialog>
    );
}