@xeplr/ui-jobs 1.6.6 → 1.6.7

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
@@ -151,6 +151,18 @@ A job's saved `acceptsInputs` (what the job before may fill) is kept as it is bu
151
151
 
152
152
  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.
153
153
 
154
+ ## Opening a new job already filled in
155
+
156
+ A host can link straight to a new job with its action and inputs chosen, e.g. BI's "+ New job" on a movement:
157
+
158
+ ```js
159
+ import { newJobQuery } from '@xeplr/ui-jobs'
160
+ navigate(jobsPath + newJobQuery({ action: 'move-run', inputs: { moveId }, name: 'Move: Orders' }))
161
+ // → ?edit=new&action=move-run&inputs={"moveId":"…"}&name=Move%3A%20Orders
162
+ ```
163
+
164
+ The link is not trusted: an action the server does not offer, or inputs that are not a JSON object, start a blank job instead (`src/lib/newJobModel.js`).
165
+
154
166
  ## Option sources
155
167
 
156
168
  An input field can ask for a choice from a kind of thing — `optionsFrom: 'connections'`. This package never learns what a connection is; your app says where each kind is listed:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-jobs",
3
- "version": "1.6.6",
3
+ "version": "1.6.7",
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/index.jsx CHANGED
@@ -103,3 +103,4 @@ export { ROUTES as jobsRoutes } from './routes.js';
103
103
  // and a copy of it in the host is a second implementation free to drift.
104
104
  export { default as CronScheduleField, CRON_PRESETS } from './components/CronScheduleField.jsx';
105
105
  export { describeCron } from './pages/jobsList.js';
106
+ export { newJobQuery } from './lib/newJobModel.js';
@@ -0,0 +1,41 @@
1
+ // A NEW JOB, STARTED FROM SOMEWHERE ELSE — the host opens the editor already
2
+ // filled in, so "+ New job" on a movement does not make somebody pick the
3
+ // action and the movement again:
4
+ //
5
+ // ?edit=new&action=move-run&inputs={"moveId":"abc"}&name=Nightly%20orders
6
+ //
7
+ // What arrives in a URL is not trusted: the action must be one this server
8
+ // offers, and inputs must be a JSON object. Anything else starts a blank job.
9
+ //
10
+ // Pure: no React.
11
+
12
+ /** The new job's starting values from the URL's action / inputs / name. */
13
+ export function newJobFrom(params, actions) {
14
+ const get = (k) => (params && typeof params.get === 'function' ? params.get(k) : params && params[k]) || null
15
+ const action = get('action')
16
+ const known = action && (actions || []).some((a) => a && a.name === action)
17
+ const out = {}
18
+ if (known) out.actionName = action
19
+ const text = get('inputs')
20
+ if (known && text) {
21
+ try {
22
+ const parsed = JSON.parse(text)
23
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) out.inputs = parsed
24
+ } catch (e) { /* not JSON: no inputs */ }
25
+ }
26
+ const name = get('name')
27
+ if (name) out.name = String(name).slice(0, 200)
28
+ return out
29
+ }
30
+
31
+ /** The URL keys this reads, cleared when the editor closes. */
32
+ export const NEW_JOB_KEYS = ['action', 'inputs', 'name']
33
+
34
+ /** The link a host builds: the jobs page's path plus these. */
35
+ export function newJobQuery({ action, inputs, name }) {
36
+ const q = new URLSearchParams({ edit: 'new' })
37
+ if (action) q.set('action', action)
38
+ if (inputs) q.set('inputs', JSON.stringify(inputs))
39
+ if (name) q.set('name', name)
40
+ return '?' + q.toString()
41
+ }
@@ -18,6 +18,7 @@ import { useJobsUrl } from '../useJobsUrl.js'
18
18
  import { useJobChainController } from '../useJobChainController.js'
19
19
  import { acceptableFields } from '../lib/jobChainModel.js'
20
20
  import { incrementalChoices, incrementalDraft, incrementalBody } from '../lib/incrementalModel.js'
21
+ import { newJobFrom, NEW_JOB_KEYS } from '../lib/newJobModel.js'
21
22
  import './Jobs.css'
22
23
 
23
24
  // SCHEDULED JOBS — @xeplr/jobs, in this product's chrome.
@@ -907,11 +908,14 @@ export default function Jobs({ breadcrumb: Breadcrumb, uploadLink } = {}) {
907
908
  const openRuns = url.runs
908
909
  const setOpenRuns = (id) => url.set({ runs: id, log: null })
909
910
  // ?edit=new is a new job; ?edit=<id> is that job once the list has loaded.
910
- const editing = url.edit === 'new' ? {}
911
+ // A new job may arrive filled in from a link (newJobFrom), once the actions
912
+ // it is checked against have loaded.
913
+ const editing = url.edit === 'new' ? newJobFrom(url.params, actions)
911
914
  : url.edit && jobs ? (jobs.find((j) => j.id === url.edit) || null)
912
915
  : null
913
916
  const editMissing = Boolean(url.edit && url.edit !== 'new' && jobs && !editing)
914
- const setEditing = (job) => url.set({ edit: job ? (job.id || 'new') : null, step: null })
917
+ const setEditing = (job) => url.set(Object.assign({ edit: job ? (job.id || 'new') : null, step: null },
918
+ Object.fromEntries(NEW_JOB_KEYS.map((k) => [k, null]))))
915
919
  const [busyId, setBusyId] = useState(null)
916
920
  // Bumped by anything that creates an occurrence, so an OPEN history refetches
917
921
  // rather than sitting there showing the state from before Run now was pressed.
@@ -1090,7 +1094,9 @@ export default function Jobs({ breadcrumb: Breadcrumb, uploadLink } = {}) {
1090
1094
 
1091
1095
  {editing && (
1092
1096
  <JobEditor
1093
- key={url.edit}
1097
+ // A new job filled in from a link is checked against the actions,
1098
+ // so it starts again once they have loaded.
1099
+ key={url.edit + (url.edit === 'new' && actions.length ? ':ready' : '')}
1094
1100
  job={editing.id ? editing : null}
1095
1101
  jobs={jobs || []}
1096
1102
  actions={actions}
package/src/useJobsUrl.js CHANGED
@@ -5,6 +5,7 @@ import { useSearchParams } from 'react-router-dom'
5
5
  // link lands on exactly this screen:
6
6
  //
7
7
  // ?edit=<jobId> | ?edit=new the editor, open on that job (or a new one)
8
+ // &action=…&inputs=…&name=… a new job already filled in (lib/newJobModel.js)
8
9
  // &step=job|inputs|schedule|after its step
9
10
  // ?runs=<jobId> that job's run history, expanded
10
11
  // &log=<occurrenceId> one run's log, open
@@ -12,7 +13,7 @@ import { useSearchParams } from 'react-router-dom'
12
13
  //
13
14
  // Opening and closing things adds a history entry, so Back closes them;
14
15
  // typing and switching steps replaces it, so Back is not a keystroke undo.
15
- const KEYS = ['edit', 'step', 'runs', 'log', 'q', 'sort']
16
+ const KEYS = ['edit', 'step', 'runs', 'log', 'q', 'sort', 'action', 'inputs', 'name']
16
17
 
17
18
  export function useJobsUrl() {
18
19
  const [params, setParams] = useSearchParams()
@@ -37,6 +38,8 @@ export function useJobsUrl() {
37
38
  log: read('log'),
38
39
  q: read('q') || '',
39
40
  sort: read('sort') || 'name',
41
+ // The raw params, for a new job filled in from the link (newJobFrom).
42
+ params: params,
40
43
  set: set,
41
44
  KEYS: KEYS
42
45
  }