@xeplr/ui-utils 1.1.2 → 1.1.4

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
@@ -500,6 +500,31 @@ Event contract: `{ key, continue: true | false, status?, data }`. `continue: fal
500
500
 
501
501
  With no source attached, every notifier renders `idle` and nothing else.
502
502
 
503
+ ## Option sources: `createOptionSources`
504
+
505
+ An action's input can name a KIND of thing to choose from (`optionsFrom: 'connections'`). The host says once where each kind lives, and a screen offers a list instead of a box to type an id into. Used by `@xeplr/ui-jobs`' job editor and X-Flow's step editor.
506
+
507
+ ```js
508
+ import { createOptionSources } from '@xeplr/ui-utils'
509
+ import { authFetch } from '@xeplr/ui-account'
510
+
511
+ const sources = createOptionSources({
512
+ connections: { url: '/connectioninfo', value: 'id', label: 'label' },
513
+ databases: { url: '/dbinfo', value: 'id', label: 'dbName' },
514
+ tables: { url: '/warehouse/tables', value: 'id', label: 'name', key: 'tables' }
515
+ }, (path) => authFetch(path))
516
+
517
+ sources.has('connections') // true: show a list
518
+ await sources.fetchOptions('connections') // [{ value, label }]
519
+ await sources.fetchOptions('databases', { name: 'connectionId', value: 'c1', clientKey: 'connectionId' })
520
+ await sources.fetchSuggestions('procedureParams', { connectionId, procedure }) // ['from', 'to']
521
+ ```
522
+
523
+ - **A kind nobody configured** is `has() === false`: show a text box. An empty dropdown looks broken.
524
+ - **`where`** keeps only rows with those values, e.g. `{ connectionType: 'api' }`: sent to the server as query values and checked again in the browser.
525
+ - **`key`** names the list in a hand-written endpoint's answer (default `dataArray`). Guessing it wrong would show an empty list and no error.
526
+ - **Pure, no React, no login of its own:** the host passes its own fetch.
527
+
503
528
  ## raiseSnackbar, raiseConfirm, raiseChoice
504
529
 
505
530
  ```js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-utils",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "UI utility components: file upload with progress, snackbar, static data (countries/states)",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.js CHANGED
@@ -237,3 +237,7 @@ export { useSettingsTreeController } from './panel/useSettingsTreeController.js'
237
237
  export { EXPANDERS_RULES, SETTINGS_TREE_RULES } from './panel/validateDesign.js';
238
238
  export { PanelSection, PanelField, ExpandersSample, SettingsTreeSample } from './panel/designs/index.js';
239
239
  export { ExpandersPage, SettingsTreePage } from './panel/pages.jsx';
240
+
241
+ // Option sources — where a KIND of thing an input names (`optionsFrom`) is
242
+ // listed. Shared by @xeplr/ui-jobs and X-Flow's step editor.
243
+ export { createOptionSources } from './optionSources/optionSources.js';
@@ -0,0 +1,77 @@
1
+ // WHERE A KIND OF THING IS LISTED, for an input that says `optionsFrom`.
2
+ //
3
+ // An action's input can name a KIND of thing to choose from
4
+ // (`optionsFrom: 'connections'`), and the host says once where that kind
5
+ // lives: { connections: { url: '/connectioninfo', value: 'id', label: 'label' } }.
6
+ // A screen then offers a list to pick from instead of a box to type a
7
+ // 25-character id into. This module never learns what a connection is, nor
8
+ // which URL serves one: it knows only "fetch this list, show labels, store ids".
9
+ //
10
+ // One copy, used by @xeplr/ui-jobs' job editor and X-Flow's step editor.
11
+ //
12
+ // Nothing configured for a kind → `has(kind)` is false, and the screen shows a
13
+ // plain text box: an empty dropdown looks broken, a text box is simply what
14
+ // you have when there is no catalogue to pick from.
15
+ //
16
+ // Pure: no React. `fetchJson(path)` is the host's own authenticated fetch
17
+ // (@xeplr/ui-account's authFetch), so this package never learns about logins.
18
+
19
+ /**
20
+ * @param {object} sources { kind: { url, value?='id', label?, key?='dataArray', where? } }
21
+ * @param {Function} fetchJson (path) → Promise of the parsed answer
22
+ */
23
+ // `where`: only rows with these values, e.g. { connectionType: 'api' } so a
24
+ // step that calls an API lists API connections and not database ones. Sent as
25
+ // query values, and checked again here for an endpoint that ignores them.
26
+ function whereQuery(src) {
27
+ return Object.entries(src.where || {}).map(([k, v]) => `&${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('')
28
+ }
29
+ function matchesWhere(src, row) {
30
+ return Object.entries(src.where || {}).every(([k, v]) => row[k] === v)
31
+ }
32
+
33
+ export function createOptionSources(sources, fetchJson) {
34
+ const all = sources || {}
35
+
36
+ function has(kind) {
37
+ return Boolean(kind && all[kind])
38
+ }
39
+
40
+ /**
41
+ * One list, as [{ value, label }]. `filter` narrows a dependent list — the
42
+ * databases on the chosen connection — as a query parameter, and, with
43
+ * `clientKey`, again in the browser for an endpoint that ignores it.
44
+ */
45
+ async function fetchOptions(kind, filter) {
46
+ const src = all[kind]
47
+ if (!src) return []
48
+ const q = (filter && filter.value ? `&${encodeURIComponent(filter.name)}=${encodeURIComponent(filter.value)}` : '') + whereQuery(src)
49
+ const res = await fetchJson(`${src.url}?limit=0${q}`)
50
+ // `dataArray` is what a generic route answers; a hand-written endpoint
51
+ // answers with its own key, which the host names. Guessing wrong would
52
+ // give an empty list and no error, which reads as "you have none".
53
+ const rows = (res && res[src.key || 'dataArray']) || []
54
+ return rows
55
+ .filter((r) => matchesWhere(src, r))
56
+ .filter((r) => (filter && filter.clientKey ? r[filter.clientKey] === filter.value : true))
57
+ .map((r) => ({ value: r[src.value || 'id'], label: r[src.label] || r[src.value || 'id'] }))
58
+ }
59
+
60
+ /**
61
+ * Suggestions asked with several values at once (`query` is { param: value }),
62
+ * as a list of strings. Empty when the source is not configured or something
63
+ * it needs is not chosen yet.
64
+ */
65
+ async function fetchSuggestions(kind, query) {
66
+ const src = all[kind]
67
+ if (!src) return []
68
+ const pairs = Object.entries(query || {})
69
+ if (pairs.some(([, v]) => v === undefined || v === null || v === '')) return []
70
+ const q = pairs.map(([k, v]) => `&${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('')
71
+ const res = await fetchJson(`${src.url}?limit=0${q}`)
72
+ const list = (res && res[src.key || 'dataArray']) || []
73
+ return list.map((o) => String(o[src.value || 'value'])).filter(Boolean)
74
+ }
75
+
76
+ return { has, fetchOptions, fetchSuggestions }
77
+ }