-
Notifications
You must be signed in to change notification settings - Fork 250
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
feat: show toast when post is shared by user #4251
base: main
Are you sure you want to change the base?
Changes from all commits
bf0bd90
600ff0a
5e5b6b3
4722612
8cbd593
719cda9
7490436
c4d908d
e19acd0
763d20d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
import Link from 'next/link'; | ||
import { useAuthContext } from '../contexts/AuthContext'; | ||
import { useToastNotification } from './useToastNotification'; | ||
import { useContentPreferenceStatusQuery } from './contentPreference/useContentPreferenceStatusQuery'; | ||
import { | ||
ContentPreferenceStatus, | ||
ContentPreferenceType, | ||
} from '../graphql/contentPreference'; | ||
import { useUserShortByIdQuery } from './user/useUserShortByIdQuery'; | ||
import { useContentPreference } from './contentPreference/useContentPreference'; | ||
import { ButtonSize } from '../components/buttons/common'; | ||
import { ProfileImageSize, ProfilePicture } from '../components/ProfilePicture'; | ||
import { Button } from '../components/buttons/Button'; | ||
import { Typography, TypographyTag } from '../components/typography/Typography'; | ||
|
||
const useSharedByToast = (): void => { | ||
const hasShownToast = useRef(false); | ||
const { user: currentUser, isAuthReady } = useAuthContext(); | ||
const { query } = useRouter(); | ||
const userId = (query?.userid as string) || null; | ||
const isSameUser = !!userId && userId === currentUser?.id; | ||
const { data: contentPreference, isPending } = | ||
useContentPreferenceStatusQuery({ | ||
id: userId, | ||
entity: ContentPreferenceType.User, | ||
}); | ||
const { data: user } = useUserShortByIdQuery({ id: userId }); | ||
const { follow } = useContentPreference({ showToastOnSuccess: true }); | ||
const { displayToast, dismissToast } = useToastNotification(); | ||
const isDataReady = isAuthReady || (currentUser && !isPending); | ||
|
||
useEffect(() => { | ||
if (!user || isSameUser || !isDataReady || hasShownToast.current) { | ||
return; | ||
} | ||
|
||
const showFollow = | ||
!!currentUser && | ||
contentPreference?.status !== ContentPreferenceStatus.Follow; | ||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we want to check even if the target user is currently blocked |
||
|
||
setTimeout(() => { | ||
hasShownToast.current = true; | ||
displayToast( | ||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not really a fan of using setTimeout inside useEffect, but I think we should handle the case where a user lands on the page and then quickly navigates back to the home page. I suggest adding a ref to store the timerId, then clearing the timer on unmount. We should also clear it if the userId changes to prevent issues when navigating between shared posts. Wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 we have to clear timeout, it introduces memory leaks |
||
<div className="flex items-center gap-4 "> | ||
<Link | ||
onClick={() => dismissToast()} | ||
className="flex items-center gap-2 " | ||
href={`/${user.username}`} | ||
> | ||
<ProfilePicture user={user} size={ProfileImageSize.Medium} /> | ||
<span> | ||
<Typography tag={TypographyTag.Span} bold> | ||
{user.name} | ||
</Typography>{' '} | ||
<Typography tag={TypographyTag.Span}>shared this post</Typography> | ||
</span> | ||
</Link> | ||
{showFollow && ( | ||
<Button | ||
onClick={() => { | ||
follow({ | ||
entity: ContentPreferenceType.User, | ||
id: user?.id, | ||
entityName: `@${user.username}`, | ||
}); | ||
}} | ||
className="bg-background-default text-text-primary" | ||
size={ButtonSize.Small} | ||
> | ||
Follow | ||
</Button> | ||
)} | ||
</div>, | ||
); | ||
// Set a small timeout to ensure its shown after the page is loaded and won't be cleared by updates. | ||
}, 1000); | ||
}, [ | ||
user, | ||
contentPreference?.status, | ||
hasShownToast, | ||
displayToast, | ||
dismissToast, | ||
follow, | ||
isSameUser, | ||
isDataReady, | ||
currentUser, | ||
]); | ||
}; | ||
|
||
export default useSharedByToast; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we currently blocking those queries if the post author is the current logged in user? We should avoid network calls in this case.