()
| 42 | * no longer belongs to the status the feed is showing. |
| 43 | */ |
| 44 | export 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 | }); |
no test coverage detected