-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuseBookmark.ts
185 lines (164 loc) · 6.21 KB
/
useBookmark.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { useCallback, useLayoutEffect, useMemo } from 'react';
import { EMPTY } from 'rxjs';
import {
Bookmark,
Bookmarks,
BookmarkUpdate,
BookmarkData,
BookmarkPayloadGenerator,
BookmarkUpdateOptions,
BookmarkProvider,
} from '@equinor/fusion-framework-module-bookmark';
import { useObservableState } from '@equinor/fusion-observable/react';
import { useBookmarkProvider } from './useBookmarkProvider';
import { BookmarkCreateArgs } from '@equinor/fusion-framework-module-bookmark';
export type useBookmarkArgs = {
provider?: BookmarkProvider;
payloadGenerator?: BookmarkPayloadGenerator<BookmarkData>;
};
export type useBookmarkResult = {
currentBookmark: Bookmark | null | undefined;
setCurrentBookmark: (bookmark: Bookmark | string | null) => void;
bookmarks: Bookmarks;
getAllBookmarks: () => Promise<Bookmarks>;
getBookmarkById: (bookmarkId: string) => Promise<Bookmark | null | undefined>;
createBookmark: <T extends BookmarkData>(args: BookmarkCreateArgs<T>) => Promise<Bookmark<T>>;
updateBookmark: <T extends BookmarkData>(
value: BookmarkUpdate<T> & { id: string },
options?: BookmarkUpdateOptions,
) => Promise<Bookmark<T> | undefined>;
deleteBookmarkById: (bookmarkId: string) => Promise<void>;
addBookmarkFavorite: (bookmarkId: string) => Promise<void>;
removeBookmarkFavorite: (bookmarkId: string) => Promise<void>;
addBookmarkCreator: (cb: BookmarkPayloadGenerator) => VoidFunction | undefined;
};
/**
* @deprecated
* For application development the useCurrentBookmark should be sufficient enough
*/
export const useBookmark = (args?: useBookmarkArgs): useBookmarkResult => {
const defaultProvider = useBookmarkProvider();
const bookmarkProvider = (args?.provider ?? defaultProvider) as BookmarkProvider;
const payloadGenerator = args?.payloadGenerator;
const { value: currentBookmark } = useObservableState(
useMemo(() => bookmarkProvider?.currentBookmark$ ?? EMPTY, [bookmarkProvider]),
{
initial: bookmarkProvider?.currentBookmark,
},
);
const { value: bookmarks } = useObservableState(
useMemo(() => bookmarkProvider?.bookmarks$ ?? EMPTY, [bookmarkProvider]),
{
initial: bookmarkProvider?.bookmarks ?? [],
},
);
useLayoutEffect(() => {
if (payloadGenerator) {
return bookmarkProvider?.addPayloadGenerator(payloadGenerator);
}
}, [payloadGenerator, bookmarkProvider]);
/**
* @deprecated use `payloadGenerator` instead {@link useBookmarkArgs.payloadGenerator}
*/
const addBookmarkCreator = useCallback(
(cb: BookmarkPayloadGenerator) => {
if (bookmarkProvider) {
return bookmarkProvider.addPayloadGenerator(cb);
}
throw Error('No BookmarkProvider found');
},
[bookmarkProvider],
);
/**
* @deprecated use provider instead {@link BookmarkProvider.createBookmarkAsync}
*/
const createBookmark = useCallback(
<T extends BookmarkData>(args: BookmarkCreateArgs<T>): Promise<Bookmark<T>> => {
if (bookmarkProvider) {
return bookmarkProvider.createBookmarkAsync(args);
}
throw Error('No BookmarkProvider found');
},
[bookmarkProvider],
);
/**
* @deprecated use provider instead {@link BookmarkProvider.updateBookmarkAsync}
*/
const updateBookmark = useCallback(
async <T extends BookmarkData>(
bookmark: BookmarkUpdate<T> & { id: string },
options?: BookmarkUpdateOptions,
): Promise<Bookmark<T> | undefined> => {
if (bookmarkProvider) {
const { id, ...updates } = bookmark;
return await bookmarkProvider.updateBookmarkAsync(id, updates, options);
}
return undefined;
},
[bookmarkProvider],
);
/**
* @deprecated use provider instead {@link BookmarkProvider.removeBookmarkAsync}
*/
const deleteBookmarkById = useCallback(
async (bookmarkId: string): Promise<void> => {
bookmarkProvider && (await bookmarkProvider.deleteBookmarkAsync(bookmarkId));
},
[bookmarkProvider],
);
/**
* @deprecated use provider instead {@link BookmarkProvider.getAllBookmarks}
*/
const getAllBookmarks = useCallback(async (): Promise<Bookmarks> => {
return bookmarkProvider ? await bookmarkProvider.getAllBookmarksAsync() : [];
}, [bookmarkProvider]);
/**
* @deprecated use provider instead {@link BookmarkProvider.addBookmarkToFavoritesAsync}
*/
const addBookmarkFavorite = useCallback(
async (bookmarkId: string): Promise<void> => {
bookmarkProvider && (await bookmarkProvider.addBookmarkToFavoritesAsync(bookmarkId));
},
[bookmarkProvider],
);
/**
* @deprecated use provider instead {@link BookmarkProvider.removeBookmarkAsync}
*/
const removeBookmarkFavorite = useCallback(
async (bookmarkId: string): Promise<void> => {
bookmarkProvider && (await bookmarkProvider.removeBookmarkAsFavoriteAsync(bookmarkId));
},
[bookmarkProvider],
);
const setCurrentBookmark = useCallback(
(IdOrItem: Bookmark | string | null): void => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
bookmarkProvider && bookmarkProvider.setCurrentBookmark(IdOrItem as any);
},
[bookmarkProvider],
);
/** @deprecated */
const getBookmarkById = useCallback(
<TData extends BookmarkData>(id: string): Promise<Bookmark<TData> | undefined | null> => {
return bookmarkProvider
? bookmarkProvider.getBookmarkAsync<TData>(id)
: Promise.resolve(undefined);
},
[bookmarkProvider],
);
return {
addBookmarkCreator,
getAllBookmarks,
createBookmark,
updateBookmark,
deleteBookmarkById,
setCurrentBookmark,
getBookmarkById,
currentBookmark,
bookmarks,
// getCurrentAppKey,
addBookmarkFavorite,
removeBookmarkFavorite,
};
};
export default useBookmark;