MCPcopy Create free account
hub / github.com/dailydotdev/apps / useBookmarkState

Function useBookmarkState

packages/shared/src/hooks/bookmark/useBookmarkState.ts:44–295  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

42 * no longer belongs to the status the feed is showing.
43 */
44export const useBookmarkState = (): UseBookmarkState => {
45 const client = useQueryClient();
46 const { user } = useAuthContext();
47 const {
48 queryKey: feedQueryKey,
49 items,
50 onRemovePost,
51 logOpts,
52 } = useActiveFeedContext();
53 const { displayToast } = useToastNotification();
54 const { logEvent } = useLogContext();
55 const postLogEvent = usePostLogEvent();
56
57 const patchBookmark = useCallback(
58 (postId: string, patch: Partial<Bookmark>) =>
59 patchBookmarkInCaches({
60 client,
61 postId,
62 feedQueryKey,
63 items,
64 mutate: (post) => ({
65 bookmarked: true,
66 bookmark: {
67 ...(post.bookmark ?? { createdAt: new Date() }),
68 ...patch,
69 },
70 }),
71 }),
72 [client, feedQueryKey, items],
73 );
74
75 const removeFromFeed = useCallback(
76 (postId: string) => {
77 const index = items.findIndex(
78 (item) => item.type === 'post' && item.post.id === postId,
79 );
80
81 if (index !== -1) {
82 onRemovePost?.(index);
83 }
84 },
85 [items, onRemovePost],
86 );
87
88 const invalidateFacets = useCallback(
89 () => client.invalidateQueries({ queryKey: [RequestKey.BookmarkFacets] }),
90 [client],
91 );
92
93 // Archiving moves the bookmark between lists. The others are reset, not
94 // invalidated, so a stale answer never shows while they refetch
95 const resetOtherBookmarkLists = useCallback(() => {
96 const feedHash = feedQueryKey && hashKey(feedQueryKey);
97
98 return client.resetQueries({
99 queryKey: generateQueryKey(RequestKey.Bookmarks, user),
100 predicate: ({ queryHash }) => queryHash !== feedHash,
101 });

Callers 3

BookmarkNoteModalFunction · 0.90
useBookmarkActionsFunction · 0.90
PostOptionButtonContentFunction · 0.90

Calls 11

useAuthContextFunction · 0.90
useActiveFeedContextFunction · 0.90
useToastNotificationFunction · 0.90
useLogContextFunction · 0.90
usePostLogEventFunction · 0.90
patchBookmarkInCachesFunction · 0.90
generateQueryKeyFunction · 0.90
displayToastFunction · 0.85
logEventFunction · 0.85
postLogEventFunction · 0.85
bookmarksFeedStatusFunction · 0.85

Tested by

no test coverage detected