@equinor/fusion-framework-react 9.0.0-next.0 → 9.0.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.
@@ -1,103 +0,0 @@
1
- import { useCallback, useMemo } from 'react';
2
-
3
- import { of } from 'rxjs';
4
- import { map } from 'rxjs/operators';
5
-
6
- import type {
7
- IFeatureFlagProvider,
8
- IFeatureFlag,
9
- } from '@equinor/fusion-framework-module-feature-flag';
10
-
11
- import type { FeatureSelectorFn } from '@equinor/fusion-framework-module-feature-flag/selectors';
12
-
13
- import { useObservableState } from '@equinor/fusion-observable/react';
14
-
15
- /**
16
- * Return type of the {@link useFeatures} hook.
17
- */
18
- export interface UseFeaturesResult {
19
- /** Array of all resolved feature flags. */
20
- features: IFeatureFlag[];
21
- /** Any error emitted by the feature-flag observable. */
22
- error: unknown;
23
- /**
24
- * Toggles a feature flag by key.
25
- *
26
- * @param key - The key of the feature flag.
27
- * @param enable - Explicit enabled state. When omitted the current state
28
- * is inverted.
29
- */
30
- toggleFeature: (key: string, enable?: boolean) => void;
31
- }
32
-
33
- /**
34
- * React hook that returns all feature flags from a provider, with optional
35
- * filtering.
36
- *
37
- * @param provider - The feature-flag provider instance, or `null`/`undefined`
38
- * when not yet available.
39
- * @param selector - Optional predicate to filter the feature flags.
40
- * @returns A {@link UseFeaturesResult} with the flags, a toggle helper,
41
- * and any error.
42
- * @throws {Error} If `toggleFeature` is called when the provider is missing.
43
- *
44
- * @example
45
- * ```ts
46
- * const { features, toggleFeature } = useFeatures(provider);
47
- * toggleFeature('beta', true);
48
- * ```
49
- */
50
- export const useFeatures = (
51
- provider?: IFeatureFlagProvider | null,
52
- selector?: FeatureSelectorFn,
53
- ): UseFeaturesResult => {
54
- /**
55
- * Custom hook that provides access to the feature flags and their values.
56
- *
57
- * @returns An object containing the features and any error that occurred while retrieving them.
58
- */
59
- const { value: features, error } = useObservableState(
60
- useMemo(() => {
61
- // Only compute the feature list when a provider is available; otherwise there's nothing to observe
62
- if (provider) {
63
- // Map the feature dictionary to a list, optionally narrowed by the caller-provided selector
64
- return provider?.features$.pipe(
65
- map((x) => {
66
- const values = Object.values(x);
67
- // Return everything when no selector is provided
68
- if (!selector) {
69
- return values;
70
- }
71
- // Apply the caller-provided selector to narrow down the feature list
72
- const filtered = values.filter(selector);
73
- return filtered;
74
- }),
75
- );
76
- }
77
- return of([]);
78
- }, [provider, selector]),
79
- { initial: Object.values(provider?.features ?? {}) },
80
- );
81
- /**
82
- * Sets the enabled state of a feature flag.
83
- *
84
- * @param key - The key of the feature flag.
85
- * @param enable - The new enabled state of the feature flag.
86
- * @throws Error if IFeatureFlagProvider is missing.
87
- */
88
- const toggleFeature = useCallback(
89
- (key: string, enable?: boolean) => {
90
- // Cannot toggle a feature without a provider to persist the change
91
- if (!provider) {
92
- throw new Error('Missing IFeatureFlagProvider.');
93
- }
94
- const enabled = enable === undefined ? !provider.getFeature(key)?.enabled : enable;
95
-
96
- provider.toggleFeature({ key, enabled });
97
- },
98
- [provider],
99
- );
100
- return { features, error, toggleFeature };
101
- };
102
-
103
- export default useFeatures;
@@ -1,30 +0,0 @@
1
- import type { FeatureFlagModule } from '@equinor/fusion-framework-module-feature-flag';
2
-
3
- import { useFrameworkModule } from '../useFrameworkModule';
4
- import { useFeature } from './useFeature';
5
-
6
- /**
7
- * React hook that retrieves a single feature flag from the **framework-level**
8
- * feature-flag provider.
9
- *
10
- * @template T - Value type carried by the feature flag.
11
- * @param key - Unique key identifying the feature flag.
12
- * @returns A {@link UseFeatureResult} with the flag value, toggle helper,
13
- * and any error.
14
- * @throws {Error} If the `FeatureFlagModule` is not enabled in the framework.
15
- *
16
- * @example
17
- * ```ts
18
- * const { feature, toggleFeature } = useFrameworkFeature('experimental-ui');
19
- * ```
20
- */
21
- export const useFrameworkFeature = <T>(key: string): ReturnType<typeof useFeature<T>> => {
22
- const provider = useFrameworkModule<FeatureFlagModule>('featureFlag');
23
- // Fail fast when the FeatureFlagModule has not been enabled on the framework
24
- if (!provider) {
25
- throw Error('Feature flagging is not enabled in the framework');
26
- }
27
- return useFeature(provider, key);
28
- };
29
-
30
- export default useFrameworkFeature;
@@ -1,28 +0,0 @@
1
- import type { FeatureFlagModule } from '@equinor/fusion-framework-module-feature-flag';
2
-
3
- import { useFrameworkModule } from '../useFrameworkModule';
4
- import { useFeatures } from './useFeatures';
5
-
6
- /**
7
- * React hook that returns all feature flags from the **framework-level**
8
- * feature-flag provider.
9
- *
10
- * @returns A {@link UseFeaturesResult} containing all framework feature flags,
11
- * a toggle helper, and any error.
12
- * @throws {Error} If the `FeatureFlagModule` is not enabled in the framework.
13
- *
14
- * @example
15
- * ```ts
16
- * const { features, toggleFeature } = useFrameworkFeatures();
17
- * ```
18
- */
19
- export const useFrameworkFeatures = (): ReturnType<typeof useFeatures> => {
20
- const provider = useFrameworkModule<FeatureFlagModule>('featureFlag');
21
- // Fail fast when the FeatureFlagModule has not been enabled on the framework
22
- if (!provider) {
23
- throw Error('Feature flagging is not enabled in the framework');
24
- }
25
- return useFeatures(provider);
26
- };
27
-
28
- export default useFrameworkFeatures;
@@ -1,21 +0,0 @@
1
- import { context } from './context';
2
-
3
- /**
4
- * Component for providing framework.
5
- *
6
- * @remarks
7
- * Should be created by {@link createFrameworkProvider}
8
- *
9
- * @example
10
- * ```tsx
11
- * import {FrameworkProvider} from '@equinor/fusion-framework-react';
12
- * export const Component = (args: React.PropsWithChildren<{framework: Fusion}>) => {
13
- * return (
14
- * <FrameworkProvider value={args.framework}>
15
- * {args.children}
16
- * </FrameworkProvider>
17
- * );
18
- * }
19
- * ```
20
- */
21
- export const FrameworkProvider = context.Provider;
@@ -1,10 +0,0 @@
1
- /**
2
- * Convenience hooks for common framework operations.
3
- *
4
- * @remarks
5
- * Available via the `@equinor/fusion-framework-react/hooks` sub-entry-point.
6
- *
7
- * @module
8
- */
9
- export { useCurrentUser } from './useCurrentUser';
10
- export { useHttpClient } from './useHttpClient';
@@ -1,21 +0,0 @@
1
- import type { AccountInfo } from '@equinor/fusion-framework-module-msal';
2
- import { useFramework } from '../useFramework';
3
-
4
- /**
5
- * React hook that returns the currently authenticated user's account info.
6
- *
7
- * @returns The {@link AccountInfo} of the signed-in user, or `undefined` if
8
- * no user is authenticated.
9
- *
10
- * @example
11
- * ```tsx
12
- * const UserGreeting = () => {
13
- * const user = useCurrentUser();
14
- * return <span>Hello, {user?.name ?? 'Guest'}</span>;
15
- * };
16
- * ```
17
- */
18
- export const useCurrentUser = (): AccountInfo | undefined => {
19
- const framework = useFramework();
20
- return framework.modules.auth.account || undefined;
21
- };
@@ -1,36 +0,0 @@
1
- import { useMemo } from 'react';
2
- import type { Fusion } from '@equinor/fusion-framework';
3
- import { useFramework } from '../useFramework';
4
-
5
- /** Resolved HTTP client instance returned by the framework HTTP module. */
6
- type HttpClient = ReturnType<Fusion['modules']['http']['createClient']>;
7
-
8
- /** Well-known HTTP client keys pre-configured by the framework. */
9
- type FrameworkHttpClient = 'portal' | 'people';
10
-
11
- /**
12
- * React hook that returns a pre-configured HTTP client from the framework.
13
- *
14
- * @param name - Key of the HTTP client to retrieve (e.g. `'portal'` or `'people'`).
15
- * @returns The resolved {@link HttpClient} instance.
16
- * @throws {Error} If no client is configured for the given key.
17
- *
18
- * @example
19
- * ```ts
20
- * const client = useHttpClient('portal');
21
- * client.fetch('/api/data').subscribe(console.log);
22
- * ```
23
- */
24
- export const useHttpClient = (name: FrameworkHttpClient): HttpClient => {
25
- const framework = useFramework();
26
-
27
- const client = useMemo(() => {
28
- // Reuse an already-configured client for this key when one exists
29
- if (framework.modules.http.hasClient(name)) {
30
- return framework.modules.http.createClient(name);
31
- }
32
- throw Error(`no configured client for key [${name}]`);
33
- }, [framework, name]);
34
- // TODO(#5085): abort on unmount?
35
- return client;
36
- };
package/src/http/index.ts DELETED
@@ -1,13 +0,0 @@
1
- /**
2
- * HTTP sub-entry-point (`@equinor/fusion-framework-react/http`).
3
- *
4
- * @remarks
5
- * Re-exports the framework-level {@link useHttpClient} hook as
6
- * `useFrameworkHttpClient` together with all exports from the
7
- * standalone HTTP React module.
8
- *
9
- * @module
10
- */
11
- export { useHttpClient as useFrameworkHttpClient } from '../hooks/useHttpClient';
12
-
13
- export * from '@equinor/fusion-framework-react-module-http';
package/src/index.tsx DELETED
@@ -1,23 +0,0 @@
1
- /**
2
- * React bindings for the Fusion Framework.
3
- *
4
- * @remarks
5
- * This package provides React components, context providers, and hooks for
6
- * initialising and consuming a Fusion Framework instance inside a React
7
- * application tree. It is the main entry-point for portals and host
8
- * applications that need to bootstrap the framework with React.
9
- *
10
- * [[include:framework-react/README.MD]]
11
- * @module
12
- */
13
-
14
- export type { Fusion } from '@equinor/fusion-framework';
15
- export { FusionConfigurator } from '@equinor/fusion-framework';
16
-
17
- export { createFrameworkProvider } from './create-framework-provider';
18
- export { FrameworkProvider } from './framework-provider';
19
-
20
- export { useFramework } from './useFramework';
21
- export { useFrameworkModule } from './useFrameworkModule';
22
-
23
- export { default, Framework } from './Framework';
@@ -1 +0,0 @@
1
- export { useSignalR } from './useSignalR.js';
@@ -1,40 +0,0 @@
1
- import { useFramework } from '../useFramework';
2
-
3
- import {
4
- type SignalRModule,
5
- useProviderTopic,
6
- } from '@equinor/fusion-framework-react-module-signalr';
7
-
8
- /**
9
- * hook for subscribing to a topic of a SignalR hub
10
- *
11
- * @example
12
- ```ts
13
- // config.ts
14
- import {enableSignalR} from '@equinor/fusion-framework-react-module-signalr';
15
- (configurator) => enableSignalR(configurator, 'notifications');
16
-
17
- // myHook.ts
18
- const myHook = () => {
19
- const topic = useSignalR('notifications', 'foo');
20
- return useObservableState(topic).next;
21
- }
22
- ```
23
- *
24
- * @see {@link [module signalr](https://equinor.github.io/fusion-framework/modules/signalr)}
25
- *
26
- * @param hubId identifier of connection hub (must be configured)
27
- * @param topicId identifier of topic to connect to
28
- * @returns Topic
29
- */
30
- export const useSignalR = <T>(
31
- hubId: string,
32
- topicId: string,
33
- ): ReturnType<typeof useProviderTopic<T>> => {
34
- const provider = useFramework<[SignalRModule]>().modules.signalR;
35
- // Fail fast when the SignalRModule has not been configured on the framework
36
- if (!provider) {
37
- throw Error('SignalR is not configured, see @equinor/fusion-framework-react-module-signalr');
38
- }
39
- return useProviderTopic(provider, hubId, topicId);
40
- };
@@ -1,41 +0,0 @@
1
- import type { Fusion } from '@equinor/fusion-framework';
2
- import type { AnyModule } from '@equinor/fusion-framework-module';
3
-
4
- import { useContext } from 'react';
5
- import { context } from './context';
6
- /**
7
- * React hook that returns the current Fusion Framework instance from context.
8
- *
9
- * @remarks
10
- * The hook first looks for a framework instance provided via
11
- * {@link FrameworkProvider}. If none is found it falls back to the global
12
- * `window.Fusion` object. A console warning / error is emitted when the
13
- * framework cannot be resolved.
14
- *
15
- * @template TModules - Tuple of additional module types expected on the
16
- * framework instance (used for type-narrowing only).
17
- * @returns The active {@link Fusion} instance.
18
- *
19
- * @example
20
- * ```ts
21
- * const useMyService = () => {
22
- * const fusion = useFramework();
23
- * return fusion.modules.http.createClient('my-service');
24
- * };
25
- * ```
26
- */
27
- export const useFramework = <TModules extends Array<AnyModule> = []>(): Fusion<TModules> => {
28
- let framework = useContext(context);
29
- // Warn loudly when the framework context is missing so integrators notice the misuse
30
- if (!framework) {
31
- console.warn('could not locate fusion in context!');
32
- }
33
- framework ??= window.Fusion;
34
- // Surface an error when neither context nor the global fallback has the framework instance
35
- if (!framework) {
36
- console.error('Could not load framework, might not be initiated?');
37
- }
38
- return framework;
39
- };
40
-
41
- export default useFramework;
@@ -1,44 +0,0 @@
1
- import type { FusionModules, FusionModulesInstance } from '@equinor/fusion-framework';
2
- import { useFramework } from './useFramework';
3
- import type {
4
- AnyModule,
5
- ModuleKey,
6
- ModuleType,
7
- ModuleTypes,
8
- } from '@equinor/fusion-framework-module';
9
-
10
- /**
11
- * React hook that retrieves a module from the Fusion Framework by name.
12
- *
13
- * @template TType - The expected module type (used for type-narrowing).
14
- * @template TKey - The module key string.
15
- * @param name - The registered name of the module to retrieve.
16
- * @returns The resolved module instance, or `undefined` if the module is
17
- * not registered.
18
- *
19
- * @example
20
- * ```ts
21
- * import type { HttpModule } from '@equinor/fusion-framework-module-http';
22
- *
23
- * const http = useFrameworkModule<HttpModule>('http');
24
- * ```
25
- */
26
- export const useFrameworkModule = <
27
- TType extends AnyModule | unknown = unknown,
28
- TKey extends string = ModuleKey<ModuleTypes<FusionModules<[TType]>>>,
29
- >(
30
- name: TKey,
31
- ): TType extends AnyModule
32
- ? ModuleType<TType> | undefined
33
- : FusionModulesInstance[Extract<keyof FusionModulesInstance, TKey>] | undefined => {
34
- const framework = useFramework();
35
- // TODO(#5082): tighten generics so this indexed lookup no longer needs @ts-expect-error
36
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
37
- // @ts-expect-error
38
- const module = framework.modules[name];
39
- // Warn when the requested module key does not resolve to a registered module
40
- if (!module) {
41
- console.warn(`the requested module [${name}] is not included in the framework instance`);
42
- }
43
- return module;
44
- };
package/src/version.ts DELETED
@@ -1,2 +0,0 @@
1
- // Generated by genversion.
2
- export const version = '9.0.0-next.0';
package/tsconfig.json DELETED
@@ -1,39 +0,0 @@
1
- {
2
- "extends": "../tsconfig.react.json",
3
- "compilerOptions": {
4
- "outDir": "dist/esm",
5
- "rootDir": "src",
6
- "declarationDir": "./dist/types",
7
- "paths": {
8
- "react": ["./node_modules/@types/react"]
9
- }
10
- },
11
- "references": [
12
- {
13
- "path": "../../framework"
14
- },
15
- {
16
- "path": "../../modules/app"
17
- },
18
- {
19
- "path": "../../modules/msal"
20
- },
21
- {
22
- "path": "../../modules/feature-flag"
23
- },
24
- {
25
- "path": "../../modules/service-discovery"
26
- },
27
- {
28
- "path": "../modules/context"
29
- },
30
- {
31
- "path": "../modules/http"
32
- },
33
- {
34
- "path": "../modules/signalr"
35
- }
36
- ],
37
- "include": ["src/**/*"],
38
- "exclude": ["node_modules", "lib"]
39
- }