/
UserProfileHeader.tsx
202 lines (179 loc) · 5.96 KB
/
UserProfileHeader.tsx
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
import React, {useMemo} from 'react';
import {styled} from '@mui/material/styles';
import {Box, Paper, Typography} from '@mui/material';
import ChangeCover, {ChangeCoverProps} from '../ChangeCover';
import ChangePicture, {ChangePictureProps} from '../ChangePicture';
import {SCUserType} from '@selfcommunity/types';
import {
SCContextType,
SCPreferences,
SCPreferencesContextType,
SCUserContextType,
useSCContext,
useSCFetchUser,
useSCPreferences,
useSCUser
} from '@selfcommunity/react-core';
import UserProfileHeaderSkeleton from './Skeleton';
import classNames from 'classnames';
import {useThemeProps} from '@mui/system';
import {PREFIX} from './constants';
const classes = {
root: `${PREFIX}-root`,
cover: `${PREFIX}-cover`,
avatar: `${PREFIX}-avatar`,
info: `${PREFIX}-info`,
infOpsSection: `${PREFIX}-infops-section`,
username: `${PREFIX}-username`,
realname: `${PREFIX}-realname`,
changePicture: `${PREFIX}-change-picture`,
changeCover: `${PREFIX}-change-cover`
};
const Root = styled(Box, {
name: PREFIX,
slot: 'Root'
})(() => ({}));
export interface UserProfileHeaderProps {
/**
* Id of user object
* @default null
*/
id?: string;
/**
* Overrides or extends the styles applied to the component.
* @default null
*/
className?: string;
/**
* Id of user object
* @default null
*/
userId?: number;
/**
* User Object
* @default null
*/
user?: SCUserType;
/**
* Props to spread change picture button
* @default {}
*/
ChangePictureProps?: ChangePictureProps;
/**
* Props to spread change cover button
* @default {}
*/
ChangeCoverProps?: ChangeCoverProps;
/**
*
*/
actions?: React.ReactNode;
/**
* Any other properties
*/
[p: string]: any;
}
/**
* > API documentation for the Community-JS User Profile AppBar component. Learn about the available props and the CSS API.
*
*
* This component renders the user profile's top section.
* Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/UserProfileHeader)
#### Import
```jsx
import {UserProfileHeader} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCUserProfileHeader` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCUserProfileHeader-root|Styles applied to the root element.|
|cover|.SCUserProfileHeader-cover|Styles applied to the cover element.|
|avatar|.SCUserProfileHeader-avatar|Styles applied to the avatar element.|
|infOpsSection|SCUserProfileHeader-infops-section|Styles applied to the section including info and actions.|
|info|SCUserProfileHeader-info|Styles applied to the info section.|
|username|SCUserProfileHeader-username|Styles applied to the username element.|
|realname|SCUserProfileHeader-realname|Styles applied to the realname element.|
|changePicture|.SCUserProfileHeader-change-picture|Styles applied to changePicture element.|
|changeCover|.SCUserProfileHeader-change-cover`|Styles applied to changeCover element.|
* @param inProps
*/
export default function UserProfileHeader(inProps: UserProfileHeaderProps): JSX.Element {
// PROPS
const props: UserProfileHeaderProps = useThemeProps({
props: inProps,
name: PREFIX
});
const {id = null, className = null, userId = null, user = null, ChangePictureProps = {}, ChangeCoverProps = {}, actions, ...rest} = props;
// PREFERENCES
const scPreferences: SCPreferencesContextType = useSCPreferences();
// CONTEXT
const scContext: SCContextType = useSCContext();
const scUserContext: SCUserContextType = useSCUser();
// HOOKS
const {scUser, setSCUser} = useSCFetchUser({id: userId, user});
// CONST
const isMe = useMemo(() => scUserContext.user && scUser?.id === scUserContext.user.id, [scUserContext.user, scUser]);
/**
* Handles Change Avatar
* Only if scUser.id === scUserContext.user.id
* If avatar === null reset the avatar with auto-generated image
* @param avatar
*/
function handleChangeAvatar(avatar) {
if (scUser.id === scUserContext.user.id && avatar) {
setSCUser(Object.assign({}, scUser, {avatar: avatar.avatar}));
} else {
setSCUser(Object.assign({}, scUser, {avatar: `${scContext.settings.portal}/api/v2/avatar/${scUser.id}`}));
}
}
/**
* Handles Change Cover
* Only if scUser.id === scUserContext.user.id
* @param cover
*/
function handleChangeCover(cover) {
if (scUser.id === scUserContext.user.id) {
setSCUser(Object.assign({}, scUser, {cover: cover}));
}
}
// RENDER
if (!scUser) {
return <UserProfileHeaderSkeleton />;
}
const _backgroundCover = {
...(scUser.cover
? {background: `url('${scUser.cover}') center / cover`}
: {background: `url('${scPreferences.preferences[SCPreferences.IMAGES_USER_DEFAULT_COVER].value}') center / cover`})
};
const realName = (isMe && scUserContext.user.real_name) || scUser.real_name;
return (
<Root id={id} className={classNames(classes.root, className)} {...rest}>
<Paper style={_backgroundCover} classes={{root: classes.cover}}>
<img src={scUser.avatar ? scUser.avatar : ''} className={classes.avatar} />
{isMe && (
<>
<ChangePicture iconButton={true} onChange={handleChangeAvatar} className={classes.changePicture} {...ChangePictureProps} />
<div className={classes.changeCover}>
<ChangeCover onChange={handleChangeCover} {...ChangeCoverProps} />
</div>
</>
)}
</Paper>
<Box className={classes.infOpsSection}>
<Box className={classes.info}>
<Typography variant="h5" className={classes.username}>
@{isMe ? scUserContext.user.username : scUser.username}
</Typography>
{realName && (
<Typography variant="h5" className={classes.realname}>
{realName}
</Typography>
)}
</Box>
{actions && actions}
</Box>
</Root>
);
}