summaryrefslogtreecommitdiff
path: root/makima/frontend/src/components/mesh/InlineSubtaskEditor.tsx
blob: 3621b08477913fb2df4c6eda3717f5728658e772 (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
import { useState, useCallback, useEffect } from "react";
import type { TaskWithSubtasks, TaskStatus } from "../../lib/api";
import { getTask, updateTask } from "../../lib/api";

interface InlineSubtaskEditorProps {
  subtaskId: string;
  onClose: () => void;
  onUpdated: () => void;
  onNavigate?: (taskId: string) => void;
}

function getStatusColor(status: TaskStatus): string {
  switch (status) {
    case "pending":
      return "text-[#9bc3ff]";
    case "running":
      return "text-green-400";
    case "paused":
      return "text-yellow-400";
    case "blocked":
      return "text-orange-400";
    case "done":
      return "text-emerald-400";
    case "failed":
      return "text-red-400";
    case "merged":
      return "text-purple-400";
    default:
      return "text-[#9bc3ff]";
  }
}

function getStatusBgColor(status: TaskStatus): string {
  switch (status) {
    case "pending":
      return "bg-[rgba(117,170,252,0.1)]";
    case "running":
      return "bg-green-400/10";
    case "paused":
      return "bg-yellow-400/10";
    case "blocked":
      return "bg-orange-400/10";
    case "done":
      return "bg-emerald-400/10";
    case "failed":
      return "bg-red-400/10";
    case "merged":
      return "bg-purple-400/10";
    default:
      return "bg-[rgba(117,170,252,0.1)]";
  }
}

export function InlineSubtaskEditor({
  subtaskId,
  onClose,
  onUpdated,
  onNavigate,
}: InlineSubtaskEditorProps) {
  const [subtask, setSubtask] = useState<TaskWithSubtasks | null>(null);
  const [loading, setLoading] = useState(true);
  const [saving, setSaving] = useState(false);
  const [isEditing, setIsEditing] = useState(false);
  const [editName, setEditName] = useState("");
  const [editDescription, setEditDescription] = useState("");
  const [editPlan, setEditPlan] = useState("");

  // Load subtask details
  useEffect(() => {
    setLoading(true);
    getTask(subtaskId)
      .then((task) => {
        setSubtask(task);
        setEditName(task.name);
        setEditDescription(task.description || "");
        setEditPlan(task.plan);
      })
      .catch((err) => {
        console.error("Failed to load subtask:", err);
      })
      .finally(() => {
        setLoading(false);
      });
  }, [subtaskId]);

  const handleSave = useCallback(async () => {
    if (!subtask || saving) return;
    setSaving(true);
    try {
      await updateTask(subtaskId, {
        name: editName,
        description: editDescription || undefined,
        plan: editPlan,
        version: subtask.version,
      });
      // Refresh subtask
      const updated = await getTask(subtaskId);
      setSubtask(updated);
      setIsEditing(false);
      onUpdated();
    } catch (err) {
      console.error("Failed to save subtask:", err);
    } finally {
      setSaving(false);
    }
  }, [subtask, subtaskId, editName, editDescription, editPlan, saving, onUpdated]);

  const handleCancel = useCallback(() => {
    if (subtask) {
      setEditName(subtask.name);
      setEditDescription(subtask.description || "");
      setEditPlan(subtask.plan);
    }
    setIsEditing(false);
  }, [subtask]);

  if (loading) {
    return (
      <div className="p-4 bg-[rgba(0,0,0,0.2)] border-l-2 border-[#3f6fb3]">
        <div className="font-mono text-xs text-[#75aafc]">Loading subtask...</div>
      </div>
    );
  }

  if (!subtask) {
    return (
      <div className="p-4 bg-[rgba(0,0,0,0.2)] border-l-2 border-red-400">
        <div className="font-mono text-xs text-red-400">Failed to load subtask</div>
      </div>
    );
  }

  return (
    <div className="bg-[rgba(0,0,0,0.2)] border-l-2 border-[#3f6fb3]">
      {/* Header */}
      <div className="flex items-center justify-between p-3 border-b border-[rgba(117,170,252,0.15)]">
        <div className="flex items-center gap-2">
          <button
            onClick={onClose}
            className="font-mono text-[10px] text-[#555] hover:text-[#75aafc]"
          >
            [close]
          </button>
          <span
            className={`px-1.5 py-0.5 font-mono text-[9px] uppercase ${getStatusColor(
              subtask.status as TaskStatus
            )} ${getStatusBgColor(subtask.status as TaskStatus)} border border-current/20`}
          >
            {subtask.status}
          </span>
          {onNavigate && (
            <button
              onClick={() => onNavigate(subtaskId)}
              className="font-mono text-[10px] text-[#75aafc] hover:text-[#9bc3ff]"
            >
              [open full view]
            </button>
          )}
        </div>
        <div className="flex items-center gap-2">
          {isEditing ? (
            <>
              <button
                onClick={handleCancel}
                disabled={saving}
                className="px-2 py-0.5 font-mono text-[10px] text-[#555] hover:text-[#9bc3ff] disabled:opacity-50"
              >
                Cancel
              </button>
              <button
                onClick={handleSave}
                disabled={saving}
                className="px-2 py-0.5 font-mono text-[10px] text-green-400 border border-green-400/30 hover:border-green-400/50 disabled:opacity-50"
              >
                {saving ? "..." : "Save"}
              </button>
            </>
          ) : (
            <button
              onClick={() => setIsEditing(true)}
              className="px-2 py-0.5 font-mono text-[10px] text-[#75aafc] hover:text-[#9bc3ff]"
            >
              Edit
            </button>
          )}
        </div>
      </div>

      {/* Content */}
      <div className="p-3 space-y-3">
        {/* Name */}
        {isEditing ? (
          <input
            type="text"
            value={editName}
            onChange={(e) => setEditName(e.target.value)}
            className="w-full bg-transparent border border-[rgba(117,170,252,0.25)] text-[#dbe7ff] font-mono text-sm px-2 py-1 outline-none focus:border-[#3f6fb3]"
            placeholder="Subtask name"
          />
        ) : (
          <div className="font-mono text-sm text-[#dbe7ff]">{subtask.name}</div>
        )}

        {/* Description */}
        {isEditing ? (
          <textarea
            value={editDescription}
            onChange={(e) => setEditDescription(e.target.value)}
            className="w-full bg-transparent border border-[rgba(117,170,252,0.25)] text-[#9bc3ff] font-mono text-xs px-2 py-1 outline-none focus:border-[#3f6fb3] min-h-[40px] resize-y"
            placeholder="Description (optional)"
          />
        ) : subtask.description ? (
          <div className="font-mono text-xs text-[#75aafc]">{subtask.description}</div>
        ) : null}

        {/* Plan */}
        <div className="space-y-1">
          <div className="font-mono text-[10px] text-[#555] uppercase">Plan</div>
          {isEditing ? (
            <textarea
              value={editPlan}
              onChange={(e) => setEditPlan(e.target.value)}
              className="w-full bg-[rgba(0,0,0,0.2)] border border-[rgba(117,170,252,0.25)] text-[#dbe7ff] font-mono text-xs px-2 py-1 outline-none focus:border-[#3f6fb3] min-h-[100px] resize-y"
              placeholder="Plan/instructions..."
            />
          ) : (
            <pre className="bg-[rgba(0,0,0,0.2)] border border-[rgba(117,170,252,0.15)] p-2 font-mono text-xs text-[#9bc3ff] whitespace-pre-wrap max-h-[150px] overflow-y-auto">
              {subtask.plan}
            </pre>
          )}
        </div>

        {/* Progress/Error */}
        {subtask.progressSummary && (
          <div className="font-mono text-[10px] text-[#75aafc]">
            <span className="text-[#555]">Progress:</span> {subtask.progressSummary}
          </div>
        )}
        {subtask.errorMessage && (
          <div className="font-mono text-[10px] text-red-400">
            <span className="text-red-400/50">Error:</span> {subtask.errorMessage}
          </div>
        )}

        {/* Nested subtasks indicator */}
        {subtask.subtasks.length > 0 && (
          <div className="font-mono text-[10px] text-[#555]">
            Has {subtask.subtasks.length} subtask{subtask.subtasks.length > 1 ? "s" : ""}
            {onNavigate && (
              <button
                onClick={() => onNavigate(subtaskId)}
                className="ml-2 text-[#75aafc] hover:text-[#9bc3ff]"
              >
                [view all]
              </button>
            )}
          </div>
        )}
      </div>
    </div>
  );
}