summaryrefslogtreecommitdiff
path: root/makima/frontend/src/components/mesh/PRPreview.tsx
blob: fc202b0a97073ef61dd0656573de7ca7bfde7eaa (plain) (blame)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { useState, useMemo } from "react";
import type { TaskWithSubtasks, TaskSummary } from "../../lib/api";
import { OverlayDiffViewer } from "./OverlayDiffViewer";

interface PRPreviewProps {
  task: TaskWithSubtasks;
  diff?: string;
  changedFiles?: string[];
  loading?: boolean;
  onCreatePR?: (title: string, body: string, draft: boolean) => Promise<void>;
  onAutoMerge?: () => Promise<void>;
  onClose: () => void;
}

interface PRFormData {
  title: string;
  body: string;
  isDraft: boolean;
}

function generatePRTitle(task: TaskWithSubtasks): string {
  // Generate a PR title based on the task name
  const prefix = task.parentTaskId ? "feat" : "feat";
  return `${prefix}: ${task.name}`;
}

function generatePRBody(task: TaskWithSubtasks, changedFiles?: string[]): string {
  const sections: string[] = [];

  // Summary
  sections.push("## Summary\n");
  if (task.description) {
    sections.push(task.description + "\n");
  } else {
    sections.push("_Add a brief description of the changes..._\n");
  }

  // Plan/Implementation details
  sections.push("\n## Implementation\n");
  if (task.plan) {
    // Truncate if too long
    const planPreview = task.plan.length > 500
      ? task.plan.substring(0, 500) + "..."
      : task.plan;
    sections.push("```\n" + planPreview + "\n```\n");
  }

  // Subtasks summary
  if (task.subtasks.length > 0) {
    sections.push("\n## Subtasks\n");
    task.subtasks.forEach((subtask: TaskSummary) => {
      const emoji = subtask.status === "done" || subtask.status === "merged" ? "+" :
                    subtask.status === "running" ? "~" : "-";
      sections.push(`- [${emoji === "+" ? "x" : " "}] ${subtask.name} (${subtask.status})\n`);
    });
  }

  // Changed files
  if (changedFiles && changedFiles.length > 0) {
    sections.push("\n## Changed Files\n");
    changedFiles.slice(0, 20).forEach((file) => {
      sections.push(`- \`${file}\`\n`);
    });
    if (changedFiles.length > 20) {
      sections.push(`\n_...and ${changedFiles.length - 20} more files_\n`);
    }
  }

  // Test plan
  sections.push("\n## Test Plan\n");
  sections.push("- [ ] Manual testing completed\n");
  sections.push("- [ ] Unit tests added/updated\n");
  sections.push("- [ ] Integration tests passing\n");

  // Footer
  sections.push("\n---\n");
  sections.push("_Generated by makima mesh orchestrator_\n");

  return sections.join("");
}

export function PRPreview({
  task,
  diff = "",
  changedFiles = [],
  loading = false,
  onCreatePR,
  onAutoMerge,
  onClose,
}: PRPreviewProps) {
  const [showDiff, setShowDiff] = useState(false);
  const [creating, setCreating] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const [formData, setFormData] = useState<PRFormData>(() => ({
    title: generatePRTitle(task),
    body: generatePRBody(task, changedFiles),
    isDraft: false,
  }));

  const handleCreatePR = async () => {
    if (!onCreatePR || creating) return;

    setCreating(true);
    setError(null);

    try {
      await onCreatePR(formData.title, formData.body, formData.isDraft);
      onClose();
    } catch (err) {
      setError(err instanceof Error ? err.message : "Failed to create PR");
    } finally {
      setCreating(false);
    }
  };

  const handleAutoMerge = async () => {
    if (!onAutoMerge || creating) return;

    if (!confirm("Are you sure you want to auto-merge this task directly to the target branch?")) {
      return;
    }

    setCreating(true);
    setError(null);

    try {
      await onAutoMerge();
      onClose();
    } catch (err) {
      setError(err instanceof Error ? err.message : "Failed to auto-merge");
    } finally {
      setCreating(false);
    }
  };

  // Calculate stats
  const stats = useMemo(() => {
    const completedSubtasks = task.subtasks.filter(
      (s) => s.status === "done" || s.status === "merged"
    ).length;
    return {
      filesChanged: changedFiles.length,
      subtasksCompleted: completedSubtasks,
      subtasksTotal: task.subtasks.length,
      isReady: completedSubtasks === task.subtasks.length || task.subtasks.length === 0,
    };
  }, [task.subtasks, changedFiles]);

  return (
    <div className="panel flex flex-col max-h-[80vh] overflow-hidden">
      {/* Header */}
      <div className="flex items-center justify-between p-4 border-b border-[rgba(117,170,252,0.2)] shrink-0">
        <div className="font-mono text-xs text-[#9bc3ff] tracking-wide uppercase">
          Create Pull Request
        </div>
        <button
          onClick={onClose}
          className="font-mono text-xs text-[#555] hover:text-[#9bc3ff]"
        >
          Cancel
        </button>
      </div>

      {/* Content */}
      <div className="flex-1 overflow-y-auto p-4 space-y-4">
        {/* Status badges */}
        <div className="flex flex-wrap gap-2">
          <span className="px-2 py-0.5 font-mono text-[10px] text-[#75aafc] bg-[rgba(117,170,252,0.1)] border border-[rgba(117,170,252,0.2)]">
            {task.baseBranch || "main"} → {task.targetBranch || task.baseBranch || "main"}
          </span>
          <span className="px-2 py-0.5 font-mono text-[10px] text-[#9bc3ff] bg-[rgba(117,170,252,0.1)] border border-[rgba(117,170,252,0.2)]">
            {stats.filesChanged} files changed
          </span>
          {task.subtasks.length > 0 && (
            <span
              className={`px-2 py-0.5 font-mono text-[10px] border ${
                stats.isReady
                  ? "text-green-400 bg-green-400/10 border-green-400/20"
                  : "text-yellow-400 bg-yellow-400/10 border-yellow-400/20"
              }`}
            >
              {stats.subtasksCompleted}/{stats.subtasksTotal} subtasks complete
            </span>
          )}
        </div>

        {/* Warning if subtasks not complete */}
        {!stats.isReady && (
          <div className="bg-yellow-400/10 border border-yellow-400/30 p-3 font-mono text-xs text-yellow-400">
            Some subtasks are not yet complete. Consider waiting before creating the PR.
          </div>
        )}

        {/* Error message */}
        {error && (
          <div className="bg-red-400/10 border border-red-400/30 p-3 font-mono text-xs text-red-400">
            {error}
          </div>
        )}

        {/* PR Title */}
        <div className="space-y-2">
          <label className="font-mono text-xs text-[#9bc3ff] tracking-wide uppercase">
            Title
          </label>
          <input
            type="text"
            value={formData.title}
            onChange={(e) => setFormData({ ...formData, title: e.target.value })}
            className="w-full bg-transparent border border-[rgba(117,170,252,0.25)] text-[#dbe7ff] font-mono text-sm px-3 py-2 outline-none focus:border-[#3f6fb3]"
            placeholder="PR title"
            disabled={creating}
          />
        </div>

        {/* PR Body */}
        <div className="space-y-2">
          <div className="flex items-center justify-between">
            <label className="font-mono text-xs text-[#9bc3ff] tracking-wide uppercase">
              Description
            </label>
            <button
              onClick={() => setFormData({
                ...formData,
                body: generatePRBody(task, changedFiles),
              })}
              className="font-mono text-[10px] text-[#555] hover:text-[#9bc3ff]"
            >
              Regenerate
            </button>
          </div>
          <textarea
            value={formData.body}
            onChange={(e) => setFormData({ ...formData, body: e.target.value })}
            className="w-full bg-transparent border border-[rgba(117,170,252,0.25)] text-[#dbe7ff] font-mono text-xs px-3 py-2 outline-none focus:border-[#3f6fb3] min-h-[200px] resize-y"
            placeholder="PR description (markdown)"
            disabled={creating}
          />
        </div>

        {/* Options */}
        <div className="flex items-center gap-4">
          <label className="flex items-center gap-2 cursor-pointer">
            <input
              type="checkbox"
              checked={formData.isDraft}
              onChange={(e) => setFormData({ ...formData, isDraft: e.target.checked })}
              className="w-4 h-4 accent-[#75aafc]"
              disabled={creating}
            />
            <span className="font-mono text-xs text-[#9bc3ff]">Create as draft</span>
          </label>
        </div>

        {/* Diff preview toggle */}
        <div className="border-t border-[rgba(117,170,252,0.2)] pt-4">
          <button
            onClick={() => setShowDiff(!showDiff)}
            className="flex items-center gap-2 font-mono text-xs text-[#75aafc] hover:text-[#9bc3ff]"
          >
            <span>{showDiff ? "▼" : "▶"}</span>
            <span>
              {showDiff ? "Hide" : "Show"} diff preview ({stats.filesChanged} files)
            </span>
          </button>
        </div>

        {/* Inline diff viewer */}
        {showDiff && (
          <div className="border border-[rgba(117,170,252,0.2)]">
            <OverlayDiffViewer
              diff={diff}
              changedFiles={changedFiles}
              loading={loading}
              title="Changes to be merged"
            />
          </div>
        )}
      </div>

      {/* Footer actions */}
      <div className="flex items-center justify-between p-4 border-t border-[rgba(117,170,252,0.2)] shrink-0">
        <div className="font-mono text-[10px] text-[#555]">
          {task.repositoryUrl && (
            <span className="truncate max-w-[200px] inline-block align-middle">
              {task.repositoryUrl}
            </span>
          )}
        </div>
        <div className="flex items-center gap-2">
          {task.mergeMode === "auto" && onAutoMerge && (
            <button
              onClick={handleAutoMerge}
              disabled={creating || !stats.isReady}
              className="px-4 py-2 font-mono text-xs text-yellow-400 border border-yellow-400/30 hover:border-yellow-400/50 hover:bg-yellow-400/10 disabled:opacity-50 disabled:cursor-not-allowed transition-colors uppercase"
            >
              {creating ? "..." : "Auto-Merge"}
            </button>
          )}
          {onCreatePR && (
            <button
              onClick={handleCreatePR}
              disabled={creating || !formData.title.trim()}
              className="px-4 py-2 font-mono text-xs text-green-400 border border-green-400/30 hover:border-green-400/50 hover:bg-green-400/10 disabled:opacity-50 disabled:cursor-not-allowed transition-colors uppercase"
            >
              {creating ? "Creating..." : formData.isDraft ? "Create Draft PR" : "Create PR"}
            </button>
          )}
        </div>
      </div>
    </div>
  );
}