@xeplr/ui-jobs 1.4.0 → 1.6.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
@@ -114,6 +114,8 @@ Every call goes through `authFetch`, which prefixes the base URL `@xeplr/ui-acco
114
114
 
115
115
  ## The screen
116
116
 
117
+ **Where you are is in the URL** (`useJobsUrl`), so a refresh or a shared link lands on the same screen: `?edit=<jobId>` or `?edit=new` opens the editor, `&step=inputs|schedule|after` its step, `?runs=<jobId>` a job's run history, `&log=<occurrenceId>` one run's log, `?q=` and `?sort=` the search and sort. Opening and closing add a history entry (Back closes them); steps and typing replace it. An `edit` id that is not there says so.
118
+
117
119
  - **Jobs table** — name, state (Running, Paused, Manual only, Scheduled), schedule in words (`describeCron`, else "Custom"), next run ("in 3 hours", "due now"), last run, an **on / paused switch**, and **Run now**, **Edit**, **Delete** (confirmed; run history is kept). Search matches name, description and action; sort by name, next run or action.
118
120
  - **Run history** — the last-run cell opens that job's newest 50 runs, each with its log.
119
121
  - **Recent runs** — the 12 latest runs across all jobs, with duration, trigger and error message.
@@ -135,7 +137,7 @@ Three steps (four with `chainsApiBase`), all reachable at any time, with **Save
135
137
 
136
138
  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).
137
139
 
138
- 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`), grouped: **This run** (start / end of the window) and **The job before** (the range it was given and the range it actually used, start / end). A job with no parameters starts with none (**+ Add parameter**). When the action names a `suggestFrom` source (`db-procedure`: `procedureParams`, asked with the connection, database and procedure), clicking a name box offers the procedure's own parameter names, minus those already used. A row with neither name nor value is not saved. An input marked `hidden` (the procedure's old **Window**) is shown only when a job already holds one.
140
+ 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`), grouped: **This run** (start / end of the window) and **The job before** (the range it was given and the range it actually used, start / end). A job with no parameters starts with none (**+ Add parameter**). When the action names a `suggestFrom` source (`db-procedure`: `procedureParams`, asked with the connection, database and procedure), clicking a name box opens a list of the procedure's own parameter names (`NameBox`), minus those already used and narrowed by what is typed; when it cannot have them yet it says which of connection, database and procedure is still to be chosen. A row with neither name nor value is not saved. An input marked `hidden` (the procedure's old **Window**) is shown only when a job already holds one.
139
141
 
140
142
  A job's saved `acceptsInputs` (what the job before may fill) is kept as it is but no longer edited here: a job takes what it needs from the job before through its parameters. The **After this job** rules offer a condition, the job to run and values to **carry** for later jobs.
141
143
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-jobs",
3
- "version": "1.4.0",
3
+ "version": "1.6.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",
@@ -18,3 +18,18 @@
18
18
  .jb-param { grid-template-columns: 1fr auto; }
19
19
  .jb-param-arrow { display: none; }
20
20
  }
21
+ /* The name box and its list, which opens on click. */
22
+ .jb-namebox { position: relative; min-width: 0; }
23
+ .jb-namebox-list {
24
+ position: absolute; z-index: 20; top: calc(100% + 4px); left: 0; right: 0; margin: 0; padding: 4px;
25
+ list-style: none; max-height: 220px; overflow: auto;
26
+ background: var(--surface, #ffffff); border: 1px solid var(--hair, #e2e1d9); border-radius: 8px;
27
+ box-shadow: 0 6px 18px rgba(0, 0, 0, .12);
28
+ }
29
+ .jb-namebox-list li {
30
+ padding: 6px 8px; border-radius: 6px; cursor: pointer;
31
+ font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; color: var(--ink, #1a1a19);
32
+ }
33
+ .jb-namebox-list li:hover, .jb-namebox-list li[aria-selected="true"] { background: var(--xeplr-hover, #f0efe9); }
34
+ .jb-namebox-list .jb-namebox-note { cursor: default; font-family: inherit; font-size: 12px; color: var(--muted, #6b6a63); }
35
+ .jb-namebox-list .jb-namebox-note:hover { background: none; }
@@ -18,6 +18,40 @@ import './ParametersField.css'
18
18
  * @param {object} props.inputs the job's other inputs — what the name
19
19
  * suggestions are asked with (field.suggestWith)
20
20
  */
21
+ // What each input the suggestions depend on is called, for "choose the … above".
22
+ const WHAT = { connectionInfoId: 'connection', dbInfoId: 'database', procedure: 'procedure' }
23
+
24
+ /**
25
+ * A name box whose list OPENS ON CLICK — the procedure's parameter names,
26
+ * narrowed by what is typed — and says why when it has none. A plain
27
+ * <datalist> shows nothing until something is typed in several browsers.
28
+ */
29
+ function NameBox({ label, value, names, emptyNote, onChange }) {
30
+ const [open, setOpen] = useState(false)
31
+ const typed = String(value || '').trim().toLowerCase()
32
+ const shown = names.filter((n) => !typed || n.toLowerCase().includes(typed))
33
+ return (
34
+ <div className="jb-namebox">
35
+ <input aria-label={label} className="jb-param-name" value={value} placeholder="name" autoComplete="off"
36
+ role="combobox" aria-expanded={open}
37
+ onFocus={() => setOpen(true)} onClick={() => setOpen(true)}
38
+ onBlur={() => setOpen(false)}
39
+ onKeyDown={(e) => { if (e.key === 'Escape') setOpen(false) }}
40
+ onChange={(e) => { onChange(e.target.value); setOpen(true) }} />
41
+ {open && (
42
+ <ul className="jb-namebox-list" role="listbox">
43
+ {shown.length ? shown.map((n) => (
44
+ <li key={n} role="option" aria-selected={n === value}
45
+ onMouseDown={(e) => { e.preventDefault(); onChange(n); setOpen(false) }}>{n}</li>
46
+ )) : (
47
+ <li className="jb-namebox-note">{names.length ? 'No parameter matches what is typed.' : (emptyNote || 'Type the name.')}</li>
48
+ )}
49
+ </ul>
50
+ )}
51
+ </div>
52
+ )
53
+ }
54
+
21
55
  export default function ParametersField({ field, value, onChange, contract, inputs }) {
22
56
  const [rows, setRows] = useState(() => rowsFromText(value, contract))
23
57
  const id = `jb-in-${field.name}`
@@ -26,15 +60,27 @@ export default function ParametersField({ field, value, onChange, contract, inpu
26
60
  // asked again whenever the connection, database or procedure changes.
27
61
  const query = Object.fromEntries(Object.entries(field.suggestWith || {}).map(([k, from]) => [k, inputs?.[from]]))
28
62
  const queryKey = JSON.stringify(query)
63
+ // What the name list says when it has nothing: which choice above is still
64
+ // missing, that it is looking, or that the procedure has none.
65
+ const missing = Object.entries(field.suggestWith || {}).filter(([k]) => !query[k]).map(([, from]) => WHAT[from] || from)
29
66
  const [names, setNames] = useState([])
67
+ const [state, setState] = useState('idle')
30
68
  useEffect(() => {
31
69
  if (!field.suggestFrom) return undefined
70
+ const q = JSON.parse(queryKey)
71
+ if (Object.values(q).some((v) => v === undefined || v === null || v === '')) { setNames([]); setState('idle'); return undefined }
32
72
  let live = true
33
- fetchSuggestions(field.suggestFrom, JSON.parse(queryKey))
34
- .then((n) => { if (live) setNames(n) })
35
- .catch(() => { if (live) setNames([]) })
73
+ setState('loading')
74
+ fetchSuggestions(field.suggestFrom, q)
75
+ .then((n) => { if (live) { setNames(n); setState('ready') } })
76
+ .catch(() => { if (live) { setNames([]); setState('failed') } })
36
77
  return () => { live = false }
37
78
  }, [field.suggestFrom, queryKey])
79
+ const emptyNote = !field.suggestFrom ? null
80
+ : missing.length ? `Choose the ${missing.join(', ')} above to see this procedure's parameters.`
81
+ : state === 'loading' ? 'Reading the procedure\'s parameters…'
82
+ : state === 'failed' ? 'Could not read the procedure\'s parameters. Type the name.'
83
+ : 'This procedure has no parameters to fill, or it was not found. Type the name.'
38
84
 
39
85
  // Saved text that is not a list (typed by hand before this control
40
86
  // existed) is shown as it is rather than guessed at.
@@ -69,12 +115,9 @@ export default function ParametersField({ field, value, onChange, contract, inpu
69
115
  const source = sourceOf(r.value, contract)
70
116
  return (
71
117
  <div key={i} className="jb-param">
72
- <input aria-label={`Parameter ${i + 1} name`} className="jb-param-name" value={r.name}
73
- list={`${id}-names-${i}`} placeholder="name"
74
- onChange={(e) => patchRow(i, { name: e.target.value })} />
75
- <datalist id={`${id}-names-${i}`}>
76
- {unusedNames(names, rows, i).map((n) => <option key={n} value={n} />)}
77
- </datalist>
118
+ <NameBox label={`Parameter ${i + 1} name`} value={r.name}
119
+ names={unusedNames(names, rows, i)} emptyNote={emptyNote}
120
+ onChange={(name) => patchRow(i, { name })} />
78
121
  <span className="jb-param-arrow" aria-hidden="true">←</span>
79
122
  <select aria-label={`Parameter ${i + 1} value`} value={source}
80
123
  onChange={(e) => patchRow(i, { value: valueForSource(e.target.value, r.value, contract) })}>
@@ -97,9 +140,7 @@ export default function ParametersField({ field, value, onChange, contract, inpu
97
140
  </div>
98
141
  <button type="button" className="jb-param-add" onClick={() => update([...rows, { name: '', value: '' }])}>+ Add parameter</button>
99
142
  <span className="jb-help">
100
- {names.length
101
- ? `Click a name box to pick one of the procedure's parameters (${names.join(', ')}), or type your own.`
102
- : 'The name is the procedure\'s own parameter name.'}
143
+ Click a name box to pick one of the procedure's parameters, or type your own.
103
144
  {' '}On MySQL, parameters go by position, so keep them in the order the procedure declares them.
104
145
  </span>
105
146
  </div>
@@ -411,3 +411,4 @@
411
411
  mark — a rotated '>' glyph never sat straight, whatever the box did. */
412
412
  .jb-caret { padding: 0; }
413
413
  .jb-caret .jb-icon { transition: none; }
414
+ .jb-linkish { padding: 0; font: inherit; color: var(--accent, #2563eb); background: none; border: 0; cursor: pointer; text-decoration: underline; }
@@ -14,6 +14,7 @@ import CronScheduleField from '../components/CronScheduleField.jsx'
14
14
  import JobChainSection from '../components/JobChainSection.jsx'
15
15
  import ParametersField from '../components/ParametersField.jsx'
16
16
  import { useRunContract } from '../useRunContract.js'
17
+ import { useJobsUrl } from '../useJobsUrl.js'
17
18
  import { useJobChainController } from '../useJobChainController.js'
18
19
  import { acceptableFields } from '../lib/jobChainModel.js'
19
20
  import './Jobs.css'
@@ -147,12 +148,12 @@ function OccurrenceLogs({ occurrenceId }) {
147
148
  * Re-fetched on `refreshKey` so that pressing Run now updates the open history
148
149
  * instead of leaving it showing the state from before the click.
149
150
  */
150
- function JobRuns({ jobId, refreshKey }) {
151
+ function JobRuns({ jobId, refreshKey, openLogId, onOpenLog }) {
151
152
  const [runs, setRuns] = useState(null)
152
153
  const [error, setError] = useState(null)
153
- // Which occurrence has its log open. One at a time: two open logs is two
154
- // scrolling regions competing inside a row that is already an expansion.
155
- const [openLogId, setOpenLogId] = useState(null)
154
+ // Which occurrence has its log open — in the URL (?log=), so a refresh keeps
155
+ // it open. One at a time: two open logs is two scrolling regions competing
156
+ // inside a row that is already an expansion.
156
157
 
157
158
  useEffect(() => {
158
159
  let alive = true
@@ -227,7 +228,7 @@ function JobRuns({ jobId, refreshKey }) {
227
228
  aria-expanded={openLogId === run.id}
228
229
  title={openLogId === run.id ? 'Hide this run’s log' : 'Show this run’s log'}
229
230
  aria-label={openLogId === run.id ? 'Hide this run’s log' : 'Show this run’s log'}
230
- onClick={() => setOpenLogId((id) => (id === run.id ? null : run.id))}>
231
+ onClick={() => onOpenLog(openLogId === run.id ? null : run.id)}>
231
232
  <Icon path={ICON_CHEVRON} size={14} />
232
233
  </button>
233
234
  </td>
@@ -508,7 +509,7 @@ function ActionInput({ field, value, onChange, inputs, contract }) {
508
509
 
509
510
  // The editor. A panel rather than a route: a job is a handful of fields, and a
510
511
  // page you navigate to and back from for those is three navigations too many.
511
- function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
512
+ function JobEditor({ job, jobs, actions, onCancel, onSaved, stepKey, onStep }) {
512
513
  const [draft, setDraft] = useState(() => ({
513
514
  name: job?.name || '',
514
515
  description: job?.description || '',
@@ -541,10 +542,12 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
541
542
  acceptsInputs: Array.isArray(job?.acceptsInputs) ? job.acceptsInputs : []
542
543
  }))
543
544
  const [saving, setSaving] = useState(false)
544
- const [step, setStep] = useState(0)
545
545
  const chain = useJobChainController(job?.id || null)
546
546
  const contract = useRunContract()
547
547
  const STEPS = chain.enabled ? [...BASE_STEPS, AFTER_STEP] : BASE_STEPS
548
+ // THE STEP IS IN THE URL (?step=), so a refresh reopens the same one.
549
+ const step = Math.max(0, STEPS.findIndex((s) => s.key === stepKey))
550
+ const setStep = (i) => onStep(STEPS[Math.max(0, Math.min(STEPS.length - 1, i))].key)
548
551
 
549
552
  const action = actions.find((a) => a.name === draft.actionName) || null
550
553
  const patch = (p) => setDraft((d) => ({ ...d, ...p }))
@@ -877,11 +880,21 @@ export default function Jobs({ breadcrumb: Breadcrumb, uploadLink } = {}) {
877
880
  const [runs, setRuns] = useState([])
878
881
  const [actions, setActions] = useState([])
879
882
  const [error, setError] = useState(null)
880
- const [query, setQuery] = useState('')
881
- const [sort, setSort] = useState('name')
882
- const [editing, setEditing] = useState(null) // job object, {} for new, null for closed
883
+ // EVERYTHING THAT SAYS WHERE YOU ARE comes from the URL — see useJobsUrl.
884
+ const url = useJobsUrl()
885
+ const query = url.q
886
+ const sort = url.sort
887
+ const setQuery = (q) => url.set({ q: q }, { replace: true })
888
+ const setSort = (v) => url.set({ sort: v === 'name' ? null : v }, { replace: true })
889
+ const openRuns = url.runs
890
+ const setOpenRuns = (id) => url.set({ runs: id, log: null })
891
+ // ?edit=new is a new job; ?edit=<id> is that job once the list has loaded.
892
+ const editing = url.edit === 'new' ? {}
893
+ : url.edit && jobs ? (jobs.find((j) => j.id === url.edit) || null)
894
+ : null
895
+ const editMissing = Boolean(url.edit && url.edit !== 'new' && jobs && !editing)
896
+ const setEditing = (job) => url.set({ edit: job ? (job.id || 'new') : null, step: null })
883
897
  const [busyId, setBusyId] = useState(null)
884
- const [openRuns, setOpenRuns] = useState(null) // jobId whose history is expanded
885
898
  // Bumped by anything that creates an occurrence, so an OPEN history refetches
886
899
  // rather than sitting there showing the state from before Run now was pressed.
887
900
  const [runsVersion, setRunsVersion] = useState(0)
@@ -1050,11 +1063,21 @@ export default function Jobs({ breadcrumb: Breadcrumb, uploadLink } = {}) {
1050
1063
  {error && <p className="report-builder-error">{error}</p>}
1051
1064
  {jobs === null && !error && <p className="muted">Loading…</p>}
1052
1065
 
1066
+ {editMissing && (
1067
+ <p className="report-builder-error">
1068
+ There is no job {url.edit} here — it may have been deleted.{' '}
1069
+ <button type="button" className="jb-linkish" onClick={() => setEditing(null)}>Back to the list</button>
1070
+ </p>
1071
+ )}
1072
+
1053
1073
  {editing && (
1054
1074
  <JobEditor
1075
+ key={url.edit}
1055
1076
  job={editing.id ? editing : null}
1056
1077
  jobs={jobs || []}
1057
1078
  actions={actions}
1079
+ stepKey={url.step}
1080
+ onStep={(key) => url.set({ step: key === 'job' ? null : key }, { replace: true })}
1058
1081
  onCancel={() => setEditing(null)}
1059
1082
  onSaved={() => { setEditing(null); load() }}
1060
1083
  />
@@ -1229,7 +1252,7 @@ export default function Jobs({ breadcrumb: Breadcrumb, uploadLink } = {}) {
1229
1252
  {open && (
1230
1253
  <tr className="jb-runs-row">
1231
1254
  <td colSpan={7}>
1232
- <JobRuns jobId={job.id} refreshKey={runsVersion} />
1255
+ <JobRuns jobId={job.id} refreshKey={runsVersion} openLogId={url.log} onOpenLog={(id) => url.set({ log: id })} />
1233
1256
  </td>
1234
1257
  </tr>
1235
1258
  )}
@@ -0,0 +1,43 @@
1
+ import { useCallback } from 'react'
2
+ import { useSearchParams } from 'react-router-dom'
3
+
4
+ // WHERE YOU ARE ON THE JOBS SCREEN LIVES IN THE URL, so a refresh or a shared
5
+ // link lands on exactly this screen:
6
+ //
7
+ // ?edit=<jobId> | ?edit=new the editor, open on that job (or a new one)
8
+ // &step=job|inputs|schedule|after its step
9
+ // ?runs=<jobId> that job's run history, expanded
10
+ // &log=<occurrenceId> one run's log, open
11
+ // ?q=…&sort=… the search and the sort
12
+ //
13
+ // Opening and closing things adds a history entry, so Back closes them;
14
+ // typing and switching steps replaces it, so Back is not a keystroke undo.
15
+ const KEYS = ['edit', 'step', 'runs', 'log', 'q', 'sort']
16
+
17
+ export function useJobsUrl() {
18
+ const [params, setParams] = useSearchParams()
19
+ const read = (k) => params.get(k) || null
20
+
21
+ const set = useCallback((patch, opts) => {
22
+ setParams((prev) => {
23
+ const next = new URLSearchParams(prev)
24
+ Object.keys(patch).forEach((k) => {
25
+ const v = patch[k]
26
+ if (v === null || v === undefined || v === '') next.delete(k)
27
+ else next.set(k, String(v))
28
+ })
29
+ return next
30
+ }, { replace: Boolean(opts && opts.replace) })
31
+ }, [setParams])
32
+
33
+ return {
34
+ edit: read('edit'),
35
+ step: read('step'),
36
+ runs: read('runs'),
37
+ log: read('log'),
38
+ q: read('q') || '',
39
+ sort: read('sort') || 'name',
40
+ set: set,
41
+ KEYS: KEYS
42
+ }
43
+ }