File size: 16,743 Bytes
34450be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';
import { Play, Plus, Trash2, Save, Rocket, Clock, FileText } from 'lucide-react';
import { Workflow, WorkflowStep, Note } from '../types';
import { STORAGE_KEYS, DEFAULT_WORKFLOWS } from '../constants';

interface WorkflowPanelProps {
    templates: Note[];
    onStartWorkflow: (workflow: Workflow) => void;
}

export const WorkflowPanel: React.FC<WorkflowPanelProps> = ({ templates, onStartWorkflow }) => {
    const [workflows, setWorkflows] = useState<Workflow[]>(() => {
        const stored = localStorage.getItem(STORAGE_KEYS.WORKFLOWS);
        return stored ? JSON.parse(stored) : DEFAULT_WORKFLOWS;
    });

    const [isEditing, setIsEditing] = useState(false);
    const [editingWorkflow, setEditingWorkflow] = useState<Workflow | null>(null);

    useEffect(() => {
        localStorage.setItem(STORAGE_KEYS.WORKFLOWS, JSON.stringify(workflows));
    }, [workflows]);

    const handleCreateNew = () => {
        setEditingWorkflow({
            id: `workflow-${Date.now()}`,
            name: 'New Workflow',
            description: '',
            steps: [{ templateId: '', delayMinutes: 0 }]
        });
        setIsEditing(true);
    };

    const handleSave = () => {
        if (!editingWorkflow) return;
        if (!editingWorkflow.name.trim()) {
            alert('Workflow name is required.');
            return;
        }

        if (editingWorkflow.steps.some(s => !s.templateId)) {
            alert('All steps must have a selected template.');
            return;
        }

        setWorkflows(prev => {
            const existingIndex = prev.findIndex(w => w.id === editingWorkflow.id);
            if (existingIndex >= 0) {
                const newWorkflows = [...prev];
                newWorkflows[existingIndex] = editingWorkflow;
                return newWorkflows;
            }
            return [...prev, editingWorkflow];
        });

        setIsEditing(false);
        setEditingWorkflow(null);
    };

    const handleDelete = (id: string) => {
        if (confirm('Delete this workflow?')) {
            setWorkflows(prev => prev.filter(w => w.id !== id));
        }
    };

    const addStep = () => {
        if (editingWorkflow) {
            setEditingWorkflow({
                ...editingWorkflow,
                steps: [...editingWorkflow.steps, { templateId: '', delayMinutes: 30 }]
            });
        }
    };

    const removeStep = (index: number) => {
        if (editingWorkflow && editingWorkflow.steps.length > 1) {
            const newSteps = [...editingWorkflow.steps];
            newSteps.splice(index, 1);
            setEditingWorkflow({ ...editingWorkflow, steps: newSteps });
        }
    };

    const updateStep = (index: number, field: keyof WorkflowStep, value: string | number) => {
        if (editingWorkflow) {
            const newSteps = [...editingWorkflow.steps];
            newSteps[index] = { ...newSteps[index], [field]: value };
            setEditingWorkflow({ ...editingWorkflow, steps: newSteps });
        }
    };

    return (
        <div className="flex flex-col h-full bg-white p-6 overflow-y-auto">
            <div className="max-w-3xl mx-auto w-full space-y-6">
                <div className="flex justify-between items-center">
                    <div>
                        <h2 className="text-2xl font-bold flex items-center gap-2 text-gray-900">
                            <Rocket className="w-6 h-6 text-indigo-600" />
                            Workflow Builder
                        </h2>
                        <p className="text-gray-500 mt-1 text-sm">Create sequences of templates that fire on a timer.</p>
                    </div>
                    {!isEditing && (
                        <button
                            onClick={handleCreateNew}
                            className="bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-2 rounded-xl text-sm font-bold flex items-center gap-2 transition-all"
                        >
                            <Plus className="w-4 h-4" /> New Workflow
                        </button>
                    )}
                </div>

                {isEditing && editingWorkflow ? (
                    <div className="bg-gray-50 border border-gray-200 rounded-2xl p-6 space-y-6 animate-in fade-in slide-in-from-top-4">
                        <div className="flex justify-between items-center border-b border-gray-200 pb-4">
                            <h3 className="text-lg font-bold text-gray-800">Edit Workflow</h3>
                            <div className="flex gap-2">
                                <button
                                    onClick={() => { setIsEditing(false); setEditingWorkflow(null); }}
                                    className="px-4 py-2 text-gray-500 hover:bg-gray-200 rounded-xl text-sm font-bold transition-all"
                                >
                                    Cancel
                                </button>
                                <button
                                    onClick={handleSave}
                                    className="bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-2 rounded-xl text-sm font-bold flex items-center gap-2 transition-all shadow-sm"
                                >
                                    <Save className="w-4 h-4" /> Save Workflow
                                </button>
                            </div>
                        </div>

                        <div className="space-y-4">
                            <div>
                                <label className="block text-xs font-bold text-gray-600 uppercase tracking-wider mb-1">Workflow Name</label>
                                <input
                                    type="text"
                                    value={editingWorkflow.name}
                                    onChange={(e) => setEditingWorkflow({ ...editingWorkflow, name: e.target.value })}
                                    className="w-full px-4 py-2 border border-gray-200 rounded-xl focus:ring-2 focus:ring-indigo-500 outline-none transition-all"
                                    placeholder="e.g. Code Review & Deploy"
                                />
                            </div>
                            <div>
                                <label className="block text-xs font-bold text-gray-600 uppercase tracking-wider mb-1">Description (Optional)</label>
                                <input
                                    type="text"
                                    value={editingWorkflow.description}
                                    onChange={(e) => setEditingWorkflow({ ...editingWorkflow, description: e.target.value })}
                                    className="w-full px-4 py-2 border border-gray-200 rounded-xl focus:ring-2 focus:ring-indigo-500 outline-none transition-all text-sm"
                                    placeholder="What does this sequence do?"
                                />
                            </div>

                            <div className="pt-4 border-t border-gray-200">
                                <div className="flex justify-between items-center mb-4">
                                    <label className="text-xs font-bold text-gray-800 uppercase tracking-wider">Execution Steps</label>
                                </div>
                                <div className="space-y-4">
                                    {editingWorkflow.steps.map((step, idx) => (
                                        <div key={idx} className="flex gap-3 bg-white p-4 rounded-xl border border-gray-200 shadow-sm relative">
                                            <div className="flex flex-col items-center justify-center bg-indigo-50 text-indigo-700 font-bold rounded-lg w-8 h-8 flex-shrink-0">
                                                {idx + 1}
                                            </div>
                                            <div className="flex-1 grid grid-cols-1 md:grid-cols-2 gap-4">
                                                <div>
                                                    <label className="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1 flex items-center gap-1">
                                                        <FileText className="w-3 h-3" /> Select Template
                                                    </label>
                                                    <select
                                                        value={step.templateId}
                                                        onChange={(e) => updateStep(idx, 'templateId', e.target.value)}
                                                        className="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm outline-none focus:ring-2 focus:ring-indigo-500 bg-white"
                                                    >
                                                        <option value="">-- Choose Template --</option>
                                                        {templates.map(t => (
                                                            <option key={t.id} value={t.id}>{t.title || t.id}</option>
                                                        ))}
                                                    </select>
                                                </div>
                                                <div>
                                                    <label className="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1 flex items-center gap-1">
                                                        <Clock className="w-3 h-3" /> Delay Before Firing (Mins)
                                                    </label>
                                                    <input
                                                        type="number"
                                                        min="0"
                                                        value={step.delayMinutes}
                                                        onChange={(e) => updateStep(idx, 'delayMinutes', parseInt(e.target.value) || 0)}
                                                        disabled={idx === 0}
                                                        className="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm outline-none focus:ring-2 focus:ring-indigo-500 disabled:bg-gray-100 disabled:text-gray-400"
                                                    />
                                                </div>
                                            </div>
                                            {idx > 0 && (
                                                <button
                                                    onClick={() => removeStep(idx)}
                                                    className="p-2 text-gray-400 hover:text-red-500 hover:bg-red-50 rounded-lg transition-all absolute right-2 top-2"
                                                >
                                                    <Trash2 className="w-4 h-4" />
                                                </button>
                                            )}
                                        </div>
                                    ))}
                                </div>
                                <button
                                    onClick={addStep}
                                    className="mt-4 flex items-center gap-1 text-sm font-bold text-indigo-600 hover:text-indigo-700 bg-indigo-50 hover:bg-indigo-100 px-3 py-2 rounded-lg transition-colors"
                                >
                                    <Plus className="w-4 h-4" /> Add Step
                                </button>
                            </div>
                        </div>
                    </div>
                ) : (
                    <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                        {workflows.map(wf => (
                            <div key={wf.id} className="bg-white border border-gray-200 rounded-2xl p-5 hover:border-indigo-300 transition-all shadow-sm hover:shadow-md flex flex-col group relative">
                                {wf.id !== 'hf-deployment-cue' && (
                                    <button
                                        onClick={() => handleDelete(wf.id)}
                                        className="absolute top-4 right-4 p-2 text-gray-400 hover:text-red-500 bg-gray-50 hover:bg-red-50 rounded-lg transition-all opacity-0 group-hover:opacity-100"
                                    >
                                        <Trash2 className="w-4 h-4" />
                                    </button>
                                )}

                                <div className="flex-1">
                                    <h3 className="text-lg font-bold text-gray-900 mb-1 flex items-center gap-2">
                                        {wf.name}
                                        {wf.id === 'hf-deployment-cue' && <span className="bg-indigo-100 text-indigo-700 text-[10px] px-2 py-0.5 rounded-full uppercase tracking-wider">System</span>}
                                    </h3>
                                    <p className="text-sm text-gray-500 mb-4">{wf.description || 'No description provided.'}</p>

                                    <div className="space-y-2 mb-6">
                                        <div className="text-xs font-bold text-gray-400 uppercase tracking-wider border-b border-gray-100 pb-1 mb-2">Sequence</div>
                                        {wf.steps.map((step, idx) => (
                                            <div key={idx} className="flex items-center text-xs text-gray-600">
                                                <span className="w-5 font-bold text-gray-400">{idx + 1}.</span>
                                                <span className="font-medium truncate flex-1">{templates.find(t => t.id === step.templateId)?.title || step.templateId}</span>
                                                {step.delayMinutes > 0 && (
                                                    <span className="text-indigo-500 flex items-center gap-1 font-bold bg-indigo-50 px-1.5 py-0.5 rounded ml-2 whitespace-nowrap">
                                                        <Clock className="w-3 h-3" /> +{step.delayMinutes}m
                                                    </span>
                                                )}
                                            </div>
                                        ))}
                                    </div>
                                </div>

                                <div className="flex gap-2">
                                    <button
                                        onClick={() => onStartWorkflow(wf)}
                                        className="flex-1 bg-indigo-50 hover:bg-indigo-600 text-indigo-700 hover:text-white py-2.5 rounded-xl text-sm font-bold flex items-center justify-center gap-2 transition-all shadow-sm group/btn"
                                    >
                                        <Play className="w-4 h-4 fill-current opacity-70 group-hover/btn:opacity-100" /> Start Workflow
                                    </button>
                                    <button
                                        onClick={() => {
                                            setEditingWorkflow(wf);
                                            setIsEditing(true);
                                        }}
                                        className="px-4 py-2.5 bg-white border border-gray-200 hover:bg-gray-50 text-gray-600 rounded-xl text-sm font-bold transition-colors"
                                    >
                                        Edit
                                    </button>
                                </div>
                            </div>
                        ))}
                        {workflows.length === 0 && (
                            <div className="col-span-full py-12 flex flex-col items-center justify-center text-gray-400 border-2 border-dashed border-gray-200 rounded-2xl">
                                <Rocket className="w-12 h-12 mb-3 text-gray-300" />
                                <p>No workflows created yet.</p>
                            </div>
                        )}
                    </div>
                )}
            </div>
        </div>
    );
};