@crombolo/backoffice-host 0.43.1 → 0.44.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/backoffice-host",
3
- "version": "0.43.1",
3
+ "version": "0.44.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Serves the Umbraco backoffice SPA: shell HTML, import map and package manifests.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -32,7 +32,7 @@
32
32
  "access": "public"
33
33
  },
34
34
  "dependencies": {
35
- "@crombolo/core": "0.43.1",
36
- "@crombolo/backoffice-dist": "0.43.1"
35
+ "@crombolo/core": "0.44.0",
36
+ "@crombolo/backoffice-dist": "0.44.0"
37
37
  }
38
38
  }
@@ -2,7 +2,7 @@
2
2
  "$schema": "../../../backoffice-dist/dist/umbraco-package-schema.json",
3
3
  "name": "Crombolo App Preview",
4
4
  "id": "Crombolo.AppPreview",
5
- "version": "0.43.1",
5
+ "version": "0.44.0",
6
6
  "allowPublicAccess": false,
7
7
  "extensions": [
8
8
  {
@@ -61,6 +61,8 @@
61
61
  --crombolo-header-line: #3b2e25;
62
62
  --crombolo-section-sidebar-background: var(--crombolo-night-surface);
63
63
  --crombolo-card-shadow: none;
64
+ --crombolo-card-header-background: #4a2a1a;
65
+ --crombolo-card-header-text: #ffffff;
64
66
  --crombolo-terracotta-tint: var(--crombolo-night-alt);
65
67
 
66
68
  /* The SEO view's search-result preview, in Google's dark-mode link blue. */
@@ -92,6 +92,9 @@
92
92
  /* The sidebar is a white panel, like the cards, beside the cream page. */
93
93
  --crombolo-section-sidebar-background: #fff;
94
94
  /* Crombolo's own cards (the Overview, Settings) stand off the page; Umbraco's stay flat. */
95
+ /* A Settings card's title sits on a dark band. */
96
+ --crombolo-card-header-background: var(--crombolo-cocoa);
97
+ --crombolo-card-header-text: #fff;
95
98
  --crombolo-card-shadow: 0 1px 2px rgb(84 50 31 / 6%), 0 10px 28px -18px rgb(84 50 31 / 30%);
96
99
 
97
100
  /* Pure white panels on a warm off-white page, so cards still separate. */
@@ -83,7 +83,7 @@ export default class CromboloBundleEditorElement extends UmbLitElement {
83
83
 
84
84
  render() {
85
85
  return html`
86
- <umb-workspace-editor back-path="section/packages/view/created">
86
+ <umb-workspace-editor back-path="section/settings/dashboard/bundles/created">
87
87
  ${
88
88
  this._definition
89
89
  ? html`<div id="header" slot="header">
@@ -20,7 +20,7 @@ import { downloadFile } from './download.js'
20
20
  const TAKE = 10
21
21
 
22
22
  /** Where the builder lives, which is Umbraco's route and not ours to rename. */
23
- export const BUILDER_PATH = 'section/packages/view/created/package-builder'
23
+ export const BUILDER_PATH = 'section/settings/dashboard/bundles/created/package-builder'
24
24
 
25
25
  export default class CromboloBundlesCreatedOverviewElement extends UmbLitElement {
26
26
  static properties = {
@@ -1,9 +1,8 @@
1
1
  /**
2
2
  * The Created view, in place of Umbraco's.
3
3
  *
4
- * Its manifest declares `overwrites: ['Umb.SectionView.Packages.Builder']`.
5
- * Umbraco's view is a router, not a screen: it mounts its own overview at
6
- * `overview` and every `package-builder` workspace at `package-builder/create`
4
+ * A tab of the Bundles tool in Settings (story 238). Umbraco's view is a
5
+ * router, not a screen: it mounts its own overview at `overview` and every `package-builder` workspace at `package-builder/create`
7
6
  * and `package-builder/edit/:unique`. Those two routes are the only way to
8
7
  * reach the builder, so this reproduces them exactly — replacing the view
9
8
  * without them would have left the builder unreachable rather than renamed.
@@ -12,7 +11,7 @@
12
11
  */
13
12
  import { createExtensionElement } from '@umbraco-cms/backoffice/extension-api'
14
13
  import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry'
15
- import { html } from '@umbraco-cms/backoffice/external/lit'
14
+ import { css, html } from '@umbraco-cms/backoffice/external/lit'
16
15
  import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'
17
16
  import { editorsOf } from './bundle-editor-routing.js'
18
17
 
@@ -42,6 +41,16 @@ export default class CromboloBundlesCreatedElement extends UmbLitElement {
42
41
  render() {
43
42
  return html`<umb-router-slot .routes=${this._routes}></umb-router-slot>`
44
43
  }
44
+
45
+ // A tab of the Bundles tool, which gives it the space under the tabs; the
46
+ // builder's workspace fills that.
47
+ static styles = css`
48
+ :host,
49
+ umb-router-slot {
50
+ display: block;
51
+ height: 100%;
52
+ }
53
+ `
45
54
  }
46
55
 
47
56
  /** Umbraco's own route table, with our overview in place of its own. */
@@ -1,8 +1,7 @@
1
1
  /**
2
2
  * The Installed view, in place of Umbraco's.
3
3
  *
4
- * Its manifest declares `overwrites: ['Umb.SectionView.Packages.Installed']`.
5
- * Umbraco's view is built around package migrations — it lists installed
4
+ * A tab of the Bundles tool in Settings (story 238). Umbraco's view is built around package migrations — it lists installed
6
5
  * packages so that pending C# migrations can be run. Nothing carries those here
7
6
  * (`docs/bundles.md`), so the view would be a list of nothing with a button
8
7
  * that cannot apply.
@@ -58,7 +57,8 @@ export default class CromboloBundlesInstalledElement extends UmbLitElement {
58
57
  this._notice = ''
59
58
  this._installs = true
60
59
  this._filter = ''
61
- this._selected = ''
60
+ // Settings links a bundle's cards here, naming it in `?bundle=`.
61
+ this._selected = new URLSearchParams(window.location.search).get('bundle') ?? ''
62
62
  }
63
63
 
64
64
  connectedCallback() {
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * The Marketplace view, in place of Umbraco's.
3
3
  *
4
- * Its manifest declares `overwrites: ['Umb.SectionView.Packages.Marketplace']`.
5
- * What it replaces is an iframe of marketplace.umbraco.com, which cannot work
4
+ * A tab of the Bundles tool in Settings (story 238), where Umbraco's Packages
5
+ * section had its Marketplace. What it replaces is an iframe of marketplace.umbraco.com, which cannot work
6
6
  * here twice over: npm refuses to be framed (`x-frame-options: SAMEORIGIN`), and
7
7
  * an Umbraco package does not install into this CMS anyway.
8
8
  *
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Bundles, a Settings tool (story 238): Marketplace, Installed and Created as
3
+ * tabs, each at an address of its own under `dashboard/bundles/`. The views
4
+ * are the ones Umbraco's Packages section showed, which this replaces.
5
+ */
6
+ import { css, html } from '@umbraco-cms/backoffice/external/lit'
7
+ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'
8
+
9
+ /** The tabs, in order: each one's segment, its label, and its view. */
10
+ export const BUNDLE_TABS = [
11
+ { path: 'marketplace', label: 'Marketplace', load: () => import('./bundles-marketplace.js') },
12
+ { path: 'installed', label: 'Installed', load: () => import('./bundles-installed.js') },
13
+ { path: 'created', label: 'Created', load: () => import('./bundles-created.js') },
14
+ ]
15
+
16
+ /** Where the tool is, relative to the back office. */
17
+ export const BUNDLES_PATH = 'section/settings/dashboard/bundles'
18
+
19
+ export default class CromboloBundlesToolElement extends UmbLitElement {
20
+ static properties = { _routerPath: { state: true }, _active: { state: true } }
21
+
22
+ constructor() {
23
+ super()
24
+ this._routerPath = BUNDLES_PATH
25
+ this._active = BUNDLE_TABS[0].path
26
+ }
27
+
28
+ #routes = [
29
+ ...BUNDLE_TABS.map((tab) => ({ path: tab.path, component: tab.load })),
30
+ { path: '', pathMatch: 'full', redirectTo: BUNDLE_TABS[0].path },
31
+ ]
32
+
33
+ render() {
34
+ return html`
35
+ <uui-tab-group>
36
+ ${BUNDLE_TABS.map(
37
+ (tab) => html`<uui-tab
38
+ label=${tab.label}
39
+ href=${`${this._routerPath}/${tab.path}`}
40
+ ?active=${this._active === tab.path}
41
+ >${tab.label}</uui-tab>`,
42
+ )}
43
+ </uui-tab-group>
44
+ <umb-router-slot
45
+ .routes=${this.#routes}
46
+ @init=${(event) => {
47
+ this._routerPath = event.target.absoluteRouterPath
48
+ }}
49
+ @change=${(event) => {
50
+ const active = event.target.absoluteActiveViewPath ?? ''
51
+ this._active = BUNDLE_TABS.find((tab) => active.endsWith(`/${tab.path}`))?.path ?? ''
52
+ }}
53
+ ></umb-router-slot>
54
+ `
55
+ }
56
+
57
+ static styles = css`
58
+ :host {
59
+ display: flex;
60
+ flex-direction: column;
61
+ height: 100%;
62
+ }
63
+ uui-tab-group {
64
+ /* UUI's tab group fills its container's height; here it is a strip. */
65
+ flex: none;
66
+ height: auto;
67
+ padding: 0 var(--uui-size-layout-1);
68
+ border-bottom: 1px solid var(--uui-color-border);
69
+ --uui-tab-divider: transparent;
70
+ }
71
+ umb-router-slot {
72
+ display: block;
73
+ flex: 1 1 auto;
74
+ min-height: 0;
75
+ }
76
+ `
77
+ }
78
+
79
+ customElements.define('crombolo-bundles-tool', CromboloBundlesToolElement)
@@ -0,0 +1,57 @@
1
+ /** CI access's words and its one API call (story 160). Pure but for `trustApi`. */
2
+
3
+ /** What a rule may hold, in the order a person reads them. */
4
+ export const CI_PERMISSIONS = [
5
+ {
6
+ verb: 'Crombolo.Remote.Pull',
7
+ label: 'Pull',
8
+ explain: "Download the site's content, without its private parts.",
9
+ },
10
+ {
11
+ verb: 'Crombolo.Remote.PullFull',
12
+ label: 'Pull everything',
13
+ explain: 'Download the site with people and private data too.',
14
+ },
15
+ { verb: 'Crombolo.Remote.Push', label: 'Push', explain: 'Replace this site from a file.' },
16
+ {
17
+ verb: 'Crombolo.Remote.PushProtected',
18
+ label: 'Push to a protected site',
19
+ explain: 'Change a site people rely on, such as production, with nobody approving it.',
20
+ },
21
+ {
22
+ verb: 'Crombolo.Remote.ApplyStructure',
23
+ label: 'Change structure and code',
24
+ explain: 'Push a whole site, or apply a bundle that changes its types, views, styles or code.',
25
+ },
26
+ ]
27
+
28
+ /** A provider by the address its tokens come from. */
29
+ export function providerName(issuer) {
30
+ if (issuer === 'https://token.actions.githubusercontent.com') return 'GitHub Actions'
31
+ if (issuer === 'https://gitlab.com') return 'GitLab'
32
+ return `GitLab at ${issuer.replace(/^https?:\/\//, '')}`
33
+ }
34
+
35
+ const base = () =>
36
+ `${(document.querySelector('base')?.getAttribute('href') ?? '/umbraco/').replace(/\/$/, '')}/crombolo/api/remote-trust`
37
+
38
+ /** Calls the trust rules; a refusal throws with the server's reasons as `items`. */
39
+ export async function trustApi(method, path = '', body) {
40
+ const response = await fetch(`${base()}${path}`, {
41
+ method,
42
+ credentials: 'include',
43
+ headers: {
44
+ accept: 'application/json',
45
+ ...(body ? { 'content-type': 'application/json' } : {}),
46
+ },
47
+ ...(body ? { body: JSON.stringify(body) } : {}),
48
+ })
49
+ if (response.status === 204) return undefined
50
+ const result = await response.json().catch(() => ({}))
51
+ if (!response.ok) {
52
+ const error = new Error(result.title ?? `CI access responded ${response.status}`)
53
+ error.items = result.items
54
+ throw error
55
+ }
56
+ return result
57
+ }
@@ -0,0 +1,270 @@
1
+ /**
2
+ * CI access, a Settings tool for administrators (story 160): the CI jobs this
3
+ * site trusts to pull and push without a person, each by the repository and
4
+ * branch, tag or environment its provider's signed identity token names. There
5
+ * is no secret to set up; a rule here is all a job needs.
6
+ */
7
+ import { css, html, nothing, repeat } from '@umbraco-cms/backoffice/external/lit'
8
+ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'
9
+ import { CI_PERMISSIONS, providerName, trustApi } from './ci-access-words.js'
10
+
11
+ const EMPTY = {
12
+ name: '',
13
+ issuer: '',
14
+ repository: '',
15
+ refKind: 'branch',
16
+ refValue: '',
17
+ permissions: [],
18
+ }
19
+
20
+ export default class CromboloCiAccessElement extends UmbLitElement {
21
+ static properties = {
22
+ _state: { state: true },
23
+ _draft: { state: true },
24
+ _problems: { state: true },
25
+ _busy: { state: true },
26
+ }
27
+
28
+ constructor() {
29
+ super()
30
+ this._state = undefined
31
+ this._draft = undefined
32
+ this._problems = []
33
+ this._busy = false
34
+ }
35
+
36
+ connectedCallback() {
37
+ super.connectedCallback()
38
+ this.#load()
39
+ }
40
+
41
+ async #load() {
42
+ try {
43
+ this._state = await trustApi('GET')
44
+ } catch (error) {
45
+ this._state = { error: error.message }
46
+ }
47
+ }
48
+
49
+ async #act(method, path, body) {
50
+ this._busy = true
51
+ this._problems = []
52
+ try {
53
+ await trustApi(method, path, body)
54
+ this._draft = undefined
55
+ await this.#load()
56
+ } catch (error) {
57
+ this._problems = error.items ?? [error.message]
58
+ } finally {
59
+ this._busy = false
60
+ }
61
+ }
62
+
63
+ #set(field, value) {
64
+ this._draft = { ...this._draft, [field]: value }
65
+ }
66
+
67
+ #toggle(verb, on) {
68
+ const permissions = new Set(this._draft.permissions)
69
+ if (on) permissions.add(verb)
70
+ else permissions.delete(verb)
71
+ this.#set('permissions', [...permissions])
72
+ }
73
+
74
+ #form() {
75
+ const draft = this._draft
76
+ const issuers = this._state.issuers ?? []
77
+ return html`<uui-box headline="Trust a CI job">
78
+ <div class="form">
79
+ <uui-label for="ci-name">Name</uui-label>
80
+ <uui-input id="ci-name" label="Name" .value=${draft.name} @input=${(e) => this.#set('name', e.target.value)}></uui-input>
81
+ <uui-label for="ci-provider">Provider</uui-label>
82
+ <uui-select
83
+ id="ci-provider"
84
+ label="Provider"
85
+ .options=${issuers.map((issuer) => ({ name: providerName(issuer), value: issuer, selected: issuer === draft.issuer }))}
86
+ @change=${(e) => this.#set('issuer', e.target.value)}
87
+ ></uui-select>
88
+ <uui-label for="ci-repository">Repository</uui-label>
89
+ <uui-input
90
+ id="ci-repository"
91
+ label="Repository"
92
+ placeholder="owner/name, or group/project"
93
+ .value=${draft.repository}
94
+ @input=${(e) => this.#set('repository', e.target.value)}
95
+ ></uui-input>
96
+ <uui-label for="ci-ref">Runs on</uui-label>
97
+ <div class="ref">
98
+ <uui-select
99
+ label="Runs on"
100
+ .options=${[
101
+ { name: 'Branch', value: 'branch', selected: draft.refKind === 'branch' },
102
+ { name: 'Tag', value: 'tag', selected: draft.refKind === 'tag' },
103
+ {
104
+ name: 'Environment',
105
+ value: 'environment',
106
+ selected: draft.refKind === 'environment',
107
+ },
108
+ ]}
109
+ @change=${(e) => this.#set('refKind', e.target.value)}
110
+ ></uui-select>
111
+ <uui-input id="ci-ref" label="Its name" placeholder="main" .value=${draft.refValue} @input=${(e) => this.#set('refValue', e.target.value)}></uui-input>
112
+ </div>
113
+ <span class="label">May</span>
114
+ <div class="may">
115
+ ${CI_PERMISSIONS.map(
116
+ (p) => html`<uui-toggle
117
+ label=${p.label}
118
+ .checked=${draft.permissions.includes(p.verb)}
119
+ @change=${(e) => this.#toggle(p.verb, e.target.checked)}
120
+ ></uui-toggle>
121
+ <small>${p.explain}</small>`,
122
+ )}
123
+ </div>
124
+ </div>
125
+ ${
126
+ this._problems.length > 0
127
+ ? html`<ul class="problems" role="alert">${this._problems.map((p) => html`<li>${p}</li>`)}</ul>`
128
+ : nothing
129
+ }
130
+ <div class="actions">
131
+ <uui-button look="secondary" label="Cancel" @click=${() => {
132
+ this._draft = undefined
133
+ this._problems = []
134
+ }}></uui-button>
135
+ <uui-button
136
+ look="primary"
137
+ label="Trust this job"
138
+ .state=${this._busy ? 'waiting' : undefined}
139
+ @click=${() => this.#act('POST', '', draft)}
140
+ ></uui-button>
141
+ </div>
142
+ </uui-box>`
143
+ }
144
+
145
+ render() {
146
+ const state = this._state
147
+ if (!state) return html`<uui-loader></uui-loader>`
148
+ if (state.error)
149
+ return html`<uui-box headline="CI access"><p role="alert">${state.error}</p></uui-box>`
150
+ return html`
151
+ ${
152
+ state.remoteEnabled
153
+ ? nothing
154
+ : html`<p class="notice" role="status">
155
+ This site does not take pulls or pushes yet, so these rules do nothing until it does.
156
+ </p>`
157
+ }
158
+ ${this._draft ? this.#form() : nothing}
159
+ <uui-box headline="Trusted CI jobs">
160
+ <uui-button
161
+ slot="header-actions"
162
+ look="primary"
163
+ label="Trust a CI job"
164
+ ?disabled=${Boolean(this._draft)}
165
+ @click=${() => {
166
+ this._draft = { ...EMPTY, issuer: state.issuers?.[0] ?? '' }
167
+ }}
168
+ ></uui-button>
169
+ <p class="intro">
170
+ A job proves where it runs with its provider's signed identity, so there is no password or
171
+ key to keep. It may only do what its rule says.
172
+ </p>
173
+ ${
174
+ state.items.length === 0
175
+ ? html`<p>No CI job is trusted yet.</p>`
176
+ : html`<uui-table>
177
+ <uui-table-head>
178
+ <uui-table-head-cell>Name</uui-table-head-cell>
179
+ <uui-table-head-cell>Runs from</uui-table-head-cell>
180
+ <uui-table-head-cell>May</uui-table-head-cell>
181
+ <uui-table-head-cell>Last used</uui-table-head-cell>
182
+ <uui-table-head-cell></uui-table-head-cell>
183
+ </uui-table-head>
184
+ ${repeat(
185
+ state.items,
186
+ (rule) => rule.key,
187
+ (rule) => html`<uui-table-row class=${rule.disabled ? 'disabled' : ''}>
188
+ <uui-table-cell>${rule.name}${rule.disabled ? html` <uui-tag look="secondary">Disabled</uui-tag>` : nothing}</uui-table-cell>
189
+ <uui-table-cell>${providerName(rule.issuer)} ${rule.repository}, ${rule.refKind} ${rule.refValue}</uui-table-cell>
190
+ <uui-table-cell>${rule.permissions.map((verb) => CI_PERMISSIONS.find((p) => p.verb === verb)?.label ?? verb).join(', ')}</uui-table-cell>
191
+ <uui-table-cell>${rule.lastUsedDate ? new Date(rule.lastUsedDate).toLocaleString() : 'Never'}</uui-table-cell>
192
+ <uui-table-cell class="row-actions">
193
+ <uui-button
194
+ look="secondary"
195
+ label=${rule.disabled ? 'Enable' : 'Disable'}
196
+ @click=${() => this.#act('POST', `/${rule.key}/${rule.disabled ? 'enable' : 'disable'}`)}
197
+ ></uui-button>
198
+ <uui-button
199
+ look="secondary"
200
+ color="danger"
201
+ label="Delete"
202
+ @click=${() => this.#act('DELETE', `/${rule.key}`)}
203
+ ></uui-button>
204
+ </uui-table-cell>
205
+ </uui-table-row>`,
206
+ )}
207
+ </uui-table>`
208
+ }
209
+ </uui-box>
210
+ `
211
+ }
212
+
213
+ static styles = css`
214
+ :host {
215
+ display: block;
216
+ padding: var(--uui-size-layout-1);
217
+ max-width: 72rem;
218
+ }
219
+ uui-box + uui-box {
220
+ margin-top: var(--uui-size-layout-1);
221
+ }
222
+ .notice {
223
+ padding: var(--uui-size-space-4) var(--uui-size-space-5);
224
+ border-left: 4px solid var(--uui-color-warning);
225
+ background: color-mix(in srgb, var(--uui-color-warning) 12%, var(--uui-color-surface));
226
+ }
227
+ .intro {
228
+ margin-top: 0;
229
+ color: var(--uui-color-text-alt);
230
+ }
231
+ .form {
232
+ display: grid;
233
+ grid-template-columns: 10rem 1fr;
234
+ gap: var(--uui-size-space-4);
235
+ align-items: center;
236
+ }
237
+ .ref {
238
+ display: flex;
239
+ gap: var(--uui-size-space-3);
240
+ }
241
+ .may {
242
+ display: grid;
243
+ grid-template-columns: auto 1fr;
244
+ gap: var(--uui-size-space-2) var(--uui-size-space-4);
245
+ align-items: center;
246
+ }
247
+ .may small {
248
+ color: var(--uui-color-text-alt);
249
+ }
250
+ .actions {
251
+ display: flex;
252
+ justify-content: flex-end;
253
+ gap: var(--uui-size-space-3);
254
+ margin-top: var(--uui-size-space-5);
255
+ }
256
+ .problems {
257
+ color: var(--uui-color-danger);
258
+ }
259
+ .row-actions {
260
+ display: flex;
261
+ gap: var(--uui-size-space-2);
262
+ justify-content: flex-end;
263
+ }
264
+ uui-table-row.disabled {
265
+ opacity: 0.6;
266
+ }
267
+ `
268
+ }
269
+
270
+ customElements.define('crombolo-ci-access', CromboloCiAccessElement)
@@ -1,13 +1,69 @@
1
1
  /** The select reserves an empty value for its disabled placeholder. */
2
2
  export const NO_FORM_VALUE = '@none'
3
3
 
4
- /** Choices for the form picker, including a saved form that has since gone. */
5
- export function formPickerOptions(forms, value) {
4
+ /**
5
+ * @typedef {{ key: string, name: string }} FormSummary
6
+ * @typedef {{ key: string, name: string, formKey: string, formName: string | null, published: boolean }} LibraryFormItem
7
+ * @typedef {{ name: string, value: string, group?: string, selected: boolean }} PickerOption
8
+ */
9
+
10
+ /**
11
+ * Choices for the form picker (story 239): each Library form, grouped by the
12
+ * form it is made from. A page saved before Library forms names a form itself,
13
+ * which stays selected, shown in the form's own words; a saved choice that has
14
+ * since gone stays too, as unavailable.
15
+ *
16
+ * @param {{ forms?: FormSummary[], libraryForms?: LibraryFormItem[] }} from
17
+ * @param {string | undefined} value
18
+ * @returns {PickerOption[]}
19
+ */
20
+ export function formPickerOptions({ forms = [], libraryForms = [] }, value) {
21
+ /** @type {PickerOption[]} */
6
22
  const options = [
7
23
  { name: 'No form', value: NO_FORM_VALUE, selected: !value },
8
- ...forms.map((form) => ({ name: form.name, value: form.key, selected: form.key === value })),
24
+ ...libraryForms.map((item) => ({
25
+ name: item.published ? item.name : `${item.name} (not published)`,
26
+ value: item.key,
27
+ group: item.formName ?? 'Unavailable form',
28
+ selected: item.key === value,
29
+ })),
9
30
  ]
10
- if (value && !forms.some((form) => form.key === value))
11
- options.push({ name: 'Unavailable form', value, selected: true })
31
+ if (!value || libraryForms.some((item) => item.key === value)) return options
32
+ const form = forms.find((candidate) => candidate.key === value)
33
+ options.push(
34
+ form
35
+ ? { name: `${form.name} (the form's own words)`, value, group: 'Forms', selected: true }
36
+ : { name: 'Unavailable form', value, selected: true },
37
+ )
12
38
  return options
13
39
  }
40
+
41
+ /**
42
+ * The form a picked value shows: a Library form's, or the form a page names itself.
43
+ *
44
+ * @param {{ forms?: FormSummary[], libraryForms?: LibraryFormItem[] }} from
45
+ * @param {string | undefined} value
46
+ */
47
+ export function pickedForm({ forms = [], libraryForms = [] }, value) {
48
+ if (!value) return undefined
49
+ const item = libraryForms.find((candidate) => candidate.key === value)
50
+ const key = item ? item.formKey : value
51
+ const form = forms.find((candidate) => candidate.key === key)
52
+ return form ? { form, library: item } : undefined
53
+ }
54
+
55
+ /**
56
+ * The entries view's Library form filter for one form; empty when it has none.
57
+ *
58
+ * @param {LibraryFormItem[]} libraryForms
59
+ * @param {string} formKey
60
+ */
61
+ export function entriesLibraryOptions(libraryForms, formKey, selected = '') {
62
+ const own = libraryForms.filter((item) => item.formKey === formKey)
63
+ if (own.length === 0) return []
64
+ return [
65
+ { name: 'Any Library form', value: '', selected: selected === '' },
66
+ ...own.map((item) => ({ name: item.name, value: item.key, selected: item.key === selected })),
67
+ { name: 'None: the form itself', value: 'none', selected: selected === 'none' },
68
+ ]
69
+ }