Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit acb5d06

Browse files
authored
user area: create a basic user profile page (#46998)
* user area: add profile page * fix build * change visibility condition
1 parent 3b6e7bf commit acb5d06

File tree

10 files changed

+98
-9
lines changed

10 files changed

+98
-9
lines changed

client/web/src/integration/batches.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ function mockCommonGraphQLResponses(
325325
viewerCanAdminister: true,
326326
builtinAuth: true,
327327
tags: [],
328+
siteAdmin: false,
329+
emails: [{ email: 'alice@example.com', verified: true, isPrimary: true }],
330+
createdAt: '2020-04-10T21:11:42Z',
328331
},
329332
}),
330333
UserSettingsAreaUserProfile: () => ({
@@ -341,7 +344,7 @@ function mockCommonGraphQLResponses(
341344
siteAdmin: true,
342345
builtinAuth: true,
343346
createdAt: '2020-04-10T21:11:42Z',
344-
emails: [{ email: 'alice@example.com', verified: true }],
347+
emails: [{ email: 'alice@example.com', verified: true, isPrimary: true }],
345348
organizations: { nodes: [] },
346349
permissionsInfo: null,
347350
tags: [],

client/web/src/integration/profile.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const USER: UserSettingsAreaUserFields = {
2828
siteAdmin: true,
2929
builtinAuth: true,
3030
createdAt: subDays(now, 732).toISOString(),
31-
emails: [{ email: 'test@example.com', verified: true }],
31+
emails: [{ email: 'test@example.com', verified: true, isPrimary: true }],
3232
organizations: { nodes: [] },
3333
tags: [],
3434
}
@@ -149,6 +149,9 @@ describe('User Different Settings Page', () => {
149149
viewerCanAdminister: true,
150150
builtinAuth: true,
151151
tags: [],
152+
emails: [{ email: 'test@sourcegraph.test', verified: true, isPrimary: true }],
153+
siteAdmin: false,
154+
createdAt: '2020-03-02T11:52:15Z',
152155
},
153156
}),
154157
UserSettingsAreaUserProfile: () => ({
@@ -165,7 +168,7 @@ describe('User Different Settings Page', () => {
165168
siteAdmin: true,
166169
builtinAuth: true,
167170
createdAt: '2020-03-02T11:52:15Z',
168-
emails: [{ email: 'test@sourcegraph.test', verified: true }],
171+
emails: [{ email: 'test@sourcegraph.test', verified: true, isPrimary: true }],
169172
organizations: { nodes: [] },
170173
permissionsInfo: null,
171174
tags: [],

client/web/src/integration/search-aggregation.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ const commonSearchGraphQLResults: Partial<WebGraphQlOperations & SharedGraphQlOp
122122
viewerCanAdminister: true,
123123
builtinAuth: true,
124124
tags: [],
125+
emails: [{ email: 'test@sourcegraph.test', verified: true, isPrimary: true }],
126+
siteAdmin: false,
127+
createdAt: '2020-03-02T11:52:15Z',
125128
},
126129
}),
127130
}

client/web/src/integration/search.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ const commonSearchGraphQLResultsWithUser: Partial<WebGraphQlOperations & SharedG
7575
viewerCanAdminister: true,
7676
builtinAuth: true,
7777
tags: [],
78+
emails: [{ email: 'test@sourcegraph.test', verified: true, isPrimary: true }],
79+
siteAdmin: false,
80+
createdAt: '2020-03-02T11:52:15Z',
7881
},
7982
}),
8083
}

client/web/src/integration/settings.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ describe('Settings', () => {
7373
viewerCanAdminister: true,
7474
builtinAuth: true,
7575
tags: [],
76+
emails: [{ email: 'test@sourcegraph.test', verified: true, isPrimary: true }],
77+
siteAdmin: false,
78+
createdAt: '2020-03-02T11:52:15Z',
7679
},
7780
}),
7881
UserSettingsAreaUserProfile: () => ({
@@ -89,7 +92,7 @@ describe('Settings', () => {
8992
siteAdmin: true,
9093
builtinAuth: true,
9194
createdAt: '2020-03-02T11:52:15Z',
92-
emails: [{ email: 'test@sourcegraph.test', verified: true }],
95+
emails: [{ email: 'test@sourcegraph.test', verified: true, isPrimary: true }],
9396
organizations: { nodes: [] },
9497
permissionsInfo: null,
9598
tags: [],

client/web/src/user/area/UserArea.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export const UserAreaGQLFragment = gql`
4444
avatarURL
4545
viewerCanAdminister
4646
builtinAuth
47+
siteAdmin
48+
createdAt
49+
emails {
50+
email
51+
isPrimary
52+
}
4753
}
4854
`
4955

client/web/src/user/area/navitems.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import AccountIcon from 'mdi-react/AccountIcon'
12
import CogOutlineIcon from 'mdi-react/CogOutlineIcon'
23
import FeatureSearchOutlineIcon from 'mdi-react/FeatureSearchOutlineIcon'
34

@@ -6,6 +7,11 @@ import { namespaceAreaHeaderNavItems } from '../../namespaces/navitems'
67
import { UserAreaHeaderNavItem } from './UserAreaHeader'
78

89
export const userAreaHeaderNavItems: readonly UserAreaHeaderNavItem[] = [
10+
{
11+
to: '/profile',
12+
label: 'Profile',
13+
icon: AccountIcon,
14+
},
915
{
1016
to: '/settings',
1117
label: 'Settings',

client/web/src/user/area/routes.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { Redirect } from 'react-router'
33
import { lazyComponent } from '@sourcegraph/shared/src/util/lazyComponent'
44

55
import { namespaceAreaRoutes } from '../../namespaces/routes'
6+
import { UserProfile } from '../profile/UserProfile'
67

78
import { UserAreaRoute } from './UserArea'
89

910
const UserSettingsArea = lazyComponent(() => import('../settings/UserSettingsArea'), 'UserSettingsArea')
1011

11-
const redirectToUserProfile: UserAreaRoute['render'] = props => <Redirect to={`${props.url}/settings/profile`} />
12+
const redirectToUserProfile: UserAreaRoute['render'] = props => <Redirect to={`${props.url}/profile`} />
1213

1314
export const userAreaRoutes: readonly UserAreaRoute[] = [
1415
{
@@ -21,15 +22,19 @@ export const userAreaRoutes: readonly UserAreaRoute[] = [
2122
/>
2223
),
2324
},
25+
{
26+
path: '/profile',
27+
render: props => <UserProfile user={props.user} isSourcegraphDotCom={props.isSourcegraphDotCom} />,
28+
},
2429
...namespaceAreaRoutes,
2530

26-
// Redirect from /users/:username -> /users/:username/settings/profile.
31+
// Redirect from /users/:username -> /users/:username/profile.
2732
{
2833
path: '/',
2934
exact: true,
3035
render: redirectToUserProfile,
3136
},
32-
// Redirect from previous /users/:username/account -> /users/:username/settings/profile.
37+
// Redirect from previous /users/:username/account -> /users/:username/profile.
3338
{
3439
path: '/account',
3540
render: redirectToUserProfile,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from 'react'
2+
3+
import { formatDistanceToNowStrict } from 'date-fns'
4+
5+
import { UserAreaRouteContext } from '../area/UserArea'
6+
7+
export const UserProfile: React.FunctionComponent<
8+
Pick<UserAreaRouteContext, 'user'> & { isSourcegraphDotCom: boolean }
9+
> = ({ user, isSourcegraphDotCom }) => {
10+
const primaryEmail = user.emails.find(email => email.isPrimary)?.email
11+
12+
const userData: {
13+
name: string
14+
value: string
15+
visible: boolean
16+
}[] = [
17+
{
18+
name: 'Username',
19+
value: user.username,
20+
visible: true,
21+
},
22+
{
23+
name: 'Display name',
24+
value: user.displayName || 'Not set',
25+
visible: !!user.displayName,
26+
},
27+
{
28+
name: 'User since',
29+
value: formatDistanceToNowStrict(new Date(user.createdAt), { addSuffix: true }),
30+
visible: true,
31+
},
32+
{
33+
name: 'Email',
34+
value: primaryEmail || 'Not set',
35+
visible: !!primaryEmail && !isSourcegraphDotCom, // Don't show email on Sourcegraph.com
36+
},
37+
{
38+
name: 'Site admin',
39+
value: user.siteAdmin ? 'Yes' : 'No',
40+
visible: user.siteAdmin && !isSourcegraphDotCom, // Don't show site admin status on Sourcegraph.com
41+
},
42+
]
43+
44+
return (
45+
<dl>
46+
{userData.map(data =>
47+
data.visible ? (
48+
<>
49+
<dt>{data.name}</dt>
50+
<dd>{data.value}</dd>
51+
</>
52+
) : null
53+
)}
54+
</dl>
55+
)
56+
}

client/web/src/user/settings/UserSettingsArea.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ const UserSettingsAreaGQLFragment = gql`
5959
settingsURL
6060
avatarURL
6161
viewerCanAdminister
62-
siteAdmin @include(if: $siteAdmin)
62+
siteAdmin
6363
builtinAuth
6464
createdAt
65-
emails @include(if: $siteAdmin) {
65+
emails {
6666
email
6767
verified
68+
isPrimary
6869
}
6970
organizations {
7071
nodes {

0 commit comments

Comments
 (0)