@xeplr/ui-utils 1.1.2 → 1.1.3
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 +24 -0
- package/package.json +1 -1
- package/src/index.js +4 -0
- package/src/optionSources/optionSources.js +66 -0
package/README.md
CHANGED
|
@@ -500,6 +500,30 @@ 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
|
+
- **`key`** names the list in a hand-written endpoint's answer (default `dataArray`). Guessing it wrong would show an empty list and no error.
|
|
525
|
+
- **Pure, no React, no login of its own:** the host passes its own fetch.
|
|
526
|
+
|
|
503
527
|
## raiseSnackbar, raiseConfirm, raiseChoice
|
|
504
528
|
|
|
505
529
|
```js
|
package/package.json
CHANGED
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,66 @@
|
|
|
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' } }
|
|
21
|
+
* @param {Function} fetchJson (path) → Promise of the parsed answer
|
|
22
|
+
*/
|
|
23
|
+
export function createOptionSources(sources, fetchJson) {
|
|
24
|
+
const all = sources || {}
|
|
25
|
+
|
|
26
|
+
function has(kind) {
|
|
27
|
+
return Boolean(kind && all[kind])
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* One list, as [{ value, label }]. `filter` narrows a dependent list — the
|
|
32
|
+
* databases on the chosen connection — as a query parameter, and, with
|
|
33
|
+
* `clientKey`, again in the browser for an endpoint that ignores it.
|
|
34
|
+
*/
|
|
35
|
+
async function fetchOptions(kind, filter) {
|
|
36
|
+
const src = all[kind]
|
|
37
|
+
if (!src) return []
|
|
38
|
+
const q = filter && filter.value ? `&${encodeURIComponent(filter.name)}=${encodeURIComponent(filter.value)}` : ''
|
|
39
|
+
const res = await fetchJson(`${src.url}?limit=0${q}`)
|
|
40
|
+
// `dataArray` is what a generic route answers; a hand-written endpoint
|
|
41
|
+
// answers with its own key, which the host names. Guessing wrong would
|
|
42
|
+
// give an empty list and no error, which reads as "you have none".
|
|
43
|
+
const rows = (res && res[src.key || 'dataArray']) || []
|
|
44
|
+
return rows
|
|
45
|
+
.filter((r) => (filter && filter.clientKey ? r[filter.clientKey] === filter.value : true))
|
|
46
|
+
.map((r) => ({ value: r[src.value || 'id'], label: r[src.label] || r[src.value || 'id'] }))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Suggestions asked with several values at once (`query` is { param: value }),
|
|
51
|
+
* as a list of strings. Empty when the source is not configured or something
|
|
52
|
+
* it needs is not chosen yet.
|
|
53
|
+
*/
|
|
54
|
+
async function fetchSuggestions(kind, query) {
|
|
55
|
+
const src = all[kind]
|
|
56
|
+
if (!src) return []
|
|
57
|
+
const pairs = Object.entries(query || {})
|
|
58
|
+
if (pairs.some(([, v]) => v === undefined || v === null || v === '')) return []
|
|
59
|
+
const q = pairs.map(([k, v]) => `&${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('')
|
|
60
|
+
const res = await fetchJson(`${src.url}?limit=0${q}`)
|
|
61
|
+
const list = (res && res[src.key || 'dataArray']) || []
|
|
62
|
+
return list.map((o) => String(o[src.value || 'value'])).filter(Boolean)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return { has, fetchOptions, fetchSuggestions }
|
|
66
|
+
}
|