-
-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user: Show custom profile fields in a user's profile
Fixes: #2900
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// @flow strict-local | ||
import * as React from 'react'; | ||
import { View } from 'react-native'; | ||
|
||
import { type UserOrBot, type UserId } from '../api/modelTypes'; | ||
import WebLink from '../common/WebLink'; | ||
import ZulipText from '../common/ZulipText'; | ||
import ZulipTextIntl from '../common/ZulipTextIntl'; | ||
import { ensureUnreachable } from '../generics'; | ||
import { useSelector } from '../react-redux'; | ||
import { tryGetUserForId } from '../selectors'; | ||
import { | ||
type CustomProfileFieldValue, | ||
getCustomProfileFieldsForUser, | ||
} from '../users/userSelectors'; | ||
import UserAvatar from '../common/UserAvatar'; | ||
|
||
type Props = {| | ||
+user: UserOrBot, | ||
|}; | ||
|
||
function CustomProfileFieldUser(props: {| +userId: UserId |}): React.Node { | ||
const { userId } = props; | ||
const user = useSelector(state => tryGetUserForId(state, userId)); | ||
|
||
const styles = React.useMemo( | ||
() => ({ | ||
text: { marginStart: 8 }, | ||
row: { flexDirection: 'row', marginVertical: 4 }, | ||
}), | ||
[], | ||
); | ||
|
||
if (!user) { | ||
return <ZulipTextIntl text="(unknown user)" />; | ||
} | ||
|
||
return ( | ||
<View style={styles.row}> | ||
<UserAvatar size={24} avatarUrl={user.avatar_url} /> | ||
<ZulipText style={styles.text} text={user.full_name} /> | ||
</View> | ||
); | ||
} | ||
|
||
function CustomProfileFieldRow(props: {| | ||
+name: string, | ||
+value: CustomProfileFieldValue, | ||
+first: boolean, | ||
|}): React.Node { | ||
const { first, name, value } = props; | ||
|
||
const styles = React.useMemo( | ||
() => ({ | ||
row: { marginTop: first ? 0 : 8, flexDirection: 'row' }, | ||
label: { width: 96, marginEnd: 8, fontWeight: 'bold' }, | ||
valueView: { flex: 1 }, | ||
valueText: { flex: 1 }, | ||
}), | ||
[first], | ||
); | ||
|
||
let valueElement = undefined; | ||
switch (value.displayType) { | ||
case 'text': | ||
valueElement = <ZulipText style={styles.valueText} text={value.text} />; | ||
break; | ||
|
||
case 'link': | ||
valueElement = ( | ||
<View style={styles.valueView}> | ||
{value.url ? ( | ||
<WebLink url={value.url} label={{ text: '{_}', values: { _: value.text } }} /> | ||
) : ( | ||
<ZulipText text={value.text} /> | ||
)} | ||
</View> | ||
); | ||
break; | ||
|
||
case 'users': | ||
valueElement = ( | ||
<View style={styles.valueView}> | ||
{value.userIds.map(userId => ( | ||
<CustomProfileFieldUser key={userId} userId={userId} /> | ||
))} | ||
</View> | ||
); | ||
break; | ||
|
||
default: | ||
ensureUnreachable(value.displayType); | ||
return null; | ||
} | ||
|
||
return ( | ||
<View style={styles.row}> | ||
<ZulipText style={styles.label} text={name} /> | ||
{valueElement} | ||
</View> | ||
); | ||
} | ||
|
||
export default function CustomProfileFields(props: Props): React.Node { | ||
const { user } = props; | ||
const realm = useSelector(state => state.realm); | ||
|
||
const fields = React.useMemo(() => getCustomProfileFieldsForUser(realm, user), [realm, user]); | ||
|
||
const styles = React.useMemo( | ||
() => ({ | ||
outer: { flexDirection: 'row', justifyContent: 'center' }, | ||
inner: { flexBasis: 400, flexShrink: 1 }, | ||
}), | ||
[], | ||
); | ||
|
||
return ( | ||
<View style={styles.outer}> | ||
<View style={styles.inner}> | ||
{fields.map((field, i) => ( | ||
<CustomProfileFieldRow | ||
key={field.fieldId} | ||
name={field.name} | ||
value={field.value} | ||
first={i === 0} | ||
/> | ||
))} | ||
</View> | ||
</View> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters