@equinor/fusion-framework-react-app 5.3.0 → 5.4.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.
@@ -0,0 +1,3 @@
1
+ export { useAppSetting } from './useAppSetting';
2
+ export { useAppSettings } from './useAppSettings';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/settings/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,77 @@
1
+ import { useCallback, useLayoutEffect, useMemo, useState } from 'react';
2
+ import { BehaviorSubject, map } from 'rxjs';
3
+ import { useCurrentApp } from '@equinor/fusion-framework-react/app';
4
+ import { useAppSettingsStatus } from './useAppSettingsStatus';
5
+ import { useObservableState } from '@equinor/fusion-observable/react';
6
+ /**
7
+ * Custom hook to manage application settings.
8
+ *
9
+ * @template TSettings - The type of the settings object. Defaults to `AppSettings`.
10
+ * @template TProp - The type of the property key in the settings object. Defaults to `keyof TSettings`.
11
+ *
12
+ * @param {TProp} prop - The property key in the settings object to manage.
13
+ * @param {TSettings[TProp]} [defaultValue] - The default value for the setting.
14
+ * @param hooks - Optional hooks to handle the status changes and errors.
15
+ *
16
+ * @returns {Array} An array containing:
17
+ * - `setting`: The current setting value or undefined.
18
+ * - `setSetting`: A function to update the setting.
19
+ *
20
+ * @example
21
+ * const { setting, setSetting } = useAppSetting('theme');
22
+ *
23
+ * @example
24
+ * // with default value
25
+ * const { setting, setSetting } = useAppSetting('theme', 'dark');
26
+ *
27
+ * @example
28
+ * // with hooks
29
+ * const [isLoading, setIsLoading] = useState(false);
30
+ * const [isUpdating, setIsUpdating] = useState(false);
31
+ * const [error, setError] = useState<Error | null>(null);
32
+ *
33
+ * const { setting, setSetting } = useAppSetting('theme', 'dark', {
34
+ * onLoading: setIsLoading,
35
+ * onUpdating: setIsUpdating,
36
+ * onError: setError,
37
+ * onUpdated: useCallback(() => console.log('Settings updated'), [])
38
+ * });
39
+ */
40
+ export const useAppSetting = (prop, defaultValue, hooks) => {
41
+ const [{ onError, onUpdated, onLoading, onUpdating }] = useState(() => hooks !== null && hooks !== void 0 ? hooks : {});
42
+ const { currentApp = null } = useCurrentApp();
43
+ // create a subject to manage the setting value
44
+ const subject = useMemo(() => {
45
+ return new BehaviorSubject(defaultValue);
46
+ // Only create a new subject when the current app changes
47
+ // eslint-disable-next-line react-hooks/exhaustive-deps
48
+ }, [currentApp]);
49
+ useLayoutEffect(() => {
50
+ const sub = currentApp === null || currentApp === void 0 ? void 0 : currentApp.settings$.pipe(map((settings) => settings[prop])).subscribe(subject);
51
+ return () => sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
52
+ }, [currentApp, subject, prop]);
53
+ // subscribe to the setting value
54
+ const { value: setting } = useObservableState(subject);
55
+ // update function
56
+ const setSetting = useCallback((update) => {
57
+ if (!currentApp) {
58
+ return onError === null || onError === void 0 ? void 0 : onError(new Error('App is not available'));
59
+ }
60
+ // resolve setting value with the provided value or function
61
+ const value = typeof update === 'function'
62
+ ? update(subject.value)
63
+ : update;
64
+ currentApp.updateSetting(prop, value).subscribe({
65
+ error: onError,
66
+ complete: onUpdated,
67
+ });
68
+ }, [currentApp, subject, prop, onError, onUpdated]);
69
+ // status hooks
70
+ useAppSettingsStatus(currentApp, {
71
+ onLoading,
72
+ onUpdating,
73
+ });
74
+ return [setting, setSetting];
75
+ };
76
+ export default useAppSetting;
77
+ //# sourceMappingURL=useAppSetting.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAppSetting.js","sourceRoot":"","sources":["../../../src/settings/useAppSetting.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,GA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AAC;IAEvD,kBAAkB;IAClB,MAAM,UAAU,GAAG,WAAW,CAC1B,CAAC,MAAkE,EAAE,EAAE;QACnE,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,OAAO,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAG,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,CAAC;QACxD,CAAC;QAED,4DAA4D;QAC5D,MAAM,KAAK,GACP,OAAO,MAAM,KAAK,UAAU;YACxB,CAAC,CAAE,MAAkD,CAAC,OAAO,CAAC,KAAK,CAAC;YACpE,CAAC,CAAC,MAAM,CAAC;QAEjB,UAAU,CAAC,aAAa,CAAmB,IAAI,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC;YAC9D,KAAK,EAAE,OAAO;YACd,QAAQ,EAAE,SAAS;SACtB,CAAC,CAAC;IACP,CAAC,EACD,CAAC,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC,CAClD,CAAC;IAEF,eAAe;IACf,oBAAoB,CAAC,UAAU,EAAE;QAC7B,SAAS;QACT,UAAU;KACb,CAAC,CAAC;IAEH,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;AACjC,CAAC,CAAC;AAEF,eAAe,aAAa,CAAC"}
@@ -0,0 +1,75 @@
1
+ import { useCallback, useLayoutEffect, useMemo } from 'react';
2
+ import { BehaviorSubject } from 'rxjs';
3
+ import { useCurrentApp } from '@equinor/fusion-framework-react/app';
4
+ import { useObservableState } from '@equinor/fusion-observable/react';
5
+ import { useAppSettingsStatus } from './useAppSettingsStatus';
6
+ /**
7
+ * Custom hook to manage application settings.
8
+ *
9
+ * @template TSettings - The type of the settings object, extending Record<string, any>. Defaults to AppSettings.
10
+ *
11
+ * @param {TSettings} [defaultValue] - The default value for the settings.
12
+ * @param hooks - Optional hooks to handle the status changes and errors.
13
+ *
14
+ * @note
15
+ * `defaultValue` will only be used on the first render.
16
+ * `hooks`must be memoized to avoid unnecessary re-renders.
17
+ *
18
+ * @returns {Array} An array containing:
19
+ * - `settings`: The current settings object.
20
+ * - `setSettings`: A function to update the settings.
21
+ *
22
+ * @example
23
+ * const [settings, setSettings] = useAppSettings();
24
+ *
25
+ * @example
26
+ * const [settings, setSettings] = useAppSettings({ theme: 'dark' });
27
+ *
28
+ * @example
29
+ * const [isLoading, setIsLoading] = useState(false);
30
+ * const [isUpdating, setIsUpdating] = useState(false);
31
+ * const [error, setError] = useState<Error | null>(null);
32
+ *
33
+ * const onUpdated = useCallback(() => console.log('Settings updated'), []);
34
+ *
35
+ * const [settings, setSettings] = useAppSettings({ theme: 'dark' }, {
36
+ * onLoading: setIsLoading,
37
+ * onUpdating: setIsUpdating,
38
+ * onError: setError,
39
+ * onUpdated,
40
+ * });
41
+ */
42
+ export const useAppSettings = (defaultValue, hooks) => {
43
+ const { onError, onUpdated, onLoading, onUpdating } = hooks !== null && hooks !== void 0 ? hooks : {};
44
+ const { currentApp = null } = useCurrentApp();
45
+ const subject = useMemo(() => {
46
+ return new BehaviorSubject(defaultValue !== null && defaultValue !== void 0 ? defaultValue : {});
47
+ // Only create a new subject when the current app changes
48
+ // eslint-disable-next-line react-hooks/exhaustive-deps
49
+ }, [currentApp]);
50
+ // connect the subject to the current app settings stream
51
+ useLayoutEffect(() => {
52
+ const sub = (currentApp === null || currentApp === void 0 ? void 0 : currentApp.settings$).subscribe(subject);
53
+ return () => sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
54
+ }, [currentApp, subject]);
55
+ // subscribe to the subject to get the latest settings
56
+ const { value: settings } = useObservableState(subject, { initial: defaultValue });
57
+ const setSettings = useCallback((update) => {
58
+ if (!currentApp) {
59
+ return onError === null || onError === void 0 ? void 0 : onError(new Error('App is not available'));
60
+ }
61
+ // resolve settings with the provided value or function
62
+ const settings = typeof update === 'function' ? update(subject.value) : update;
63
+ currentApp.updateSettings(settings).subscribe({
64
+ next: () => {
65
+ onUpdated === null || onUpdated === void 0 ? void 0 : onUpdated();
66
+ onError === null || onError === void 0 ? void 0 : onError(null);
67
+ },
68
+ error: onError,
69
+ });
70
+ }, [currentApp, subject, onError, onUpdated]);
71
+ useAppSettingsStatus(currentApp, { onLoading, onUpdating });
72
+ return [settings, setSettings];
73
+ };
74
+ export default useAppSettings;
75
+ //# sourceMappingURL=useAppSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAppSettings.js","sourceRoot":"","sources":["../../../src/settings/useAppSettings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAc,MAAM,MAAM,CAAC;AAInD,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAC;AACpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAEtE,OAAO,EAAE,oBAAoB,EAA+B,MAAM,wBAAwB,CAAC;AAI3F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC1B,YAAwB,EACxB,KAGC,EAC6E,EAAE;IAChF,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE,CAAC;IAClE,MAAM,EAAE,UAAU,GAAG,IAAI,EAAE,GAAG,aAAa,EAAE,CAAC;IAE9C,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE;QACzB,OAAO,IAAI,eAAe,CAAY,YAAY,aAAZ,YAAY,cAAZ,YAAY,GAAK,EAAgB,CAAC,CAAC;QACzE,yDAAyD;QACzD,uDAAuD;IAC3D,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;IAEjB,yDAAyD;IACzD,eAAe,CAAC,GAAG,EAAE;QACjB,MAAM,GAAG,GAAG,CAAC,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,SAAmC,CAAA,CAAC,S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';
3
+ /**
4
+ * Custom hook to handle app settings status updates.
5
+ *
6
+ * @param {IApp | null} app - The app instance to monitor settings status.
7
+ * @param {AppSettingsStatusHooks} [hooks] - Optional hooks to handle loading and updating status.
8
+ * @param {function} [hooks.onLoading] - Callback function to handle loading status.
9
+ * @param {function} [hooks.onUpdating] - Callback function to handle updating status.
10
+ *
11
+ * @returns {void}
12
+ *
13
+ * @example
14
+ * const hooks = useMemo(() => ({
15
+ * onLoading: (isLoading) => console.log('Loading:', isLoading),
16
+ * onUpdating: (isUpdating) => console.log('Updating:', isUpdating),
17
+ * }, []);
18
+ * useAppSettingsStatus(app, hooks);
19
+ */
20
+ export const useAppSettingsStatus = (app, hooks) => {
21
+ const { onLoading, onUpdating } = hooks !== null && hooks !== void 0 ? hooks : {};
22
+ useLayoutEffect(() => {
23
+ if (app && onLoading) {
24
+ const subscription = app.status$
25
+ .pipe(map((status) => status.has('fetch_settings')))
26
+ .subscribe(onLoading);
27
+ return () => subscription.unsubscribe();
28
+ }
29
+ }, [app, onLoading]);
30
+ useLayoutEffect(() => {
31
+ if (app && onUpdating) {
32
+ const subscription = app.status$
33
+ .pipe(map((status) => status.has('update_settings')))
34
+ .subscribe(onUpdating);
35
+ return () => subscription.unsubscribe();
36
+ }
37
+ }, [app, onUpdating]);
38
+ };
39
+ //# sourceMappingURL=useAppSettingsStatus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAppSettingsStatus.js","sourceRoot":"","sources":["../../../src/settings/useAppSettingsStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AACxC,OAAO,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAS3B;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,GAAgB,EAAE,KAA8B,EAAE,EAAE;IACrF,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE,CAAC;IAE9C,eAAe,CAAC,GAAG,EAAE;QACjB,IAAI,GAAG,IAAI,SAAS,EAAE,CAAC;YACnB,MAAM,YAAY,GAAG,GAAG,CAAC,OAAO;iBAC3B,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC;iBACnD,SAAS,CAAC,SAAS,CAAC,CAAC;YAC1B,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;QAC5C,CAAC;IACL,CAAC,EAAE,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC;IAErB,eAAe,CAAC,GAAG,EAAE;QACjB,IAAI,GAAG,IAAI,UAAU,EAAE,CAAC;YACpB,MAAM,YAAY,GAAG,GAAG,CAAC,OAAO;iBAC3B,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC;iBACpD,SAAS,CAAC,UAAU,CAAC,CAAC;YAC3B,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;QAC5C,CAAC;IACL,CAAC,EAAE,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC;AAC1B,CAAC,CAAC"}
@@ -1,3 +1,3 @@
1
1
  // Generated by genversion.
2
- export const version = '5.3.0';
2
+ export const version = '5.4.0';
3
3
  //# sourceMappingURL=version.js.map