@trackunit/react-core-contexts 0.3.14 → 0.4.1

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/index.js CHANGED
@@ -5,12 +5,11 @@ import { onError } from '@apollo/client/link/error';
5
5
  import * as Sentry from '@sentry/browser';
6
6
  import { useEnvironment, useToken, ToastProvider, AssetSortingProvider, EnvironmentContextProvider, GlobalSelectionProvider, TokenProvider, CurrentUserProvider, UserSubscriptionProvider as UserSubscriptionProvider$1, useURLSynchronization } from '@trackunit/react-core-hooks';
7
7
  import { createUploadLink } from 'apollo-upload-client';
8
- import { ToastRuntime, AssetSortingRuntime, EnvironmentRuntime, GlobalSelectionRuntime, TokenRuntime, CurrentUserRuntime, UserSubscriptionRuntime } from '@trackunit/iris-app-runtime-core';
8
+ import { ToastRuntime, setupHostConnector, AssetSortingRuntime, EnvironmentRuntime, GlobalSelectionRuntime, TokenRuntime, CurrentUserRuntime, UserSubscriptionRuntime } from '@trackunit/iris-app-runtime-core';
9
9
  import * as React from 'react';
10
- import { useMemo, useState, useEffect, useCallback } from 'react';
10
+ import { useMemo, useEffect, useState } from 'react';
11
11
  import { registerTranslations, initializeTranslationsForApp } from '@trackunit/i18n-library-translation';
12
12
  import { Spinner } from '@trackunit/react-components';
13
- import { SortOrder } from '@trackunit/react-core-contexts-api';
14
13
  import { BrowserRouter } from 'react-router-dom';
15
14
 
16
15
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -2447,6 +2446,21 @@ for (var COLLECTION_NAME in DOMIterables) {
2447
2446
 
2448
2447
  handlePrototype(DOMTokenListPrototype, 'DOMTokenList');
2449
2448
 
2449
+ /**
2450
+ * Subscribe to methods initiated by changes in the host
2451
+ *
2452
+ * @param methods STABLE object with the methods you'd want to subscribe to. Wrap in useMemo for instance to make it stable
2453
+ */
2454
+
2455
+ const useSubscribeToHostChanges = methods => {
2456
+ useEffect(() => {
2457
+ const connection = setupHostConnector(methods);
2458
+ return () => {
2459
+ connection.destroy();
2460
+ };
2461
+ }, [methods]);
2462
+ };
2463
+
2450
2464
  const TileAssetSortingProvider = ({
2451
2465
  children
2452
2466
  }) => {
@@ -2454,21 +2468,14 @@ const TileAssetSortingProvider = ({
2454
2468
  useEffect(() => {
2455
2469
  AssetSortingRuntime.getAssetSortingState().catch(() => null).then(setAssetSortingState);
2456
2470
  }, []);
2457
- const setSortingState = useCallback((...args) => {
2458
- // TODO when we can trigger a reload of the state from the host, remove this logic that updates the local state
2459
- AssetSortingRuntime.setAssetSortingState(...args).then(() => {
2460
- var _a;
2461
-
2462
- setAssetSortingState({
2463
- sortBy: args[0],
2464
- order: (_a = args[1]) !== null && _a !== void 0 ? _a : SortOrder.Desc
2465
- });
2466
- });
2467
- }, []);
2471
+ const methods = useMemo(() => ({
2472
+ onAssetSortingStateChanged: setAssetSortingState
2473
+ }), [setAssetSortingState]);
2474
+ useSubscribeToHostChanges(methods);
2468
2475
  const contextValue = useMemo(() => ({
2469
2476
  sortingState: assetSortingState,
2470
- setSortBy: setSortingState
2471
- }), [assetSortingState, setSortingState]);
2477
+ setSortBy: AssetSortingRuntime.setAssetSortingState
2478
+ }), [assetSortingState]);
2472
2479
 
2473
2480
  if (!assetSortingState) {
2474
2481
  return null;
@@ -2517,6 +2524,10 @@ const TileGlobalSelectionProvider = ({
2517
2524
  useEffect(() => {
2518
2525
  GlobalSelectionRuntime.getGlobalSelection().catch(() => null).then(setGlobalSelectionContext);
2519
2526
  }, []);
2527
+ const methods = useMemo(() => ({
2528
+ onGlobalSelectionChanged: setGlobalSelectionContext
2529
+ }), [setGlobalSelectionContext]);
2530
+ useSubscribeToHostChanges(methods);
2520
2531
 
2521
2532
  if (!GlobalSelectionContext) {
2522
2533
  return null;
@@ -2544,6 +2555,10 @@ const TileTokenProvider = ({
2544
2555
 
2545
2556
  updateContext();
2546
2557
  }, []);
2558
+ const methods = React.useMemo(() => ({
2559
+ onTokenChanged: setTokenContext
2560
+ }), [setTokenContext]);
2561
+ useSubscribeToHostChanges(methods);
2547
2562
 
2548
2563
  if (!tokenContext) {
2549
2564
  return null;
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts",
3
- "version": "0.3.14",
3
+ "version": "0.4.1",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "dependencies": {
7
7
  "graphql": "^15.8.0",
8
- "@trackunit/iris-app-runtime-core": "0.2.6",
8
+ "@trackunit/iris-app-runtime-core": "0.3.0",
9
9
  "@trackunit/react-core-contexts-api": "0.2.3",
10
- "@trackunit/react-core-contexts-test": "0.1.7",
11
- "@trackunit/react-core-hooks": "0.2.7",
10
+ "@trackunit/react-core-contexts-test": "0.1.8",
11
+ "@trackunit/react-core-hooks": "0.2.8",
12
12
  "react": "17.0.2",
13
13
  "@apollo/client": "3.6.9",
14
14
  "@sentry/browser": "^7.15.0",
15
15
  "apollo-upload-client": "^14.1.3",
16
16
  "@trackunit/i18n-library-translation": "0.0.10",
17
- "@trackunit/react-components": "0.0.194",
17
+ "@trackunit/react-components": "0.0.196",
18
18
  "react-router-dom": "6.4.1"
19
19
  },
20
20
  "module": "./index.js",
@@ -0,0 +1,7 @@
1
+ import { setupHostConnector } from "@trackunit/iris-app-runtime-core";
2
+ /**
3
+ * Subscribe to methods initiated by changes in the host
4
+ *
5
+ * @param methods STABLE object with the methods you'd want to subscribe to. Wrap in useMemo for instance to make it stable
6
+ */
7
+ export declare const useSubscribeToHostChanges: (methods: Parameters<typeof setupHostConnector>[0]) => void;