Skip to content

Commit ad48a7f

Browse files
Merge upstream develop
2 parents c914b07 + 6aa1d76 commit ad48a7f

4 files changed

Lines changed: 58 additions & 13 deletions

File tree

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import { ComponentProps, useState } from 'react';
22
import { Avatar } from './Avatar';
3-
import { identiconUrl } from '../../lib/polycentric-hooks';
43
import * as ImagePicker from 'expo-image-picker';
5-
import type { ImagePickerAsset } from 'expo-image-picker';
64

75
type AvatarEditProps = {
8-
identity: string;
6+
/** Avatar shown until the user picks a new image. */
7+
defaultUri: string;
98
onSelect?: (uri: string) => void;
10-
} & ComponentProps<typeof Avatar>;
9+
} & Omit<ComponentProps<typeof Avatar>, 'source' | 'onPress'>;
10+
1111
export default function AvatarEdit({
12-
identity,
12+
defaultUri,
1313
onSelect,
1414
...props
1515
}: AvatarEditProps) {
16-
const [avatarUrl, setAvatarUrl] = useState<string>(
17-
identiconUrl(identity, 160),
18-
);
16+
const [selectedUri, setSelectedUri] = useState<string>();
1917

2018
const onPress = async () => {
2119
const result = await ImagePicker.launchImageLibraryAsync();
@@ -25,9 +23,15 @@ export default function AvatarEdit({
2523
}
2624

2725
const asset = result.assets[0];
28-
setAvatarUrl(asset.uri);
26+
setSelectedUri(asset.uri);
2927
onSelect?.(asset.uri);
3028
};
3129

32-
return <Avatar {...props} source={{ uri: avatarUrl }} onPress={onPress} />;
30+
return (
31+
<Avatar
32+
{...props}
33+
source={{ uri: selectedUri ?? defaultUri }}
34+
onPress={onPress}
35+
/>
36+
);
3337
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ComponentProps, useMemo } from 'react';
2+
import { resolveAvatarSize } from './Avatar';
3+
import AvatarEdit from './AvatarEdit';
4+
import {
5+
identiconUrl,
6+
pickImageVariant,
7+
usePolycentric,
8+
} from '../../lib/polycentric-hooks';
9+
import { useProfile } from '@/src/features/profile/hooks/useProfile';
10+
11+
type ProfileEditAvatarProps = {
12+
identityKey: string;
13+
} & Omit<ComponentProps<typeof AvatarEdit>, 'defaultUri'>;
14+
15+
/**
16+
* Edit-mode counterpart to {@link ProfileAvatar}: seeds the image picker with
17+
* the identity's current avatar (resolved the same way — best-fitting variant
18+
* from the profile's `avatar` ImageSet via the blob CDN, falling back to a
19+
* Dicebear identicon) before the user chooses a replacement.
20+
*/
21+
export function ProfileEditAvatar({
22+
identityKey,
23+
size = 'md',
24+
...rest
25+
}: ProfileEditAvatarProps) {
26+
const profile = useProfile(identityKey);
27+
const client = usePolycentric();
28+
const pixelSize = resolveAvatarSize(size);
29+
30+
const uri = useMemo(() => {
31+
const variant = pickImageVariant(profile.avatar, pixelSize);
32+
if (variant?.blob?.digest) {
33+
const url = client.blobUrl(variant.blob.digest);
34+
if (url) return url;
35+
}
36+
return identiconUrl(identityKey, pixelSize);
37+
}, [profile.avatar, client, identityKey, pixelSize]);
38+
39+
return <AvatarEdit {...rest} size={size} defaultUri={uri} />;
40+
}

apps/polycentric/src/common/components/primitives/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from '../Avatar/Avatar';
22
export * from '../Avatar/ProfileAvatar';
3+
export * from '../Avatar/ProfileEditAvatar';
34
export * from '../TextArea';
45
export * from './Button';
56
export * from './Chip';

apps/polycentric/src/features/profile/EditProfileScreen.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Button, Text, TextInput } from '@/src/common/components';
2-
import AvatarEdit from '@/src/common/components/Avatar/AvatarEdit';
2+
import { ProfileEditAvatar } from '@/src/common/components/Avatar/ProfileEditAvatar';
33
import {
44
useCurrentIdentity,
55
useProfileEdit,
@@ -44,8 +44,8 @@ function EditProfileSheet({
4444
/>
4545
<View style={[Atoms.p_lg, Atoms.gap_xl]}>
4646
<View style={[Atoms.items_center, Atoms.gap_md]}>
47-
<AvatarEdit
48-
identity={identityKey}
47+
<ProfileEditAvatar
48+
identityKey={identityKey}
4949
size="massive"
5050
onSelect={edit.setAvatarUri}
5151
/>

0 commit comments

Comments
 (0)