@protobase/ui 0.22.1 → 0.22.2

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.
@@ -16,6 +16,6 @@ const listHandOrder = (model, permissions) => model.position?.within === void 0
16
16
  * The field a related section sorts its records by hand in: those of one parent, when the section lists every record
17
17
  * whose `within` relation points at the page's record, no filter or join resource leaving some out.
18
18
  */
19
- const relatedHandOrder = (item, model, permissions) => model.position?.within !== void 0 && item.field === model.position.within && !item.through && !item.filter ? writablePosition(model, permissions) : void 0;
19
+ const relatedHandOrder = (item, model, permissions) => model.position?.within !== void 0 && item.field === model.position.within && !item.through && !item.from && !item.filter ? writablePosition(model, permissions) : void 0;
20
20
  //#endregion
21
21
  export { listHandOrder, relatedHandOrder };
@@ -11,7 +11,7 @@ const where$1 = createWhere();
11
11
  */
12
12
  const incomingRelations = (model, resources, views, layout, lines) => {
13
13
  if (model.primaryKey.length !== 1) return [];
14
- const listed = new Set(layoutItems(layout).flatMap((item) => item.kind === "related" && !item.through ? [`${item.resource}.${item.field}`] : []));
14
+ const listed = new Set(layoutItems(layout).flatMap((item) => item.kind === "related" && !item.through && !item.from ? [`${item.resource}.${item.field}`] : []));
15
15
  const found = Object.values(resources).flatMap((source) => Object.values(source.fields).filter((field) => field.type === "relation" && field.relation?.resource === model.name && field.relation.columns.length === 1 && field.filterable).filter((field) => field.name !== source.tenant && source.name !== lines?.name && !listed.has(`${source.name}.${field.name}`)).map((field) => ({
16
16
  source,
17
17
  field: field.name
@@ -59,6 +59,7 @@ import { useRecordEditor } from "./use-record-editor.js";
59
59
  import { useRelatedRecords } from "./use-related-records.js";
60
60
  import { IncomingCard } from "./incoming-card.js";
61
61
  import { RelatedSection } from "./related-section.js";
62
+ import { relatedValue } from "./related-value.js";
62
63
  import { Fragment, useMemo } from "react";
63
64
  import { Check } from "lucide-react";
64
65
  import { isApiError } from "@protobase/client";
@@ -361,11 +362,14 @@ const LoadedRecord = ({ model, recordKey, stored, preview }) => {
361
362
  children
362
363
  }, `group:${item.title}`)];
363
364
  }
364
- if (item.kind === "related") return [/* @__PURE__ */ jsx(RelatedSection, {
365
- item,
366
- recordKey,
367
- owner: model.name
368
- }, `related:${item.title}`)];
365
+ if (item.kind === "related") {
366
+ if (item.from !== void 0 && !known(item.from)) return [];
367
+ return [/* @__PURE__ */ jsx(RelatedSection, {
368
+ item,
369
+ value: relatedValue(item, stored.record, recordKey),
370
+ owner: model.name
371
+ }, `related:${item.title}`)];
372
+ }
369
373
  if (preview && (item.kind === "custom" || item.kind === "map" || item.kind === "gallery")) return [];
370
374
  if (item.kind === "custom") return item.sidebar ? [] : [/* @__PURE__ */ jsx(CustomSection, {
371
375
  item,
@@ -28,13 +28,13 @@ const and = (...parts) => {
28
28
  * all the join records that point at this record hold, read page by page. `null` when no join record does, so nothing
29
29
  * can match.
30
30
  */
31
- const useMatch = (item, recordKey) => {
31
+ const useMatch = (item, value) => {
32
32
  const client = useClient();
33
33
  const { resources } = useAdminMeta();
34
34
  const through = item.through;
35
35
  const via = through ? resources[through.resource] : void 0;
36
36
  const key = through?.key ?? via?.primaryKey[0];
37
- const viaFilter = through ? and(printFilter(where$1.eq(through.field, recordKey)), through.filter) : "";
37
+ const viaFilter = through ? and(printFilter(where$1.eq(through.field, value)), through.filter) : "";
38
38
  const collected = useQuery({
39
39
  queryKey: keys.list(via?.name ?? "", viaFilter, "", 0, `related:${key}`),
40
40
  enabled: Boolean(via && key),
@@ -46,8 +46,13 @@ const useMatch = (item, recordKey) => {
46
46
  return [...new Set(joined.items.flatMap((row) => row[key] === null || row[key] === void 0 ? [] : [String(row[key])]))];
47
47
  }
48
48
  });
49
+ if (value === null) return {
50
+ filter: null,
51
+ isPending: false,
52
+ error: null
53
+ };
49
54
  if (!through) return {
50
- filter: and(printFilter(where$1.eq(item.field, recordKey)), item.filter),
55
+ filter: and(printFilter(where$1.eq(item.field, value)), item.filter),
51
56
  isPending: false,
52
57
  error: null
53
58
  };
@@ -75,11 +80,11 @@ const Empty = ({ text }) => /* @__PURE__ */ jsx("p", {
75
80
  * a join resource, in the list page's table or display, with a link to them all in the list. When they are ordered by
76
81
  * hand within this record, **Sort** shows them all in their order, to be moved by their drag handles.
77
82
  */
78
- const RelatedSection = ({ item, recordKey, owner }) => {
83
+ const RelatedSection = ({ item, value, owner }) => {
79
84
  const { resources, views } = useAdminMeta();
80
85
  const model = resources[item.resource];
81
86
  const view = model && views[model.name];
82
- const match = useMatch(item, recordKey);
87
+ const match = useMatch(item, value);
83
88
  const [display, pick] = useEmbeddedDisplay(view, `related:${owner}:${item.title}`, item.display);
84
89
  const permissions = usePermissions(item.resource);
85
90
  const [sorting, setSorting] = useState(false);
@@ -116,7 +121,7 @@ const RelatedSection = ({ item, recordKey, owner }) => {
116
121
  model,
117
122
  view,
118
123
  field: handOrder,
119
- filter: printFilter(where$1.eq(item.field, recordKey)),
124
+ filter: printFilter(where$1.eq(item.field, value)),
120
125
  columns
121
126
  })
122
127
  });
@@ -0,0 +1,14 @@
1
+ import { relationTarget } from "./related-opens.js";
2
+ //#region src/app/related-value.ts
3
+ /**
4
+ * The value a related section's `field` is matched against: the record's key, or with `from` that field's value as it
5
+ * is (a relation by its key). Null when the record holds none, so the section lists nothing.
6
+ */
7
+ const relatedValue = (item, record, recordKey) => {
8
+ if (item.from === void 0) return recordKey;
9
+ const value = record[item.from];
10
+ if (typeof value === "boolean") return value;
11
+ return relationTarget(record, item.from) ?? null;
12
+ };
13
+ //#endregion
14
+ export { relatedValue };
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.22.1";
2
+ var version = "0.22.2";
3
3
  var protobase = { "pluginApi": 1 };
4
4
  //#endregion
5
5
  export { protobase, version };