File size: 9,850 Bytes
bcf46c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect, useRef } from 'react';
import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer } from 'recharts';
import { initLocalEngine, routeInference } from '../utils/inferenceRouter';
import HCaptcha from '@hcaptcha/react-hcaptcha';
import { supabase, useAuth, GATEWAY_URL, LEMON_CHECKOUT_URL } from '../context';

export default function WatchDashboardSection({ usage, setPage }) {
  const [watches, setWatches] = useState([]);
  const [monitors, setMonitors] = useState([]);
  const [loading, setLoading] = useState(true);
  const [view, setView] = useState('opticparse'); // 'opticparse' or 'phishvision'
  const [showCreate, setShowCreate] = useState(false);
  const [createUrl, setCreateUrl] = useState('');
  const [createQuery, setCreateQuery] = useState('');

  const fetchData = async () => {
    setLoading(true);
    try {
      const resOp = await fetch('https://opticparse-python-sg.onrender.com/gateway/watches');
      const dataOp = await resOp.json();
      setWatches(dataOp.watches || []);
      
      const resPv = await fetch('https://opticparse-1opticparse-node-sg.onrender.com/api/monitors');
      const dataPv = await resPv.json();
      setMonitors(dataPv.monitors || []);
    } catch (e) {
      console.error(e);
    }
    setLoading(false);
  };

  useEffect(() => {
    fetchData();
  }, []);

  const handleDelete = async (id, type) => {
    if (!confirm("Delete this monitor?")) return;
    try {
      if (type === 'opticparse') {
        await fetch(`https://opticparse-python-sg.onrender.com/api/watch/${id}`, { method: 'DELETE' });
      } else {
        await fetch(`https://opticparse-1opticparse-node-sg.onrender.com/api/monitor/${id}`, { method: 'DELETE' });
      }
      fetchData();
    } catch (e) {
      console.error(e);
      console.error(e);
    }
  };

  const handleCreateOpticParseMonitor = async (e) => {
    e.preventDefault();
    try {
      await fetch('https://opticparse-python-sg.onrender.com/gateway/watches', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ url: createUrl, query: createQuery })
      });
      setShowCreate(false);
      setCreateUrl('');
      setCreateQuery('');
      fetchData();
    } catch (err) {
      console.error("Failed to create monitor", err);
    }
  };

  const handleCreatePhishVisionMonitor = async (e) => {
    e.preventDefault();
    try {
      await fetch('https://opticparse-1opticparse-node-sg.onrender.com/api/monitors', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ 
          url: createUrl, 
          webhook_url: localStorage.getItem('opticparse_webhook_url') || 'https://example.com/webhook',
          interval_minutes: 60
        })
      });
      setShowCreate(false);
      setCreateUrl('');
      fetchData();
    } catch (err) {
      console.error("Failed to create PV monitor", err);
    }
  };



  return (
    <div className="card mt-2 animate-in">
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
        <h2>Cron Jobs & Monitors</h2>
        <div style={{ display: 'flex', gap: '0.5rem' }}>
          <button className={`btn ${view === 'opticparse' ? 'btn-primary' : 'btn-outline'}`} onClick={() => setView('opticparse')} style={{ padding: '0.25rem 0.75rem', fontSize: '0.8rem' }}>
            OpticParse ({watches.length})
          </button>
          <button className={`btn ${view === 'phishvision' ? 'btn-primary' : 'btn-outline'}`} onClick={() => setView('phishvision')} style={{ padding: '0.25rem 0.75rem', fontSize: '0.8rem' }}>
            PhishVision ({monitors.length})
          </button>
          <button className="btn btn-primary" onClick={() => setShowCreate(true)} style={{ padding: '0.25rem 0.75rem', fontSize: '0.8rem' }}>
            + Create
          </button>
        </div>
      </div>
      
      {showCreate && view === 'opticparse' && (
        <form onSubmit={handleCreateOpticParseMonitor} className="mb-4 mt-3 p-3 rounded" style={{ background: 'var(--surface-color)', border: '1px solid var(--border-color)' }}>
          <h4 className="mb-3">Create OpticParse Monitor</h4>
          <div className="form-group mb-2">
            <label>Target URL</label>
            <input type="url" className="input-field" value={createUrl} onChange={e => setCreateUrl(e.target.value)} placeholder="https://example.com" required />
          </div>
          <div className="form-group mb-3">
            <label>Extraction Query</label>
            <input type="text" className="input-field" value={createQuery} onChange={e => setCreateQuery(e.target.value)} placeholder="What data to track?" required />
          </div>
          <div style={{ display: 'flex', gap: '0.5rem' }}>
            <button type="submit" className="btn btn-primary">Create</button>
            <button type="button" className="btn btn-outline" onClick={() => setShowCreate(false)}>Cancel</button>
          </div>
        </form>
      )}

      {showCreate && view === 'phishvision' && (
        <form onSubmit={handleCreatePhishVisionMonitor} className="mb-4 mt-3 p-3 rounded" style={{ background: 'var(--surface-color)', border: '1px solid var(--border-color)' }}>
          <h4 className="mb-3">Create PhishVision Monitor</h4>
          <div className="form-group mb-3">
            <label>Target URL</label>
            <input type="url" className="input-field" value={createUrl} onChange={e => setCreateUrl(e.target.value)} placeholder="https://example.com" required />
          </div>
          <div style={{ display: 'flex', gap: '0.5rem' }}>
            <button type="submit" className="btn btn-primary">Create</button>
            <button type="button" className="btn btn-outline" onClick={() => setShowCreate(false)}>Cancel</button>
          </div>
        </form>
      )}

      
      <div style={{ marginTop: '1rem', overflowX: 'auto' }}>
        {loading ? (
          <div style={{ color: 'var(--muted)' }}>Loading active monitors...</div>
        ) : view === 'opticparse' ? (
          watches.length === 0 ? <div style={{ color: 'var(--muted)' }}>No active OpticParse watches.</div> : (
            <table style={{ width: '100%', borderCollapse: 'collapse', fontSize: '0.85rem' }}>
              <thead>
                <tr style={{ borderBottom: '1px solid var(--border)', textAlign: 'left' }}>
                  <th style={{ padding: '0.5rem' }}>Target URL</th>
                  <th style={{ padding: '0.5rem' }}>Created At</th>
                  <th style={{ padding: '0.5rem' }}>Last Result</th>
                  <th style={{ padding: '0.5rem' }}>Actions</th>
                </tr>
              </thead>
              <tbody>
                {watches.map(w => (
                  <tr key={w.id} style={{ borderBottom: '1px solid rgba(255,255,255,0.05)' }}>
                    <td style={{ padding: '0.5rem', maxWidth: '200px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }} title={w.url}>{w.url}</td>
                    <td style={{ padding: '0.5rem', color: 'var(--muted)' }}>{new Date(w.created_at * 1000).toLocaleString()}</td>
                    <td style={{ padding: '0.5rem', maxWidth: '200px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{w.last_result?.substring(0, 50)}...</td>
                    <td style={{ padding: '0.5rem' }}>
                      <button className="btn btn-outline" style={{ fontSize: '0.75rem', padding: '0.25rem 0.5rem', color: 'var(--red)', borderColor: 'var(--red)' }} onClick={() => handleDelete(w.id, 'opticparse')}>Delete</button>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          )
        ) : (
          monitors.length === 0 ? <div style={{ color: 'var(--muted)' }}>No active PhishVision monitors.</div> : (
            <table style={{ width: '100%', borderCollapse: 'collapse', fontSize: '0.85rem' }}>
              <thead>
                <tr style={{ borderBottom: '1px solid var(--border)', textAlign: 'left' }}>
                  <th style={{ padding: '0.5rem' }}>Target URL</th>
                  <th style={{ padding: '0.5rem' }}>Interval (ms)</th>
                  <th style={{ padding: '0.5rem' }}>Last Verdict</th>
                  <th style={{ padding: '0.5rem' }}>Actions</th>
                </tr>
              </thead>
              <tbody>
                {monitors.map(m => (
                  <tr key={m.id} style={{ borderBottom: '1px solid rgba(255,255,255,0.05)' }}>
                    <td style={{ padding: '0.5rem', maxWidth: '200px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }} title={m.url}>{m.url}</td>
                    <td style={{ padding: '0.5rem', color: 'var(--muted)' }}>{m.interval_ms || m.intervalMs}</td>
                    <td style={{ padding: '0.5rem', color: (m.last_run_verdict || m.lastRunVerdict) === 'malicious' ? 'var(--red)' : 'var(--green)' }}>
                      {((m.last_run_verdict || m.lastRunVerdict) || 'pending').toUpperCase()}
                    </td>
                    <td style={{ padding: '0.5rem' }}>
                      <button className="btn btn-outline" style={{ fontSize: '0.75rem', padding: '0.25rem 0.5rem', color: 'var(--red)', borderColor: 'var(--red)' }} onClick={() => handleDelete(m.id, 'phishvision')}>Delete</button>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          )
        )}
      </div>
      <div style={{ marginTop: '1rem', fontSize: '0.8rem', color: 'var(--muted)' }}>
        To create a new watch/monitor, please use the <b>API Docs</b> endpoints or the <b>Integrations</b> tab.
      </div>
    </div>
  );
}