@evoke-platform/context 1.0.0-dev.101 → 1.0.0-dev.102

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 CHANGED
@@ -160,7 +160,16 @@ Returns a function that can be used to navigate to another page. The returned fu
160
160
 
161
161
  #### `useApp()`
162
162
 
163
- Returns the currently loaded Evoke app.
163
+ Returns the currently loaded Evoke App as well as the following function.
164
+
165
+ - `findDefaultPageSlugFor`: An asynchronous function that takes an `objectId` as a parameter and returns the default page slug for that object, if no default page slug is found and the object is a subtype, the page slug of the first ancestor with a default page will be used. It returns `undefined` if no default page slug is found for any ancestor type.
166
+
167
+ Example usage:
168
+
169
+ ```javascript
170
+ const { id: appId, findDefaultPageSlugFor } = useApp();
171
+ const defaultPageSlug = await findDefaultPageSlugFor(objectId);
172
+ ```
164
173
 
165
174
  ### REST API calls
166
175
 
@@ -38,10 +38,19 @@ export type NavigationItem = {
38
38
  pageId: string;
39
39
  pageName: string;
40
40
  };
41
+ export type AppExtended = App & {
42
+ /**
43
+ * Looks up the default page slug for a given object or its nearest type ancestor.
44
+ *
45
+ * @param {string} objectId - The ID of the object to start the search from.
46
+ * @returns {Promise<string | undefined>} The default page slug, or `undefined` if no default page is found.
47
+ */
48
+ findDefaultPageSlugFor: (objectId: string) => Promise<string | undefined>;
49
+ };
41
50
  export type AppProviderProps = {
42
51
  app: App;
43
52
  children?: ReactNode;
44
53
  };
45
54
  declare function AppProvider(props: AppProviderProps): import("react/jsx-runtime").JSX.Element;
46
- export declare function useApp(): App;
55
+ export declare function useApp(): AppExtended;
47
56
  export default AppProvider;
@@ -1,13 +1,62 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import { jsx as _jsx } from "react/jsx-runtime";
2
11
  // Copyright (c) 2023 System Automation Corporation.
3
12
  // This file is licensed under the MIT License.
4
- import { createContext, useContext } from 'react';
5
- const defaultApp = { id: '_evoke', name: 'Evoke Platform', type: 'public' };
6
- const AppContext = createContext(defaultApp);
13
+ import { createContext, useCallback, useContext } from 'react';
14
+ import { useApiServices } from '../api';
15
+ const defaultApp = {
16
+ id: '_evoke',
17
+ name: 'Evoke Platform',
18
+ type: 'public',
19
+ };
20
+ const defaultAppExtended = Object.assign(Object.assign({}, defaultApp), { findDefaultPageSlugFor: (objectId) => Promise.resolve(undefined) });
21
+ const AppContext = createContext(defaultAppExtended);
7
22
  AppContext.displayName = 'AppContext';
8
23
  function AppProvider(props) {
9
24
  const { app, children } = props;
10
- return _jsx(AppContext.Provider, { value: app, children: children });
25
+ const apiServices = useApiServices();
26
+ const appExtended = Object.assign(Object.assign({}, app), { findDefaultPageSlugFor: useCallback((objectId) => __awaiter(this, void 0, void 0, function* () {
27
+ var _a, _b, _c, _d;
28
+ let defaultPageId;
29
+ let currentObjectId = objectId;
30
+ while (currentObjectId !== undefined) {
31
+ if ((_a = app.defaultPages) === null || _a === void 0 ? void 0 : _a[currentObjectId]) {
32
+ defaultPageId = app.defaultPages[currentObjectId];
33
+ break;
34
+ }
35
+ const effectiveObject = yield apiServices.get(`data/objects/${currentObjectId}/effective`, {
36
+ params: { filter: { fields: ['baseObject'] } },
37
+ });
38
+ currentObjectId = (_c = (_b = effectiveObject === null || effectiveObject === void 0 ? void 0 : effectiveObject.baseObject) === null || _b === void 0 ? void 0 : _b.objectId) !== null && _c !== void 0 ? _c : undefined;
39
+ }
40
+ let defaultPage;
41
+ if (defaultPageId) {
42
+ const pageId = defaultPageId.includes('/')
43
+ ? defaultPageId.split('/').slice(2).join('/')
44
+ : defaultPageId;
45
+ try {
46
+ defaultPage = yield apiServices.get(`/webContent/apps/${app.id}/pages/${encodeURIComponent(encodeURIComponent(pageId))}`);
47
+ }
48
+ catch (error) {
49
+ const err = error;
50
+ if (((_d = err.response) === null || _d === void 0 ? void 0 : _d.status) === 404) {
51
+ defaultPage = undefined;
52
+ }
53
+ }
54
+ }
55
+ if (defaultPage === null || defaultPage === void 0 ? void 0 : defaultPage.slug) {
56
+ return defaultPage.slug;
57
+ }
58
+ }), [app]) });
59
+ return _jsx(AppContext.Provider, { value: appExtended, children: children });
11
60
  }
12
61
  export function useApp() {
13
62
  return useContext(AppContext);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evoke-platform/context",
3
- "version": "1.0.0-dev.101",
3
+ "version": "1.0.0-dev.102",
4
4
  "description": "Utilities that provide context to Evoke platform widgets",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",