Skip to content

Commit 9508fc0

Browse files
CopilotomBratteng
andcommitted
Use query param for preview mode and move toggle to sidebar
Co-authored-by: omBratteng <1681525+omBratteng@users.noreply.github.com>
1 parent be82962 commit 9508fc0

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

packages/shared/src/features/profile/components/ProfileWidgets/ProfileWidgets.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import classNames from 'classnames';
44
import { useQuery } from '@tanstack/react-query';
55
import { startOfTomorrow, subDays, subMonths } from 'date-fns';
66
import dynamic from 'next/dynamic';
7+
import { useRouter } from 'next/router';
78
import { useAuthContext } from '../../../../contexts/AuthContext';
89
import { ActiveOrRecomendedSquads } from './ActiveOrRecomendedSquads';
910
import type { ProfileReadingData, ProfileV2 } from '../../../../graphql/users';
@@ -16,6 +17,7 @@ import { ProfileCompletion } from './ProfileCompletion';
1617
import { Share } from './Share';
1718
import { ProfileViewsWidget } from './ProfileViewsWidget';
1819
import { useProfileCompletionIndicator } from '../../../../hooks/profile/useProfileCompletionIndicator';
20+
import { ProfilePreviewToggle } from '../../../../components/profile/ProfilePreviewToggle';
1921

2022
const BadgesAndAwards = dynamic(() =>
2123
import('./BadgesAndAwards').then((mod) => mod.BadgesAndAwards),
@@ -31,10 +33,29 @@ export function ProfileWidgets({
3133
sources,
3234
className,
3335
}: ProfileWidgetsProps): ReactElement {
36+
const router = useRouter();
3437
const { user: loggedUser, tokenRefreshed } = useAuthContext();
3538
const { showIndicator: showProfileCompletion } =
3639
useProfileCompletionIndicator();
3740
const isSameUser = loggedUser?.id === user.id;
41+
const isPreviewMode = router.query.preview === 'true';
42+
43+
const handleTogglePreview = () => {
44+
const newQuery = { ...router.query };
45+
if (isPreviewMode) {
46+
delete newQuery.preview;
47+
} else {
48+
newQuery.preview = 'true';
49+
}
50+
router.push(
51+
{
52+
pathname: router.pathname,
53+
query: newQuery,
54+
},
55+
undefined,
56+
{ shallow: true },
57+
);
58+
};
3859

3960
const before = startOfTomorrow();
4061
const after = subMonths(subDays(before, 2), 5);
@@ -64,6 +85,12 @@ export function ProfileWidgets({
6485
className,
6586
)}
6687
>
88+
{isSameUser && (
89+
<ProfilePreviewToggle
90+
isPreviewMode={isPreviewMode}
91+
onToggle={handleTogglePreview}
92+
/>
93+
)}
6794
{isSameUser && showProfileCompletion && (
6895
<ProfileCompletion className="hidden laptop:flex" />
6996
)}

packages/webapp/pages/[userId]/index.tsx

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ReactElement } from 'react';
2-
import React, { useMemo, useState } from 'react';
2+
import React, { useMemo } from 'react';
33
import { AboutMe } from '@dailydotdev/shared/src/features/profile/components/AboutMe';
44
import { Activity } from '@dailydotdev/shared/src/features/profile/components/Activity';
55
import { useProfile } from '@dailydotdev/shared/src/hooks/profile/useProfile';
@@ -27,7 +27,7 @@ import { Header } from '@dailydotdev/shared/src/components/profile/Header';
2727
import classNames from 'classnames';
2828
import { ProfileCompletion } from '@dailydotdev/shared/src/features/profile/components/ProfileWidgets/ProfileCompletion';
2929
import { Share } from '@dailydotdev/shared/src/features/profile/components/ProfileWidgets/Share';
30-
import { ProfilePreviewToggle } from '@dailydotdev/shared/src/components/profile/ProfilePreviewToggle';
30+
import { useRouter } from 'next/router';
3131
import {
3232
getLayout as getProfileLayout,
3333
getProfileSeoDefaults,
@@ -44,15 +44,25 @@ const ProfilePage = ({
4444
sources,
4545
}: ProfileLayoutProps): ReactElement => {
4646
useJoinReferral();
47+
const router = useRouter();
4748
const { status, onUpload, shouldShow } = useUploadCv();
4849
const { checkHasCompleted } = useActions();
4950
const hasClosedBanner = useMemo(
5051
() => checkHasCompleted(ActionType.ClosedProfileBanner),
5152
[checkHasCompleted],
5253
);
5354

54-
const { user, isUserSame } = useProfile(initialUser);
55-
const [isPreviewMode, setIsPreviewMode] = useState(false);
55+
const { user, isUserSame: isUserSameBase } = useProfile(initialUser);
56+
57+
// Check if preview mode is enabled via query param
58+
const isPreviewMode = router.query.preview === 'true';
59+
60+
// When in preview mode, act as a visitor (not same user)
61+
const isSameUser = useMemo(
62+
() => isUserSameBase && !isPreviewMode,
63+
[isUserSameBase, isPreviewMode],
64+
);
65+
5666
const { ref: stickyRef, progress: stickyProgress } =
5767
useDynamicHeader<HTMLDivElement>(true);
5868
const hideSticky = !stickyProgress;
@@ -61,41 +71,29 @@ const ProfilePage = ({
6171
...getProfileSeoDefaults(user, {}, noindex),
6272
};
6373

64-
const shouldShowBanner = isUserSame && shouldShow && !hasClosedBanner;
65-
66-
// When in preview mode, hide owner-only elements
67-
const showAsVisitor = isUserSame && isPreviewMode;
74+
const shouldShowBanner = isSameUser && shouldShow && !hasClosedBanner;
6875

6976
return (
7077
<div className="rounded-16 border border-t-0 border-border-subtlest-tertiary laptop:border-t">
7178
<NextSeo {...seo} />
7279
<Header
7380
user={user}
74-
isSameUser={isUserSame && !showAsVisitor}
81+
isSameUser={isSameUser}
7582
sticky={!hideSticky}
7683
className={classNames(
7784
'left-0 top-0 z-3 w-full bg-background-default transition-all duration-75 laptop:hidden',
7885
!hideSticky ? 'fixed tablet:pl-20' : 'relative',
7986
)}
8087
/>
81-
{isUserSame && !showAsVisitor && (
82-
<ProfileCompletion className="laptop:hidden" />
83-
)}
88+
{isSameUser && <ProfileCompletion className="laptop:hidden" />}
8489
<div ref={stickyRef} />
8590
<ProfileHeader
8691
user={user}
8792
userStats={userStats}
88-
isSameUser={isUserSame && !showAsVisitor}
93+
isSameUser={isSameUser}
8994
/>
9095
<div className="flex flex-col divide-y divide-border-subtlest-tertiary p-6">
91-
{isUserSame && !showAsVisitor && (
92-
<ProfilePreviewToggle
93-
isPreviewMode={isPreviewMode}
94-
onToggle={() => setIsPreviewMode(!isPreviewMode)}
95-
className="mb-4"
96-
/>
97-
)}
98-
{shouldShowBanner && !showAsVisitor && (
96+
{shouldShowBanner && (
9997
<AutofillProfileBanner
10098
onUpload={onUpload}
10199
isLoading={status === 'pending'}
@@ -108,7 +106,7 @@ const ProfilePage = ({
108106
<ProfileUserHotTakes user={user} />
109107
<ProfileUserWorkspacePhotos user={user} />
110108
<Activity user={user} />
111-
{isUserSame && !showAsVisitor && (
109+
{isSameUser && (
112110
<Share permalink={user?.permalink} className="laptop:hidden" />
113111
)}
114112
<div className="py-4 laptop:hidden">

0 commit comments

Comments
 (0)