Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d9ee6db
feat: show skeleton and grey background while avatar loads
Rohit3523 Sep 30, 2026
cd926ea
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 Sep 30, 2026
4718ddf
change absoluteFillObject to absoluteFill
Rohit3523 Sep 30, 2026
b39b23f
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 Oct 5, 2026
00e3988
fix: update avatar snapshots and tidy skeleton code
Rohit3523 Oct 19, 2026
8ad2305
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 Oct 19, 2026
0d75cf4
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 Oct 20, 2026
4c41741
refactor: move avatar skeleton delay into Skeleton and test uri chang…
Rohit3523 Oct 20, 2026
defc501
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 Oct 22, 2026
4cac1e8
test: pin avatar skeleton delay and shared pulse
Rohit3523 Oct 22, 2026
8f129d4
refactor: move the avatar skeleton into its own file
Rohit3523 Oct 22, 2026
0f35a00
fix: clear the previous avatar while the new one loads
Rohit3523 Oct 22, 2026
383edca
refactor: name the avatar skeleton settle handler and pulse constants
Rohit3523 Oct 22, 2026
ff3f5c4
test: move the avatar story snapshots into __tests__
Rohit3523 Oct 22, 2026
9a38ceb
refactor: drop the avatar image wrapper view and the Loading story
Rohit3523 Oct 22, 2026
9511cdc
refactor: give each avatar skeleton its own full-range pulse
Rohit3523 Oct 22, 2026
2fafac7
test: pin the avatar skeleton staying when the uri changes mid-load
Rohit3523 Oct 22, 2026
27d3310
refactor: use StyleSheet.absoluteFill directly in the avatar skeleton
Rohit3523 Oct 22, 2026
70bb818
fix: hide the avatar skeleton when the uri is removed after a load
Rohit3523 Oct 22, 2026
e11bc13
fix: fade the avatar skeleton in and share one pulse across avatars
Rohit3523 Oct 22, 2026
b060962
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 Oct 27, 2026
7572ebd
refactor: use react-native-skeleton-placeholder for the avatar skelet…
Rohit3523 Oct 27, 2026
5b45228
fix: keep the avatar skeleton static when reduce motion is on
Rohit3523 Oct 27, 2026
e601e61
refactor: use one skeleton colour pair and drop reduce motion
Rohit3523 Oct 27, 2026
88b7af0
refactor: share the avatar skeleton delay with the tests and rename t…
Rohit3523 Oct 30, 2026
54ef9ee
Merge remote-tracking branch 'origin/develop' into feat/avatar-loadin…
Rohit3523 Oct 30, 2026
cbbae02
refactor: pass size and borderRadius to the avatar skeleton and drop …
Rohit3523 Oct 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/containers/Avatar/Avatar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ export const RoomAvatarExternalProviderUrl = () => (

export const WrongServer = () => <Avatar text='Avatar' server='https://google.com' size={56} />;

export const Loading = () => <Avatar avatar='https://10.255.255.1/avatar.png' server={server} isStatic size={56} />;

export const CustomStyle = () => <Avatar text='Avatar' server={server} size={56} style={styles.custom} />;
15 changes: 2 additions & 13 deletions app/containers/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { memo } from 'react';
import { View } from 'react-native';
import { Image } from 'expo-image';
import { settings as RocketChatSettings } from '@rocket.chat/sdk';

import Emoji from '../markdown/components/emoji/Emoji';
import { getAvatarURL } from '~/lib/methods/helpers/getAvatarUrl';
import { headers } from '~/lib/methods/helpers/fetch';
import { SubscriptionType } from '~/definitions';
import { type IAvatar } from './interfaces';
import I18n from '~/i18n';
import Touch from '../Touch';
import AvatarImage from './AvatarImage';
import { isIOS } from '~/lib/methods/helpers/deviceInfo';

const Avatar = memo(
Expand Down Expand Up @@ -78,16 +76,7 @@ const Avatar = memo(
});
}

image = (
<Image
style={avatarStyle}
source={{
uri,
headers: RocketChatSettings.customHeaders ?? headers
}}
priority='high'
/>
);
image = <AvatarImage style={avatarStyle} uri={uri} />;
}

if (onPress) {
Expand Down
87 changes: 87 additions & 0 deletions app/containers/Avatar/AvatarImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useEffect, useState } from 'react';
import { StyleSheet, View, type ImageStyle, type ViewStyle } from 'react-native';
import { Image } from 'expo-image';
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
import Animated, { makeMutable, useAnimatedStyle, withRepeat, withTiming } from 'react-native-reanimated';

import { headers } from '~/lib/methods/helpers/fetch';
import { useTheme } from '~/theme';

interface IAvatarImage {
uri?: string;
style: ImageStyle;
}

const styles = StyleSheet.create({
skeleton: {
...StyleSheet.absoluteFill
}
});

const SKELETON_DELAY = 150;

const pulse = makeMutable(1);
let activeSkeletons = 0;

const startPulse = () => {
if (activeSkeletons++ === 0) {
pulse.value = withRepeat(withTiming(0.4, { duration: 800 }), -1, true);
}
};

const stopPulse = () => {
if (--activeSkeletons === 0) {
pulse.value = 1;
}
};

const Skeleton = ({ borderRadius }: { borderRadius: ViewStyle['borderRadius'] }) => {
const { colors } = useTheme();

useEffect(() => {
startPulse();
return stopPulse;
}, []);

const animatedStyle = useAnimatedStyle(() => ({ opacity: pulse.value }));

return (
<Animated.View
pointerEvents='none'
testID='avatar-skeleton'
style={[styles.skeleton, { backgroundColor: colors.surfaceSelected, borderRadius }, animatedStyle]}
/>
);
};

const AvatarImage = ({ uri, style }: IAvatarImage) => {
const { colors } = useTheme();
const [loadedUri, setLoadedUri] = useState<string | undefined>();
const [delayedUri, setDelayedUri] = useState<string | undefined>();
const showSkeleton = loadedUri !== uri && delayedUri === uri;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

useEffect(() => {
const timer = setTimeout(() => setDelayedUri(uri), SKELETON_DELAY);
return () => clearTimeout(timer);
}, [uri]);

const onEnd = () => setLoadedUri(uri);

return (
<View style={[style, { backgroundColor: colors.surfaceNeutral }]}>
<Image
style={style}
source={{
uri,
headers: RocketChatSettings.customHeaders ?? headers
}}
priority='high'
onLoad={onEnd}
onError={onEnd}
/>
{showSkeleton ? <Skeleton borderRadius={style.borderRadius} /> : null}
</View>
);
};

export default AvatarImage;
1,650 changes: 990 additions & 660 deletions app/containers/Avatar/__snapshots__/Avatar.test.tsx.snap

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions app/containers/Avatar/__tests__/AvatarImage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { act, fireEvent, render } from '@testing-library/react-native';
import { Image } from 'expo-image';

import AvatarImage from '../AvatarImage';

const style = { width: 25, height: 25, borderRadius: 4 };
const showDelay = () => act(() => jest.advanceTimersByTime(150));

beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());

describe('AvatarImage', () => {
it('shows the skeleton until the image loads', () => {
const { getByTestId, queryByTestId, UNSAFE_getByType } = render(<AvatarImage uri='https://a/1.png' style={style} />);
expect(queryByTestId('avatar-skeleton')).toBeNull();
showDelay();
expect(getByTestId('avatar-skeleton')).toBeTruthy();
fireEvent(UNSAFE_getByType(Image), 'load');
expect(queryByTestId('avatar-skeleton')).toBeNull();
});

it('hides the skeleton when the image fails to load', () => {
const { queryByTestId, UNSAFE_getByType } = render(<AvatarImage uri='https://a/1.png' style={style} />);
showDelay();
fireEvent(UNSAFE_getByType(Image), 'error');
expect(queryByTestId('avatar-skeleton')).toBeNull();
});

it('shows the skeleton again when the uri changes', () => {
const { getByTestId, queryByTestId, rerender, UNSAFE_getByType } = render(
<AvatarImage uri='https://a/1.png' style={style} />
);
fireEvent(UNSAFE_getByType(Image), 'load');
expect(queryByTestId('avatar-skeleton')).toBeNull();
rerender(<AvatarImage uri='https://a/2.png' style={style} />);
showDelay();
expect(getByTestId('avatar-skeleton')).toBeTruthy();
});

it('never shows the skeleton when the image loads before the delay', () => {
const { queryByTestId, UNSAFE_getByType } = render(<AvatarImage uri='https://a/1.png' style={style} />);
fireEvent(UNSAFE_getByType(Image), 'load');
showDelay();
expect(queryByTestId('avatar-skeleton')).toBeNull();
});
});
Loading
Loading