@equinor/fusion-framework-module-bookmark 0.1.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +11 -0
  4. package/dist/esm/bookmark-config-builder.js +70 -0
  5. package/dist/esm/bookmark-config-builder.js.map +1 -0
  6. package/dist/esm/bookmark-provider.js +130 -0
  7. package/dist/esm/bookmark-provider.js.map +1 -0
  8. package/dist/esm/client/bookmarkActions.js +8 -0
  9. package/dist/esm/client/bookmarkActions.js.map +1 -0
  10. package/dist/esm/client/bookmarkClient.js +212 -0
  11. package/dist/esm/client/bookmarkClient.js.map +1 -0
  12. package/dist/esm/client/bookmarkFlows.js +30 -0
  13. package/dist/esm/client/bookmarkFlows.js.map +1 -0
  14. package/dist/esm/client/bookmarkReducer.js +23 -0
  15. package/dist/esm/client/bookmarkReducer.js.map +1 -0
  16. package/dist/esm/configurator.js +172 -0
  17. package/dist/esm/configurator.js.map +1 -0
  18. package/dist/esm/enable-bookmark.js +10 -0
  19. package/dist/esm/enable-bookmark.js.map +1 -0
  20. package/dist/esm/index.js +6 -0
  21. package/dist/esm/index.js.map +1 -0
  22. package/dist/esm/module.js +27 -0
  23. package/dist/esm/module.js.map +1 -0
  24. package/dist/esm/types.js +2 -0
  25. package/dist/esm/types.js.map +1 -0
  26. package/dist/esm/utils/handle-url.js +11 -0
  27. package/dist/esm/utils/handle-url.js.map +1 -0
  28. package/dist/esm/utils/index.js +2 -0
  29. package/dist/esm/utils/index.js.map +1 -0
  30. package/dist/tsconfig.tsbuildinfo +1 -0
  31. package/dist/types/bookmark-config-builder.d.ts +25 -0
  32. package/dist/types/bookmark-provider.d.ts +63 -0
  33. package/dist/types/client/bookmarkActions.d.ts +21 -0
  34. package/dist/types/client/bookmarkClient.d.ts +48 -0
  35. package/dist/types/client/bookmarkFlows.d.ts +22 -0
  36. package/dist/types/client/bookmarkReducer.d.ts +5 -0
  37. package/dist/types/configurator.d.ts +47 -0
  38. package/dist/types/enable-bookmark.d.ts +4 -0
  39. package/dist/types/index.d.ts +5 -0
  40. package/dist/types/module.d.ts +22 -0
  41. package/dist/types/types.d.ts +54 -0
  42. package/dist/types/utils/handle-url.d.ts +2 -0
  43. package/dist/types/utils/index.d.ts +1 -0
  44. package/package.json +48 -0
  45. package/src/bookmark-config-builder.ts +171 -0
  46. package/src/bookmark-provider.ts +288 -0
  47. package/src/client/bookmarkActions.ts +34 -0
  48. package/src/client/bookmarkClient.ts +290 -0
  49. package/src/client/bookmarkFlows.ts +66 -0
  50. package/src/client/bookmarkReducer.ts +29 -0
  51. package/src/configurator.ts +260 -0
  52. package/src/enable-bookmark.ts +32 -0
  53. package/src/index.ts +18 -0
  54. package/src/module.ts +40 -0
  55. package/src/types.ts +65 -0
  56. package/src/utils/handle-url.ts +13 -0
  57. package/src/utils/index.ts +1 -0
  58. package/tsconfig.json +36 -0
@@ -0,0 +1,47 @@
1
+ import { BookmarkConfigBuilderCallback } from './bookmark-config-builder';
2
+ import type { ModuleInitializerArgs } from '@equinor/fusion-framework-module';
3
+ import type { IApiProvider, ServicesModule } from '@equinor/fusion-framework-module-services';
4
+ import type { SourceSystem } from './types';
5
+ import type { ContextModule, IContextProvider } from '@equinor/fusion-framework-module-context';
6
+ import type { AppModule, AppModuleProvider } from '@equinor/fusion-framework-module-app';
7
+ import type { EventModule, IEventModuleProvider } from '@equinor/fusion-framework-module-event';
8
+ import type { IBookmarkModuleProvider } from './bookmark-provider';
9
+ import type { INavigationProvider, NavigationModule } from '@equinor/fusion-framework-module-navigation';
10
+ import type { BookmarkClientConfig } from './client/bookmarkClient';
11
+ export interface BookmarkModuleConfig {
12
+ sourceSystem: SourceSystem;
13
+ clientConfiguration: BookmarkClientConfig;
14
+ getContextId: () => string | undefined;
15
+ getCurrentAppIdentification?(): string | undefined;
16
+ resolveBookmarkId?(): string | null;
17
+ event?: IEventModuleProvider;
18
+ }
19
+ export type IBookmarkModuleConfigurator = {
20
+ addConfigBuilder(init: BookmarkConfigBuilderCallback): void;
21
+ createConfig(init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [
22
+ ServicesModule,
23
+ AppModule,
24
+ ContextModule
25
+ ]>, parentProvider?: IBookmarkModuleProvider): Promise<BookmarkModuleConfig>;
26
+ };
27
+ export declare class BookmarkModuleConfigurator implements IBookmarkModuleConfigurator {
28
+ #private;
29
+ defaultExpireTime: number;
30
+ addConfigBuilder(init: BookmarkConfigBuilderCallback): void;
31
+ createConfig(init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [
32
+ ServicesModule,
33
+ AppModule,
34
+ ContextModule
35
+ ]>, parentProvider?: IBookmarkModuleProvider): Promise<BookmarkModuleConfig>;
36
+ protected _createClientConfiguration(init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [
37
+ ServicesModule,
38
+ AppModule,
39
+ ContextModule
40
+ ]>, config?: Partial<BookmarkClientConfig>): Promise<BookmarkClientConfig>;
41
+ protected _getServiceProvider(init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [ServicesModule]>): Promise<IApiProvider>;
42
+ protected _getContextProvider(init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [ContextModule]>): Promise<IContextProvider>;
43
+ protected _getAppProvider(init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [AppModule]>): Promise<AppModuleProvider>;
44
+ protected _getEventProvider(init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [EventModule]>): Promise<IEventModuleProvider>;
45
+ protected _getNavigationProvider(init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [NavigationModule]>): Promise<INavigationProvider>;
46
+ }
47
+ export default BookmarkModuleConfigurator;
@@ -0,0 +1,4 @@
1
+ import type { AnyModule, IModulesConfigurator, ModuleInitializerArgs } from '@equinor/fusion-framework-module';
2
+ import { BookmarkConfigBuilder } from './bookmark-config-builder';
3
+ import { IBookmarkModuleConfigurator } from './configurator';
4
+ export declare const enableBookmark: (configurator: IModulesConfigurator<any, any>, builder?: (<TDeps extends AnyModule[] = []>(builder: BookmarkConfigBuilder<TDeps, ModuleInitializerArgs<IBookmarkModuleConfigurator, TDeps>>) => void | Promise<void>) | undefined) => void;
@@ -0,0 +1,5 @@
1
+ export { BookmarkModuleConfigurator, IBookmarkModuleConfigurator, BookmarkModuleConfig, } from './configurator';
2
+ export { default, BookmarkModule, module as bookmarkModule, moduleKey as bookmarkModuleKey, } from './module';
3
+ export { IBookmarkModuleProvider, BookmarkModuleProvider } from './bookmark-provider';
4
+ export { enableBookmark } from './enable-bookmark';
5
+ export * from './types';
@@ -0,0 +1,22 @@
1
+ import { Module } from '@equinor/fusion-framework-module';
2
+ import { EventModule } from '@equinor/fusion-framework-module-event';
3
+ import { ServicesModule } from '@equinor/fusion-framework-module-services';
4
+ import { AppModule } from '@equinor/fusion-framework-module-app';
5
+ import { ContextModule } from '@equinor/fusion-framework-module-context';
6
+ import { IBookmarkModuleProvider } from './bookmark-provider';
7
+ import { IBookmarkModuleConfigurator } from './configurator';
8
+ export type BookmarkModuleKey = 'bookmark';
9
+ export declare const moduleKey: BookmarkModuleKey;
10
+ export type BookmarkModule = Module<BookmarkModuleKey, IBookmarkModuleProvider, IBookmarkModuleConfigurator, [
11
+ EventModule,
12
+ ServicesModule,
13
+ AppModule,
14
+ ContextModule
15
+ ]>;
16
+ export declare const module: BookmarkModule;
17
+ declare module '@equinor/fusion-framework-module' {
18
+ interface Modules {
19
+ bookmark: BookmarkModule;
20
+ }
21
+ }
22
+ export default module;
@@ -0,0 +1,54 @@
1
+ import { QueryFn, QueryCtorOptions } from '@equinor/fusion-query';
2
+ export interface Bookmark<TData = unknown> {
3
+ id: string;
4
+ name: string;
5
+ description: string;
6
+ isShared: boolean;
7
+ payload: TData;
8
+ appKey: string;
9
+ context?: Context;
10
+ createdBy: CreatedBy;
11
+ updatedBy: CreatedBy;
12
+ created: string;
13
+ updated: string;
14
+ sourceSystem: SourceSystem;
15
+ }
16
+ export type CreateBookmark<TData = unknown> = {
17
+ name: string;
18
+ description?: string;
19
+ isShared: boolean;
20
+ appKey: string;
21
+ contextId?: string;
22
+ payload: TData;
23
+ sourceSystem?: Partial<SourceSystem>;
24
+ };
25
+ export interface SourceSystem {
26
+ identifier: string;
27
+ name: string;
28
+ subSystem: string;
29
+ }
30
+ interface CreatedBy {
31
+ azureUniqueId: string;
32
+ mail: string;
33
+ name: string;
34
+ phoneNumber: string;
35
+ jobTitle: string;
36
+ accountType: number;
37
+ accountClassification: number;
38
+ }
39
+ interface Context {
40
+ id: string;
41
+ name: string;
42
+ type: string;
43
+ }
44
+ export type GetBookmarkParameters = {
45
+ id: string;
46
+ };
47
+ export type GetAllBookmarksParameters = {
48
+ isValid: boolean;
49
+ };
50
+ export interface BookmarkQueryClient {
51
+ getAllBookmarks: QueryFn<Array<Bookmark<unknown>>, GetAllBookmarksParameters> | QueryCtorOptions<Array<Bookmark<unknown>>, GetAllBookmarksParameters>;
52
+ getBookmarkById: QueryFn<Bookmark<unknown>, GetBookmarkParameters> | QueryCtorOptions<Bookmark<unknown>, GetBookmarkParameters>;
53
+ }
54
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare function removeBookmarkIdFromURL(): void;
2
+ export declare const getBookmarkIdFormURL: () => string | null;
@@ -0,0 +1 @@
1
+ export { getBookmarkIdFormURL, removeBookmarkIdFromURL } from './handle-url';
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@equinor/fusion-framework-module-bookmark",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "main": "./dist/esm/index.js",
6
+ "exports": {
7
+ ".": "./dist/esm/index.js",
8
+ "./package.json": "./package.json",
9
+ "./utils": "./dist/esm/utils/index.js"
10
+ },
11
+ "types": "./dist/types/index.d.ts",
12
+ "typesVersions": {
13
+ "*": {
14
+ "utils": [
15
+ "dist/types/utils/index.d.ts"
16
+ ]
17
+ }
18
+ },
19
+ "scripts": {
20
+ "build": "tsc -b",
21
+ "prepack": "yarn build"
22
+ },
23
+ "keywords": [],
24
+ "author": "",
25
+ "license": "ISC",
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/equinor/fusion-framework.git",
32
+ "directory": "packages/modules/context"
33
+ },
34
+ "dependencies": {
35
+ "@equinor/fusion-query": "^2.0.7"
36
+ },
37
+ "devDependencies": {
38
+ "@equinor/fusion-framework-module": "^2.0.0",
39
+ "@equinor/fusion-framework-module-event": "^2.0.0",
40
+ "@equinor/fusion-framework-module-services": "^2.1.0",
41
+ "rxjs": "^7.5.7"
42
+ },
43
+ "peerDependencies": {
44
+ "@equinor/fusion-framework-module": ">=1.2.5",
45
+ "rxjs": "^7.5.7"
46
+ },
47
+ "gitHead": "d177e1925ca7b8692435b1fdefbcc0c8418d7625"
48
+ }
@@ -0,0 +1,171 @@
1
+ import {
2
+ AnyModule,
3
+ ModuleInitializerArgs,
4
+ Modules,
5
+ ModuleType,
6
+ } from '@equinor/fusion-framework-module';
7
+ import { IEventModuleProvider } from '@equinor/fusion-framework-module-event';
8
+ import { IHttpClient } from '@equinor/fusion-framework-module-http';
9
+ import { BookmarksApiClient } from '@equinor/fusion-framework-module-services/bookmarks';
10
+
11
+ import { QueryCtorOptions, QueryFn } from '@equinor/fusion-query';
12
+
13
+ import {
14
+ BookmarkModuleConfig,
15
+ BookmarkModuleConfigurator,
16
+ IBookmarkModuleConfigurator,
17
+ } from './configurator';
18
+ import { Bookmark, GetAllBookmarksParameters, GetBookmarkParameters, SourceSystem } from './types';
19
+
20
+ export type BookmarkConfigBuilderCallback = <TDeps extends Array<AnyModule> = []>(
21
+ builder: BookmarkConfigBuilder<TDeps, ModuleInitializerArgs<IBookmarkModuleConfigurator, TDeps>>
22
+ ) => void | Promise<void>;
23
+
24
+ export class BookmarkConfigBuilder<
25
+ TModules extends Array<AnyModule> = [],
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ TInit extends ModuleInitializerArgs<any, any> = ModuleInitializerArgs<
28
+ BookmarkModuleConfigurator,
29
+ TModules
30
+ >
31
+ > {
32
+ #init: TInit;
33
+ constructor(init: TInit, public config: Partial<BookmarkModuleConfig> = {}) {
34
+ this.#init = init;
35
+ }
36
+
37
+ requireInstance<TKey extends string = Extract<keyof Modules, string>>(
38
+ module: TKey
39
+ ): Promise<ModuleType<Modules[TKey]>>;
40
+
41
+ requireInstance<T>(module: string): Promise<T>;
42
+
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ requireInstance(module: string): Promise<any> {
45
+ return this.#init.requireInstance(module);
46
+ }
47
+
48
+ /**
49
+ *
50
+ * This will sett the SourceSystem used when creating a new bookmark,
51
+ * used as the identifier for the current client. Only used in app shell / portal configuration.
52
+ *
53
+ * @param {SourceSystem} sourceSystem
54
+ * ```ts
55
+ * interface SourceSystem {
56
+ * identifier: string;
57
+ * name: string;
58
+ * subSystem: string;
59
+ * }
60
+ * ```
61
+ * @memberof BookmarkConfigBuilder
62
+ */
63
+ setSourceSystem(sourceSystem: SourceSystem) {
64
+ this.config.sourceSystem = sourceSystem;
65
+ }
66
+
67
+ /**
68
+ * Used to over write the default Url id resolving.
69
+ *
70
+ * @param {(() => string | null)} fn - Resolver for bookmarkId
71
+ * @memberof BookmarkConfigBuilder
72
+ */
73
+ setBookmarkIdResolver(fn: () => string | null) {
74
+ this.config.resolveBookmarkId = fn;
75
+ }
76
+
77
+ /**
78
+ * This allow for custom BookmarkApiClient to be added.
79
+ *
80
+ * @param {BookmarksApiClient<'fetch', IHttpClient, Bookmark<unknown>>} client - Custom Api client
81
+ * @memberof BookmarkConfigBuilder
82
+ */
83
+ serCustomBookmarkApiClient(
84
+ client: BookmarksApiClient<'fetch', IHttpClient, Bookmark<unknown>>
85
+ ) {
86
+ if (this.config.clientConfiguration) {
87
+ this.config.clientConfiguration.client = client;
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Used to over with the default context id resolver.
93
+ *
94
+ * @param {(() => string | undefined)} fn - Function for providing current contextId
95
+ * @memberof BookmarkConfigBuilder
96
+ */
97
+ setContextIdResolver(fn: () => string | undefined) {
98
+ this.config.getContextId = fn;
99
+ }
100
+
101
+ /**
102
+ * Used to over withe the default function for getting the current applications
103
+ * key identifier.
104
+ *
105
+ * @param {(() => string | undefined)} - A function for getting the current application key
106
+ * @memberof BookmarkConfigBuilder
107
+ */
108
+ setCurrentAppIdResolver(fn: () => string | undefined) {
109
+ this.config.getCurrentAppIdentification = fn;
110
+ }
111
+
112
+ /**
113
+ * Enable the over write default Event provider if needed.
114
+ * Should not be used if all event providers are changes in the current environment
115
+ *
116
+ * @param {IEventModuleProvider} provider - Custom Event provider
117
+ * @memberof BookmarkConfigBuilder
118
+ */
119
+ setEventProvider(provider: IEventModuleProvider) {
120
+ this.config.event = provider;
121
+ }
122
+
123
+ /**
124
+ * Use for override the bookmark query client
125
+ *
126
+ * @param {({
127
+ * getAllBookmarks:
128
+ * | QueryFn<Array<Bookmark<unknown>>, GetAllBookmarksParameters>
129
+ * | QueryCtorOptions<Array<Bookmark<unknown>>, GetAllBookmarksParameters>;
130
+ * getBookmarkById:
131
+ * | QueryFn<Bookmark<unknown>, GetBookmarkParameters>
132
+ * | QueryCtorOptions<Bookmark<unknown>, GetBookmarkParameters>;
133
+ * })} client - Custom client definition.
134
+ * @memberof BookmarkConfigBuilder
135
+ */
136
+ setBookmarkQueryClient(client: {
137
+ getAllBookmarks:
138
+ | QueryFn<Array<Bookmark<unknown>>, GetAllBookmarksParameters>
139
+ | QueryCtorOptions<Array<Bookmark<unknown>>, GetAllBookmarksParameters>;
140
+ getBookmarkById:
141
+ | QueryFn<Bookmark<unknown>, GetBookmarkParameters>
142
+ | QueryCtorOptions<Bookmark<unknown>, GetBookmarkParameters>;
143
+ }): void {
144
+ const expire: number = 1 * 60 * 1000;
145
+ if (this.config.clientConfiguration) {
146
+ this.config.clientConfiguration.queryClientConfig = {
147
+ getAllBookmarks:
148
+ typeof client.getAllBookmarks === 'function'
149
+ ? {
150
+ key: () => 'all-bookmarks',
151
+ client: {
152
+ fn: client.getAllBookmarks,
153
+ },
154
+ validate: ({ args }) => args.isValid,
155
+ }
156
+ : client.getAllBookmarks,
157
+ getBookmarkById:
158
+ typeof client.getBookmarkById === 'function'
159
+ ? {
160
+ key: ({ id }) => id,
161
+ client: {
162
+ fn: client.getBookmarkById,
163
+ },
164
+ expire,
165
+ validate: () => false,
166
+ }
167
+ : client.getBookmarkById,
168
+ };
169
+ }
170
+ }
171
+ }
@@ -0,0 +1,288 @@
1
+ import { ModuleType } from '@equinor/fusion-framework-module';
2
+
3
+ import {
4
+ EventModule,
5
+ FrameworkEvent,
6
+ FrameworkEventInit,
7
+ } from '@equinor/fusion-framework-module-event';
8
+
9
+ import { BookmarkClient } from './client/bookmarkClient';
10
+
11
+ import { BookmarkModuleConfig } from './configurator';
12
+ import { Observable, Subscription } from 'rxjs';
13
+ import { Bookmark, CreateBookmark } from './types';
14
+
15
+ export interface IBookmarkModuleProvider {
16
+ /**
17
+ * @type {BookmarkModuleConfig} Readonly configuration for the Bookmark Module used as a backdoor enabling bookmark configuration in a sub application.
18
+ */
19
+ readonly config: BookmarkModuleConfig;
20
+
21
+ /**
22
+ * Observable Bookmark providing current resolved bookmark.
23
+ * @type {(Observable<Bookmark<any> | undefined>)}
24
+ */
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ readonly currentBookmark$: Observable<Bookmark<any> | undefined>;
27
+ /**
28
+ *
29
+ * Observable list of bookmarks in the current configured system,
30
+ * filtered by the confirmed SourceSystem identifier. This is done by the portal development team;
31
+ * @type {Observable<Array<Bookmark<unknown>>>}
32
+ */
33
+ readonly bookmarks$: Observable<Array<Bookmark<unknown>>>;
34
+
35
+ /**
36
+ * The current bookmark available
37
+ * @type {Bookmark<unknown>}
38
+ */
39
+ currentBookmark?: Bookmark<unknown>;
40
+
41
+ /**
42
+ * Function fo setting the current applications state creator, is used to collect the state stored in a bookmark.
43
+ * This will enable the bookmark functionality for the application.
44
+ *
45
+ * @template T - Payload Type
46
+ * @param {CreateBookmarkFn<T>} cb - For creating the bookmark payload, this should ne wrapped in a useCallback, payload return can be a partial.
47
+ * @param {keyof T} [key] - User to that property to add partial payload to ,
48
+ * @return {*} {VoidFunction} - Return a cleanup function for removing the stateCreator.
49
+ */
50
+ addStateCreator<T>(cb: CreateBookmarkFn<T>, key?: keyof T): VoidFunction;
51
+
52
+ /**
53
+ * Function for resolving a bookmark form api client
54
+ * @template T - Bookmark Payload type.
55
+ * @param {string} bookmarkId - bookmark indemnificator.
56
+ * @returns {Promise<Bookmark<T>>}
57
+ */
58
+ getBookmarkById<T>(bookmarkId: string): Promise<Bookmark<T>>;
59
+
60
+ /**
61
+ * Function for resolving all bookmarks for the current sub system.
62
+ * @return {Observable<Bookmark<T>>} - An observable of the all Bookmarks.
63
+ */
64
+ getAllBookmarks(): Observable<Array<Bookmark>>;
65
+
66
+ /**
67
+ * Function for resolving all bookmarks forthe current sub system.
68
+ * @return {Promise<Array<Bookmark<T><>} - Promise of all Bookmarks
69
+ */
70
+ getAllBookmarksAsync(): Promise<Array<Bookmark>>;
71
+
72
+ /**
73
+ * Function for updating bookmark a bookmark when successful this will update the bookmark list.
74
+ * @template T - Payload Type
75
+ * @param {string} bookmarkId
76
+ * @param {Bookmark<T>} bookmark
77
+ * @return {Observable<Bookmark<T>>} - An observable of the updated Bookmark.
78
+ */
79
+ updateBookmark<T>(bookmark: Bookmark<T>): Observable<Bookmark<T>>;
80
+
81
+ /**
82
+ * Function for updating bookmark a bookmark when successful this will update the bookmark list.
83
+ * @template T - Payload Type
84
+ * @param {string} bookmarkId
85
+ * @param {Bookmark<T>} bookmark
86
+ * @return {Promise<Bookmark<T>>} - Promise of a Bookmark
87
+ */
88
+ updateBookmarkAsync<T>(bookmark: Bookmark<T>): Promise<Bookmark<T>>;
89
+
90
+ /**
91
+ * Function for deleting a bookmark, when successful this will update the bookmark list.
92
+ * @param {string} bookmarkId
93
+ * @return {Observable<string>} Observable of the deletes BookmarkId
94
+ */
95
+ deleteBookmarkById(bookmarkId: string): Observable<string>;
96
+
97
+ /**
98
+ * Function for deleting a bookmark, when successful this will update the bookmark list.
99
+ * @param {string} bookmarkId
100
+ * @return {Promise<string>} - Promise of the deleted bookmarkId
101
+ */
102
+ deleteBookmarkByIdAsync(bookmarkId: string): Promise<string>;
103
+
104
+ /**
105
+ * Function for setting the current bookmark, when successful this will update the bookmark list.
106
+ * @template TData - Bookmark payload type.
107
+ * @param {(string | Bookmark<TData> | undefined)} idOrItem - can be full bookmark object or bookmarkId.
108
+ * If not provided the current bookmark state will be set to undefined.
109
+ */
110
+ setCurrentBookmark<TData>(idOrItem?: string | Bookmark<TData>): void;
111
+
112
+ /**
113
+ * Creates a new bookmark with the given arguments, and utilizes teh provided stateCreator to create the bookmark payload.
114
+ * @param {{ name: string; description: string; isShared: boolean }} args - Name, Description and isSheared
115
+ */
116
+ createBookmark<T>(args: {
117
+ name: string;
118
+ description: string;
119
+ isShared: boolean;
120
+ }): Promise<Bookmark<T>>;
121
+
122
+ /**
123
+ * A parent provider if configuration if application is needed. A backdoor enabling bookmark configuration in a sub application.
124
+ * This is not recommence and may result in unwanted behaviors if not used correctly.
125
+ * @type {IBookmarkModuleProvider}
126
+ */
127
+ parentBookmarkProvider?: IBookmarkModuleProvider;
128
+
129
+ /**
130
+ * Record of state creator, is used to collect the state stored in a bookmark.
131
+ * @type {Record<string, CreateBookmarkFn<unknown>>}
132
+ */
133
+ bookmarkCreators: Record<string, CreateBookmarkFn<unknown>>;
134
+ }
135
+
136
+ export type CreateBookmarkFn<T> = () => Promise<Partial<T>> | Partial<T>;
137
+
138
+ export class BookmarkModuleProvider implements IBookmarkModuleProvider {
139
+ protected _bookmarkClient: BookmarkClient;
140
+
141
+ #event?: ModuleType<EventModule>;
142
+
143
+ config: BookmarkModuleConfig;
144
+ #subscriptions = new Subscription();
145
+
146
+ bookmarkCreators: Record<string | number | symbol, CreateBookmarkFn<unknown>> = {};
147
+ currentBookmark?: Bookmark<unknown>;
148
+ parentBookmarkProvider?: IBookmarkModuleProvider;
149
+
150
+ public get currentBookmark$() {
151
+ return this._bookmarkClient.currentBookmark$;
152
+ }
153
+
154
+ public get bookmarks$() {
155
+ return this._bookmarkClient.bookmarks$;
156
+ }
157
+
158
+ constructor(config: BookmarkModuleConfig, ref?: IBookmarkModuleProvider) {
159
+ this.config = config;
160
+ this.#event = config.event;
161
+ this._bookmarkClient = ref
162
+ ? (ref as unknown as BookmarkModuleProvider)._bookmarkClient
163
+ : new BookmarkClient(config.clientConfiguration, config.sourceSystem, config.event);
164
+
165
+ const initialBookmarkId = config.resolveBookmarkId && config.resolveBookmarkId();
166
+
167
+ if (initialBookmarkId) {
168
+ this._bookmarkClient.setCurrentBookmark(initialBookmarkId);
169
+ }
170
+
171
+ if (this.#event) {
172
+ this.#subscriptions.add(
173
+ this.#event.addEventListener('onCurrentAppChanged', this.#clearStateCreators)
174
+ );
175
+ }
176
+ }
177
+
178
+ public async getBookmarkById<T>(bookmarkId: string): Promise<Bookmark<T>> {
179
+ return await this._bookmarkClient.getBookmarkById<T>(bookmarkId);
180
+ }
181
+
182
+ public async setCurrentBookmark<TData>(idOrItem?: string | Bookmark<TData>): Promise<void> {
183
+ this._bookmarkClient.setCurrentBookmark(idOrItem);
184
+ }
185
+
186
+ public getAllBookmarks(): Observable<Bookmark<unknown>[]> {
187
+ return this._bookmarkClient.getAllBookmarks();
188
+ }
189
+
190
+ public async getAllBookmarksAsync(): Promise<Bookmark<unknown>[]> {
191
+ return this._bookmarkClient.getAllBookmarksAsync();
192
+ }
193
+
194
+ public updateBookmark<T>(bookmark: Bookmark<T>): Observable<Bookmark<T>> {
195
+ return this._bookmarkClient.updateBookmark<T>(bookmark);
196
+ }
197
+
198
+ public async updateBookmarkAsync<T>(bookmark: Bookmark<T>): Promise<Bookmark<T>> {
199
+ return this._bookmarkClient.updateBookmarkAsync<T>(bookmark);
200
+ }
201
+
202
+ public deleteBookmarkById(bookmarkId: string): Observable<string> {
203
+ return this._bookmarkClient.deleteBookmarkById(bookmarkId);
204
+ }
205
+
206
+ public deleteBookmarkByIdAsync(bookmarkId: string): Promise<string> {
207
+ return this._bookmarkClient.deleteBookmarkByIdAsync(bookmarkId);
208
+ }
209
+
210
+ public addStateCreator<T>(cb: CreateBookmarkFn<T>, key?: keyof T): VoidFunction {
211
+ const bookmarkCreatorKey = key ? key : '#creator';
212
+
213
+ if (this.#event) {
214
+ this.#event?.dispatchEvent('onAddCreator', {
215
+ source: this,
216
+ canBubble: true,
217
+ detail: { key, cb },
218
+ });
219
+ }
220
+
221
+ this.bookmarkCreators[bookmarkCreatorKey] = cb;
222
+ return () => {
223
+ delete this.bookmarkCreators[bookmarkCreatorKey];
224
+ };
225
+ }
226
+
227
+ public async createBookmark<T>(args: {
228
+ name: string;
229
+ description: string;
230
+ isShared: boolean;
231
+ }): Promise<Bookmark<T>> {
232
+ const payload = await this.#createPayload();
233
+
234
+ const contextId = this.config.getContextId && this.config.getContextId();
235
+ const appKey =
236
+ this.config.getCurrentAppIdentification && this.config.getCurrentAppIdentification();
237
+
238
+ if (!appKey)
239
+ throw new Error(`There is noe current application selected, can't create bookmark.`);
240
+
241
+ const bookmark: CreateBookmark<unknown> = {
242
+ ...args,
243
+ appKey,
244
+ contextId,
245
+ sourceSystem: this.config.sourceSystem,
246
+ payload,
247
+ };
248
+
249
+ return this._bookmarkClient.createBookmarkAsync(bookmark) as Promise<Bookmark<T>>;
250
+ }
251
+
252
+ #clearStateCreators = () => {
253
+ this.bookmarkCreators = {};
254
+ };
255
+
256
+ async #createPayload() {
257
+ const creator = this.bookmarkCreators;
258
+ if (!creator || Object.keys(creator).length === 0) {
259
+ throw Error('No creator registered on this BookmarkProvider');
260
+ }
261
+
262
+ if (typeof creator['#creator'] === 'function') {
263
+ return creator['#creator']();
264
+ }
265
+
266
+ return await Object.entries(creator).reduce(async (cur, [key, fn]) => {
267
+ return Object.assign(await cur, { [key]: await fn() });
268
+ }, Promise.resolve({}));
269
+ }
270
+
271
+ public dispose() {
272
+ this.#subscriptions.unsubscribe();
273
+ this._bookmarkClient.dispose();
274
+ }
275
+ }
276
+
277
+ declare module '@equinor/fusion-framework-module-event' {
278
+ interface FrameworkEventMap {
279
+ /** dispatch before context changes */
280
+ onNavigateToApp: FrameworkEvent<FrameworkEventInit<URL, IBookmarkModuleProvider>>;
281
+ onAddCreator: FrameworkEvent<
282
+ FrameworkEventInit<
283
+ { cb: CreateBookmarkFn<unknown>; key?: keyof unknown },
284
+ BookmarkModuleProvider
285
+ >
286
+ >;
287
+ }
288
+ }
@@ -0,0 +1,34 @@
1
+ import { createAsyncAction, ActionInstanceMap, ActionTypes } from '@equinor/fusion-observable';
2
+
3
+ import { Bookmark, CreateBookmark } from '../types';
4
+
5
+ export const actions = {
6
+ getAll: createAsyncAction(
7
+ 'getAll',
8
+ (isValid: boolean) => ({ payload: isValid }),
9
+ (bookmarks: Bookmark[]) => ({ payload: bookmarks }),
10
+ (err: unknown) => ({ payload: err })
11
+ ),
12
+ delete: createAsyncAction(
13
+ 'delete',
14
+ (id: string) => ({ payload: id }),
15
+ (id: string) => ({ payload: id })
16
+ ),
17
+ update: createAsyncAction(
18
+ 'update',
19
+ (bookmark: Bookmark) => ({ payload: bookmark }),
20
+ (bookmark: Bookmark) => ({ payload: bookmark })
21
+ ),
22
+ create: createAsyncAction(
23
+ 'create',
24
+ (bookmark: CreateBookmark) => ({ payload: bookmark }),
25
+ (bookmark: Bookmark) => ({ payload: bookmark }),
26
+ (err: unknown) => ({ payload: err })
27
+ ),
28
+ };
29
+
30
+ export type ActionBuilder = typeof actions;
31
+
32
+ export type ActionMap = ActionInstanceMap<ActionBuilder>;
33
+
34
+ export type Actions = ActionTypes<typeof actions>;