@aglyn/plugins-data 1.12.0 → 1.14.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.
Files changed (43) hide show
  1. package/package.json +10 -10
  2. package/src/lib/artifact/dataset-schema-artifact.server.js +6 -3
  3. package/src/lib/artifact/dataset-schema-artifact.server.js.map +1 -1
  4. package/src/lib/components/dataset-schema-dialog.component.d.ts +14 -0
  5. package/src/lib/components/dataset-schema-dialog.component.js +317 -239
  6. package/src/lib/components/dataset-schema-dialog.component.js.map +1 -1
  7. package/src/lib/components/host-datasets-card.component.js +71 -34
  8. package/src/lib/components/host-datasets-card.component.js.map +1 -1
  9. package/src/lib/components/use-dataset-writes-blocked.d.ts +50 -12
  10. package/src/lib/components/use-dataset-writes-blocked.js +86 -19
  11. package/src/lib/components/use-dataset-writes-blocked.js.map +1 -1
  12. package/src/lib/declarations.d.ts +25 -0
  13. package/src/lib/declarations.js +37 -0
  14. package/src/lib/declarations.js.map +1 -0
  15. package/src/lib/model/data-permissions.d.ts +34 -0
  16. package/src/lib/model/data-permissions.js +108 -0
  17. package/src/lib/model/data-permissions.js.map +1 -0
  18. package/src/lib/model/dataset-access.d.ts +22 -0
  19. package/src/lib/model/dataset-access.js +30 -0
  20. package/src/lib/model/dataset-access.js.map +1 -0
  21. package/src/lib/model/dataset-record.js +8 -3
  22. package/src/lib/model/dataset-record.js.map +1 -1
  23. package/src/lib/plugin.js +4 -1
  24. package/src/lib/plugin.js.map +1 -1
  25. package/src/lib/server/dataset-access.server.d.ts +41 -0
  26. package/src/lib/server/dataset-access.server.js +49 -0
  27. package/src/lib/server/dataset-access.server.js.map +1 -0
  28. package/src/lib/server/dataset-figures.js +12 -0
  29. package/src/lib/server/dataset-figures.js.map +1 -1
  30. package/src/lib/server/dataset-site-erasure.d.ts +2 -2
  31. package/src/lib/server/dataset-site-erasure.js +4 -26
  32. package/src/lib/server/dataset-site-erasure.js.map +1 -1
  33. package/src/lib/server/datasets-route.d.ts +2 -0
  34. package/src/lib/server/datasets-route.js +178 -27
  35. package/src/lib/server/datasets-route.js.map +1 -1
  36. package/src/lib/site-bundle/datasets-site-bundle.server.d.ts +4 -2
  37. package/src/lib/site-bundle/datasets-site-bundle.server.js +8 -4
  38. package/src/lib/site-bundle/datasets-site-bundle.server.js.map +1 -1
  39. package/src/lib/transfer/dataset-transfer-access.server.d.ts +23 -0
  40. package/src/lib/transfer/dataset-transfer-access.server.js +45 -0
  41. package/src/lib/transfer/dataset-transfer-access.server.js.map +1 -0
  42. package/src/lib/transfer/dataset-transfer-resource.js +2 -1
  43. package/src/lib/transfer/dataset-transfer-resource.js.map +1 -1
@@ -16,17 +16,61 @@
16
16
  */ 'use client';
17
17
  import { _ as _extends } from "@swc/helpers/_/_extends";
18
18
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
19
- import { getCustomFieldType, listCustomFieldTypes, describeScope, HOST_ACCESS_ROLES, hostScopeToken, narrowsScope, scopeCovers, ORG_SCOPE_TOKEN, scopeToStore, storedScope, visibleToHost } from "@aglyn/aglyn";
19
+ import { getCustomFieldType, listCustomFieldTypes, describeSharing, HOST_ACCESS_ROLES, narrowsScope, scopeSharingOf, sharingCovers, sharingFromChoice, storedScope, visibleToHost } from "@aglyn/aglyn";
20
20
  import { useConfirmationContext } from "@aglyn/shared-ui-jsx";
21
+ import { SiteSharingField } from "@aglyn/shared-ui-jsx/components/site-sharing-field.component";
22
+ import { authorizedFetch } from "@aglyn/shared-util-http/authorized-token";
21
23
  import { useSnackbar } from "@aglyn/shared-ui-snackstack";
22
- import { Button, Checkbox, Alert, Chip, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, IconButton, MenuItem, Select, Stack, TextField, Typography } from "@mui/material";
24
+ import { Button, Checkbox, Alert, Box, Chip, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, IconButton, MenuItem, Stack, TextField, Typography } from "@mui/material";
23
25
  import { collection, doc, limit, query, updateDoc, where } from "firebase/firestore";
24
- import { useCallback, useEffect, useState } from "react";
26
+ import { useCallback, useEffect, useMemo, useState } from "react";
25
27
  import { useFirestore, useFirestoreCollection, useOrgDataScope, useScopeTokens, useUser, writeGuardedBySeed } from "@aglyn/tenant-feature-instance";
26
28
  import { DATASET_AUTHORABLE_FIELD_TYPES, DATASET_FIELD_TYPES, DATASET_FIELD_TYPE_LABELS, effectiveDatasetModel } from "../model/dataset-models.js";
27
29
  import { datasetDisplayName, defaultDatasetFieldId, validateDatasetFieldId } from "../model/datasets.js";
28
30
  import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-pages/record-pages.js";
29
31
  /** Types surfaced in the picker; the rest exist for compat, not authoring. */ const AUTHORABLE_TYPES = DATASET_AUTHORABLE_FIELD_TYPES;
32
+ /** The dialog's schema controls, switched off together where only the sharing is this member's. */ function SchemaControls(props) {
33
+ return props.locked ? /*#__PURE__*/ _jsx(Box, {
34
+ component: "fieldset",
35
+ disabled: true,
36
+ sx: {
37
+ border: 0,
38
+ m: 0,
39
+ p: 0,
40
+ minWidth: 0
41
+ },
42
+ children: props.children
43
+ }) : /*#__PURE__*/ _jsx(_Fragment, {
44
+ children: props.children
45
+ });
46
+ }
47
+ /**
48
+ * How a dataset is shared, as the choice the sharing field edits, or null where
49
+ * it stores no sharing (AGL-1484). The seed and the save both read it from here,
50
+ * so a dialog nobody touched compares equal to its own document and writes none.
51
+ */ function sharingChoiceOf(dataset) {
52
+ const sharing = scopeSharingOf(dataset);
53
+ if (sharing.mode === 'all') return {
54
+ mode: 'all'
55
+ };
56
+ if (sharing.mode === 'selected') return {
57
+ mode: 'selected',
58
+ hostIds: sharing.hostIds
59
+ };
60
+ if (sharing.mode === 'allExcept') return {
61
+ mode: 'allExcept',
62
+ exceptHostIds: sharing.exceptHostIds
63
+ };
64
+ return null;
65
+ }
66
+ /** Two choices compare by what they pick, in any order. */ function sharingChoiceKey(choice) {
67
+ if (!choice) return 'unset';
68
+ if (choice.mode === 'all') return 'all';
69
+ const picked = choice.mode === 'selected' ? choice.hostIds : choice.exceptHostIds;
70
+ return `${choice.mode}:${[
71
+ ...picked
72
+ ].sort().join(',')}`;
73
+ }
30
74
  /**
31
75
  * Model builder (AGL-178): edits the AGL-177 typed model on a dataset —
32
76
  * singular/plural display names (dod.ts Schema.Name), add/edit/remove/
@@ -37,9 +81,9 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
37
81
  * lazily on next document save; fieldIds are stable keys — only display
38
82
  * names rename.
39
83
  */ export function DatasetSchemaDialog(props) {
40
- var _ref, _ref1, _ref2, _ref3, _ref4, _editorDefinition_slugFrom, _ref5, _ref6, _find, _ref7, _ref8, _ref9, _ref10;
84
+ var _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _editorDefinition_slugFrom, _ref6, _ref7, _find, _ref8, _ref9, _ref10, _ref11;
41
85
  var _editorDefinition_reference, _editorDefinition_reference1, _editorDefinition_reference2, _editorDefinition_reference3, _editorDefinition_validation;
42
- const { hostId, dataset, datasets, recordCount, seedFromCache, seedUnreadable, seedListen, onClose } = props;
86
+ const { hostId, dataset, datasets, recordCount, seedFromCache, seedUnreadable, seedListen, canChangeSharing = true, sharingOnlyReason, onClose } = props;
43
87
  // Org-shared data root (AGL-237/239). Null until the async org lookup
44
88
  // settles (AGL-1061), and for a host with no owning org. Saving is held
45
89
  // on it rather than redirected: the host path this used to fall back to
@@ -55,7 +99,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
55
99
  // Sharing scope (AGL-1044) — org datasets only; the legacy host path is
56
100
  // site-private by construction.
57
101
  // `orgWide` reads TRUE while the member doc loads (AGL-1047), which would
58
- // offer a scoped collaborator the scope Select for a beat before replacing
102
+ // offer a scoped collaborator the sharing field for a beat before replacing
59
103
  // it with the read-only summary — and offering a control the AGL-1041
60
104
  // rules will reject is the thing the branch below exists to avoid. Hold
61
105
  // the whole block until `loaded` rather than flash the wrong one.
@@ -86,10 +130,22 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
86
130
  ], {
87
131
  idField: '$id'
88
132
  });
89
- const orgHostList = orgHostDocs != null ? orgHostDocs : [];
90
- const [visibleTo, setVisibleTo] = useState([]);
133
+ const orgHostList = useMemo(()=>orgHostDocs != null ? orgHostDocs : [], [
134
+ orgHostDocs
135
+ ]);
136
+ const sitesReady = Array.isArray(orgHostDocs);
137
+ const sharingSites = useMemo(()=>orgHostList.map((host)=>{
138
+ var _ref, _host_name;
139
+ return {
140
+ id: host.$id,
141
+ name: (_ref = (_host_name = host.name) != null ? _host_name : host.subdomain) != null ? _ref : host.$id
142
+ };
143
+ }), [
144
+ orgHostList
145
+ ]);
91
146
  /**
92
- * The dataset stores NO scope (AGL-1484) — as opposed to storing `['org']`.
147
+ * Who this dataset is shared with, as edited here. Null while the dataset
148
+ * stores NO scope (AGL-1484) — as opposed to storing `['org']`.
93
149
  *
94
150
  * The dialog used to render the two identically, which for a dataset is a
95
151
  * worse lie than it was for a media file: `orgs/{orgId}/datasets` is read
@@ -97,7 +153,34 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
97
153
  * own `organizations.ts`, so an unstamped dataset is matched by no reader
98
154
  * in the product. It renders on no site and is missing from the reference
99
155
  * cards even for an org-wide member — while this control said "All sites".
100
- */ const [scopeUnset, setScopeUnset] = useState(false);
156
+ */ const [sharing, setSharing] = useState(null);
157
+ const scopeUnset = sharing === null;
158
+ /**
159
+ * What the choice would write, or why it cannot be. The same answer the
160
+ * `set-sharing` route gives from the workspace's sites, which it reads for
161
+ * itself: the sites a choice leaves out stay known here even where this member
162
+ * can't name them, so they aren't taken for sites that are gone.
163
+ */ const sharingDraft = useMemo(()=>{
164
+ if (!sharing) return null;
165
+ const leftOut = sharing.mode === 'allExcept' ? sharing.exceptHostIds : [];
166
+ return sharingFromChoice(sharing, [
167
+ ...orgHostList.map((host)=>host.$id),
168
+ ...leftOut
169
+ ]);
170
+ }, [
171
+ sharing,
172
+ orgHostList
173
+ ]);
174
+ // Nothing to say about leaving sites out until the sites have been read.
175
+ const sharingProblem = (sharing == null ? void 0 : sharing.mode) !== 'allExcept' || sitesReady ? (_ref = sharingDraft == null ? void 0 : sharingDraft.problem) != null ? _ref : null : null;
176
+ /** The dataset as this choice would leave it, for what depends on where it is. */ const sharingPreview = useMemo(()=>{
177
+ var _ref;
178
+ return (_ref = sharingDraft == null ? void 0 : sharingDraft.fields) != null ? _ref : {
179
+ visibleTo: []
180
+ };
181
+ }, [
182
+ sharingDraft
183
+ ]);
101
184
  const [model, setModel] = useState({
102
185
  fields: {},
103
186
  order: []
@@ -115,11 +198,9 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
115
198
  // AGL-1466 ruled it out: opening a dialog would write, the AGL-1042
116
199
  // rules refuse that write from anyone who is not org-wide, and it
117
200
  // repairs only what somebody happens to look at — a repair mechanism
118
- // wearing a UI. `previousScope` in `handleSave` reads the SAME helper;
119
- // see the note there for why the pair is the safety property.
120
- const storedDatasetScope = storedScope(dataset.visibleTo);
121
- setVisibleTo(storedDatasetScope != null ? storedDatasetScope : []);
122
- setScopeUnset(!storedDatasetScope);
201
+ // wearing a UI. `handleSave` reads the SAME helper; see the note there
202
+ // for why the pair is the safety property.
203
+ setSharing(sharingChoiceOf(dataset));
123
204
  const effective = effectiveDatasetModel(dataset);
124
205
  setModel({
125
206
  fields: JSON.parse(JSON.stringify(effective.fields)),
@@ -255,7 +336,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
255
336
  var _storedScope;
256
337
  if (!dataset || !dataScope) return;
257
338
  /**
258
- * What the document actually carries, `[]` when it carries nothing
339
+ * What the document actually carries, nothing when it carries nothing
259
340
  * (AGL-1484) — matching the seed above, through the same helper.
260
341
  *
261
342
  * The pairing is the whole safety property, not tidiness. Both sides
@@ -267,38 +348,37 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
267
348
  * — one the AGL-1041 rules then reject for any member who is not
268
349
  * org-wide, failing the WHOLE schema save with it.
269
350
  *
270
- * `[]` is also the honest previous value in the other direction: a
351
+ * Nothing is also the honest previous value in the other direction: a
271
352
  * deliberate save of exactly `['org']` on an unset dataset used to
272
353
  * compare equal to the default and be dropped, and that is the one save
273
354
  * an unstamped dataset most needs to land.
274
- */ const previousScope = (_storedScope = storedScope(dataset.visibleTo)) != null ? _storedScope : [];
275
- const scopeChanged = JSON.stringify([
276
- ...previousScope
277
- ].sort()) !== JSON.stringify([
278
- ...visibleTo
279
- ].sort());
355
+ */ const scopeChanged = sharingChoiceKey(sharingChoiceOf(dataset)) !== sharingChoiceKey(sharing);
280
356
  /**
281
- * The scope this save writes, or null when it writes none.
357
+ * The sharing this save writes, or null when it writes none.
282
358
  *
283
- * Only an org-wide member may change the scope — the AGL-1041 rules deny
284
- * anyone else, and a rejected field fails the WHOLE write.
359
+ * Only an org-wide member may change it — the route and the AGL-1041 rules
360
+ * refuse anyone else, and it is written through the route because a marker
361
+ * is the server's alone (AGL-4047).
285
362
  *
286
- * A selection `scopeToStore` cannot store — "Selected sites…" with no site
287
- * picked, or more sites than one query can match — refuses the save rather
288
- * than substituting a scope: the org token would share the collection with
289
- * every site, and anything narrower would drop sites somebody picked.
290
- */ const scopeWrite = orgId && viewerOrgWide && scopeChanged ? scopeToStore(visibleTo) : null;
291
- if (scopeWrite == null ? void 0 : scopeWrite.problem) {
292
- return void enqueueSnackbar(scopeWrite.problem, {
363
+ * A choice that cannot be stored — "Selected sites" with no site picked, or
364
+ * more sites than one query can match, or nothing left out, or everything —
365
+ * refuses the save rather than substituting a scope: the org token would
366
+ * share the collection with every site, and anything narrower would drop
367
+ * sites somebody picked.
368
+ */ const sharingWrite = orgId && viewerOrgWide && canChangeSharing && scopeChanged ? sharing : null;
369
+ if (sharingWrite && (sharingDraft == null ? void 0 : sharingDraft.problem)) {
370
+ return void enqueueSnackbar(sharingDraft.problem, {
293
371
  variant: 'warning',
294
372
  persist: false
295
373
  });
296
374
  }
297
- if (scopeChanged && narrowsScope(previousScope, visibleTo)) {
375
+ const previousScope = (_storedScope = storedScope(dataset.visibleTo)) != null ? _storedScope : [];
376
+ const nextScope = sharingPreview.visibleTo;
377
+ if (scopeChanged && narrowsScope(previousScope, nextScope)) {
298
378
  // Narrowing takes a dataset away from sites whose pages may bind it,
299
379
  // and a repeatable bound to a dataset it can no longer see renders
300
380
  // nothing (AGL-1039). Name the cost before paying it.
301
- const losing = orgHostList.filter((host)=>visibleToHost(previousScope, host.$id)).filter((host)=>!visibleToHost(visibleTo, host.$id)).map((host)=>{
381
+ const losing = orgHostList.filter((host)=>visibleToHost(previousScope, host.$id)).filter((host)=>!visibleToHost(nextScope, host.$id)).map((host)=>{
302
382
  var _ref, _host_name;
303
383
  return (_ref = (_host_name = host.name) != null ? _host_name : host.subdomain) != null ? _ref : host.$id;
304
384
  });
@@ -309,6 +389,8 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
309
389
  }).then(()=>true).catch(()=>false);
310
390
  if (!proceed) return;
311
391
  }
392
+ // Nothing of theirs to write when the sharing did not move.
393
+ if (sharingOnlyReason && !sharingWrite) return void onClose();
312
394
  if (!model.order.length) {
313
395
  return void enqueueSnackbar('A collection needs at least one field', {
314
396
  variant: 'warning',
@@ -330,26 +412,51 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
330
412
  * not even warn about the sites it is really taking access from.
331
413
  * - `model` is the entire schema. A cached seed reinstates fields that
332
414
  * were deleted and drops fields that were added.
333
- */ await writeGuardedBySeed({
415
+ *
416
+ * The schema goes first, because who may write it depends on where the
417
+ * dataset sits as it stands, the question the rule asks of this one save.
418
+ */ let sharingRefusal = null;
419
+ await writeGuardedBySeed({
334
420
  subject: 'collection schema',
335
421
  unreadable: seedUnreadable,
336
422
  fromCache: seedFromCache,
337
423
  listen: seedListen
338
- }, async ()=>void await updateDoc(doc(firestore, dataScope[0], dataScope[1], 'datasets', dataset.$id), _extends({
339
- model,
340
- names: {
341
- singular: names.singular.trim(),
342
- plural: names.plural.trim()
424
+ }, async ()=>{
425
+ if (!sharingOnlyReason) {
426
+ await updateDoc(doc(firestore, dataScope[0], dataScope[1], 'datasets', dataset.$id), _extends({
427
+ model,
428
+ names: {
429
+ singular: names.singular.trim(),
430
+ plural: names.plural.trim()
431
+ }
432
+ }, names.plural.trim() ? {
433
+ displayName: names.plural.trim()
434
+ } : {}, {
435
+ // v1 compat: keep the flat column list mirroring the model order
436
+ // so unmigrated consumers (AGL-103 bindings) keep working.
437
+ fields: model.order
438
+ }));
439
+ }
440
+ if (sharingWrite) {
441
+ const response = await authorizedFetch(user, '/api/orgs/datasets', {
442
+ method: 'POST',
443
+ headers: {
444
+ 'Content-Type': 'application/json'
445
+ },
446
+ body: JSON.stringify({
447
+ orgId,
448
+ action: 'set-sharing',
449
+ datasetId: dataset.$id,
450
+ choice: sharingWrite
451
+ })
452
+ });
453
+ if (!response.ok) {
454
+ var _ref;
455
+ const payload = await response.json().catch(()=>({}));
456
+ sharingRefusal = String((_ref = payload == null ? void 0 : payload.error) != null ? _ref : 'The sharing could not be changed.');
343
457
  }
344
- }, names.plural.trim() ? {
345
- displayName: names.plural.trim()
346
- } : {}, {
347
- // v1 compat: keep the flat column list mirroring the model order
348
- // so unmigrated consumers (AGL-103 bindings) keep working.
349
- fields: model.order
350
- }, (scopeWrite == null ? void 0 : scopeWrite.scope) ? {
351
- visibleTo: scopeWrite.scope
352
- } : {}))).then((result)=>{
458
+ }
459
+ }).then((result)=>{
353
460
  // A refusal leaves the dialog open with the edited schema intact.
354
461
  if (!result.ok) {
355
462
  return void enqueueSnackbar(result.message, {
@@ -357,7 +464,13 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
357
464
  persist: false
358
465
  });
359
466
  }
360
- enqueueSnackbar('Schema saved', {
467
+ if (sharingRefusal) {
468
+ return void enqueueSnackbar(sharingOnlyReason ? sharingRefusal : `Schema saved. Its sharing didn’t change: ${sharingRefusal}`, {
469
+ variant: 'warning',
470
+ persist: false
471
+ });
472
+ }
473
+ enqueueSnackbar(sharingOnlyReason ? 'Sharing saved' : 'Schema saved', {
361
474
  variant: 'success',
362
475
  persist: false
363
476
  });
@@ -369,8 +482,13 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
369
482
  dataset,
370
483
  model,
371
484
  names,
372
- visibleTo,
485
+ sharing,
486
+ sharingDraft,
487
+ sharingPreview,
488
+ user,
373
489
  viewerOrgWide,
490
+ canChangeSharing,
491
+ sharingOnlyReason,
374
492
  orgHostList,
375
493
  confirm,
376
494
  firestore,
@@ -405,32 +523,39 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
405
523
  gap: 2
406
524
  },
407
525
  children: [
408
- /*#__PURE__*/ _jsxs(Stack, {
409
- direction: "row",
410
- spacing: 1,
411
- sx: {
412
- mt: 1
413
- },
414
- children: [
415
- /*#__PURE__*/ _jsx(TextField, {
416
- size: "small",
417
- label: "Singular name",
418
- value: names.singular,
419
- onChange: (event)=>setNames((prev)=>_extends({}, prev, {
420
- singular: event.target.value
421
- })),
422
- helperText: "e.g. Product"
423
- }),
424
- /*#__PURE__*/ _jsx(TextField, {
425
- size: "small",
426
- label: "Plural name",
427
- value: names.plural,
428
- onChange: (event)=>setNames((prev)=>_extends({}, prev, {
429
- plural: event.target.value
430
- })),
431
- helperText: "e.g. Products (shown as the title)"
432
- })
433
- ]
526
+ sharingOnlyReason ? /*#__PURE__*/ _jsx(Alert, {
527
+ severity: "info",
528
+ children: sharingOnlyReason
529
+ }) : null,
530
+ /*#__PURE__*/ _jsx(SchemaControls, {
531
+ locked: Boolean(sharingOnlyReason),
532
+ children: /*#__PURE__*/ _jsxs(Stack, {
533
+ direction: "row",
534
+ spacing: 1,
535
+ sx: {
536
+ mt: 1
537
+ },
538
+ children: [
539
+ /*#__PURE__*/ _jsx(TextField, {
540
+ size: "small",
541
+ label: "Singular name",
542
+ value: names.singular,
543
+ onChange: (event)=>setNames((prev)=>_extends({}, prev, {
544
+ singular: event.target.value
545
+ })),
546
+ helperText: "e.g. Product"
547
+ }),
548
+ /*#__PURE__*/ _jsx(TextField, {
549
+ size: "small",
550
+ label: "Plural name",
551
+ value: names.plural,
552
+ onChange: (event)=>setNames((prev)=>_extends({}, prev, {
553
+ plural: event.target.value
554
+ })),
555
+ helperText: "e.g. Products (shown as the title)"
556
+ })
557
+ ]
558
+ })
434
559
  }),
435
560
  orgId ? /*#__PURE__*/ _jsxs(Stack, {
436
561
  spacing: 0.5,
@@ -444,179 +569,132 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
444
569
  variant: "body2",
445
570
  color: "text.secondary",
446
571
  children: 'Checking your access…'
447
- }) : viewerOrgWide ? /*#__PURE__*/ _jsxs(_Fragment, {
572
+ }) : viewerOrgWide && canChangeSharing ? /*#__PURE__*/ _jsxs(_Fragment, {
448
573
  children: [
449
574
  scopeUnset ? /*#__PURE__*/ _jsx(Alert, {
450
575
  severity: "warning",
451
576
  children: 'This collection has never been shared. Nothing is ' + 'stored, so it is hidden from every site and any ' + 'page repeating over it renders nothing. Choose who ' + 'it is shared with to fix that.'
452
577
  }) : null,
453
- /*#__PURE__*/ _jsxs(Select, {
454
- size: "small",
455
- value: visibleTo.includes(ORG_SCOPE_TOKEN) ? 'org' : scopeUnset ? 'unset' : 'hosts',
456
- onChange: (event)=>{
457
- // Any choice ends the unset state — from here the
458
- // control shows what was picked, not what was (not)
459
- // stored.
460
- setScopeUnset(false);
461
- setVisibleTo(event.target.value === 'org' ? [
462
- ORG_SCOPE_TOKEN
463
- ] : []);
464
- },
465
- children: [
466
- scopeUnset ? /*#__PURE__*/ _jsx(MenuItem, {
467
- value: "unset",
468
- disabled: true,
469
- children: 'Not shared with any site'
470
- }) : null,
471
- /*#__PURE__*/ _jsx(MenuItem, {
472
- value: "org",
473
- children: 'All sites'
474
- }),
475
- /*#__PURE__*/ _jsx(MenuItem, {
476
- value: "hosts",
477
- children: 'Selected sites…'
478
- })
479
- ]
578
+ /*#__PURE__*/ _jsx(SiteSharingField, {
579
+ value: sharing,
580
+ onChange: setSharing,
581
+ sites: sharingSites,
582
+ sitesReady: sitesReady,
583
+ problem: sharingProblem
480
584
  }),
481
585
  /*#__PURE__*/ _jsx(Typography, {
482
586
  variant: "caption",
483
587
  color: "text.secondary",
484
588
  children: 'Sites you exclude cannot see or use this dataset — in the console or on their pages.'
485
- }),
486
- !scopeUnset && !visibleTo.includes(ORG_SCOPE_TOKEN) ? /*#__PURE__*/ _jsx(Stack, {
487
- direction: "row",
488
- spacing: 0.5,
489
- useFlexGap: true,
490
- sx: {
491
- flexWrap: 'wrap'
492
- },
493
- children: orgHostList.map((host)=>{
494
- var _ref, _host_name;
495
- const token = hostScopeToken(host.$id);
496
- const on = visibleTo.includes(token);
497
- return /*#__PURE__*/ _jsx(Chip, {
498
- size: "small",
499
- label: (_ref = (_host_name = host.name) != null ? _host_name : host.subdomain) != null ? _ref : host.$id,
500
- color: on ? 'primary' : 'default',
501
- variant: on ? 'filled' : 'outlined',
502
- onClick: ()=>setVisibleTo((prev)=>on ? prev.filter((t)=>t !== token) : [
503
- ...prev,
504
- token
505
- ])
506
- }, host.$id);
507
- })
508
- }) : null,
509
- !scopeUnset && scopeToStore(visibleTo).problem ? /*#__PURE__*/ _jsx(Typography, {
510
- variant: "caption",
511
- color: "error",
512
- children: scopeToStore(visibleTo).problem
513
- }) : null
589
+ })
514
590
  ]
515
591
  }) : /*#__PURE__*/ _jsx(Typography, {
516
592
  variant: "body2",
517
- children: scopeUnset ? 'Never shared — hidden from every site' : describeScope(visibleTo, Object.fromEntries(orgHostList.map((host)=>{
518
- var _ref, _host_name;
519
- return [
520
- host.$id,
521
- (_ref = (_host_name = host.name) != null ? _host_name : host.subdomain) != null ? _ref : host.$id
522
- ];
523
- })))
593
+ children: scopeUnset ? 'Never shared — hidden from every site' : describeSharing(dataset, Object.fromEntries(sharingSites.map((site)=>[
594
+ site.id,
595
+ site.name
596
+ ])))
524
597
  })
525
598
  ]
526
599
  }) : null,
527
- /*#__PURE__*/ _jsx(Stack, {
528
- spacing: 0.5,
529
- children: model.order.map((fieldId, index)=>{
530
- var _DATASET_FIELD_TYPE_LABELS_field_type;
531
- var _field_validation;
532
- const field = model.fields[fieldId];
533
- if (!field) return null;
534
- return /*#__PURE__*/ _jsxs(Stack, {
535
- direction: "row",
536
- spacing: 1,
537
- sx: {
538
- alignItems: 'center',
539
- px: 1,
540
- py: 0.5,
541
- border: '1px solid',
542
- borderColor: 'divider',
543
- borderRadius: 1
544
- },
545
- children: [
546
- /*#__PURE__*/ _jsxs(Stack, {
600
+ /*#__PURE__*/ _jsxs(SchemaControls, {
601
+ locked: Boolean(sharingOnlyReason),
602
+ children: [
603
+ /*#__PURE__*/ _jsx(Stack, {
604
+ spacing: 0.5,
605
+ children: model.order.map((fieldId, index)=>{
606
+ var _DATASET_FIELD_TYPE_LABELS_field_type;
607
+ var _field_validation;
608
+ const field = model.fields[fieldId];
609
+ if (!field) return null;
610
+ return /*#__PURE__*/ _jsxs(Stack, {
611
+ direction: "row",
612
+ spacing: 1,
547
613
  sx: {
548
- flex: 1,
549
- minWidth: 0
614
+ alignItems: 'center',
615
+ px: 1,
616
+ py: 0.5,
617
+ border: '1px solid',
618
+ borderColor: 'divider',
619
+ borderRadius: 1
550
620
  },
551
621
  children: [
552
- /*#__PURE__*/ _jsxs(Typography, {
553
- variant: "body2",
554
- noWrap: true,
622
+ /*#__PURE__*/ _jsxs(Stack, {
623
+ sx: {
624
+ flex: 1,
625
+ minWidth: 0
626
+ },
555
627
  children: [
556
- field.name,
557
- /*#__PURE__*/ _jsx(Typography, {
558
- component: "span",
628
+ /*#__PURE__*/ _jsxs(Typography, {
629
+ variant: "body2",
630
+ noWrap: true,
631
+ children: [
632
+ field.name,
633
+ /*#__PURE__*/ _jsx(Typography, {
634
+ component: "span",
635
+ variant: "caption",
636
+ color: "text.secondary",
637
+ children: ` · ${fieldId}`
638
+ })
639
+ ]
640
+ }),
641
+ field.description ? /*#__PURE__*/ _jsx(Typography, {
559
642
  variant: "caption",
560
643
  color: "text.secondary",
561
- children: ` · ${fieldId}`
562
- })
644
+ noWrap: true,
645
+ children: field.description
646
+ }) : null
563
647
  ]
564
648
  }),
565
- field.description ? /*#__PURE__*/ _jsx(Typography, {
566
- variant: "caption",
567
- color: "text.secondary",
568
- noWrap: true,
569
- children: field.description
570
- }) : null
649
+ /*#__PURE__*/ _jsx(Chip, {
650
+ size: "small",
651
+ label: (_DATASET_FIELD_TYPE_LABELS_field_type = DATASET_FIELD_TYPE_LABELS[field.type]) != null ? _DATASET_FIELD_TYPE_LABELS_field_type : field.type
652
+ }),
653
+ field.required || ((_field_validation = field.validation) == null ? void 0 : _field_validation.required) ? /*#__PURE__*/ _jsx(Chip, {
654
+ size: "small",
655
+ color: "primary",
656
+ label: "required"
657
+ }) : null,
658
+ /*#__PURE__*/ _jsx(IconButton, {
659
+ size: "small",
660
+ disabled: index === 0,
661
+ onClick: moveField(fieldId, -1),
662
+ "aria-label": "Move up",
663
+ children: '▲'
664
+ }),
665
+ /*#__PURE__*/ _jsx(IconButton, {
666
+ size: "small",
667
+ disabled: index === model.order.length - 1,
668
+ onClick: moveField(fieldId, 1),
669
+ "aria-label": "Move down",
670
+ children: '▼'
671
+ }),
672
+ /*#__PURE__*/ _jsx(Button, {
673
+ size: "small",
674
+ onClick: openFieldEditor(fieldId),
675
+ children: 'Edit'
676
+ }),
677
+ /*#__PURE__*/ _jsx(Button, {
678
+ size: "small",
679
+ color: "error",
680
+ onClick: handleFieldRemove(fieldId),
681
+ children: 'Remove'
682
+ })
571
683
  ]
572
- }),
573
- /*#__PURE__*/ _jsx(Chip, {
574
- size: "small",
575
- label: (_DATASET_FIELD_TYPE_LABELS_field_type = DATASET_FIELD_TYPE_LABELS[field.type]) != null ? _DATASET_FIELD_TYPE_LABELS_field_type : field.type
576
- }),
577
- field.required || ((_field_validation = field.validation) == null ? void 0 : _field_validation.required) ? /*#__PURE__*/ _jsx(Chip, {
578
- size: "small",
579
- color: "primary",
580
- label: "required"
581
- }) : null,
582
- /*#__PURE__*/ _jsx(IconButton, {
583
- size: "small",
584
- disabled: index === 0,
585
- onClick: moveField(fieldId, -1),
586
- "aria-label": "Move up",
587
- children: '▲'
588
- }),
589
- /*#__PURE__*/ _jsx(IconButton, {
590
- size: "small",
591
- disabled: index === model.order.length - 1,
592
- onClick: moveField(fieldId, 1),
593
- "aria-label": "Move down",
594
- children: '▼'
595
- }),
596
- /*#__PURE__*/ _jsx(Button, {
597
- size: "small",
598
- onClick: openFieldEditor(fieldId),
599
- children: 'Edit'
600
- }),
601
- /*#__PURE__*/ _jsx(Button, {
602
- size: "small",
603
- color: "error",
604
- onClick: handleFieldRemove(fieldId),
605
- children: 'Remove'
606
- })
607
- ]
608
- }, fieldId);
609
- })
610
- }),
611
- /*#__PURE__*/ _jsx(Button, {
612
- size: "small",
613
- variant: "outlined",
614
- color: "primary",
615
- onClick: openFieldEditor(null),
616
- sx: {
617
- alignSelf: 'flex-start'
618
- },
619
- children: 'Add field'
684
+ }, fieldId);
685
+ })
686
+ }),
687
+ /*#__PURE__*/ _jsx(Button, {
688
+ size: "small",
689
+ variant: "outlined",
690
+ color: "primary",
691
+ onClick: openFieldEditor(null),
692
+ sx: {
693
+ alignSelf: 'flex-start'
694
+ },
695
+ children: 'Add field'
696
+ })
697
+ ]
620
698
  })
621
699
  ]
622
700
  }),
@@ -631,7 +709,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
631
709
  color: "primary",
632
710
  disabled: !dataScope,
633
711
  onClick: handleSave,
634
- children: 'Save schema'
712
+ children: sharingOnlyReason ? 'Save sharing' : 'Save schema'
635
713
  })
636
714
  ]
637
715
  })
@@ -656,7 +734,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
656
734
  /*#__PURE__*/ _jsx(TextField, {
657
735
  size: "small",
658
736
  label: "Display name",
659
- value: (_ref = editorDefinition == null ? void 0 : editorDefinition.name) != null ? _ref : '',
737
+ value: (_ref1 = editorDefinition == null ? void 0 : editorDefinition.name) != null ? _ref1 : '',
660
738
  autoFocus: true,
661
739
  onChange: (event)=>{
662
740
  const name = event.target.value;
@@ -683,7 +761,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
683
761
  /*#__PURE__*/ _jsx(TextField, {
684
762
  size: "small",
685
763
  label: "Reference ID",
686
- value: isNewField ? (_ref1 = fieldEditor == null ? void 0 : fieldEditor.idDraft) != null ? _ref1 : '' : (_ref2 = fieldEditor == null ? void 0 : fieldEditor.fieldId) != null ? _ref2 : '',
764
+ value: isNewField ? (_ref2 = fieldEditor == null ? void 0 : fieldEditor.idDraft) != null ? _ref2 : '' : (_ref3 = fieldEditor == null ? void 0 : fieldEditor.fieldId) != null ? _ref3 : '',
687
765
  disabled: !isNewField,
688
766
  onChange: (event)=>setFieldEditor((prev)=>prev ? _extends({}, prev, {
689
767
  idDraft: event.target.value,
@@ -697,7 +775,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
697
775
  label: "Description",
698
776
  multiline: true,
699
777
  minRows: 2,
700
- value: (_ref3 = editorDefinition == null ? void 0 : editorDefinition.description) != null ? _ref3 : '',
778
+ value: (_ref4 = editorDefinition == null ? void 0 : editorDefinition.description) != null ? _ref4 : '',
701
779
  onChange: (event)=>setFieldEditor((prev)=>prev ? _extends({}, prev, {
702
780
  definition: _extends({}, prev.definition, {
703
781
  description: event.target.value
@@ -709,7 +787,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
709
787
  select: true,
710
788
  size: "small",
711
789
  label: "Type",
712
- value: (editorDefinition == null ? void 0 : editorDefinition.customType) ? `custom:${editorDefinition.customType}` : (_ref4 = editorDefinition == null ? void 0 : editorDefinition.type) != null ? _ref4 : 'text',
790
+ value: (editorDefinition == null ? void 0 : editorDefinition.customType) ? `custom:${editorDefinition.customType}` : (_ref5 = editorDefinition == null ? void 0 : editorDefinition.type) != null ? _ref5 : 'text',
713
791
  onChange: (event)=>setFieldEditor((prev)=>{
714
792
  if (!prev) return prev;
715
793
  const selected = event.target.value;
@@ -784,7 +862,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
784
862
  select: true,
785
863
  size: "small",
786
864
  label: "Target collection",
787
- value: (_ref5 = editorDefinition == null ? void 0 : (_editorDefinition_reference = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference.datasetId) != null ? _ref5 : '',
865
+ value: (_ref6 = editorDefinition == null ? void 0 : (_editorDefinition_reference = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference.datasetId) != null ? _ref6 : '',
788
866
  onChange: (event)=>setFieldEditor((prev)=>{
789
867
  var _prev_definition_reference;
790
868
  return prev ? _extends({}, prev, {
@@ -805,7 +883,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
805
883
  // scope would silently blank an existing field.
806
884
  .filter((target)=>{
807
885
  var _editorDefinition_reference;
808
- return target.$id === (editorDefinition == null ? void 0 : (_editorDefinition_reference = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference.datasetId) || scopeCovers(target.visibleTo, visibleTo);
886
+ return target.$id === (editorDefinition == null ? void 0 : (_editorDefinition_reference = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference.datasetId) || sharingCovers(target, sharingPreview);
809
887
  }).map((target)=>/*#__PURE__*/ _jsx(MenuItem, {
810
888
  value: target.$id,
811
889
  children: datasetDisplayName(target) || target.$id
@@ -818,7 +896,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
818
896
  // is not any more.
819
897
  const targetId = editorDefinition == null ? void 0 : (_editorDefinition_reference = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference.datasetId;
820
898
  const target = (datasets != null ? datasets : []).find((d)=>d.$id === targetId);
821
- if (!target || scopeCovers(target.visibleTo, visibleTo)) {
899
+ if (!target || sharingCovers(target, sharingPreview)) {
822
900
  return null;
823
901
  }
824
902
  return /*#__PURE__*/ _jsx(Alert, {
@@ -830,7 +908,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
830
908
  select: true,
831
909
  size: "small",
832
910
  label: "Display field",
833
- value: (_ref6 = editorDefinition == null ? void 0 : (_editorDefinition_reference1 = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference1.displayFieldId) != null ? _ref6 : '',
911
+ value: (_ref7 = editorDefinition == null ? void 0 : (_editorDefinition_reference1 = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference1.displayFieldId) != null ? _ref7 : '',
834
912
  onChange: (event)=>setFieldEditor((prev)=>(prev == null ? void 0 : prev.definition.reference) ? _extends({}, prev, {
835
913
  definition: _extends({}, prev.definition, {
836
914
  reference: _extends({}, prev.definition.reference, {
@@ -851,7 +929,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
851
929
  select: true,
852
930
  size: "small",
853
931
  label: "When a referenced document is deleted",
854
- value: (_ref7 = editorDefinition == null ? void 0 : (_editorDefinition_reference2 = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference2.onDelete) != null ? _ref7 : 'setNull',
932
+ value: (_ref8 = editorDefinition == null ? void 0 : (_editorDefinition_reference2 = editorDefinition.reference) == null ? void 0 : _editorDefinition_reference2.onDelete) != null ? _ref8 : 'setNull',
855
933
  onChange: (event)=>setFieldEditor((prev)=>(prev == null ? void 0 : prev.definition.reference) ? _extends({}, prev, {
856
934
  definition: _extends({}, prev.definition, {
857
935
  reference: _extends({}, prev.definition.reference, {
@@ -901,7 +979,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
901
979
  /*#__PURE__*/ _jsx(TextField, {
902
980
  size: "small",
903
981
  label: "Default value",
904
- value: String((_ref8 = editorDefinition == null ? void 0 : editorDefinition.default) != null ? _ref8 : ''),
982
+ value: String((_ref9 = editorDefinition == null ? void 0 : editorDefinition.default) != null ? _ref9 : ''),
905
983
  onChange: (event)=>setFieldEditor((prev)=>prev ? _extends({}, prev, {
906
984
  definition: _extends({}, prev.definition, event.target.value ? {
907
985
  default: event.target.value
@@ -916,7 +994,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
916
994
  /*#__PURE__*/ _jsx(TextField, {
917
995
  size: "small",
918
996
  label: "Pattern (regex)",
919
- value: (_ref9 = editorDefinition == null ? void 0 : (_editorDefinition_validation = editorDefinition.validation) == null ? void 0 : _editorDefinition_validation.regex) != null ? _ref9 : '',
997
+ value: (_ref10 = editorDefinition == null ? void 0 : (_editorDefinition_validation = editorDefinition.validation) == null ? void 0 : _editorDefinition_validation.regex) != null ? _ref10 : '',
920
998
  onChange: (event)=>setFieldEditor((prev)=>{
921
999
  var _prev_definition_validation;
922
1000
  return prev ? _extends({}, prev, {
@@ -933,7 +1011,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, isRecordAddressField } from "../record-
933
1011
  /*#__PURE__*/ _jsx(TextField, {
934
1012
  size: "small",
935
1013
  label: "Options",
936
- value: (_ref10 = fieldEditor == null ? void 0 : fieldEditor.optionsText) != null ? _ref10 : '',
1014
+ value: (_ref11 = fieldEditor == null ? void 0 : fieldEditor.optionsText) != null ? _ref11 : '',
937
1015
  onChange: (event)=>setFieldEditor((prev)=>prev ? _extends({}, prev, {
938
1016
  optionsText: event.target.value
939
1017
  }) : prev),