From 0ae133a7a52817aa6713094d1b3ef56c21018eb5 Mon Sep 17 00:00:00 2001 From: Abhi-Bohora Date: Mon, 18 Nov 2024 20:24:06 +0545 Subject: [PATCH 1/8] fix: card overlay not toggling between upvote and bookmark --- .../shared/src/hooks/feed/useCardCover.tsx | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/shared/src/hooks/feed/useCardCover.tsx b/packages/shared/src/hooks/feed/useCardCover.tsx index 89d0730873..327cb35fd8 100644 --- a/packages/shared/src/hooks/feed/useCardCover.tsx +++ b/packages/shared/src/hooks/feed/useCardCover.tsx @@ -1,4 +1,4 @@ -import React, { ReactNode, useMemo } from 'react'; +import React, { ReactNode, useEffect, useMemo, useState } from 'react'; import { Post } from '../../graphql/posts'; import { usePostShareLoop } from '../post/usePostShareLoop'; import { CardCoverShare } from '../../components/cards/common/CardCoverShare'; @@ -28,9 +28,21 @@ export const useCardCover = ({ }: UseCardCoverProps): UseCardCover => { const { shouldShowOverlay, onInteract } = usePostShareLoop(post); const shouldShowReminder = useBookmarkReminderCover(post); + const [lastInteraction, setLastInteraction] = useState<'upvote' | 'bookmark' | null>(null); + useEffect(() => { + if (shouldShowOverlay) { + setLastInteraction('upvote'); + } + }, [shouldShowOverlay]); + + useEffect(() => { + if (shouldShowReminder) { + setLastInteraction('bookmark'); + } + }, [shouldShowReminder]); const overlay = useMemo(() => { - if (shouldShowOverlay && onShare) { + if (shouldShowOverlay && onShare && lastInteraction == 'upvote') { return ( + + + ); + } + + if (shouldShowOverlay && onShare) { + return ( + { + onInteract(); + setLastInteraction(null); + }} + onShare={() => { + onInteract(); + onShare(post); + setLastInteraction(null); + }} + /> + ); + } + return undefined; }, [ className?.bookmark?.container, @@ -69,6 +117,7 @@ export const useCardCover = ({ post, shouldShowOverlay, shouldShowReminder, + lastInteraction ]); return { overlay }; From 23390ebfb157fcb9bdb4847dc3bb3c5081b08098 Mon Sep 17 00:00:00 2001 From: Abhi-Bohora Date: Tue, 19 Nov 2024 10:00:34 +0545 Subject: [PATCH 2/8] checking with === --- packages/shared/src/hooks/feed/useCardCover.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/shared/src/hooks/feed/useCardCover.tsx b/packages/shared/src/hooks/feed/useCardCover.tsx index 327cb35fd8..a0f5ccc276 100644 --- a/packages/shared/src/hooks/feed/useCardCover.tsx +++ b/packages/shared/src/hooks/feed/useCardCover.tsx @@ -28,21 +28,21 @@ export const useCardCover = ({ }: UseCardCoverProps): UseCardCover => { const { shouldShowOverlay, onInteract } = usePostShareLoop(post); const shouldShowReminder = useBookmarkReminderCover(post); - const [lastInteraction, setLastInteraction] = useState<'upvote' | 'bookmark' | null>(null); + const [interaction, setInteraction] = useState<'upvote' | 'bookmark' | null>(null); useEffect(() => { if (shouldShowOverlay) { - setLastInteraction('upvote'); + setInteraction('upvote'); } }, [shouldShowOverlay]); useEffect(() => { if (shouldShowReminder) { - setLastInteraction('bookmark'); + setInteraction('bookmark'); } }, [shouldShowReminder]); const overlay = useMemo(() => { - if (shouldShowOverlay && onShare && lastInteraction == 'upvote') { + if (shouldShowOverlay && onShare && interaction === 'upvote') { return ( { onInteract(); - setLastInteraction(null); + setInteraction(null); }} onShare={() => { onInteract(); onShare(post); - setLastInteraction(null); + setInteraction(null); }} /> ); @@ -117,7 +117,7 @@ export const useCardCover = ({ post, shouldShowOverlay, shouldShowReminder, - lastInteraction + interaction ]); return { overlay }; From 9b24e8a4d63f2fad470a64c7e2966c9534e71305 Mon Sep 17 00:00:00 2001 From: Abhi-Bohora Date: Tue, 19 Nov 2024 14:26:16 +0545 Subject: [PATCH 3/8] removed unnecessary if statements --- .../shared/src/hooks/feed/useCardCover.tsx | 40 +------------------ 1 file changed, 2 insertions(+), 38 deletions(-) diff --git a/packages/shared/src/hooks/feed/useCardCover.tsx b/packages/shared/src/hooks/feed/useCardCover.tsx index a0f5ccc276..aa0b92aa0c 100644 --- a/packages/shared/src/hooks/feed/useCardCover.tsx +++ b/packages/shared/src/hooks/feed/useCardCover.tsx @@ -42,7 +42,7 @@ export const useCardCover = ({ }, [shouldShowReminder]); const overlay = useMemo(() => { - if (shouldShowOverlay && onShare && interaction === 'upvote') { + if (shouldShowOverlay && onShare && (interaction === 'upvote' || !shouldShowReminder)) { return ( - - - ); - } - - if (shouldShowOverlay && onShare) { - return ( - { - onInteract(); - setInteraction(null); - }} - onShare={() => { - onInteract(); - onShare(post); - setInteraction(null); - }} - /> - ); - } - return undefined; }, [ className?.bookmark?.container, From ba7b38e50d5981337510feadc92e645cf06b97d0 Mon Sep 17 00:00:00 2001 From: Abhi-Bohora Date: Thu, 21 Nov 2024 14:23:32 +0545 Subject: [PATCH 4/8] tracking current interaction --- .../shared/src/hooks/feed/useCardCover.tsx | 22 ++++------------- .../shared/src/hooks/post/usePostShareLoop.ts | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/shared/src/hooks/feed/useCardCover.tsx b/packages/shared/src/hooks/feed/useCardCover.tsx index aa0b92aa0c..210f7cd308 100644 --- a/packages/shared/src/hooks/feed/useCardCover.tsx +++ b/packages/shared/src/hooks/feed/useCardCover.tsx @@ -1,4 +1,4 @@ -import React, { ReactNode, useEffect, useMemo, useState } from 'react'; +import React, { ReactNode, useMemo } from 'react'; import { Post } from '../../graphql/posts'; import { usePostShareLoop } from '../post/usePostShareLoop'; import { CardCoverShare } from '../../components/cards/common/CardCoverShare'; @@ -26,23 +26,11 @@ export const useCardCover = ({ onShare, className = {}, }: UseCardCoverProps): UseCardCover => { - const { shouldShowOverlay, onInteract } = usePostShareLoop(post); + const { shouldShowOverlay, onInteract, currentInteraction } = usePostShareLoop(post); const shouldShowReminder = useBookmarkReminderCover(post); - const [interaction, setInteraction] = useState<'upvote' | 'bookmark' | null>(null); - useEffect(() => { - if (shouldShowOverlay) { - setInteraction('upvote'); - } - }, [shouldShowOverlay]); - - useEffect(() => { - if (shouldShowReminder) { - setInteraction('bookmark'); - } - }, [shouldShowReminder]); const overlay = useMemo(() => { - if (shouldShowOverlay && onShare && (interaction === 'upvote' || !shouldShowReminder)) { + if (shouldShowOverlay && onShare && currentInteraction === 'upvote') { return ( void; + currentInteraction: 'upvote' | 'bookmark' | null } export const usePostShareLoop = (post: Post): UsePostShareLoop => { @@ -18,6 +19,8 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { const [justUpvoted, setJustUpvoted] = useState(false); const [hasInteracted, setHasInteracted] = useState(false); const shouldShowOverlay = justUpvoted && !hasInteracted; + const [isBookmarked, setIsBookmarked] = useState(false); + const [lastInteraction, setLastInteraction] = useState<'upvote' | 'bookmark' | null>(null); const key = useMemo( () => createVoteMutationKey({ @@ -39,11 +42,32 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { } setJustUpvoted(vars.vote === UserVote.Up); + if(vars.vote === UserVote.Up){ + setLastInteraction('upvote') + } }, }); + useMemo(() => { + const bookmarked = post?.bookmarked; + setIsBookmarked(!!bookmarked); + if (bookmarked) { + setLastInteraction('bookmark'); + } + }, [post?.bookmarked]); + + const currentInteraction = useMemo(() => { + if (justUpvoted && isBookmarked) { + return lastInteraction; + } + if (justUpvoted) return 'upvote'; + if (isBookmarked) return 'bookmark'; + return null; + }, [justUpvoted, isBookmarked, lastInteraction]); + return { shouldShowOverlay, onInteract: useCallback(() => setHasInteracted(true), []), + currentInteraction }; }; From 8e0d896d224c63799d4b997d904ad3c7efa00b87 Mon Sep 17 00:00:00 2001 From: Abhi-Bohora Date: Thu, 21 Nov 2024 14:38:44 +0545 Subject: [PATCH 5/8] fix:lint error --- packages/shared/src/hooks/post/usePostShareLoop.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/shared/src/hooks/post/usePostShareLoop.ts b/packages/shared/src/hooks/post/usePostShareLoop.ts index 31dc65b333..f0b733eff3 100644 --- a/packages/shared/src/hooks/post/usePostShareLoop.ts +++ b/packages/shared/src/hooks/post/usePostShareLoop.ts @@ -11,7 +11,7 @@ import { Post, UserVote } from '../../graphql/posts'; interface UsePostShareLoop { shouldShowOverlay: boolean; onInteract: () => void; - currentInteraction: 'upvote' | 'bookmark' | null + currentInteraction: 'upvote' | 'bookmark' | null; } export const usePostShareLoop = (post: Post): UsePostShareLoop => { @@ -20,7 +20,9 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { const [hasInteracted, setHasInteracted] = useState(false); const shouldShowOverlay = justUpvoted && !hasInteracted; const [isBookmarked, setIsBookmarked] = useState(false); - const [lastInteraction, setLastInteraction] = useState<'upvote' | 'bookmark' | null>(null); + const [lastInteraction, setLastInteraction] = useState< + 'upvote' | 'bookmark' | null + >(null); const key = useMemo( () => createVoteMutationKey({ @@ -42,8 +44,8 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { } setJustUpvoted(vars.vote === UserVote.Up); - if(vars.vote === UserVote.Up){ - setLastInteraction('upvote') + if (vars.vote === UserVote.Up) { + setLastInteraction('upvote'); } }, }); @@ -68,6 +70,6 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { return { shouldShowOverlay, onInteract: useCallback(() => setHasInteracted(true), []), - currentInteraction + currentInteraction, }; }; From 98b6b1f0dc1ab0d8c30ad456816a10ab58db9958 Mon Sep 17 00:00:00 2001 From: Abhi-Bohora Date: Thu, 21 Nov 2024 14:51:19 +0545 Subject: [PATCH 6/8] fix:lint err --- packages/shared/src/hooks/feed/useCardCover.tsx | 5 +++-- packages/shared/src/hooks/post/usePostShareLoop.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/shared/src/hooks/feed/useCardCover.tsx b/packages/shared/src/hooks/feed/useCardCover.tsx index 210f7cd308..e731ca4a83 100644 --- a/packages/shared/src/hooks/feed/useCardCover.tsx +++ b/packages/shared/src/hooks/feed/useCardCover.tsx @@ -26,7 +26,8 @@ export const useCardCover = ({ onShare, className = {}, }: UseCardCoverProps): UseCardCover => { - const { shouldShowOverlay, onInteract, currentInteraction } = usePostShareLoop(post); + const { shouldShowOverlay, onInteract, currentInteraction } = + usePostShareLoop(post); const shouldShowReminder = useBookmarkReminderCover(post); const overlay = useMemo(() => { @@ -69,7 +70,7 @@ export const useCardCover = ({ post, shouldShowOverlay, shouldShowReminder, - currentInteraction + currentInteraction, ]); return { overlay }; diff --git a/packages/shared/src/hooks/post/usePostShareLoop.ts b/packages/shared/src/hooks/post/usePostShareLoop.ts index f0b733eff3..c1b21fc947 100644 --- a/packages/shared/src/hooks/post/usePostShareLoop.ts +++ b/packages/shared/src/hooks/post/usePostShareLoop.ts @@ -62,8 +62,12 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { if (justUpvoted && isBookmarked) { return lastInteraction; } - if (justUpvoted) return 'upvote'; - if (isBookmarked) return 'bookmark'; + if (justUpvoted) { + return 'upvote'; + } + if (isBookmarked) { + return 'bookmark'; + } return null; }, [justUpvoted, isBookmarked, lastInteraction]); From 7c5305f55f7c0670e623b25af690d4e040300f87 Mon Sep 17 00:00:00 2001 From: Abhi-Bohora Date: Fri, 22 Nov 2024 09:10:35 +0545 Subject: [PATCH 7/8] using useBookmarkReminderCover --- .../shared/src/hooks/post/usePostShareLoop.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/shared/src/hooks/post/usePostShareLoop.ts b/packages/shared/src/hooks/post/usePostShareLoop.ts index c1b21fc947..8df07403f1 100644 --- a/packages/shared/src/hooks/post/usePostShareLoop.ts +++ b/packages/shared/src/hooks/post/usePostShareLoop.ts @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useState } from 'react'; +import { useCallback, useEffect, useMemo, useState } from 'react'; import { useActiveFeedNameContext } from '../../contexts'; import { useMutationSubscription } from '../mutationSubscription'; import { @@ -7,6 +7,7 @@ import { createVoteMutationKey, } from '../vote'; import { Post, UserVote } from '../../graphql/posts'; +import { useBookmarkReminderCover } from '../bookmark/useBookmarkReminderCover'; interface UsePostShareLoop { shouldShowOverlay: boolean; @@ -19,7 +20,7 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { const [justUpvoted, setJustUpvoted] = useState(false); const [hasInteracted, setHasInteracted] = useState(false); const shouldShowOverlay = justUpvoted && !hasInteracted; - const [isBookmarked, setIsBookmarked] = useState(false); + const shouldShowReminder = useBookmarkReminderCover(post); const [lastInteraction, setLastInteraction] = useState< 'upvote' | 'bookmark' | null >(null); @@ -50,26 +51,24 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { }, }); - useMemo(() => { - const bookmarked = post?.bookmarked; - setIsBookmarked(!!bookmarked); - if (bookmarked) { + useEffect(() => { + if (shouldShowReminder) { setLastInteraction('bookmark'); } - }, [post?.bookmarked]); + }, [shouldShowReminder]); const currentInteraction = useMemo(() => { - if (justUpvoted && isBookmarked) { + if (justUpvoted && shouldShowReminder) { return lastInteraction; } if (justUpvoted) { return 'upvote'; } - if (isBookmarked) { + if (shouldShowReminder) { return 'bookmark'; } return null; - }, [justUpvoted, isBookmarked, lastInteraction]); + }, [justUpvoted, shouldShowReminder, lastInteraction]); return { shouldShowOverlay, From a7ef2e4b5e64d360ba7d7d0bedb0165830a01c66 Mon Sep 17 00:00:00 2001 From: Abhi-Bohora Date: Sun, 24 Nov 2024 09:06:37 +0545 Subject: [PATCH 8/8] returning shouldShowReminder from hook --- packages/shared/src/hooks/feed/useCardCover.tsx | 10 ++++++---- packages/shared/src/hooks/post/usePostShareLoop.ts | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/shared/src/hooks/feed/useCardCover.tsx b/packages/shared/src/hooks/feed/useCardCover.tsx index e731ca4a83..3111ad456a 100644 --- a/packages/shared/src/hooks/feed/useCardCover.tsx +++ b/packages/shared/src/hooks/feed/useCardCover.tsx @@ -5,7 +5,6 @@ import { CardCoverShare } from '../../components/cards/common/CardCoverShare'; import { CardCoverContainer } from '../../components/cards/common/CardCoverContainer'; import { PostReminderOptions } from '../../components/post/common/PostReminderOptions'; import { ButtonSize, ButtonVariant } from '../../components/buttons/common'; -import { useBookmarkReminderCover } from '../bookmark/useBookmarkReminderCover'; interface UseCardCover { overlay: ReactNode; @@ -26,9 +25,12 @@ export const useCardCover = ({ onShare, className = {}, }: UseCardCoverProps): UseCardCover => { - const { shouldShowOverlay, onInteract, currentInteraction } = - usePostShareLoop(post); - const shouldShowReminder = useBookmarkReminderCover(post); + const { + shouldShowOverlay, + onInteract, + currentInteraction, + shouldShowReminder, + } = usePostShareLoop(post); const overlay = useMemo(() => { if (shouldShowOverlay && onShare && currentInteraction === 'upvote') { diff --git a/packages/shared/src/hooks/post/usePostShareLoop.ts b/packages/shared/src/hooks/post/usePostShareLoop.ts index 8df07403f1..414667f827 100644 --- a/packages/shared/src/hooks/post/usePostShareLoop.ts +++ b/packages/shared/src/hooks/post/usePostShareLoop.ts @@ -13,6 +13,7 @@ interface UsePostShareLoop { shouldShowOverlay: boolean; onInteract: () => void; currentInteraction: 'upvote' | 'bookmark' | null; + shouldShowReminder: boolean; } export const usePostShareLoop = (post: Post): UsePostShareLoop => { @@ -74,5 +75,6 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => { shouldShowOverlay, onInteract: useCallback(() => setHasInteracted(true), []), currentInteraction, + shouldShowReminder, }; };