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

feat: show toast when post is shared by user #4251

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
65 changes: 0 additions & 65 deletions packages/shared/src/components/SharedByUserBanner.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions packages/shared/src/components/post/PostContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import { cloudinaryPostImageCoverPlaceholder } from '../../lib/image';
import { withPostById } from './withPostById';
import { PostClickbaitShield } from './common/PostClickbaitShield';
import { useSmartTitle } from '../../hooks/post/useSmartTitle';
import { SharedByUserBanner } from '../SharedByUserBanner';
import { SmartPrompt } from './smartPrompts/SmartPrompt';
import useSharedByToast from '../../hooks/useSharedByToast';
import { PostTagList } from './tags/PostTagList';

export const SCROLL_OFFSET = 80;
Expand Down Expand Up @@ -71,6 +71,7 @@ export function PostContentRaw({
'laptop:flex-row laptop:pb-0',
className?.container,
);
useSharedByToast();

const navigationProps: PostNavigationProps = {
postPosition,
Expand Down Expand Up @@ -147,7 +148,6 @@ export function PostContentRaw({
origin={origin}
post={post}
>
<SharedByUserBanner />
<div className="my-6">
<h1
className="break-words font-bold typo-large-title"
Expand Down
5 changes: 2 additions & 3 deletions packages/shared/src/components/post/SquadPostContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { PostContentProps, PostNavigationProps } from './common';
import ShareYouTubeContent from './ShareYouTubeContent';
import { useViewPost } from '../../hooks/post';
import { withPostById } from './withPostById';
import { SharedByUserBanner } from '../SharedByUserBanner';
import useSharedByToast from '../../hooks/useSharedByToast';

const ContentMap = {
[PostType.Freeform]: MarkdownPostContent,
Expand Down Expand Up @@ -51,7 +51,6 @@ function SquadPostContentRaw({
source: post?.source,
user: post?.author,
});

const navigationProps: PostNavigationProps = {
post,
onPreviousPost,
Expand All @@ -61,6 +60,7 @@ function SquadPostContentRaw({
inlineActions,
onRemovePost,
};
useSharedByToast();

useEffect(() => {
if (!post?.id || !user?.id) {
Expand Down Expand Up @@ -117,7 +117,6 @@ function SquadPostContentRaw({
origin={origin}
post={post}
>
<SharedByUserBanner className="mb-3" />
<PostSourceInfo
source={post.source}
className={classNames('!typo-body', customNavigation && 'mt-6')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { webappUrl } from '../../../lib/constants';
import { useViewPost } from '../../../hooks/post/useViewPost';
import { DateFormat } from '../../utilities';
import { withPostById } from '../withPostById';
import { SharedByUserBanner } from '../../SharedByUserBanner';
import useSharedByToast from '../../../hooks/useSharedByToast';
import { PostTagList } from '../tags/PostTagList';

const CollectionPostContentRaw = ({
Expand Down Expand Up @@ -67,6 +67,7 @@ const CollectionPostContentRaw = ({
inlineActions,
onRemovePost,
};
useSharedByToast();

const onSendViewPost = useViewPost();

Expand Down Expand Up @@ -118,7 +119,6 @@ const CollectionPostContentRaw = ({
origin={origin}
post={post}
>
<SharedByUserBanner className="mb-3" />
<div
className={classNames(
'mb-6 flex flex-col gap-6',
Expand Down
92 changes: 92 additions & 0 deletions packages/shared/src/hooks/useSharedByToast.tsx
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 });
Comment on lines +24 to +30
Copy link
Contributor

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.

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

@ilasw ilasw Mar 4, 2025

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

@capJavert capJavert Mar 4, 2025

Choose a reason for hiding this comment

The 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;
10 changes: 7 additions & 3 deletions packages/shared/src/hooks/useToastNotification.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { ReactNode } from 'react';
import { useMemo } from 'react';
import { useQuery, useQueryClient } from '@tanstack/react-query';

type AnyFunction = (() => Promise<unknown>) | (() => unknown);

interface UseToastNotification {
displayToast: (message: string, params?: NotifyOptionalProps) => void;
displayToast: (
message: string | ReactNode,
params?: NotifyOptionalProps,
) => void;
dismissToast: () => void;
subject?: ToastSubject;
}
Expand All @@ -15,7 +19,7 @@ export enum ToastSubject {
}

export interface ToastNotification {
message: string;
message: string | ReactNode;
timer: number;
subject?: ToastSubject;
onUndo?: AnyFunction;
Expand All @@ -39,7 +43,7 @@ export const useToastNotification = (): UseToastNotification => {
client.setQueryData(TOAST_NOTIF_KEY, data);

const displayToast = (
message: string,
message: string | ReactNode,
{ timer = 5000, ...props }: NotifyOptionalProps = {},
) => setToastNotification({ message, timer, ...props });

Expand Down