File size: 8,248 Bytes
005df22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';
import { Download, Mail, Loader2, CheckCircle2, Search, Filter } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Progress } from '@/components/ui/progress';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { motion, AnimatePresence } from 'framer-motion';
import SequenceCard from './SequenceCard';
import { apiFetch } from '@/lib/api';
import { useGeneratorWorkflow } from '@/context/GeneratorWorkflowContext';


export default function SequenceViewer() {
    const {
        isGenerating,
        generationComplete,
        selectedProducts,
        uploadedFile,
        includeLinkedin,
        genPhase,
        contacts,
        sequences,
        progress,
        contactCount,
    } = useGeneratorWorkflow();

    const [searchQuery, setSearchQuery] = useState('');
    const [filterProduct, setFilterProduct] = useState('all');
    const [displayedCount, setDisplayedCount] = useState(50);

    useEffect(() => {
        setDisplayedCount(50);
    }, [searchQuery, filterProduct]);

    const handleDownload = async () => {
        if (!uploadedFile?.fileId) return;
        try {
            const response = await apiFetch(`/api/download-sequences?file_id=${uploadedFile.fileId}`);
            if (response.ok) {
                const blob = await response.blob();
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = 'email_sequences_fixed.csv';
                a.click();
                URL.revokeObjectURL(url);
            } else {
                alert('Failed to download CSV. Please try again.');
            }
        } catch (error) {
            console.error('Download error:', error);
            alert('Error downloading CSV. Please try again.');
        }
    };

    const filteredContacts = contacts.filter((contact) => {
        const matchesSearch =
            searchQuery === '' ||
            contact.firstName?.toLowerCase().includes(searchQuery.toLowerCase()) ||
            contact.lastName?.toLowerCase().includes(searchQuery.toLowerCase()) ||
            contact.company?.toLowerCase().includes(searchQuery.toLowerCase()) ||
            contact.email?.toLowerCase().includes(searchQuery.toLowerCase());

        const matchesFilter = filterProduct === 'all' || contact.product === filterProduct;

        return matchesSearch && matchesFilter;
    });

    const displayedContacts = filteredContacts.slice(0, displayedCount);
    const hasMore = filteredContacts.length > displayedCount;

    const loadMore = () => {
        setDisplayedCount((prev) => Math.min(prev + 50, filteredContacts.length));
    };

    const showProgress = isGenerating || !generationComplete;
    const phaseLabel =
        includeLinkedin && genPhase === 'linkedin' && isGenerating
            ? 'LinkedIn sequences'
            : 'Email sequences';

    return (
        <div className="w-full">
            <div className="rounded-2xl border border-slate-200 bg-white p-6 mb-6">
                <div className="flex items-center justify-between mb-4">
                    <div className="flex items-center gap-3">
                        {generationComplete ? (
                            <div className="rounded-xl bg-green-100 p-3">
                                <CheckCircle2 className="h-6 w-6 text-green-600" />
                            </div>
                        ) : (
                            <div className="rounded-xl bg-violet-100 p-3">
                                <Loader2 className="h-6 w-6 text-violet-600 animate-spin" />
                            </div>
                        )}
                        <div>
                            <h3 className="font-semibold text-slate-800">
                                {generationComplete ? 'Generation complete!' : `Generating ${phaseLabel}…`}
                            </h3>
                            <p className="text-sm text-slate-500">
                                {contacts.length} contacts · {sequences.length} generated rows
                                {contactCount ? ` · ~${contactCount} contacts in file` : ''}
                            </p>
                        </div>
                    </div>
                    {generationComplete && (
                        <Button onClick={handleDownload} className="bg-green-600 hover:bg-green-700">
                            <Download className="h-4 w-4 mr-2" />
                            Download CSV
                        </Button>
                    )}
                </div>
                {showProgress ? <Progress value={progress} className="h-2" /> : null}
            </div>

            {sequences.length > 0 && (
                <motion.div
                    initial={{ opacity: 0, y: -10 }}
                    animate={{ opacity: 1, y: 0 }}
                    className="flex flex-col sm:flex-row gap-3 mb-6"
                >
                    <div className="relative flex-1">
                        <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-slate-400" />
                        <Input
                            placeholder="Search contacts..."
                            value={searchQuery}
                            onChange={(e) => setSearchQuery(e.target.value)}
                            className="pl-10"
                        />
                    </div>
                    <Select value={filterProduct} onValueChange={setFilterProduct}>
                        <SelectTrigger className="w-full sm:w-48">
                            <Filter className="h-4 w-4 mr-2 text-slate-400" />
                            <SelectValue placeholder="Filter by product" />
                        </SelectTrigger>
                        <SelectContent>
                            <SelectItem value="all">All Products</SelectItem>
                            {selectedProducts.map((product) => (
                                <SelectItem key={product.id} value={product.name}>
                                    {product.name}
                                </SelectItem>
                            ))}
                        </SelectContent>
                    </Select>
                </motion.div>
            )}

            <div className="space-y-3 max-h-[600px] overflow-y-auto pr-2 custom-scrollbar">
                <AnimatePresence>
                    {displayedContacts.map((contact, index) => (
                        <SequenceCard
                            key={contact.id || `${contact.firstName}-${contact.lastName}-${contact.email}`}
                            contact={contact}
                            index={index}
                        />
                    ))}
                </AnimatePresence>
                {hasMore && (
                    <div className="text-center py-4">
                        <Button variant="outline" onClick={loadMore} className="mx-auto">
                            Load More ({filteredContacts.length - displayedCount} remaining)
                        </Button>
                    </div>
                )}
                {filteredContacts.length > 0 && (
                    <div className="text-center py-2 text-sm text-slate-500">
                        Showing {displayedContacts.length} of {filteredContacts.length} contacts
                    </div>
                )}
            </div>

            {!isGenerating && contacts.length === 0 && sequences.length === 0 && (
                <div className="text-center py-16">
                    <div className="mx-auto w-16 h-16 rounded-2xl bg-slate-100 flex items-center justify-center mb-4">
                        <Mail className="h-8 w-8 text-slate-300" />
                    </div>
                    <h3 className="text-lg font-semibold text-slate-400 mb-2">No sequences yet</h3>
                    <p className="text-sm text-slate-400">Click &quot;Generate Sequences&quot; to start</p>
                </div>
            )}
        </div>
    );
}