-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathprofile-page.tsx
More file actions
349 lines (323 loc) · 17.8 KB
/
Copy pathprofile-page.tsx
File metadata and controls
349 lines (323 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import APAvatar from '@src/components/global/ap-avatar';
import EditProfile from '@src/views/preferences/components/edit-profile';
import FollowButton from '@src/components/global/follow-button';
import Layout from '@src/components/layout';
import ProfileMenu from './profile-menu';
import UnblockButton from './unblock-button';
import {Account} from '@src/api/activitypub';
import {Badge, Button, Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, NoValueLabel, NoValueLabelIcon, Skeleton, Tabs, TabsContent, TabsList, TabsTrigger, TabsTriggerCount} from '@tryghost/shade/components';
import {EmptyViewIcon, EmptyViewIndicator} from '@src/components/global/empty-view-indicator';
import {H2, H4} from '@tryghost/shade/primitives';
import {LucideIcon, abbreviateNumber} from '@tryghost/shade/utils';
import {SettingAction} from '@src/views/preferences/components/settings';
import {openLinksInNewTab, sanitizeHtml, stripHtml} from '@src/utils/content-formatters';
import {toast} from 'sonner';
import {useAccountForUser, useBlockDomainMutationForUser, useBlockMutationForUser, useUnblockDomainMutationForUser, useUnblockMutationForUser} from '@src/hooks/use-activity-pub-queries';
import {useEffect, useMemo, useRef, useState} from 'react';
import {useLocation, useNavigationStack, useParams} from '@tryghost/admin-x-framework';
import {useNavigateWithBasePath} from '@src/hooks/use-navigate-with-base-path';
import type {ProfileTab} from '../profile';
const noop = () => {};
type ProfilePageProps = {
account: Account,
customFields: Array<{
name: string;
value: string;
}>,
postsTab: React.ReactNode,
likesTab: React.ReactNode,
followingTab: React.ReactNode,
followersTab: React.ReactNode,
isLoadingAccount: boolean
}
const ProfilePage:React.FC<ProfilePageProps> = ({
account,
customFields,
isLoadingAccount,
postsTab,
likesTab,
followingTab,
followersTab
}) => {
const params = useParams<{handle?: string; tab?: string}>();
const location = useLocation();
const navigate = useNavigateWithBasePath();
const {canGoBack} = useNavigationStack();
const profilePath = params.handle ? `/profile/${params.handle}` : '/profile';
const likesEnabled = !params.handle;
const tabSlug = params.handle
? (params.tab || '')
: (location.pathname.split('/').pop() || '');
const allowedTabs = useMemo<ProfileTab[]>(() => (
likesEnabled ? ['likes', 'following', 'followers'] : ['following', 'followers']
), [likesEnabled]);
const activeTab: ProfileTab = allowedTabs.includes(tabSlug as ProfileTab) ? tabSlug as ProfileTab : 'posts';
const blockMutation = useBlockMutationForUser('index');
const unblockMutation = useUnblockMutationForUser('index');
const blockDomainMutation = useBlockDomainMutationForUser('index');
const unblockDomainMutation = useUnblockDomainMutationForUser('index');
const currentAccountQuery = useAccountForUser('index', 'me');
const {data: currentUser} = params.handle ? currentAccountQuery : {data: undefined};
const isCurrentUser = params.handle === currentUser?.handle || !params.handle;
const isBlocked = account?.blockedByMe;
const isDomainBlocked = account?.domainBlockedByMe;
const [viewBlockedPosts, setViewBlockedPosts] = useState(false);
const [copied, setCopied] = useState(false);
const copyTimeoutRef = useRef<number | null>(null);
useEffect(() => () => {
if (copyTimeoutRef.current) {
window.clearTimeout(copyTimeoutRef.current);
}
}, []);
const handleBlock = () => {
if (isBlocked) {
unblockMutation.mutate(account);
} else {
blockMutation.mutate(account);
toast.success('User blocked');
}
setViewBlockedPosts(false);
};
const handleDomainBlock = () => {
if (isDomainBlocked) {
unblockDomainMutation.mutate({url: account.apId, handle: account.handle});
} else {
blockDomainMutation.mutate({url: account.apId, handle: account.handle});
toast.success('Domain blocked');
}
setViewBlockedPosts(false);
};
const handleCopy = async () => {
if (!account?.handle || !navigator?.clipboard?.writeText) {
toast.error('Unable to copy handle');
return;
}
try {
await navigator.clipboard.writeText(account.handle);
setCopied(true);
toast.success('Handle copied');
if (copyTimeoutRef.current) {
window.clearTimeout(copyTimeoutRef.current);
}
copyTimeoutRef.current = window.setTimeout(() => setCopied(false), 2000);
} catch {
toast.error('Failed to copy handle');
setCopied(false);
}
};
const [isExpanded, setisExpanded] = useState(false);
const [isEditingProfile, setIsEditingProfile] = useState(false);
const toggleExpand = () => {
setisExpanded(!isExpanded);
};
const contentRef = useRef<HTMLDivElement | null>(null);
const [isOverflowing, setIsOverflowing] = useState(false);
useEffect(() => {
if (contentRef.current) {
setIsOverflowing(contentRef.current.scrollHeight > 160); // Compare content height to max height
}
}, [isExpanded, account?.bio, customFields, isLoadingAccount]);
useEffect(() => {
if (!tabSlug) {
return;
}
if (!allowedTabs.includes(tabSlug as ProfileTab)) {
navigate(profilePath, {replace: true});
}
}, [allowedTabs, profilePath, navigate, tabSlug]);
const createTabPath = (tab: ProfileTab) => {
if (tab === 'posts') {
return profilePath;
}
return `${profilePath}/${tab}`;
};
const handleTabChange = (tab: ProfileTab) => {
if (tab === activeTab) {
return;
}
navigate(createTabPath(tab), {replace: true});
};
if (!isLoadingAccount && !account) {
return (
<Layout>
<div className='mx-auto mt-4 flex w-full max-w-[620px] flex-col items-center [&_svg]:translate-x-px'>
<EmptyViewIndicator>
<EmptyViewIcon><LucideIcon.UserRoundX /></EmptyViewIcon>
<div>Profile not found</div>
</EmptyViewIndicator>
</div>
</Layout>
);
}
return (
<Layout>
<div className='z-0 mx-[max(-4vw,-24px)] -mt-9 flex flex-col items-center pb-16'>
<div className='mx-auto w-full'>
<>
{account?.bannerImageUrl ?
<div className='h-[15vw] min-h-[200px] w-full overflow-hidden bg-gradient-to-tr from-gray-200 to-gray-100'>
<img
alt={account?.name}
className='size-full object-cover'
referrerPolicy='no-referrer'
src={account?.bannerImageUrl}
/>
</div>
:
<div className='h-[max(8vw,132px)] w-full overflow-hidden bg-gradient-to-tr from-white to-white dark:from-black dark:to-black'></div>
}
<div className={`mx-auto max-w-[620px] px-6 ${(!account?.bannerImageUrl && !canGoBack) ? '-mt-8' : '-mt-12'}`}>
<div className='flex items-end justify-between'>
<div className='-ml-2 rounded-full bg-white p-1 dark:bg-background'>
{isLoadingAccount ?
<Skeleton className='size-[92px] rounded-full' />
:
<APAvatar
author={
{
icon: {
url: account?.avatarUrl
},
name: account?.name,
handle: account?.handle
}
}
size='lg'
/>
}
</div>
{!isCurrentUser && !isLoadingAccount &&
<div className='flex gap-2'>
{!(isBlocked || isDomainBlocked) ?
<FollowButton
following={account?.followedByMe}
handle={account?.handle}
type='primary'
onFollow={noop}
onUnfollow={noop}
/> :
<UnblockButton
account={account}
onDomainUnblock={handleDomainBlock}
onUnblock={handleBlock}
/>
}
<ProfileMenu
account={account}
isBlocked={isBlocked}
isDomainBlocked={isDomainBlocked}
onBlockAccount={handleBlock}
onBlockDomain={handleDomainBlock}
onCopyHandle={handleCopy}
>
<Button aria-label='Open profile menu' variant='outline'><LucideIcon.Ellipsis /></Button>
</ProfileMenu>
</div>
}
{isCurrentUser && !isLoadingAccount &&
<Dialog open={isEditingProfile} onOpenChange={setIsEditingProfile}>
<DialogTrigger>
<SettingAction><Button variant='secondary'>Edit profile</Button></SettingAction>
</DialogTrigger>
<DialogContent className='w-full max-w-[520px]' onOpenAutoFocus={e => e.preventDefault()}>
<DialogHeader>
<DialogTitle>Profile settings</DialogTitle>
</DialogHeader>
{account && <EditProfile account={account} setIsEditingProfile={setIsEditingProfile} />}
</DialogContent>
</Dialog>
}
</div>
<H2 className='break-anywhere mt-4 truncate'>{!isLoadingAccount ? account?.name : <Skeleton className='w-32' />}</H2>
<div className='mb-4 flex items-center gap-2'>
<a className='inline-flex max-w-full truncate text-[1.5rem] text-gray-800 hover:text-gray-900 dark:text-gray-600 dark:hover:text-gray-500' href={account?.url} rel='noopener noreferrer' target='_blank'>
<span className='truncate'>{!isLoadingAccount ? account?.handle : <Skeleton className='w-full max-w-56' />}</span>
</a>
{!isLoadingAccount && (
<Button className='-ml-1.5 size-6 p-0 text-gray-800 hover:text-gray-900 dark:text-gray-700 dark:hover:text-gray-600' title='Copy handle' variant='link' onClick={handleCopy}>
{!copied ?
<LucideIcon.Copy size={16} /> :
<LucideIcon.Check size={16} />
}
</Button>
)}
{account?.followsMe && !isLoadingAccount && (
<Badge className='mt-px whitespace-nowrap' variant='secondary'>Follows you</Badge>
)}
</div>
{(account?.bio || customFields?.length > 0) && (<div ref={contentRef} className={`ap-profile-content break-anywhere relative text-[1.5rem] [&>p]:mb-3 ${isExpanded ? 'max-h-none pb-7' : 'max-h-[160px] overflow-hidden'} relative`}>
{!isLoadingAccount ?
<div dangerouslySetInnerHTML={{__html: sanitizeHtml(openLinksInNewTab(stripHtml(account?.bio ?? '', ['a', 'br'])))}} /> :
<>
<Skeleton />
<Skeleton className='w-full max-w-48' />
</>
}
{customFields?.map((attachment: {name: string, value: string}) => (
<span key={attachment.name} className='mt-3 line-clamp-1 flex flex-col text-[1.5rem]'>
<span className={`text-xs font-semibold`}>{attachment.name}</span>
<span dangerouslySetInnerHTML={{__html: sanitizeHtml(attachment.value)}} className='ap-profile-content truncate'/>
</span>
))}
{!isExpanded && isOverflowing && (
<div className='absolute inset-x-0 bottom-0 h-16 bg-gradient-to-t from-white via-white/100 via-60% to-transparent dark:from-black dark:via-black/100' />
)}
{isOverflowing && <Button
className='absolute bottom-0 h-auto p-0 text-md'
variant='link'
onClick={toggleExpand}
>{isExpanded ? 'Show less' : 'Show all'}</Button>}
</div>)}
<Tabs
key={params.handle || account?.handle || 'current-user'}
className='mt-5'
value={activeTab}
variant='underline'
onValueChange={value => handleTabChange(value as ProfileTab)}
>
<TabsList>
<TabsTrigger value="posts">Posts</TabsTrigger>
{likesEnabled && <TabsTrigger value="likes">
Likes
<TabsTriggerCount>{abbreviateNumber(account?.likedCount || 0)}</TabsTriggerCount>
</TabsTrigger>}
<TabsTrigger value="following">
Following
<TabsTriggerCount>{abbreviateNumber(account?.followingCount || 0)}</TabsTriggerCount>
</TabsTrigger>
<TabsTrigger value="followers">
Followers
<TabsTriggerCount>{abbreviateNumber(account?.followerCount || 0)}</TabsTriggerCount>
</TabsTrigger>
</TabsList>
<TabsContent value='posts'>
{((isBlocked || isDomainBlocked) && !viewBlockedPosts) ?
<NoValueLabel>
<NoValueLabelIcon><LucideIcon.Ban /></NoValueLabelIcon>
<div className='mt-2 flex flex-col items-center gap-0.5'>
<H4>{account.name} is blocked</H4>
<p>You can view the posts, but it won't unblock the user.</p>
<Button className='mt-4' variant='secondary' onClick={() => setViewBlockedPosts(true)}>View posts</Button>
</div>
</NoValueLabel> :
postsTab
}
</TabsContent>
{likesEnabled && <TabsContent value='likes'>
{likesTab}
</TabsContent>}
<TabsContent value='following'>
{followingTab}
</TabsContent>
<TabsContent value='followers'>
{followersTab}
</TabsContent>
</Tabs>
</div>
</>
</div>
</div>
</Layout>
);
};
export default ProfilePage;