@xeplr/ui-jobs 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -134,6 +134,8 @@ Three steps (four with `chainsApiBase`), all reachable at any time, with **Save
134
134
 
135
135
  The period this job ran for passes to the next job by itself. Text that is not JSON, a missing job, or an "otherwise" that is not last is refused before anything is saved; the server checks the meaning and names the rule. An existing job's rules are saved before the job, and a new job's right after it (it needs its id).
136
136
 
137
+ An input the action marks `control: 'parameters'` (a procedure's parameters) is shown as rows, not JSON (`ParametersField`, `lib/parametersModel.js`): a parameter's name, and where its value comes from: **Start of the window**, **End of the window** (saved as `{window.from}` / `{window.to}`, which `@xeplr/jobs` fills in with the run's dates) or a **Fixed value**. The choices are the jobs package's run contract (`GET /jobs/run-contract`, `useRunContract`). A job with no parameters yet starts with the two window rows, and one whose name is left empty is not saved. An input marked `hidden` (the procedure's old **Window**) is shown only when a job already holds one.
138
+
137
139
  With `chainsApiBase`, **Inputs** also has **From the job before**: tickboxes for the inputs another job may fill when it runs this one (the job's `acceptsInputs`). No field is ticked by default, and a field naming a connection is never offered.
138
140
 
139
141
  A new job with a schedule defaults to **Paused**, so it can be written now and started later. A job with no schedule is saved as `ready` and runs only when triggered.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-jobs",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "The screens for @xeplr/jobs — schedule, run, pause and watch jobs — as routes a host app registers, or a standalone app",
5
5
  "type": "module",
6
6
  "main": "src/index.jsx",
package/src/api/jobs.js CHANGED
@@ -185,3 +185,13 @@ export async function fetchOccurrenceLogs(occurrenceId) {
185
185
  const res = await authFetch(`/logs?referenceId=${encodeURIComponent(occurrenceId)}`)
186
186
  return res.dataArray || []
187
187
  }
188
+
189
+ /**
190
+ * What every run of a job receives — its window's start and end — as the
191
+ * names an input binds to: [{ key, label, description }]. From @xeplr/jobs'
192
+ * GET /jobs/run-contract, so the dropdowns offer exactly what the server fills.
193
+ */
194
+ export async function getRunContract() {
195
+ const res = await authFetch(url('/jobs/run-contract'))
196
+ return res.dataArray || []
197
+ }
@@ -0,0 +1,20 @@
1
+ /* Parameters as rows — name ← where its value comes from. Widths set here
2
+ because the editor gives every input full width. */
3
+ .jb-params { display: grid; gap: 8px; margin: 6px 0 8px; }
4
+ .jb-param {
5
+ display: grid; align-items: center; gap: 8px;
6
+ grid-template-columns: minmax(140px, 220px) auto minmax(180px, 240px) minmax(0, 1fr) auto;
7
+ }
8
+ .jb-editor .jb-param input, .jb-editor .jb-param select { width: 100%; min-width: 0; }
9
+ .jb-param-name { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
10
+ .jb-param-arrow { color: var(--muted, #6b6a63); }
11
+ .jb-param-gap { min-width: 0; }
12
+ .jb-param-remove, .jb-param-add {
13
+ padding: 5px 10px; font: inherit; font-size: 12px; color: var(--ink, #1a1a19);
14
+ background: var(--surface, #ffffff); border: 1px solid var(--hair, #e2e1d9); border-radius: 7px; cursor: pointer;
15
+ }
16
+ .jb-param-add { justify-self: start; font-size: 13px; }
17
+ @media (max-width: 640px) {
18
+ .jb-param { grid-template-columns: 1fr auto; }
19
+ .jb-param-arrow { display: none; }
20
+ }
@@ -0,0 +1,80 @@
1
+ import { useState } from 'react'
2
+ import { FIXED, rowsFromText, rowsToText, sourceOf, valueForSource } from '../lib/parametersModel.js'
3
+ import './ParametersField.css'
4
+
5
+ /**
6
+ * Parameters as rows: a name, and where its value comes from — the start or
7
+ * end of the run's window, or a fixed value. Presentation and row editing only;
8
+ * the conversion is lib/parametersModel.js.
9
+ *
10
+ * `value` / `onChange` carry the saved text ([{ name, value }] as JSON), so
11
+ * the editor saves this field like any other list.
12
+ *
13
+ * @param {object} props.field the action's input ({ name, label, description })
14
+ * @param {string} props.value saved JSON text
15
+ * @param {Function} props.onChange
16
+ * @param {object[]} props.contract what every run receives (useRunContract)
17
+ */
18
+ export default function ParametersField({ field, value, onChange, contract }) {
19
+ const [rows, setRows] = useState(() => rowsFromText(value, contract))
20
+ const id = `jb-in-${field.name}`
21
+
22
+ // Saved text that is not a list (typed by hand before this control
23
+ // existed) is shown as it is rather than guessed at.
24
+ if (rows === null) {
25
+ return (
26
+ <div className="jb-field jb-field--wide">
27
+ <label htmlFor={id}>{field.label || field.name}</label>
28
+ <textarea id={id} rows={4} spellCheck="false" value={value} onChange={(e) => onChange(e.target.value)} />
29
+ <span className="jb-help jb-warn">This is not a list of parameters, so it is shown as it was saved.</span>
30
+ </div>
31
+ )
32
+ }
33
+
34
+ function update(next) {
35
+ setRows(next)
36
+ onChange(rowsToText(next))
37
+ }
38
+ const patchRow = (i, p) => update(rows.map((r, j) => (j === i ? { ...r, ...p } : r)))
39
+
40
+ return (
41
+ <div className="jb-field jb-field--wide">
42
+ <label>{field.label || field.name}</label>
43
+ {contract.length > 0 && (
44
+ <span className="jb-help">
45
+ Every run receives a window, from this job's schedule or from the job before it:{' '}
46
+ {contract.map((c) => c.label.toLowerCase()).join(' and ')}. Pick it as a parameter's value.
47
+ </span>
48
+ )}
49
+ <div className="jb-params">
50
+ {rows.map((r, i) => {
51
+ const source = sourceOf(r.value, contract)
52
+ return (
53
+ <div key={i} className="jb-param">
54
+ <input aria-label={`Parameter ${i + 1} name`} className="jb-param-name" value={r.name}
55
+ placeholder={source === FIXED ? 'name' : 'e.g. startDate'}
56
+ onChange={(e) => patchRow(i, { name: e.target.value })} />
57
+ <span className="jb-param-arrow" aria-hidden="true">←</span>
58
+ <select aria-label={`Parameter ${i + 1} value`} value={source}
59
+ onChange={(e) => patchRow(i, { value: valueForSource(e.target.value, r.value, contract) })}>
60
+ {contract.map((c) => <option key={c.key} value={c.key}>{c.label}</option>)}
61
+ <option value={FIXED}>Fixed value</option>
62
+ </select>
63
+ {source === FIXED ? (
64
+ <input aria-label={`Parameter ${i + 1} fixed value`} className="jb-param-value" value={r.value == null ? '' : String(r.value)}
65
+ placeholder="value" onChange={(e) => patchRow(i, { value: e.target.value })} />
66
+ ) : <span className="jb-param-gap" />}
67
+ <button type="button" className="jb-param-remove" title="Remove this parameter"
68
+ onClick={() => update(rows.filter((_, j) => j !== i))}>✕</button>
69
+ </div>
70
+ )
71
+ })}
72
+ </div>
73
+ <button type="button" className="jb-param-add" onClick={() => update([...rows, { name: '', value: '' }])}>+ Add parameter</button>
74
+ <span className="jb-help">
75
+ The name is the procedure's own parameter name. On MySQL, parameters go by position, so keep
76
+ them in the order the procedure declares them.
77
+ </span>
78
+ </div>
79
+ )
80
+ }
@@ -0,0 +1,55 @@
1
+ // A PROCEDURE'S PARAMETERS AS ROWS — the form's half, as pure logic.
2
+ //
3
+ // What a job saves is the list the procedure step takes, [{ name, value }]
4
+ // (or bare values, positional). A value is either fixed text or BOUND to the
5
+ // run's window: "{window.from}" / "{window.to}", which @xeplr/jobs fills in
6
+ // with this run's dates (its run contract, GET /jobs/run-contract). This file
7
+ // turns that list into rows a person edits — a name, and where its value
8
+ // comes from — and back.
9
+
10
+ export const FIXED = 'fixed'
11
+
12
+ export function bindingFor(key) {
13
+ return '{' + key + '}'
14
+ }
15
+
16
+ /** The contract key a value is bound to, or FIXED. */
17
+ export function sourceOf(value, contract) {
18
+ const hit = (contract || []).find((c) => value === bindingFor(c.key))
19
+ return hit ? hit.key : FIXED
20
+ }
21
+
22
+ /**
23
+ * Saved text → rows. A job with no parameters yet starts with one row per
24
+ * contract value (the window's start and end), named by the person — the
25
+ * period is what nearly every scheduled procedure takes.
26
+ */
27
+ export function rowsFromText(text, contract) {
28
+ let list = []
29
+ try { list = text ? JSON.parse(text) : [] } catch (e) { list = null }
30
+ if (!Array.isArray(list)) return null
31
+ if (!list.length) {
32
+ return (contract || []).map((c) => ({ name: '', value: bindingFor(c.key), starter: true }))
33
+ }
34
+ return list.map((p) => (p && typeof p === 'object' && !Array.isArray(p)
35
+ ? { name: p.name == null ? '' : String(p.name), value: p.value == null ? '' : p.value }
36
+ : { name: '', value: p == null ? '' : p, bare: true }))
37
+ }
38
+
39
+ /**
40
+ * Rows → the text the form saves. A starter row nobody named is left out, so
41
+ * a procedure with no date parameters gets none. A row with no name is kept
42
+ * as a bare (positional) value.
43
+ */
44
+ export function rowsToText(rows) {
45
+ const list = (rows || [])
46
+ .filter((r) => !(r.starter && !String(r.name || '').trim()))
47
+ .map((r) => (String(r.name || '').trim() ? { name: String(r.name).trim(), value: r.value } : r.value))
48
+ return list.length ? JSON.stringify(list, null, 2) : ''
49
+ }
50
+
51
+ /** A row's value when its source changes: a binding, or empty fixed text. */
52
+ export function valueForSource(source, current, contract) {
53
+ if (source === FIXED) return sourceOf(current, contract) === FIXED ? current : ''
54
+ return bindingFor(source)
55
+ }
@@ -12,6 +12,8 @@ import {
12
12
  } from './jobsList.js'
13
13
  import CronScheduleField from '../components/CronScheduleField.jsx'
14
14
  import JobChainSection from '../components/JobChainSection.jsx'
15
+ import ParametersField from '../components/ParametersField.jsx'
16
+ import { useRunContract } from '../useRunContract.js'
15
17
  import { useJobChainController } from '../useJobChainController.js'
16
18
  import { acceptableFields, toggleAccepted } from '../lib/jobChainModel.js'
17
19
  import './Jobs.css'
@@ -274,8 +276,14 @@ const AFTER_STEP = { key: 'after', label: 'After this job', hint: 'What runs whe
274
276
  * would be asking somebody to type a password into a form, which is exactly
275
277
  * what storing an id instead was for.
276
278
  */
277
- function visibleFields(action) {
278
- return (action?.inputSchema || []).filter((f) => !f.system)
279
+ function visibleFields(action, inputs) {
280
+ // `hidden` (an action's hint): left out unless this job already holds one,
281
+ // so nothing saved is ever invisible.
282
+ const held = (f) => {
283
+ const v = inputs?.[f.name]
284
+ return v !== undefined && v !== '' && v !== '{}' && v !== '[]'
285
+ }
286
+ return (action?.inputSchema || []).filter((f) => !f.system && (!f.hidden || held(f)))
279
287
  }
280
288
 
281
289
  // ── labels ────────────────────────────────────────────────────────────────
@@ -435,8 +443,13 @@ function RemoteSelect({ field, value, onChange, dependsOnValue }) {
435
443
  }
436
444
 
437
445
  /** One input, rendered as whatever its declared type actually is. */
438
- function ActionInput({ field, value, onChange, inputs }) {
446
+ function ActionInput({ field, value, onChange, inputs, contract }) {
439
447
  const id = `jb-in-${field.name}`
448
+
449
+ // Rows rather than JSON, for an input the action marks as parameters.
450
+ if (field.control === 'parameters') {
451
+ return <ParametersField field={field} value={value} onChange={onChange} contract={contract || []} />
452
+ }
440
453
  const common = { id, value, onChange: (e) => onChange(e.target.value) }
441
454
 
442
455
  // A host-provided list, when the host has one. Without a source configured
@@ -529,6 +542,7 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
529
542
  const [saving, setSaving] = useState(false)
530
543
  const [step, setStep] = useState(0)
531
544
  const chain = useJobChainController(job?.id || null)
545
+ const contract = useRunContract()
532
546
  const STEPS = chain.enabled ? [...BASE_STEPS, AFTER_STEP] : BASE_STEPS
533
547
 
534
548
  const action = actions.find((a) => a.name === draft.actionName) || null
@@ -604,7 +618,7 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
604
618
  }
605
619
  }
606
620
 
607
- const fields = visibleFields(action)
621
+ const fields = visibleFields(action, draft.inputs)
608
622
 
609
623
  return (
610
624
  <form className="jb-editor" onSubmit={submit}>
@@ -678,6 +692,7 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
678
692
  key={f.name}
679
693
  field={f}
680
694
  inputs={draft.inputs}
695
+ contract={contract}
681
696
  value={draft.inputs?.[f.name] ?? ''}
682
697
  onChange={(v) => patch({ inputs: { ...draft.inputs, [f.name]: v } })}
683
698
  />
@@ -0,0 +1,23 @@
1
+ import { useEffect, useState } from 'react'
2
+ import { getRunContract } from './api/jobs.js'
3
+
4
+ // Fetched once per page load: it is a fact about the server, not about a job.
5
+ let cached = null
6
+
7
+ /**
8
+ * The run contract (what every run receives), for the parameter dropdowns.
9
+ * Empty until it arrives, and empty when the server has none to give — the
10
+ * rows then offer only a fixed value.
11
+ */
12
+ export function useRunContract() {
13
+ const [contract, setContract] = useState(cached || [])
14
+ useEffect(() => {
15
+ if (cached) return
16
+ let live = true
17
+ getRunContract()
18
+ .then((c) => { cached = c; if (live) setContract(c) })
19
+ .catch(() => { if (live) setContract([]) })
20
+ return () => { live = false }
21
+ }, [])
22
+ return contract
23
+ }