File size: 11,007 Bytes
2002023
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, useEffect, useRef } from 'react';
import gsap from 'gsap';

const API_BASE = '/api';

export default function App() {
  const [niche, setNiche] = useState('');
  const [location, setLocation] = useState('');
  const [limit, setLimit] = useState(10);
  const [jobStatus, setJobStatus] = useState({ status: 'idle', message: 'Awaiting transmission...', progress: 0 });
  const [results, setResults] = useState([]);
  const [activeTab, setActiveTab] = useState('search');

  const heroRef = useRef(null);
  const panelRef = useRef(null);
  const gridRef = useRef(null);
  const particlesRef = useRef([]);

  const isRunning = ['scraping','enriching','saving'].includes(jobStatus.status);

  /* ── Entrance GSAP animation ── */
  useEffect(() => {
    const ctx = gsap.context(() => {
      gsap.fromTo(heroRef.current,
        { opacity: 0, y: -40 },
        { opacity: 1, y: 0, duration: 1.1, ease: 'power4.out' }
      );
      gsap.fromTo(panelRef.current,
        { opacity: 0, y: 30, scale: 0.97 },
        { opacity: 1, y: 0, scale: 1, duration: 0.9, delay: 0.3, ease: 'back.out(1.4)' }
      );
    });
    return () => ctx.revert();
  }, []);

  /* ── Animate particles ── */
  useEffect(() => {
    particlesRef.current.forEach((el, i) => {
      if (!el) return;
      gsap.to(el, {
        y: `random(-30, 30)`,
        x: `random(-20, 20)`,
        opacity: `random(0.2, 0.8)`,
        duration: `random(3, 6)`,
        repeat: -1,
        yoyo: true,
        ease: 'sine.inOut',
        delay: i * 0.3,
      });
    });
  }, []);

  /* ── Poll status while running ── */
  useEffect(() => {
    if (!isRunning) return;
    const id = setInterval(async () => {
      try {
        const res = await fetch(`${API_BASE}/status`);
        const data = await res.json();
        setJobStatus(data);
        if (data.status === 'complete') {
          clearInterval(id);
          loadResults();
        } else if (data.status === 'error') {
          clearInterval(id);
        }
      } catch (_) {}
    }, 1000);
    return () => clearInterval(id);
  }, [isRunning]);

  /* ── Animate result rows in ── */
  useEffect(() => {
    if (results.length > 0 && gridRef.current) {
      const rows = gridRef.current.querySelectorAll('tbody tr');
      gsap.fromTo(rows, { opacity: 0, x: -15 }, {
        opacity: 1, x: 0, stagger: 0.06, duration: 0.4, ease: 'power2.out'
      });
    }
  }, [results]);

  const loadResults = async () => {
    try {
      const r = await fetch(`${API_BASE}/results`);
      setResults(await r.json());
      setActiveTab('results');
    } catch (_) {}
  };

  const handleSubmit = async (e) => {
    e.preventDefault();
    if (!niche.trim() || !location.trim()) return;
    setResults([]);
    setJobStatus({ status: 'scraping', message: 'Initiating agent...', progress: 3 });
    try {
      await fetch(`${API_BASE}/scrape`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ niche, location, limit }),
      });
    } catch (_) {
      setJobStatus({ status: 'error', message: 'Cannot reach backend on :8000', progress: 0 });
    }
  };

  const exportCSV = () => {
    const cols = ['name','website','phone','email','rating','facebook','instagram','linkedin','status'];
    const lines = [cols.join(','), ...results.map(r => cols.map(k => `"${(r[k]||'').replace(/"/g,'""')}"`).join(','))];
    const a = document.createElement('a');
    a.href = URL.createObjectURL(new Blob([lines.join('\n')], { type: 'text/csv' }));
    a.download = `leads_${niche}_${location}.csv`;
    a.click();
  };

  const safeHost = url => { try { return new URL(url).hostname; } catch { return url; } };

  const statusColor = { idle:'#4a5568', scraping:'#00f2ff', enriching:'#8b5cf6', saving:'#10b981', complete:'#22c55e', error:'#ef4444' };

  /* ── Grid layout ── */
  return (
    <div id="root-app">
      {/* Particles */}
      <div className="particles">
        {[...Array(18)].map((_, i) => (
          <div key={i} className="particle" ref={el => particlesRef.current[i] = el}
            style={{ left: `${Math.random()*100}%`, top: `${Math.random()*100}%`, width: `${2+Math.random()*3}px`, height: `${2+Math.random()*3}px`, opacity: 0.3 + Math.random() * 0.5 }} />
        ))}
      </div>

      <div className="layout">

        {/* ── HERO ── */}
        <header className="hero" ref={heroRef}>
          <div className="hero-icon">
            <svg width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="#00f2ff" strokeWidth="1.5">
              <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>
            </svg>
          </div>
          <div>
            <h1>LEAD HUNTER <span className="dim">AI</span></h1>
            <p className="subtitle">AGENTIC BUSINESS INTELLIGENCE ENGINE v2.0</p>
          </div>
        </header>

        {/* ── STATUS BAR ── */}
        {jobStatus.status !== 'idle' && (
          <div className="status-bar">
            <div className="status-dot" style={{ background: statusColor[jobStatus.status] || '#4a5568' }} />
            <span className="status-msg">{jobStatus.message}</span>
            <span className="status-pct">{jobStatus.progress}%</span>
          </div>
        )}
        {jobStatus.status !== 'idle' && (
          <div className="progress-track">
            <div className="progress-fill" style={{ width: `${jobStatus.progress}%`, background: statusColor[jobStatus.status] || '#00f2ff' }} />
          </div>
        )}

        {/* ── TABS ── */}
        <div className="tabs">
          <button className={`tab ${activeTab==='search'?'active':''}`} onClick={() => setActiveTab('search')}>⚑ Search</button>
          <button className={`tab ${activeTab==='results'?'active':''}`} onClick={() => setActiveTab('results')}>
            πŸ“‘ Results {results.length > 0 && <span className="badge">{results.length}</span>}
          </button>
        </div>

        {/* ── SEARCH PANEL ── */}
        {activeTab === 'search' && (
          <div className="panel" ref={panelRef}>
            <form onSubmit={handleSubmit} className="search-form">
              <div className="field-group">
                <label className="field-label">TARGET NICHE</label>
                <div className="input-wrap">
                  <span className="input-icon">πŸ”</span>
                  <input className="input" placeholder="e.g. Roofers, Pool Cleaners..." value={niche} onChange={e => setNiche(e.target.value)} disabled={isRunning} required />
                </div>
              </div>
              <div className="field-group">
                <label className="field-label">TARGET LOCATION</label>
                <div className="input-wrap">
                  <span className="input-icon">πŸ“</span>
                  <input className="input" placeholder="e.g. Miami, New York..." value={location} onChange={e => setLocation(e.target.value)} disabled={isRunning} required />
                </div>
              </div>
              <div className="field-group">
                <label className="field-label">LEAD COUNT</label>
                <div className="input-wrap">
                  <span className="input-icon">#</span>
                  <input className="input" type="number" min="1" max="50" value={limit} onChange={e => setLimit(Number(e.target.value))} disabled={isRunning} />
                </div>
              </div>
              <button className="btn-execute" type="submit" disabled={isRunning}>
                {isRunning ? <span className="spinner">⟳</span> : '⚑'} {isRunning ? 'AGENT ACTIVE...' : 'INITIALIZE HUNT'}
              </button>
            </form>

            <div className="info-cards">
              <div className="info-card"><div className="info-num">3</div><div className="info-label">AI Agents</div></div>
              <div className="info-card"><div className="info-num">∞</div><div className="info-label">Niches</div></div>
              <div className="info-card"><div className="info-num">Free</div><div className="info-label">Cost</div></div>
            </div>
          </div>
        )}

        {/* ── RESULTS PANEL ── */}
        {activeTab === 'results' && (
          <div className="panel results-panel">
            {results.length === 0 ? (
              <div className="empty-state">
                <p>πŸ“‘ No data yet. Run a search first.</p>
              </div>
            ) : (
              <>
                <div className="results-header">
                  <span className="results-count">{results.length} leads discovered</span>
                  <button className="btn-export" onClick={exportCSV}>⬇ Export CSV</button>
                </div>
                <div className="table-wrap">
                  <table className="results-table" ref={gridRef}>
                    <thead>
                      <tr>
                        <th>BUSINESS</th>
                        <th>CONTACT</th>
                        <th>RATING</th>
                        <th>CHANNELS</th>
                        <th>STATUS</th>
                      </tr>
                    </thead>
                    <tbody>
                      {results.map((row, i) => (
                        <tr key={i}>
                          <td>
                            <div className="biz-name">{row.name}</div>
                            {row.website && <a className="biz-url" href={row.website} target="_blank" rel="noreferrer">{safeHost(row.website)}</a>}
                            {row.phone && <div className="biz-phone">{row.phone}</div>}
                          </td>
                          <td><div className="email-cell">{row.email || <span className="dim-text">β€”</span>}</div></td>
                          <td><span className="rating-badge">β˜… {row.rating || 'β€”'}</span></td>
                          <td>
                            <div className="channels">
                              {row.facebook && <a href={row.facebook} target="_blank" rel="noreferrer" className="ch-btn">fb</a>}
                              {row.instagram && <a href={row.instagram} target="_blank" rel="noreferrer" className="ch-btn">ig</a>}
                              {row.linkedin && <a href={row.linkedin} target="_blank" rel="noreferrer" className="ch-btn">in</a>}
                            </div>
                          </td>
                          <td><span className={`status-chip ${row.status === 'Success' ? 'chip-ok' : 'chip-warn'}`}>{row.status || 'β€”'}</span></td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              </>
            )}
          </div>
        )}

        <footer className="footer">LEAD HUNTER AI Β· Running on localhost Β· $0 Budget</footer>
      </div>
    </div>
  );
}