fsanyoto commited on
Commit
222485d
·
verified ·
1 Parent(s): b36e641

Prune files not in the deploy set (stale build context)

Browse files
web/src/automation/AutomationCreate.tsx DELETED
@@ -1,550 +0,0 @@
1
- // ---------------------------------------------------------------------------
2
- // automation/AutomationCreate.tsx — the one form that is NOT a step list.
3
- //
4
- // WHY A FORM AND NOT AN EMPTY CANVAS. The node graph is derived by the server
5
- // from a stored definition (`engine.graph`), which is what stops the drawing
6
- // from ever describing a step the engine no longer runs. An automation that does
7
- // not exist yet has no definition, so drawing it would mean a SECOND, client-side
8
- // idea of what the steps are — exactly the drift the split was built to prevent.
9
- //
10
- // So creation is a short form and everything after it is the numbered steps. That is also
11
- // how the two kinds actually differ: you cannot map columns you have not seen, so
12
- // "Read the page" has to happen before a scrape can be created at all.
13
- // ---------------------------------------------------------------------------
14
- import { useCallback, useEffect, useState } from "react";
15
-
16
- import type { AutomationKind, SourcePreview, UserTable } from "./automationApi";
17
- import {
18
- AutomationError,
19
- createAutomation,
20
- fieldKeyFor,
21
- listTables,
22
- previewSource,
23
- } from "./automationApi";
24
-
25
- interface Props {
26
- kinds: { key: AutomationKind; label: string }[];
27
- onCreated: (id: string) => void | Promise<void>;
28
- onCancel: () => void;
29
- }
30
-
31
- interface ColumnPlan {
32
- column: string;
33
- include: boolean;
34
- key: string;
35
- }
36
-
37
- /**
38
- * C2's THREE CARDS — the wizard's first question, asked before the kind.
39
- *
40
- * ⛔ ONE LINE EACH (R13). The temptation here is to explain what a "machine database" is; the
41
- * person reading has already decided to make an automation and every extra word is spent on
42
- * someone who is leaving. What each mode DOES is visible one screen later, where the choice
43
- * has consequences.
44
- */
45
- const TARGET_MODES: { mode: string; label: string; line: string }[] = [
46
- { mode: "existing", label: "Use an existing database",
47
- line: "It works on rows that are already there." },
48
- { mode: "new", label: "Create a new database",
49
- line: "An empty one, made now, named by you." },
50
- { mode: "automated", label: "Create an automated database",
51
- line: "The automation makes it on its first run." },
52
- ];
53
-
54
- export default function AutomationCreate({ kinds, onCreated, onCancel }: Props) {
55
- /**
56
- * ⚠ TWO STEPS, NOT A WIZARD WITH A BACK STACK. The first answers "what does it work on", the
57
- * second "what does it do" — and the second is the form this file has always been, because
58
- * the engine still requires one of its three machine KINDS (`clean_definition` refuses an
59
- * automation without one). C2 anticipates trigger-and-actions-only automations for the
60
- * `existing`/`new` modes; there is no server kind for that yet, so the kind question stays
61
- * and the wave mailbox carries the ask. When a plain kind exists, step 2 collapses for those
62
- * two modes and nothing else here changes.
63
- */
64
- const [mode, setMode] = useState("");
65
- const [newLabel, setNewLabel] = useState("");
66
- const [name, setName] = useState("");
67
- const [kind, setKind] = useState<AutomationKind>("scrape_db");
68
- const [problem, setProblem] = useState("");
69
- const [message, setMessage] = useState("");
70
- const [saving, setSaving] = useState(false);
71
-
72
- const [url, setUrl] = useState("");
73
- const [extract, setExtract] = useState("table");
74
- const [tableIndex, setTableIndex] = useState(0);
75
- const [targetLabel, setTargetLabel] = useState("");
76
- const [preview, setPreview] = useState<SourcePreview | null>(null);
77
- const [reading, setReading] = useState(false);
78
- const [plan, setPlan] = useState<ColumnPlan[]>([]);
79
- const [keyField, setKeyField] = useState("");
80
-
81
- const [tables, setTables] = useState<UserTable[]>([]);
82
- const [targetTable, setTargetTable] = useState("");
83
- const [fieldKey, setFieldKey] = useState("");
84
- const [urlField, setUrlField] = useState("");
85
-
86
- useEffect(() => {
87
- const ac = new AbortController();
88
- listTables(ac.signal)
89
- .then((r) => setTables(r.tables))
90
- .catch(() => setTables([]));
91
- return () => ac.abort();
92
- }, []);
93
-
94
- const table = tables.find((t) => t.key === targetTable) || null;
95
-
96
- const readPage = useCallback(async () => {
97
- setReading(true);
98
- setProblem("");
99
- try {
100
- const p = await previewSource(url, extract, tableIndex);
101
- setPreview(p);
102
- if (!p.ok) {
103
- setProblem(p.note || `That page answered ${p.status}.`);
104
- return;
105
- }
106
- setPlan(
107
- p.columns.map((column) => ({ column, include: true, key: fieldKeyFor(column) }))
108
- );
109
- setKeyField(fieldKeyFor(p.columns[0] || ""));
110
- if (!targetLabel) setTargetLabel(p.title || "Scraped table");
111
- setMessage(
112
- `Read ${p.rowCount} rows and ${p.columns.length} columns` +
113
- (p.tableCount && p.tableCount > 1 ? ` (of ${p.tableCount} tables on the page)` : "")
114
- );
115
- } catch (e) {
116
- setProblem(e instanceof AutomationError ? e.message : "That page could not be read.");
117
- } finally {
118
- setReading(false);
119
- }
120
- }, [url, extract, tableIndex, targetLabel]);
121
-
122
- const create = async () => {
123
- setSaving(true);
124
- setProblem("");
125
- try {
126
- const fieldMap: Record<string, string> = {};
127
- for (const c of plan) if (c.include && c.key) fieldMap[c.column] = c.key;
128
- const res = await createAutomation({
129
- name,
130
- kind,
131
- // C2's target, resolved SERVER-SIDE in the same call that creates the automation
132
- // (`resolve_target` runs before validation), so a `new` database is never a
133
- // create-then-patch this client has to sequence — and an automation is never saved
134
- // pointing at a table that does not exist yet.
135
- target:
136
- mode === "existing"
137
- ? { mode, table: targetTable }
138
- : mode === "new"
139
- ? { mode, label: newLabel }
140
- : { mode: "automated", label: targetLabel },
141
- schedule: { cron: "0 6 * * *", enabled: false },
142
- config:
143
- kind === "scrape_db"
144
- ? { url, extract, tableIndex, fieldMap, keyField, targetLabel }
145
- : kind === "discover_instagram"
146
- ? {
147
- // A search is created with a SMALL, NARROW default and no schedule, then
148
- // widened in the steps, where the cost estimate is. The one shape that must
149
- // never be the default is the expensive one.
150
- recordsLimit: 25,
151
- operator: "and",
152
- // ⚠ EVERY DEFAULT PREDICATE MUST BE ONE THE SERVER ACCEPTS (item 8). This
153
- // list carried `{biography includes ""}` — a condition with no value, which
154
- // `clean_predicates` refuses by design ("give a value to compare
155
- // 'biography' against"), so EVERY attempt to create a search failed on a
156
- // condition nobody had typed and the form gave no way to remove. The
157
- // opposite mistake is just as easy: an EMPTY list is refused too (an empty
158
- // filter matches the whole corpus), so this is one valid condition, never
159
- // none.
160
- predicates: [{ name: "followers", operator: ">=", value: 10000 }],
161
- }
162
- : { targetTable, fieldKey, urlField, maxPosts: 24, tier: "anonymous" },
163
- });
164
- await onCreated(res.automation.id);
165
- } catch (e) {
166
- setProblem(e instanceof AutomationError ? e.message : "That could not be created.");
167
- } finally {
168
- setSaving(false);
169
- }
170
- };
171
-
172
- const canCreate =
173
- !!name.trim() &&
174
- // The MODE's own question has to be answered too — `resolve_target` refuses a blank one
175
- // with its own sentence ("choose the database this automation works on"), and offering a
176
- // Create button that can only produce that refusal is a control that lies.
177
- (mode === "existing" ? !!targetTable : mode === "new" ? !!newLabel.trim() : true) &&
178
- (kind === "scrape_db"
179
- ? !!url.trim() && plan.some((c) => c.include && c.key === keyField)
180
- : kind === "discover_instagram"
181
- ? true
182
- : !!targetTable && !!fieldKey);
183
-
184
- /* ── STEP 1: what does it work on? (C2, images 1-3's front door) ───────────────────────── */
185
- if (!mode) {
186
- return (
187
- <div className="auto-create-pane">
188
- <div className="auto-create-card">
189
- <h1>New automation</h1>
190
- <div className="autox-modes">
191
- {TARGET_MODES.map((m) => (
192
- <button
193
- key={m.mode}
194
- type="button"
195
- className="autox-mode"
196
- onClick={() => setMode(m.mode)}
197
- >
198
- <span className="autox-mode-label">{m.label}</span>
199
- <span className="autox-mode-line">{m.line}</span>
200
- </button>
201
- ))}
202
- </div>
203
- <div className="auto-head-actions">
204
- <button type="button" className="auto-btn" onClick={onCancel}>
205
- Cancel
206
- </button>
207
- </div>
208
- </div>
209
- </div>
210
- );
211
- }
212
-
213
- return (
214
- <div className="auto-create-pane">
215
- <div className="auto-create-card">
216
- <h1>New automation</h1>
217
-
218
- {/* STEP 2's answer, and a way back to change it. A chosen mode that cannot be unchosen
219
- is a dead end one click deep. */}
220
- <div className="autox-modeback">
221
- <span className="autox-mode-label">
222
- {TARGET_MODES.find((m) => m.mode === mode)?.label || mode}
223
- </span>
224
- <button type="button" className="autoc-add" onClick={() => setMode("")}>
225
- Change
226
- </button>
227
- </div>
228
-
229
- {mode === "existing" ? (
230
- <div className="auto-field">
231
- <label htmlFor="auto-new-target">Database</label>
232
- <select
233
- id="auto-new-target"
234
- className="auto-input"
235
- value={targetTable}
236
- onChange={(e) => {
237
- setTargetTable(e.target.value);
238
- setFieldKey("");
239
- setUrlField("");
240
- }}
241
- >
242
- <option value="">Choose a database…</option>
243
- {tables.map((t) => (
244
- <option key={t.key} value={t.key}>
245
- {t.label} ({t.rowCount} {t.rowCount === 1 ? "row" : "rows"})
246
- </option>
247
- ))}
248
- </select>
249
- </div>
250
- ) : mode === "new" ? (
251
- <div className="auto-field">
252
- <label htmlFor="auto-new-dblabel">Name the new database</label>
253
- <input
254
- id="auto-new-dblabel"
255
- className="auto-input"
256
- value={newLabel}
257
- placeholder="Supplier leads"
258
- onChange={(e) => setNewLabel(e.target.value)}
259
- />
260
- <p className="auto-hint">It is created empty, with this automation.</p>
261
- </div>
262
- ) : null}
263
-
264
- {problem ? (
265
- <div className="auto-banner is-error" role="alert">
266
- {problem}
267
- </div>
268
- ) : null}
269
- {message ? (
270
- <div className="auto-banner is-ok" role="status">
271
- {message}
272
- </div>
273
- ) : null}
274
-
275
- <div className="auto-field">
276
- <label htmlFor="auto-new-name">Name</label>
277
- <input
278
- id="auto-new-name"
279
- className="auto-input"
280
- value={name}
281
- placeholder="S&P 500 companies"
282
- onChange={(e) => setName(e.target.value)}
283
- />
284
- </div>
285
-
286
- <div className="auto-kinds">
287
- {kinds.map((k) => (
288
- <label key={k.key} className={"auto-kind" + (kind === k.key ? " is-on" : "")}>
289
- <input
290
- type="radio"
291
- name="auto-kind"
292
- checked={kind === k.key}
293
- onChange={() => setKind(k.key)}
294
- />
295
- <span>{k.label}</span>
296
- </label>
297
- ))}
298
- </div>
299
-
300
- {kind === "discover_instagram" ? (
301
- <>
302
- <p className="auto-hint">
303
- It searches a corpus of pre-collected public Instagram profiles for accounts you
304
- have never heard of, and collects the matches into a candidates database. You set
305
- the conditions and how many profiles to fetch in its steps, where the cost
306
- estimate is — searching is the one thing this workspace does that spends money per
307
- run.
308
- </p>
309
- <p className="auto-hint">
310
- It starts with a narrow default and no schedule. Nothing it finds is ever tracked
311
- automatically; you tick the accounts you want.
312
- </p>
313
- </>
314
- ) : kind === "scrape_db" ? (
315
- <>
316
- <div className="auto-field">
317
- <label htmlFor="auto-new-url">Page URL</label>
318
- <div className="auto-row-inline">
319
- <input
320
- id="auto-new-url"
321
- className="auto-input"
322
- value={url}
323
- placeholder="https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
324
- onChange={(e) => setUrl(e.target.value)}
325
- />
326
- <button
327
- type="button"
328
- className="auto-btn"
329
- disabled={!url.trim() || reading}
330
- onClick={() => void readPage()}
331
- >
332
- {reading ? "Reading…" : "Read the page"}
333
- </button>
334
- </div>
335
- <p className="auto-hint">
336
- Public web pages only. Private, loopback and link-local addresses are refused —
337
- including a public URL that redirects to one.
338
- </p>
339
- </div>
340
- <div className="auto-field-row">
341
- <div className="auto-field">
342
- <label htmlFor="auto-new-extract">Read</label>
343
- <select
344
- id="auto-new-extract"
345
- className="auto-input"
346
- value={extract}
347
- onChange={(e) => setExtract(e.target.value)}
348
- >
349
- <option value="table">An HTML table</option>
350
- <option value="jsonld">Structured data (JSON-LD)</option>
351
- </select>
352
- </div>
353
- {extract === "table" ? (
354
- <div className="auto-field">
355
- <label htmlFor="auto-new-tidx">Which table</label>
356
- <input
357
- id="auto-new-tidx"
358
- className="auto-input"
359
- type="number"
360
- min={0}
361
- value={tableIndex}
362
- onChange={(e) => setTableIndex(Number(e.target.value) || 0)}
363
- />
364
- </div>
365
- ) : null}
366
- <div className="auto-field">
367
- <label htmlFor="auto-new-tlabel">Database name</label>
368
- <input
369
- id="auto-new-tlabel"
370
- className="auto-input"
371
- value={targetLabel}
372
- placeholder="S&P 500 companies"
373
- onChange={(e) => setTargetLabel(e.target.value)}
374
- />
375
- </div>
376
- </div>
377
-
378
- {plan.length ? (
379
- <div className="auto-map">
380
- <h3>Columns</h3>
381
- <p className="auto-hint">
382
- Pick the key — the column that identifies a row. On every re-run, a row whose
383
- key already exists is updated; a new key is inserted; a key that has stopped
384
- appearing is counted and kept.
385
- </p>
386
- <div className="auto-drill-scroll">
387
- <table className="auto-table">
388
- <thead>
389
- <tr>
390
- <th>Use</th>
391
- <th>Source column</th>
392
- <th>Field key</th>
393
- <th>Key</th>
394
- {preview?.sample?.length ? <th>First value</th> : null}
395
- </tr>
396
- </thead>
397
- <tbody>
398
- {plan.map((c, i) => (
399
- <tr key={c.column}>
400
- <td>
401
- <input
402
- type="checkbox"
403
- checked={c.include}
404
- aria-label={`Include ${c.column}`}
405
- onChange={(e) =>
406
- setPlan((p) =>
407
- p.map((x, j) =>
408
- j === i ? { ...x, include: e.target.checked } : x
409
- )
410
- )
411
- }
412
- />
413
- </td>
414
- <td className="auto-cell-name">{c.column}</td>
415
- <td>
416
- <input
417
- className="auto-input is-small"
418
- value={c.key}
419
- aria-label={`Field key for ${c.column}`}
420
- onChange={(e) =>
421
- setPlan((p) =>
422
- p.map((x, j) =>
423
- j === i ? { ...x, key: fieldKeyFor(e.target.value) } : x
424
- )
425
- )
426
- }
427
- />
428
- </td>
429
- <td>
430
- <input
431
- type="radio"
432
- name="auto-new-keyfield"
433
- checked={keyField === c.key && c.include}
434
- disabled={!c.include}
435
- aria-label={`Use ${c.column} as the key`}
436
- onChange={() => setKeyField(c.key)}
437
- />
438
- </td>
439
- {preview?.sample?.length ? (
440
- <td className="auto-cell-sample">
441
- {String(preview.sample[0]?.[c.column] ?? "")}
442
- </td>
443
- ) : null}
444
- </tr>
445
- ))}
446
- </tbody>
447
- </table>
448
- </div>
449
- </div>
450
- ) : (
451
- <p className="auto-note">Read the page to see what columns it offers.</p>
452
- )}
453
- </>
454
- ) : (
455
- <>
456
- <div className="auto-field-row">
457
- {/* ⚠ ONE CONTROL PER FACT. Step 2 already asked which database for the
458
- `existing`/`new` modes and binds the SAME state — two selects for one value
459
- is how a form starts disagreeing with itself. Only the `automated` mode,
460
- which asks nothing up front, needs this one. */}
461
- {mode === "automated" ? (
462
- <div className="auto-field">
463
- <label htmlFor="auto-new-table">Database</label>
464
- <select
465
- id="auto-new-table"
466
- className="auto-input"
467
- value={targetTable}
468
- onChange={(e) => {
469
- setTargetTable(e.target.value);
470
- setFieldKey("");
471
- setUrlField("");
472
- }}
473
- >
474
- <option value="">Choose a database…</option>
475
- {tables.map((t) => (
476
- <option key={t.key} value={t.key}>
477
- {t.label} ({t.rowCount} {t.rowCount === 1 ? "row" : "rows"})
478
- </option>
479
- ))}
480
- </select>
481
- </div>
482
- ) : null}
483
- <div className="auto-field">
484
- <label htmlFor="auto-new-fieldkey">Automation column</label>
485
- <select
486
- id="auto-new-fieldkey"
487
- className="auto-input"
488
- value={fieldKey}
489
- onChange={(e) => setFieldKey(e.target.value)}
490
- >
491
- <option value="">Choose a column…</option>
492
- {(table?.fields || [])
493
- .filter((f) => f.type === "automation")
494
- .map((f) => (
495
- <option key={f.key} value={f.key}>
496
- {f.label}
497
- </option>
498
- ))}
499
- </select>
500
- </div>
501
- <div className="auto-field">
502
- <label htmlFor="auto-new-urlfield">Profile URL column</label>
503
- <select
504
- id="auto-new-urlfield"
505
- className="auto-input"
506
- value={urlField}
507
- onChange={(e) => setUrlField(e.target.value)}
508
- >
509
- <option value="">The column the field is bound to</option>
510
- {(table?.fields || []).map((f) => (
511
- <option key={f.key} value={f.key}>
512
- {f.label}
513
- </option>
514
- ))}
515
- </select>
516
- </div>
517
- </div>
518
- <p className="auto-hint">
519
- It starts on the free anonymous rung. The Bright Data step turns on
520
- the paid one, which returns exact counts and the profile&rsquo;s top posts, and
521
- works from a datacenter. Likes and comments per post are a separate step again —
522
- they cost one record per post rather than one per profile, so they are off until
523
- you turn them on.
524
- </p>
525
- {table && !(table.fields || []).some((f) => f.type === "automation") ? (
526
- <p className="auto-note">
527
- That database has no automation column yet. Add one from the grid&rsquo;s column
528
- menu, then choose it here.
529
- </p>
530
- ) : null}
531
- </>
532
- )}
533
-
534
- <div className="auto-head-actions">
535
- <button
536
- type="button"
537
- className="auto-btn is-primary"
538
- disabled={!canCreate || saving}
539
- onClick={() => void create()}
540
- >
541
- {saving ? "Creating…" : "Create"}
542
- </button>
543
- <button type="button" className="auto-btn" onClick={onCancel}>
544
- Cancel
545
- </button>
546
- </div>
547
- </div>
548
- </div>
549
- );
550
- }