summaryrefslogtreecommitdiff
path: root/makima/frontend/src/components/history/CheckpointCard.tsx
blob: fee5bdc5bc75a92049a58e5548a472b0586685c5 (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
import { useState } from "react";
import type { TaskCheckpoint } from "../../lib/api";
import { forkTask, resumeFromCheckpoint } from "../../lib/api";

interface CheckpointCardProps {
  checkpoint: TaskCheckpoint;
  taskId: string;
  onActionComplete: () => void;
}

export function CheckpointCard({ checkpoint, taskId, onActionComplete }: CheckpointCardProps) {
  const [showActions, setShowActions] = useState(false);
  const [showForkDialog, setShowForkDialog] = useState(false);
  const [showResumeDialog, setShowResumeDialog] = useState(false);
  const [forkName, setForkName] = useState(`Fork from checkpoint ${checkpoint.checkpointNumber}`);
  const [forkPlan, setForkPlan] = useState("");
  const [resumePlan, setResumePlan] = useState("");
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const handleFork = async () => {
    if (!forkName.trim() || !forkPlan.trim()) {
      setError("Name and plan are required");
      return;
    }

    setIsLoading(true);
    setError(null);
    try {
      await forkTask(taskId, {
        forkFromType: "checkpoint",
        forkFromValue: String(checkpoint.checkpointNumber),
        newTaskName: forkName,
        newTaskPlan: forkPlan,
        includeConversation: true,
      });
      setShowForkDialog(false);
      onActionComplete();
    } catch (e) {
      setError(e instanceof Error ? e.message : "Failed to fork task");
    } finally {
      setIsLoading(false);
    }
  };

  const handleResume = async () => {
    if (!resumePlan.trim()) {
      setError("Plan is required");
      return;
    }

    setIsLoading(true);
    setError(null);
    try {
      await resumeFromCheckpoint(taskId, checkpoint.id, {
        plan: resumePlan,
        includeConversation: true,
      });
      setShowResumeDialog(false);
      onActionComplete();
    } catch (e) {
      setError(e instanceof Error ? e.message : "Failed to resume from checkpoint");
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <>
      <div className="p-3 hover:bg-[rgba(117,170,252,0.05)] transition-colors">
        <div className="flex items-start justify-between gap-4">
          {/* Checkpoint info */}
          <div className="flex-1 min-w-0">
            <div className="flex items-center gap-2">
              <span className="font-mono text-sm text-purple-400">#{checkpoint.checkpointNumber}</span>
              <span className="font-mono text-[10px] text-[#7788aa]">
                {checkpoint.commitSha.slice(0, 7)}
              </span>
              <span className="font-mono text-[10px] text-[#556677]">
                on {checkpoint.branchName}
              </span>
            </div>

            {checkpoint.message && (
              <div className="font-mono text-xs text-[#9bc3ff] mt-1">{checkpoint.message}</div>
            )}

            {/* Files changed */}
            {checkpoint.filesChanged.length > 0 && (
              <div className="mt-2 flex flex-wrap gap-2">
                {checkpoint.filesChanged.slice(0, 5).map((file, i) => (
                  <span
                    key={i}
                    className={`font-mono text-[9px] px-1.5 py-0.5 border ${
                      file.action === "A"
                        ? "text-green-400 border-green-400/30"
                        : file.action === "D"
                        ? "text-red-400 border-red-400/30"
                        : "text-yellow-400 border-yellow-400/30"
                    }`}
                  >
                    {file.action} {file.path.split("/").pop()}
                  </span>
                ))}
                {checkpoint.filesChanged.length > 5 && (
                  <span className="font-mono text-[9px] text-[#556677]">
                    +{checkpoint.filesChanged.length - 5} more
                  </span>
                )}
              </div>
            )}

            {/* Stats */}
            <div className="mt-2 flex items-center gap-4 font-mono text-[9px] text-[#556677]">
              <span className="text-green-400">+{checkpoint.linesAdded}</span>
              <span className="text-red-400">-{checkpoint.linesRemoved}</span>
              <span>{new Date(checkpoint.createdAt).toLocaleString()}</span>
            </div>
          </div>

          {/* Actions button */}
          <button
            onClick={() => setShowActions(!showActions)}
            className="shrink-0 p-1 text-[#7788aa] hover:text-[#9bc3ff] transition-colors"
          >
            <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path
                strokeLinecap="round"
                strokeLinejoin="round"
                strokeWidth={2}
                d="M12 5v.01M12 12v.01M12 19v.01M12 6a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2z"
              />
            </svg>
          </button>
        </div>

        {/* Actions dropdown */}
        {showActions && (
          <div className="mt-3 flex gap-2">
            <button
              onClick={() => {
                setShowForkDialog(true);
                setShowActions(false);
              }}
              className="px-3 py-1.5 font-mono text-[10px] uppercase text-purple-400 border border-purple-400/30 hover:bg-purple-400/10 transition-colors"
            >
              Fork from here
            </button>
            <button
              onClick={() => {
                setShowResumeDialog(true);
                setShowActions(false);
              }}
              className="px-3 py-1.5 font-mono text-[10px] uppercase text-cyan-400 border border-cyan-400/30 hover:bg-cyan-400/10 transition-colors"
            >
              Resume from here
            </button>
          </div>
        )}
      </div>

      {/* Fork dialog */}
      {showForkDialog && (
        <div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50">
          <div className="bg-[#0d1b2d] border border-[rgba(117,170,252,0.35)] max-w-lg w-full mx-4">
            <div className="p-4 border-b border-[rgba(117,170,252,0.15)] flex justify-between items-center">
              <h2 className="font-mono text-sm uppercase tracking-wide text-[#9bc3ff]">
                Fork from Checkpoint #{checkpoint.checkpointNumber}
              </h2>
              <button
                onClick={() => setShowForkDialog(false)}
                className="text-[#7788aa] hover:text-[#9bc3ff] transition-colors"
              >
                <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                </svg>
              </button>
            </div>
            <div className="p-4 space-y-4">
              {error && (
                <div className="p-2 bg-red-400/10 border border-red-400/30 font-mono text-xs text-red-400">
                  {error}
                </div>
              )}
              <div>
                <label className="block font-mono text-[10px] text-[#7788aa] uppercase mb-1">
                  New Task Name
                </label>
                <input
                  type="text"
                  value={forkName}
                  onChange={(e) => setForkName(e.target.value)}
                  className="w-full font-mono text-xs text-[#9bc3ff] bg-[#0a1525] border border-[rgba(117,170,252,0.25)] px-3 py-2 focus:border-[#3f6fb3] focus:outline-none"
                />
              </div>
              <div>
                <label className="block font-mono text-[10px] text-[#7788aa] uppercase mb-1">
                  Plan for New Task
                </label>
                <textarea
                  value={forkPlan}
                  onChange={(e) => setForkPlan(e.target.value)}
                  rows={4}
                  className="w-full font-mono text-xs text-[#9bc3ff] bg-[#0a1525] border border-[rgba(117,170,252,0.25)] px-3 py-2 focus:border-[#3f6fb3] focus:outline-none resize-none"
                  placeholder="Describe what this forked task should accomplish..."
                />
              </div>
              <div className="flex justify-end gap-2">
                <button
                  onClick={() => setShowForkDialog(false)}
                  className="px-4 py-2 font-mono text-xs uppercase text-[#7788aa] border border-[rgba(117,170,252,0.25)] hover:text-[#9bc3ff] transition-colors"
                >
                  Cancel
                </button>
                <button
                  onClick={handleFork}
                  disabled={isLoading}
                  className="px-4 py-2 font-mono text-xs uppercase text-white bg-purple-600 border border-purple-500 hover:bg-purple-500 transition-colors disabled:opacity-50"
                >
                  {isLoading ? "Creating..." : "Create Fork"}
                </button>
              </div>
            </div>
          </div>
        </div>
      )}

      {/* Resume dialog */}
      {showResumeDialog && (
        <div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50">
          <div className="bg-[#0d1b2d] border border-[rgba(117,170,252,0.35)] max-w-lg w-full mx-4">
            <div className="p-4 border-b border-[rgba(117,170,252,0.15)] flex justify-between items-center">
              <h2 className="font-mono text-sm uppercase tracking-wide text-[#9bc3ff]">
                Resume from Checkpoint #{checkpoint.checkpointNumber}
              </h2>
              <button
                onClick={() => setShowResumeDialog(false)}
                className="text-[#7788aa] hover:text-[#9bc3ff] transition-colors"
              >
                <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                </svg>
              </button>
            </div>
            <div className="p-4 space-y-4">
              {error && (
                <div className="p-2 bg-red-400/10 border border-red-400/30 font-mono text-xs text-red-400">
                  {error}
                </div>
              )}
              <div>
                <label className="block font-mono text-[10px] text-[#7788aa] uppercase mb-1">
                  Plan for Resumed Task
                </label>
                <textarea
                  value={resumePlan}
                  onChange={(e) => setResumePlan(e.target.value)}
                  rows={4}
                  className="w-full font-mono text-xs text-[#9bc3ff] bg-[#0a1525] border border-[rgba(117,170,252,0.25)] px-3 py-2 focus:border-[#3f6fb3] focus:outline-none resize-none"
                  placeholder="Describe what the resumed task should do..."
                />
              </div>
              <div className="flex justify-end gap-2">
                <button
                  onClick={() => setShowResumeDialog(false)}
                  className="px-4 py-2 font-mono text-xs uppercase text-[#7788aa] border border-[rgba(117,170,252,0.25)] hover:text-[#9bc3ff] transition-colors"
                >
                  Cancel
                </button>
                <button
                  onClick={handleResume}
                  disabled={isLoading}
                  className="px-4 py-2 font-mono text-xs uppercase text-white bg-cyan-600 border border-cyan-500 hover:bg-cyan-500 transition-colors disabled:opacity-50"
                >
                  {isLoading ? "Creating..." : "Resume Task"}
                </button>
              </div>
            </div>
          </div>
        </div>
      )}
    </>
  );
}