Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
5f30f3f1
Commit
5f30f3f1
authored
Nov 15, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add fallback for user avatar
parent
407a3504
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
26 deletions
+37
-26
UserAvatar.tsx
ui/shared/UserAvatar.tsx
+37
-26
No files found.
ui/shared/UserAvatar.tsx
View file @
5f30f3f1
import
{
useColorModeValue
,
chakra
,
SkeletonCircle
,
Image
}
from
'
@chakra-ui/react
'
;
import
{
useColorModeValue
,
useToken
,
SkeletonCircle
,
Image
,
Box
}
from
'
@chakra-ui/react
'
;
import
React
from
'
react
'
;
import
React
from
'
react
'
;
import
Identicon
from
'
react-identicons
'
;
import
Identicon
from
'
react-identicons
'
;
...
@@ -7,7 +7,28 @@ import type { UserInfo } from 'types/api/account';
...
@@ -7,7 +7,28 @@ import type { UserInfo } from 'types/api/account';
import
{
useAppContext
}
from
'
lib/appContext
'
;
import
{
useAppContext
}
from
'
lib/appContext
'
;
import
*
as
cookies
from
'
lib/cookies
'
;
import
*
as
cookies
from
'
lib/cookies
'
;
const
ProfileIcon
=
chakra
(
Identicon
);
// for those who haven't got profile
// or if we cannot download the profile picture for some reasons
const
FallbackImage
=
({
size
,
id
}:
{
size
:
number
;
id
:
string
})
=>
{
const
bgColor
=
useToken
(
'
colors
'
,
useColorModeValue
(
'
blackAlpha.100
'
,
'
white
'
));
return
(
<
Box
flexShrink=
{
0
}
maxWidth=
{
`${ size }px`
}
maxHeight=
{
`${ size }px`
}
>
<
Box
boxSize=
{
size
*
2
}
transformOrigin=
"left top"
transform=
"scale(0.5)"
borderRadius=
"full"
overflow=
"hidden"
>
<
Identicon
bg=
{
bgColor
}
string=
{
id
}
// the displayed size is doubled for retina displays and then scaled down
size=
{
size
*
2
}
/>
</
Box
>
</
Box
>
);
};
interface
Props
{
interface
Props
{
size
:
number
;
size
:
number
;
...
@@ -18,41 +39,31 @@ interface Props {
...
@@ -18,41 +39,31 @@ interface Props {
const
UserAvatar
=
({
size
,
data
,
isFetched
}:
Props
)
=>
{
const
UserAvatar
=
({
size
,
data
,
isFetched
}:
Props
)
=>
{
const
appProps
=
useAppContext
();
const
appProps
=
useAppContext
();
const
hasAuth
=
Boolean
(
cookies
.
get
(
cookies
.
NAMES
.
API_TOKEN
,
appProps
.
cookies
));
const
hasAuth
=
Boolean
(
cookies
.
get
(
cookies
.
NAMES
.
API_TOKEN
,
appProps
.
cookies
));
const
[
isImageLoadError
,
setImageLoadError
]
=
React
.
useState
(
false
);
const
sizeString
=
`
${
size
}
px`
;
const
sizeString
=
`
${
size
}
px`
;
const
bgColor
=
useColorModeValue
(
'
blackAlpha.100
'
,
'
white
'
);
const
handleImageLoadError
=
React
.
useCallback
(()
=>
{
setImageLoadError
(
true
);
},
[]);
if
(
hasAuth
&&
!
isFetched
)
{
if
(
hasAuth
&&
!
isFetched
)
{
return
<
SkeletonCircle
h=
{
sizeString
}
w=
{
sizeString
}
/>;
return
<
SkeletonCircle
h=
{
sizeString
}
w=
{
sizeString
}
/>;
}
}
if
(
data
?.
avatar
)
{
return
(
<
Image
flexShrink=
{
0
}
src=
{
data
.
avatar
}
alt=
{
`Profile picture of ${ data?.name || data?.nickname || '' }`
}
w=
{
sizeString
}
minW=
{
sizeString
}
h=
{
sizeString
}
minH=
{
sizeString
}
borderRadius=
"full"
overflow=
"hidden"
/>
);
}
return
(
return
(
<
ProfileIcon
<
Image
flexShrink=
{
0
}
flexShrink=
{
0
}
maxWidth=
{
sizeString
}
src=
{
data
?.
avatar
}
maxHeight=
{
sizeString
}
alt=
{
`Profile picture of ${ data?.name || data?.nickname || '' }`
}
string=
{
data
?.
email
||
'
randomness
'
}
w=
{
sizeString
}
// the displayed size is doubled for retina displays
minW=
{
sizeString
}
size=
{
size
*
2
}
h=
{
sizeString
}
bg=
{
bgColor
}
minH=
{
sizeString
}
borderRadius=
"full"
borderRadius=
"full"
overflow=
"hidden"
overflow=
"hidden"
fallback=
{
isImageLoadError
||
!
data
?.
avatar
?
<
FallbackImage
size=
{
size
}
id=
{
data
?.
email
||
'
randomness
'
}
/>
:
undefined
}
onError=
{
handleImageLoadError
}
/>
/>
);
);
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment