@equinor/fusion-framework-react-module-context 0.0.0-context-error-20240131144633
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/CHANGELOG.md +507 -0
- package/LICENSE +21 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/useCurrentContext.js +26 -0
- package/dist/esm/useCurrentContext.js.map +1 -0
- package/dist/esm/useQueryContext.js +16 -0
- package/dist/esm/useQueryContext.js.map +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/useCurrentContext.d.ts +11 -0
- package/dist/types/useQueryContext.d.ts +15 -0
- package/dist/types/version.d.ts +1 -0
- package/package.json +53 -0
- package/src/index.ts +12 -0
- package/src/useCurrentContext.ts +34 -0
- package/src/useQueryContext.ts +21 -0
- package/src/version.ts +2 -0
- package/tsconfig.json +27 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export {
|
|
2
|
+
enableContext,
|
|
3
|
+
ContextModule,
|
|
4
|
+
ContextItem,
|
|
5
|
+
ContextItemType,
|
|
6
|
+
IContextProvider,
|
|
7
|
+
IContextModuleConfigurator,
|
|
8
|
+
} from '@equinor/fusion-framework-module-context';
|
|
9
|
+
export { FusionContextSearchError } from '@equinor/fusion-framework-module-context/errors.js';
|
|
10
|
+
|
|
11
|
+
export { useCurrentContext, useModuleCurrentContext } from './useCurrentContext';
|
|
12
|
+
export { useQueryContext, useModuleQueryContext } from './useQueryContext';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ContextItem, ContextModule } from '@equinor/fusion-framework-module-context';
|
|
2
|
+
import { IContextProvider, contextModuleKey } from '@equinor/fusion-framework-module-context';
|
|
3
|
+
import { useModule } from '@equinor/fusion-framework-react-module';
|
|
4
|
+
import { useObservableState } from '@equinor/fusion-observable/react';
|
|
5
|
+
import { useCallback, useMemo } from 'react';
|
|
6
|
+
|
|
7
|
+
export const useCurrentContext = (provider: IContextProvider) => {
|
|
8
|
+
const currentContext$ = useMemo(() => provider.currentContext$, [provider]);
|
|
9
|
+
const { value: currentContext } = useObservableState(currentContext$, {
|
|
10
|
+
initial: provider.currentContext,
|
|
11
|
+
});
|
|
12
|
+
const setCurrentContext = useCallback(
|
|
13
|
+
(entry?: ContextItem | string | null) => {
|
|
14
|
+
if (!entry) {
|
|
15
|
+
return provider.clearCurrentContext();
|
|
16
|
+
} else if (typeof entry === 'string') {
|
|
17
|
+
return provider.setCurrentContextByIdAsync(entry);
|
|
18
|
+
}
|
|
19
|
+
return provider.setCurrentContextAsync(entry);
|
|
20
|
+
},
|
|
21
|
+
[provider],
|
|
22
|
+
);
|
|
23
|
+
return { currentContext, setCurrentContext };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* uses context provider from closes module provider
|
|
28
|
+
*/
|
|
29
|
+
export const useModuleCurrentContext = () => {
|
|
30
|
+
const provider = useModule<ContextModule>(contextModuleKey);
|
|
31
|
+
return useCurrentContext(provider);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export default useModuleCurrentContext;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ContextModule,
|
|
3
|
+
contextModuleKey,
|
|
4
|
+
IContextProvider,
|
|
5
|
+
} from '@equinor/fusion-framework-module-context';
|
|
6
|
+
import { useModule } from '@equinor/fusion-framework-react-module';
|
|
7
|
+
import { useObservableState, useDebounce } from '@equinor/fusion-observable/react';
|
|
8
|
+
import { useMemo } from 'react';
|
|
9
|
+
|
|
10
|
+
export const useQueryContext = (provider: IContextProvider, options?: { debounce?: number }) => {
|
|
11
|
+
const args = Object.assign({}, { debounce: 500 }, options);
|
|
12
|
+
const searchFn = useMemo(() => provider.queryContext.bind(provider), [provider]);
|
|
13
|
+
const { idle, next, value$ } = useDebounce(searchFn, args);
|
|
14
|
+
const { value } = useObservableState(value$);
|
|
15
|
+
return { value, querying: !idle, query: next };
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const useModuleQueryContext = (options?: { debounce?: number }) => {
|
|
19
|
+
const provider = useModule<ContextModule>(contextModuleKey);
|
|
20
|
+
return useQueryContext(provider, options);
|
|
21
|
+
};
|
package/src/version.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.react.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dist/esm",
|
|
5
|
+
"rootDir": "src",
|
|
6
|
+
"declarationDir": "./dist/types",
|
|
7
|
+
|
|
8
|
+
},
|
|
9
|
+
"references": [
|
|
10
|
+
{
|
|
11
|
+
"path": "../../../utils/query"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"path": "../../../modules/context"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"path": "../module"
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
"include": [
|
|
21
|
+
"src/**/*"
|
|
22
|
+
],
|
|
23
|
+
"exclude": [
|
|
24
|
+
"node_modules",
|
|
25
|
+
"lib"
|
|
26
|
+
]
|
|
27
|
+
}
|