File size: 3,710 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { external, Icon } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import React, { useEffect, useRef, useState } from 'react';
import ReaderAvatar from 'calypso/blocks/reader-avatar';
import AutoDirection from 'calypso/components/auto-direction';
import SectionNav from 'calypso/components/section-nav';
import NavItem from 'calypso/components/section-nav/item';
import NavTabs from 'calypso/components/section-nav/tabs';
import { UserData } from 'calypso/lib/user/user';
import { getUserProfileUrl } from 'calypso/reader/user-profile/user-profile.utils';

import './style.scss';

interface UserProfileHeaderProps {
	user: UserData;
	view: string;
}

const UserProfileHeader = ( { user, view }: UserProfileHeaderProps ): JSX.Element => {
	const translate = useTranslate();
	const userProfileUrlWithUsername = getUserProfileUrl( user.user_login ?? '' );
	const navigationItems = [
		{
			label: translate( 'Posts' ),
			path: userProfileUrlWithUsername,
			selected: view === 'posts',
		},
		{
			label: translate( 'Lists' ),
			path: `${ userProfileUrlWithUsername }/lists`,
			selected: view === 'lists',
		},
		{
			label: translate( 'Recommended Blogs' ),
			path: `${ userProfileUrlWithUsername }/recommended-blogs`,
			selected: view === 'recommended-blogs',
		},
	];

	const selectedTab = navigationItems.find( ( item ) => item.selected )?.label || '';

	const avatarElement = (
		<ReaderAvatar author={ { ...user, has_avatar: !! user.avatar_URL } } iconSize={ 116 } />
	);

	const bioRef = useRef< HTMLSpanElement >( null );
	const [ isClamped, setIsClamped ] = useState( false );

	useEffect( () => {
		if ( bioRef.current ) {
			const element = bioRef.current;
			const originalHeight = element.offsetHeight;

			// Temporarily remove the clamp
			element.style.webkitLineClamp = 'unset';
			const fullHeight = element.scrollHeight;

			// Restore the clamp
			element.style.webkitLineClamp = '3';

			// Determine if the text is clamped
			setIsClamped( fullHeight > originalHeight );
		}
	}, [ user.bio ] );

	return (
		<div className="user-profile-header">
			<header className="user-profile-header__main">
				<div
					className="user-profile-header__avatar user-profile-header__avatar-desktop"
					data-testid="desktop-avatar"
				>
					{ avatarElement }
				</div>
				<AutoDirection>
					<div className="user-profile-header__details">
						<div className="user-profile-header__display-name">
							<div
								className="user-profile-header__avatar user-profile-header__avatar-mobile"
								data-testid="mobile-avatar"
							>
								{ avatarElement }
							</div>

							<h1>{ user.display_name }</h1>
						</div>
						{ user.bio && (
							<div className="user-profile-header__bio">
								<p className="user-profile-header__bio-desc">
									<span ref={ bioRef } className="user-profile-header__bio-desc-text">
										{ user.bio }
									</span>

									{ isClamped && user.profile_URL && (
										<>
											<span className="user-profile-header__bio-desc-fader"></span>
											<a className="user-profile-header__bio-desc-link" href={ user.profile_URL }>
												{ translate( 'Read More' ) }{ ' ' }
												<Icon width={ 18 } height={ 18 } icon={ external } />
											</a>
										</>
									) }
								</p>
							</div>
						) }
					</div>
				</AutoDirection>
			</header>
			<SectionNav enforceTabsView selectedText={ selectedTab }>
				<NavTabs>
					{ navigationItems.map( ( item ) => (
						<NavItem key={ item.path } path={ item.path } selected={ item.selected }>
							{ item.label }
						</NavItem>
					) ) }
				</NavTabs>
			</SectionNav>
		</div>
	);
};

export default UserProfileHeader;