File size: 13,792 Bytes
35765b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { useState } from 'react';
import { useUser } from '../context/UserContext';
import { useProject } from '../context/ProjectContext';
import { api } from '../api/client';

interface LocalTask {
  id: string;
  title: string;
  description: string;
}

export function TaskSetupPage({ onComplete }: { onComplete: () => void }) {
  const { user, logout } = useUser();
  const { currentProject, clearProject } = useProject();

  const [tasks, setTasks] = useState<LocalTask[]>([]);

  // Manual form state
  const [manualTitle, setManualTitle] = useState('');
  const [manualDescription, setManualDescription] = useState('');

  // Edit state
  const [editingId, setEditingId] = useState<string | null>(null);
  const [editTitle, setEditTitle] = useState('');
  const [editDescription, setEditDescription] = useState('');

  const [isGenerating, setIsGenerating] = useState(false);
  const [isSaving, setIsSaving] = useState(false);
  const [error, setError] = useState('');
  const [saveProgress, setSaveProgress] = useState({ current: 0, total: 0 });

  const handleLogout = () => {
    clearProject();
    logout();
  };

  const generateId = () => Math.random().toString(36).substring(2, 9);

  const handleAddManualTask = () => {
    if (!manualTitle.trim()) return;

    setTasks([
      ...tasks,
      {
        id: generateId(),
        title: manualTitle.trim(),
        description: manualDescription.trim(),
      },
    ]);
    setManualTitle('');
    setManualDescription('');
  };

  const handleGenerateTasks = async () => {
    if (!currentProject) return;

    setIsGenerating(true);
    setError('');

    try {
      const result = await api.generateTasks(currentProject.id, 50);

      const newTasks = result.tasks.map((t) => ({
        id: generateId(),
        title: t.title,
        description: t.description,
      }));

      setTasks([...tasks, ...newTasks]);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to generate tasks');
    } finally {
      setIsGenerating(false);
    }
  };

  const handleDeleteTask = (id: string) => {
    setTasks(tasks.filter((t) => t.id !== id));
  };

  const handleStartEdit = (task: LocalTask) => {
    setEditingId(task.id);
    setEditTitle(task.title);
    setEditDescription(task.description);
  };

  const handleSaveEdit = () => {
    if (!editingId) return;

    setTasks(
      tasks.map((t) =>
        t.id === editingId
          ? { ...t, title: editTitle.trim(), description: editDescription.trim() }
          : t
      )
    );
    setEditingId(null);
    setEditTitle('');
    setEditDescription('');
  };

  const handleCancelEdit = () => {
    setEditingId(null);
    setEditTitle('');
    setEditDescription('');
  };

  const handleSaveAndContinue = async () => {
    if (tasks.length === 0 || !currentProject) return;

    setIsSaving(true);
    setError('');
    setSaveProgress({ current: 0, total: tasks.length });

    try {
      for (let i = 0; i < tasks.length; i++) {
        const task = tasks[i];
        await api.createTask(currentProject.id, {
          title: task.title,
          description: task.description,
        });
        setSaveProgress({ current: i + 1, total: tasks.length });
      }

      onComplete();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to save tasks');
    } finally {
      setIsSaving(false);
    }
  };

  const handleClearAll = () => {
    setTasks([]);
  };

  if (!user || !currentProject) return null;

  return (
    <div className="min-h-screen bg-gradient-to-br from-slate-900 via-purple-900 to-slate-900">
      {/* Header */}
      <header className="bg-white/5 backdrop-blur-lg border-b border-white/10">
        <div className="max-w-6xl mx-auto px-4 py-4 flex items-center justify-between">
          <div className="flex items-center gap-3">
            <h1 className="text-xl font-bold text-white">Project Memory</h1>
            <span className="text-purple-300/50">|</span>
            <span className="text-purple-300">{currentProject.name}</span>
          </div>
          <div className="flex items-center gap-4">
            <span className="text-purple-300 text-sm">
              {user.firstName} ({user.id})
            </span>
            <button
              onClick={handleLogout}
              className="px-3 py-1.5 bg-white/10 hover:bg-white/20 text-white rounded-lg transition-all text-sm"
            >
              Logout
            </button>
          </div>
        </div>
      </header>

      {/* Main Content */}
      <main className="max-w-6xl mx-auto px-4 py-6">
        {/* Page Title */}
        <div className="mb-6">
          <h2 className="text-2xl font-bold text-white mb-1">Set Up Tasks</h2>
          <p className="text-purple-300 text-sm">
            Add tasks manually or generate demo tasks with AI (max 50)
          </p>
        </div>

        {/* Error Display */}
        {error && (
          <div className="mb-4 p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-red-200 text-sm">
            {error}
          </div>
        )}

        {/* Two Column Layout */}
        <div className="grid lg:grid-cols-3 gap-6">
          {/* Left Column - Add Tasks */}
          <div className="lg:col-span-1 space-y-4">
            {/* Manual Add */}
            <div className="bg-white/10 backdrop-blur-lg rounded-xl p-4 border border-white/20">
              <h3 className="text-white font-medium mb-3">Add Task</h3>
              <div className="space-y-3">
                <input
                  type="text"
                  value={manualTitle}
                  onChange={(e) => setManualTitle(e.target.value)}
                  placeholder="Task title"
                  className="w-full px-3 py-2 bg-white/5 border border-white/10 rounded-lg text-white placeholder-white/40 focus:outline-none focus:ring-2 focus:ring-purple-500 text-sm"
                  onKeyDown={(e) => e.key === 'Enter' && handleAddManualTask()}
                />
                <textarea
                  value={manualDescription}
                  onChange={(e) => setManualDescription(e.target.value)}
                  placeholder="Description (optional)"
                  rows={2}
                  className="w-full px-3 py-2 bg-white/5 border border-white/10 rounded-lg text-white placeholder-white/40 focus:outline-none focus:ring-2 focus:ring-purple-500 resize-none text-sm"
                />
                <button
                  onClick={handleAddManualTask}
                  disabled={!manualTitle.trim()}
                  className="w-full px-3 py-2 bg-purple-600 hover:bg-purple-700 disabled:bg-purple-800 disabled:cursor-not-allowed text-white rounded-lg transition-all text-sm"
                >
                  + Add Task
                </button>
              </div>
            </div>

            {/* AI Generate */}
            <div className="bg-purple-600/20 backdrop-blur-lg rounded-xl p-4 border border-purple-500/30">
              <h3 className="text-white font-medium mb-2">AI Generate</h3>
              <p className="text-purple-300/70 text-xs mb-3">
                Generate 50 demo tasks based on your project
              </p>
              <button
                onClick={handleGenerateTasks}
                disabled={isGenerating}
                className="w-full px-3 py-2 bg-purple-600 hover:bg-purple-700 disabled:bg-purple-800 text-white rounded-lg transition-all text-sm flex items-center justify-center gap-2"
              >
                {isGenerating ? (
                  <>
                    <span className="animate-spin"></span> Generating...
                  </>
                ) : (
                  <>✨ Generate Demo Tasks</>
                )}
              </button>
            </div>

            {/* Actions */}
            {tasks.length > 0 && (
              <div className="space-y-2">
                <button
                  onClick={handleSaveAndContinue}
                  disabled={isSaving}
                  className="w-full px-4 py-3 bg-green-600 hover:bg-green-700 disabled:bg-green-800 text-white font-medium rounded-lg transition-all flex items-center justify-center gap-2"
                >
                  {isSaving ? (
                    <>Saving... ({saveProgress.current}/{saveProgress.total})</>
                  ) : (
                    <>Save & Continue →</>
                  )}
                </button>
                <button
                  onClick={handleClearAll}
                  disabled={isSaving}
                  className="w-full px-3 py-2 bg-white/5 hover:bg-white/10 text-red-400 hover:text-red-300 rounded-lg transition-all text-sm"
                >
                  Clear All Tasks
                </button>
              </div>
            )}
          </div>

          {/* Right Column - Task List */}
          <div className="lg:col-span-2">
            <div className="bg-white/5 backdrop-blur-lg rounded-xl border border-white/10 overflow-hidden">
              <div className="px-4 py-3 border-b border-white/10 flex items-center justify-between">
                <h3 className="text-white font-medium">
                  Tasks ({tasks.length})
                </h3>
                {tasks.length > 0 && (
                  <span className="text-purple-300/50 text-xs">
                    Click ✏️ to edit, 🗑️ to delete
                  </span>
                )}
              </div>

              {tasks.length === 0 ? (
                <div className="p-8 text-center text-purple-300/50">
                  <div className="text-4xl mb-2">📋</div>
                  <p>No tasks yet</p>
                  <p className="text-xs mt-1">Add manually or generate with AI</p>
                </div>
              ) : (
                <div className="max-h-[60vh] overflow-y-auto">
                  {tasks.map((task, index) => (
                    <div
                      key={task.id}
                      className="px-4 py-3 border-b border-white/5 hover:bg-white/5 transition-colors"
                    >
                      {editingId === task.id ? (
                        <div className="space-y-2">
                          <input
                            type="text"
                            value={editTitle}
                            onChange={(e) => setEditTitle(e.target.value)}
                            className="w-full px-3 py-2 bg-white/10 border border-white/20 rounded text-white focus:outline-none focus:ring-2 focus:ring-purple-500 text-sm"
                            autoFocus
                          />
                          <textarea
                            value={editDescription}
                            onChange={(e) => setEditDescription(e.target.value)}
                            rows={2}
                            className="w-full px-3 py-2 bg-white/10 border border-white/20 rounded text-white focus:outline-none focus:ring-2 focus:ring-purple-500 resize-none text-sm"
                          />
                          <div className="flex gap-2">
                            <button
                              onClick={handleSaveEdit}
                              className="px-3 py-1.5 bg-green-600 hover:bg-green-700 text-white rounded text-xs"
                            >
                              Save
                            </button>
                            <button
                              onClick={handleCancelEdit}
                              className="px-3 py-1.5 bg-white/10 hover:bg-white/20 text-white rounded text-xs"
                            >
                              Cancel
                            </button>
                          </div>
                        </div>
                      ) : (
                        <div className="flex items-start justify-between gap-3">
                          <div className="flex-1 min-w-0">
                            <div className="flex items-center gap-2">
                              <span className="text-purple-400 text-xs font-mono">
                                #{index + 1}
                              </span>
                              <h4 className="text-white text-sm font-medium truncate">
                                {task.title}
                              </h4>
                            </div>
                            {task.description && (
                              <p className="text-purple-300/60 text-xs mt-0.5 line-clamp-1">
                                {task.description}
                              </p>
                            )}
                          </div>
                          <div className="flex gap-1 flex-shrink-0">
                            <button
                              onClick={() => handleStartEdit(task)}
                              className="p-1 text-purple-300 hover:text-white hover:bg-white/10 rounded transition-all"
                              title="Edit"
                            >
                              ✏️
                            </button>
                            <button
                              onClick={() => handleDeleteTask(task.id)}
                              className="p-1 text-red-400 hover:text-red-300 hover:bg-white/10 rounded transition-all"
                              title="Delete"
                            >
                              🗑️
                            </button>
                          </div>
                        </div>
                      )}
                    </div>
                  ))}
                </div>
              )}
            </div>
          </div>
        </div>
      </main>
    </div>
  );
}