@protobase/ui 0.46.6 → 0.46.7

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.
@@ -0,0 +1,25 @@
1
+ import { createWhere, printFilter } from "@protobase/schema";
2
+ //#region src/app/record-elsewhere.ts
3
+ const w = createWhere();
4
+ /**
5
+ * The list request that finds a record the session's organization does not have, and the organization it is in: for a
6
+ * resource whose lists read across the person's organizations (`tenantAcross`), with a key of one field they can
7
+ * filter on. `undefined` for any other resource, whose record is simply not found.
8
+ */
9
+ const elsewhereRequest = (model, recordKey) => {
10
+ const [key, ...more] = model.primaryKey;
11
+ if (!model.tenantAcross || model.tenant === void 0 || key === void 0 || more.length > 0 || !model.fields[key]?.filterable) return void 0;
12
+ return {
13
+ filter: printFilter(w.eq(key, recordKey)),
14
+ pageSize: 1,
15
+ fields: [key, model.tenant]
16
+ };
17
+ };
18
+ /** The organization a found row is in, when it is another than `current`; `undefined` when it is not found or is there. */
19
+ const otherOrganization = (model, row, current) => {
20
+ const tenant = row?.[model.tenant];
21
+ if (tenant === void 0 || tenant === null || String(tenant) === current) return void 0;
22
+ return String(tenant);
23
+ };
24
+ //#endregion
25
+ export { elsewhereRequest, otherOrganization };
@@ -9,6 +9,7 @@ const text = defineMessages({
9
9
  loading: "Loading",
10
10
  fetching: "Fetching the record.",
11
11
  noRecord: "This record does not exist, or you cannot see it.",
12
+ openingElsewhere: "Opening the record in its organization.",
12
13
  couldNotLoad: "Could not load the record",
13
14
  someoneElse: "Someone else",
14
15
  justNow: "just now",
@@ -45,6 +46,7 @@ const text = defineMessages({
45
46
  loading: "Laden",
46
47
  fetching: "Het record wordt opgehaald.",
47
48
  noRecord: "Dit record bestaat niet, of je mag het niet zien.",
49
+ openingElsewhere: "Het record wordt in zijn organisatie geopend.",
48
50
  couldNotLoad: "Kon het record niet laden",
49
51
  someoneElse: "Iemand anders",
50
52
  justNow: "zojuist",
@@ -61,6 +61,7 @@ import { HistorySection } from "./history/history-section.js";
61
61
  import { CommentsSection } from "./comments/comments-section.js";
62
62
  import { useShownLayout } from "./record-mode/use-shown-layout.js";
63
63
  import { useRecordEditor } from "./use-record-editor.js";
64
+ import { useRecordElsewhere } from "./use-record-elsewhere.js";
64
65
  import { useRelatedRecords } from "./use-related-records.js";
65
66
  import { IncomingCard } from "./incoming-card.js";
66
67
  import { RelatedSection } from "./related-section.js";
@@ -113,9 +114,9 @@ const RecordPage = ({ resource, recordKey }) => {
113
114
  });
114
115
  if (record.error) return /* @__PURE__ */ jsx("div", {
115
116
  className: "p-8",
116
- children: isApiError(record.error) && record.error.status === 404 ? /* @__PURE__ */ jsx(Notice, {
117
- title: text("notFound"),
118
- children: text$1("noRecord")
117
+ children: isApiError(record.error) && record.error.status === 404 ? /* @__PURE__ */ jsx(RecordNotFound, {
118
+ model,
119
+ recordKey
119
120
  }) : /* @__PURE__ */ jsx(ErrorBanner, {
120
121
  title: text$1("couldNotLoad"),
121
122
  error: record.error
@@ -128,6 +129,25 @@ const RecordPage = ({ resource, recordKey }) => {
128
129
  preview: record.isPlaceholderData
129
130
  }, record.isPlaceholderData ? "preview" : "loaded");
130
131
  };
132
+ const RecordNotFound = ({ model, recordKey }) => {
133
+ const elsewhere = useRecordElsewhere(model, recordKey);
134
+ if (elsewhere.state === "looking") return /* @__PURE__ */ jsx(Notice, {
135
+ title: text$1("loading"),
136
+ children: text$1("fetching")
137
+ });
138
+ if (elsewhere.state === "switching") return /* @__PURE__ */ jsx(Notice, {
139
+ title: text$1("loading"),
140
+ children: text$1("openingElsewhere")
141
+ });
142
+ if (elsewhere.state === "failed") return /* @__PURE__ */ jsx(ErrorBanner, {
143
+ title: text$1("couldNotLoad"),
144
+ error: elsewhere.error
145
+ });
146
+ return /* @__PURE__ */ jsx(Notice, {
147
+ title: text("notFound"),
148
+ children: text$1("noRecord")
149
+ });
150
+ };
131
151
  const LoadedRecord = ({ model, recordKey, stored, preview }) => {
132
152
  const meta = useAdminMeta();
133
153
  const { address, go } = useAppAddress();
@@ -0,0 +1,43 @@
1
+ import { useAuth } from "../auth/auth-provider.js";
2
+ import { useClient } from "../data/api-provider.js";
3
+ import { elsewhereRequest, otherOrganization } from "./record-elsewhere.js";
4
+ import { useEffect, useMemo, useState } from "react";
5
+ //#region src/app/use-record-elsewhere.ts
6
+ /**
7
+ * For a record not found in the session's organization: looks for it across the person's organizations, and when it is
8
+ * in another one, switches the session there and opens the page again, so the record and everything on its page are
9
+ * read in its own organization. `missing` when it is nowhere else, or the resource does not list across organizations.
10
+ */
11
+ const useRecordElsewhere = (model, recordKey) => {
12
+ const client = useClient();
13
+ const { session, organizations } = useAuth();
14
+ const request = useMemo(() => organizations ? elsewhereRequest(model, recordKey) : void 0, [
15
+ organizations,
16
+ model,
17
+ recordKey
18
+ ]);
19
+ const [found, setFound] = useState(request ? { state: "looking" } : { state: "missing" });
20
+ useEffect(() => {
21
+ if (!request) return;
22
+ const look = async () => {
23
+ const [page, current] = await Promise.all([client.list(model.name, request), session.organizations.current()]);
24
+ const other = otherOrganization(model, page.items[0], current.organization?.id);
25
+ if (other === void 0) return setFound({ state: "missing" });
26
+ setFound({ state: "switching" });
27
+ await session.organizations.switchTo(other);
28
+ window.location.reload();
29
+ };
30
+ look().catch((error) => setFound({
31
+ state: "failed",
32
+ error
33
+ }));
34
+ }, [
35
+ client,
36
+ session,
37
+ model,
38
+ request
39
+ ]);
40
+ return found;
41
+ };
42
+ //#endregion
43
+ export { useRecordElsewhere };
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.46.6";
2
+ var version = "0.46.7";
3
3
  var protobase = { "pluginApi": 1 };
4
4
  //#endregion
5
5
  export { protobase, version };