Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: card overlay not toggling between upvote and bookmark #3831

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/shared/src/hooks/feed/useCardCover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export const useCardCover = ({
onShare,
className = {},
}: UseCardCoverProps): UseCardCover => {
const { shouldShowOverlay, onInteract } = usePostShareLoop(post);
const { shouldShowOverlay, onInteract, currentInteraction } =
usePostShareLoop(post);
const shouldShowReminder = useBookmarkReminderCover(post);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now remove this line and have it coming from the hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry but which line are you referring this line ? const shouldShowReminder = useBookmarkReminderCover(post);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this exact line 31. Then from the hook, we return shouldShowReminder from inside of it and be used here.


const overlay = useMemo(() => {
if (shouldShowOverlay && onShare) {
if (shouldShowOverlay && onShare && currentInteraction === 'upvote') {
return (
<CardCoverShare
post={post}
Expand All @@ -43,7 +44,7 @@ export const useCardCover = ({
);
}

if (shouldShowReminder) {
if (shouldShowReminder && currentInteraction === 'bookmark') {
return (
<CardCoverContainer
title="Don’t have time now? Set a reminder"
Expand All @@ -69,6 +70,7 @@ export const useCardCover = ({
post,
shouldShowOverlay,
shouldShowReminder,
currentInteraction,
]);

return { overlay };
Expand Down
31 changes: 30 additions & 1 deletion packages/shared/src/hooks/post/usePostShareLoop.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -7,17 +7,23 @@ import {
createVoteMutationKey,
} from '../vote';
import { Post, UserVote } from '../../graphql/posts';
import { useBookmarkReminderCover } from '../bookmark/useBookmarkReminderCover';

interface UsePostShareLoop {
shouldShowOverlay: boolean;
onInteract: () => void;
currentInteraction: 'upvote' | 'bookmark' | null;
}

export const usePostShareLoop = (post: Post): UsePostShareLoop => {
const { feedName } = useActiveFeedNameContext();
const [justUpvoted, setJustUpvoted] = useState(false);
const [hasInteracted, setHasInteracted] = useState(false);
const shouldShowOverlay = justUpvoted && !hasInteracted;
const shouldShowReminder = useBookmarkReminderCover(post);
const [lastInteraction, setLastInteraction] = useState<
'upvote' | 'bookmark' | null
>(null);
const key = useMemo(
() =>
createVoteMutationKey({
Expand All @@ -39,11 +45,34 @@ export const usePostShareLoop = (post: Post): UsePostShareLoop => {
}

setJustUpvoted(vars.vote === UserVote.Up);
if (vars.vote === UserVote.Up) {
setLastInteraction('upvote');
}
},
});

useEffect(() => {
if (shouldShowReminder) {
setLastInteraction('bookmark');
}
}, [shouldShowReminder]);

const currentInteraction = useMemo(() => {
if (justUpvoted && shouldShowReminder) {
return lastInteraction;
}
if (justUpvoted) {
return 'upvote';
}
if (shouldShowReminder) {
return 'bookmark';
}
return null;
}, [justUpvoted, shouldShowReminder, lastInteraction]);

return {
shouldShowOverlay,
onInteract: useCallback(() => setHasInteracted(true), []),
currentInteraction,
};
};