File size: 12,048 Bytes
84ca4b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useMemo, useRef } from 'react';
import { Sparkles, ArrowRight, ArrowLeft } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { motion, AnimatePresence } from 'framer-motion';
import UploadStep from '@/components/upload/UploadStep';
import ProductSelector from '@/components/products/ProductSelector';
import PromptEditor from '@/components/prompts/PromptEditor';
import SequenceViewer from '@/components/sequences/SequenceViewer';
import { apiFetch } from '@/lib/api';
import { useGeneratorWorkflow } from '@/context/GeneratorWorkflowContext';

export default function EmailGeneratorTab() {
    const {
        step,
        setStep,
        uploadedFile,
        setUploadedFile,
        selectedProducts,
        setSelectedProducts,
        prompts,
        setPrompts,
        linkedinPrompts,
        setLinkedinPrompts,
        includeLinkedin,
        setIncludeLinkedin,
        isGenerating,
        generationComplete,
        beginGeneration,
    } = useGeneratorWorkflow();

    const generateButtonRef = useRef(null);

    const canProceedToStep2 = uploadedFile && selectedProducts.length > 0;
    const canProceedToStep3 = useMemo(() => {
        const emailOk = selectedProducts.length > 0 && selectedProducts.every((p) => (prompts[p.name] || '').trim());
        if (!emailOk) return false;
        if (!includeLinkedin) return true;
        return selectedProducts.every((p) => (linkedinPrompts[p.name] || '').trim());
    }, [selectedProducts, prompts, linkedinPrompts, includeLinkedin]);

    const scrollToGenerateButton = () => {
        if (generateButtonRef.current) {
            generateButtonRef.current.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }
    };

    const handleGenerate = async () => {
        if (!uploadedFile?.fileId || !canProceedToStep3) {
            alert('Please complete all steps before generating sequences.');
            return;
        }
        try {
            const res = await apiFetch('/api/save-prompts', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    file_id: uploadedFile.fileId,
                    prompts,
                    products: selectedProducts.map((p) => p.name),
                    linkedin_prompts: includeLinkedin ? linkedinPrompts : {},
                }),
            });
            if (!res.ok) {
                const err = await res.json().catch(() => ({}));
                throw new Error(err.detail || res.statusText);
            }
        } catch (error) {
            console.error('Error saving prompts:', error);
            alert('Failed to save templates. Please try again.');
            return;
        }
        setStep(3);
        beginGeneration(true);
    };

    return (
        <div>
            <div className="mb-10">
                <div className="flex items-center justify-center gap-4">
                    {[
                        { num: 1, label: 'Upload & Select' },
                        { num: 2, label: 'Configure Prompts' },
                        { num: 3, label: 'Generate & Export' },
                    ].map((s, idx) => (
                        <React.Fragment key={s.num}>
                            <div className="flex items-center gap-2">
                                <div
                                    className={`h-8 w-8 rounded-full flex items-center justify-center text-sm font-semibold transition-all duration-300 ${
                                        step >= s.num
                                            ? 'bg-violet-600 text-white shadow-lg shadow-violet-200'
                                            : 'bg-slate-100 text-slate-400'
                                    }`}
                                >
                                    {s.num}
                                </div>
                                <span
                                    className={`text-sm font-medium hidden sm:block ${
                                        step >= s.num ? 'text-slate-800' : 'text-slate-400'
                                    }`}
                                >
                                    {s.label}
                                </span>
                            </div>
                            {idx < 2 && (
                                <div
                                    className={`h-0.5 w-12 rounded-full transition-colors duration-300 ${
                                        step > s.num ? 'bg-violet-600' : 'bg-slate-200'
                                    }`}
                                />
                            )}
                        </React.Fragment>
                    ))}
                </div>
            </div>

            <AnimatePresence mode="wait">
                {step === 1 && (
                    <motion.div
                        key="step1"
                        initial={{ opacity: 0, x: -20 }}
                        animate={{ opacity: 1, x: 0 }}
                        exit={{ opacity: 0, x: 20 }}
                        transition={{ duration: 0.3 }}
                        className="space-y-8"
                    >
                        <div className="text-center mb-8">
                            <h2 className="text-2xl font-bold text-slate-800 mb-2">Upload Your Contacts</h2>
                            <p className="text-slate-500">
                                Import your Apollo CSV and select products for your campaign
                            </p>
                        </div>
                        <UploadStep
                            onFileUploaded={setUploadedFile}
                            uploadedFile={uploadedFile}
                            onRemoveFile={() => setUploadedFile(null)}
                        />
                        {uploadedFile && (
                            <motion.div initial={{ opacity: 0, y: 20 }} animate={{ opacity: 1, y: 0 }}>
                                <ProductSelector
                                    selectedProducts={selectedProducts}
                                    onProductsChange={setSelectedProducts}
                                />
                            </motion.div>
                        )}
                        <div className="flex justify-end pt-4">
                            <Button
                                onClick={() => setStep(2)}
                                disabled={!canProceedToStep2}
                                className="bg-violet-600 hover:bg-violet-700 px-6"
                            >
                                Continue to Prompts
                                <ArrowRight className="h-4 w-4 ml-2" />
                            </Button>
                        </div>
                    </motion.div>
                )}

                {step === 2 && (
                    <motion.div
                        key="step2"
                        initial={{ opacity: 0, x: -20 }}
                        animate={{ opacity: 1, x: 0 }}
                        exit={{ opacity: 0, x: 20 }}
                        transition={{ duration: 0.3 }}
                        className="space-y-8"
                    >
                        <div className="text-center mb-8">
                            <h2 className="text-2xl font-bold text-slate-800 mb-2">Customize Your Prompts</h2>
                            <p className="text-slate-500">Edit templates before generation.</p>
                        </div>
                        <PromptEditor
                            selectedProducts={selectedProducts}
                            prompts={prompts}
                            onPromptsChange={setPrompts}
                            onSaveComplete={scrollToGenerateButton}
                            variant="email"
                        />
                        <label className="flex cursor-pointer items-start gap-3 rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
                            <input
                                type="checkbox"
                                className="mt-1 h-4 w-4 rounded border-slate-300 text-violet-600 focus:ring-violet-500"
                                checked={includeLinkedin}
                                onChange={(e) => setIncludeLinkedin(e.target.checked)}
                            />
                            <span>
                                <span className="font-medium text-slate-800">
                                    Also generate LinkedIn sequences for the same contacts
                                </span>
                            </span>
                        </label>
                        {includeLinkedin ? (
                            <div className="space-y-2">
                                <h3 className="text-lg font-semibold text-slate-800">LinkedIn prompts</h3>
                                <PromptEditor
                                    selectedProducts={selectedProducts}
                                    prompts={linkedinPrompts}
                                    onPromptsChange={setLinkedinPrompts}
                                    onSaveComplete={scrollToGenerateButton}
                                    variant="linkedin"
                                />
                            </div>
                        ) : null}
                        <div className="flex justify-between pt-4">
                            <Button variant="outline" onClick={() => setStep(1)} className="px-6">
                                <ArrowLeft className="h-4 w-4 mr-2" />
                                Back
                            </Button>
                            <Button
                                ref={generateButtonRef}
                                onClick={handleGenerate}
                                disabled={!canProceedToStep3}
                                className="bg-gradient-to-r from-violet-600 to-purple-600 hover:from-violet-700 hover:to-purple-700 px-8 shadow-lg shadow-violet-200"
                            >
                                <Sparkles className="h-4 w-4 mr-2" />
                                Generate Sequences
                            </Button>
                        </div>
                    </motion.div>
                )}

                {step === 3 && (
                    <motion.div
                        key="step3"
                        initial={{ opacity: 0, x: -20 }}
                        animate={{ opacity: 1, x: 0 }}
                        exit={{ opacity: 0, x: 20 }}
                        transition={{ duration: 0.3 }}
                        className="space-y-8"
                    >
                        <div className="text-center mb-8">
                            <h2 className="text-2xl font-bold text-slate-800 mb-2">
                                {generationComplete ? 'Your Sequences Are Ready!' : 'Generating…'}
                            </h2>
                            <p className="text-slate-500">
                                {generationComplete
                                    ? 'Review and export below.'
                                    : 'You can navigate away; generation continues in background.'}
                            </p>
                        </div>
                        <SequenceViewer />
                        {!isGenerating && (
                            <div className="flex justify-start pt-4">
                                <Button variant="outline" onClick={() => setStep(2)} className="px-6">
                                    <ArrowLeft className="h-4 w-4 mr-2" />
                                    Edit Templates
                                </Button>
                            </div>
                        )}
                    </motion.div>
                )}
            </AnimatePresence>
        </div>
    );
}