|
| 1 | +import styled from 'styled-components'; |
1 | 2 | import { useIsClosedGroup, useSortedGroupMembers } from '../../../hooks/useParamSelector';
|
2 | 3 | import { UserUtils } from '../../../session/utils';
|
3 |
| -import { assertUnreachable } from '../../../types/sqlSharedTypes'; |
4 | 4 | import { MemberAvatarPlaceHolder } from '../../icon/MemberAvatarPlaceHolder';
|
5 | 5 | import { Avatar, AvatarSize } from '../Avatar';
|
6 | 6 | import { useAvatarBgColor } from './AvatarPlaceHolder';
|
7 |
| - |
8 |
| -function getClosedGroupAvatarsSize(size: AvatarSize): AvatarSize { |
9 |
| - // Always use the size directly under the one requested |
10 |
| - switch (size) { |
11 |
| - case AvatarSize.XS: |
12 |
| - throw new Error('AvatarSize.XS is not supported for closed group avatar sizes'); |
13 |
| - case AvatarSize.S: |
14 |
| - return AvatarSize.XS; |
15 |
| - case AvatarSize.M: |
16 |
| - return AvatarSize.S; |
17 |
| - case AvatarSize.L: |
18 |
| - return AvatarSize.M; |
19 |
| - case AvatarSize.XL: |
20 |
| - return AvatarSize.L; |
21 |
| - case AvatarSize.HUGE: |
22 |
| - return AvatarSize.XL; |
23 |
| - default: |
24 |
| - assertUnreachable(size, `Invalid size request for closed group avatar "${size}"`); |
25 |
| - return AvatarSize.XL; // just to make eslint happy |
26 |
| - } |
27 |
| -} |
| 7 | +import { StyledAvatar } from './StyledAvatar'; |
28 | 8 |
|
29 | 9 | /**
|
30 | 10 | * Move our pubkey at the end of the list if we are in the list of members.
|
@@ -67,41 +47,58 @@ function useGroupMembersAvatars(convoId: string | undefined) {
|
67 | 47 | return sortAndSlice(sortedMembers, us);
|
68 | 48 | }
|
69 | 49 |
|
| 50 | +const StyledAvatarClosedContainer = styled.div<{ containerSize: number }>` |
| 51 | + width: ${({ containerSize }) => containerSize}px; |
| 52 | + height: ${({ containerSize }) => containerSize}px; |
| 53 | + mask-image: url(images/avatar-svg-mask.svg); |
| 54 | +
|
| 55 | + .module-avatar:last-child { |
| 56 | + position: absolute; |
| 57 | + right: 0px; |
| 58 | + bottom: 0px; |
| 59 | + } |
| 60 | +`; |
| 61 | + |
70 | 62 | export const ClosedGroupAvatar = ({
|
71 | 63 | convoId,
|
72 |
| - size, |
| 64 | + size: containerSize, |
73 | 65 | onAvatarClick,
|
74 | 66 | }: {
|
75 |
| - size: number; |
| 67 | + size: AvatarSize; |
76 | 68 | convoId: string;
|
77 | 69 | onAvatarClick?: () => void;
|
78 | 70 | }) => {
|
79 | 71 | const memberAvatars = useGroupMembersAvatars(convoId);
|
80 | 72 | const firstMemberId = memberAvatars?.firstMember || '';
|
81 | 73 | const secondMemberID = memberAvatars?.secondMember || '';
|
82 |
| - const avatarsDiameter = getClosedGroupAvatarsSize(size); |
| 74 | + |
| 75 | + const avatarSize = Math.floor((containerSize * 8) / 11); |
| 76 | + |
| 77 | + if (!avatarSize) { |
| 78 | + throw new Error(`Invalid avatar size ${containerSize}`); |
| 79 | + } |
83 | 80 |
|
84 | 81 | const { bgColor } = useAvatarBgColor(secondMemberID || convoId);
|
85 | 82 |
|
86 | 83 | if (firstMemberId && secondMemberID) {
|
87 | 84 | return (
|
88 |
| - <div className="module-avatar__icon-closed"> |
89 |
| - <Avatar size={avatarsDiameter} pubkey={firstMemberId} onAvatarClick={onAvatarClick} /> |
90 |
| - <Avatar size={avatarsDiameter} pubkey={secondMemberID} onAvatarClick={onAvatarClick} /> |
91 |
| - </div> |
| 85 | + <StyledAvatarClosedContainer containerSize={containerSize}> |
| 86 | + <Avatar size={avatarSize} pubkey={firstMemberId} onAvatarClick={onAvatarClick} /> |
| 87 | + <Avatar size={avatarSize} pubkey={secondMemberID} onAvatarClick={onAvatarClick} /> |
| 88 | + </StyledAvatarClosedContainer> |
92 | 89 | );
|
93 | 90 | }
|
94 | 91 |
|
95 | 92 | return (
|
96 |
| - <div className="module-avatar__icon-closed"> |
| 93 | + <StyledAvatarClosedContainer containerSize={containerSize}> |
97 | 94 | <Avatar
|
98 |
| - size={avatarsDiameter} |
| 95 | + size={avatarSize} |
99 | 96 | pubkey={UserUtils.getOurPubKeyStrFromCache()}
|
100 | 97 | onAvatarClick={onAvatarClick}
|
101 | 98 | />
|
102 |
| - <div className={`module-avatar module-avatar--${avatarsDiameter} module-avatar--no-image`}> |
| 99 | + <StyledAvatar $diameter={avatarSize} className={`module-avatar`}> |
103 | 100 | <MemberAvatarPlaceHolder bgColor={bgColor} />
|
104 |
| - </div> |
105 |
| - </div> |
| 101 | + </StyledAvatar> |
| 102 | + </StyledAvatarClosedContainer> |
106 | 103 | );
|
107 | 104 | };
|
0 commit comments