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: profile follow button #3575

Merged
merged 5 commits into from
Sep 25, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const FollowButton = ({

const isLoading = isLoadingFollow || isLoadingNotify;

if (user.id === userId) {
if (!user || user.id === userId) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Edge-case :)

return null;
}

Expand Down
16 changes: 12 additions & 4 deletions packages/shared/src/components/profile/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React, { CSSProperties, ReactElement, useState } from 'react';
import classNames from 'classnames';
import { PublicProfile } from '../../lib/user';
import { ShareIcon, SettingsIcon } from '../icons';
import { LoggedUser, PublicProfile } from '../../lib/user';
import { SettingsIcon, ShareIcon } from '../icons';
import { Button, ButtonSize, ButtonVariant } from '../buttons/Button';
import { useShareOrCopyLink } from '../../hooks/useShareOrCopyLink';
import { ProfileImageSize, ProfilePicture } from '../ProfilePicture';
import { largeNumberFormat, ReferralCampaignKey } from '../../lib';
import { ProfileSettingsMenu } from './ProfileSettingsMenu';
import { RootPortal } from '../tooltips/Portal';
import { GoBackButton } from '../post/GoBackHeaderMobile';
import { ViewSize, useViewSize } from '../../hooks';
import { useViewSize, ViewSize } from '../../hooks';
import { FollowButton } from '../contentPreference/FollowButton';
import { ContentPreferenceType } from '../../graphql/contentPreference';

export interface HeaderProps {
user: PublicProfile;
Expand All @@ -34,7 +36,6 @@ export function Header({
});
const isMobile = useViewSize(ViewSize.MobileL);
const [isMenuOpen, setIsMenuOpen] = useState(false);

return (
<header
className={classNames('flex h-12 items-center px-4', className)}
Expand Down Expand Up @@ -80,6 +81,13 @@ export function Header({
icon={<ShareIcon />}
onClick={() => onShareOrCopyLink()}
/>
<FollowButton
userId={user.id}
type={ContentPreferenceType.User}
status={(user as LoggedUser).contentPreference?.status}
entityName={`@${user.username}`}
className="ml-2"
/>
{isSameUser && (
<>
<Button
Expand Down
23 changes: 14 additions & 9 deletions packages/shared/src/hooks/profile/useProfile.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { useContext } from 'react';
import { useQuery } from '@tanstack/react-query';
import { getProfile, PublicProfile } from '../../lib/user';
import AuthContext from '../../contexts/AuthContext';
import { generateQueryKey, RequestKey, StaleTime } from '../../lib/query';
import { disabledRefetch } from '../../lib/func';
import { useAuthContext } from '../../contexts/AuthContext';

export function useProfile(initialUser?: PublicProfile): PublicProfile {
const { user: loggedUser } = useContext(AuthContext);
const isSameUser = loggedUser?.id === initialUser?.id;

export function useProfile(initialUser?: PublicProfile): {
user: PublicProfile;
userQueryKey: unknown[];
} {
const { user: loggedUser } = useAuthContext();
const userQueryKey = generateQueryKey(RequestKey.Profile, initialUser, {
id: loggedUser?.id,
});
const { data: user } = useQuery(
generateQueryKey(RequestKey.Profile, initialUser),
userQueryKey,
() => getProfile(initialUser?.id),
{
...disabledRefetch,
cacheTime: StaleTime.OneHour,
initialData: initialUser,
enabled: !!initialUser && isSameUser,
},
);

return user;
return {
user,
userQueryKey,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { RequestKey } from '../../lib/query';
import { useMutationSubscription } from '../mutationSubscription';
import {
contentPreferenceMutationMatcher,
mutationKeyToContentPreferenceStatusMap,
} from '../contentPreference/types';
import { PublicProfile } from '../../lib/user';

type UseProfileContentPreferenceMutationSubscriptionProps = {
queryKey: unknown[];
};

type UseProfileContentPreferenceMutationSubscription = ReturnType<
typeof useMutationSubscription
>;

export const useProfileContentPreferenceMutationSubscription = ({
queryKey,
}: UseProfileContentPreferenceMutationSubscriptionProps): UseProfileContentPreferenceMutationSubscription => {
return useMutationSubscription({
matcher: contentPreferenceMutationMatcher,
callback: ({ mutation, queryClient: mutationQueryClient }) => {
const currentData = mutationQueryClient.getQueryData(queryKey);
const [requestKey] = mutation.options.mutationKey as [
RequestKey,
...unknown[],
];

if (!currentData) {
return;
}

const nextStatus = mutationKeyToContentPreferenceStatusMap[requestKey];

mutationQueryClient.setQueryData<PublicProfile>(queryKey, (data) => {
return {
...data,
contentPreference: nextStatus
? {
...data.contentPreference,
status: nextStatus,
}
: undefined,
};
});
},
});
};
1 change: 1 addition & 0 deletions packages/shared/src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface PublicProfile {
readmeHtml?: string;
readme?: string;
companies?: Company[];
contentPreference?: ContentPreference;
}

export enum UserExperienceLevel {
Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/components/layouts/ProfileLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function ProfileLayout({
}: ProfileLayoutProps): ReactElement {
const router = useRouter();
const { isFallback } = router;
const user = useProfile(initialUser);
const { user } = useProfile(initialUser);

if (!isFallback && !user) {
return <Custom404 />;
Expand Down
6 changes: 5 additions & 1 deletion packages/webapp/pages/[userId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useProfile } from '@dailydotdev/shared/src/hooks/profile/useProfile';
import { useJoinReferral } from '@dailydotdev/shared/src/hooks';
import { useReadingStreak } from '@dailydotdev/shared/src/hooks/streaks';
import { gqlClient } from '@dailydotdev/shared/src/graphql/common';
import { useProfileContentPreferenceMutationSubscription } from '@dailydotdev/shared/src/hooks/profile/useProfileContentPreferenceMutationSubscription';
import {
getLayout as getProfileLayout,
getStaticPaths as getProfileStaticPaths,
Expand All @@ -36,7 +37,10 @@ const ProfilePage = ({
const { selectedHistoryYear, before, after, yearOptions, fullHistory } =
useActivityTimeFilter();

const user = useProfile(initialUser);
const { user, userQueryKey } = useProfile(initialUser);
useProfileContentPreferenceMutationSubscription({
queryKey: userQueryKey,
});

const { data: readingHistory, isLoading } = useQuery<ProfileReadingData>(
generateQueryKey(RequestKey.ReadingStats, user, selectedHistoryYear),
Expand Down