Skip to content

Commit dc8d82e

Browse files
committed
refactor: keep using Avatar component as a default avatar everywhere
1 parent 398cc6d commit dc8d82e

File tree

13 files changed

+49
-70
lines changed

13 files changed

+49
-70
lines changed

docusaurus/docs/React/components/contexts/channel-list-context.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: ChannelListContext
55

66
The context value is provided by `ChannelListContextProvider` which wraps the contents rendered by [`ChannelList`](../core-components/channel-list.mdx). It exposes API that the default and custom components rendered by `ChannelList` can take advantage of. The components that can consume the context are customizable via `ChannelListProps`:
77

8-
- `ChannelAvatar` - component used to display channel image
8+
- `Avatar` - component used to display channel image
99
- `ChannelSearch` - renders channel search input and results
1010
- `EmptyStateIndicator` - rendered when the channels query returns and empty array
1111
- `LoadingErrorIndicator` - rendered when the channels query fails

docusaurus/docs/React/components/contexts/component-context.mdx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,6 @@ The [default `BaseImage` component](../../utility-components/base-image) tries t
131131
| --------- | ----------------------------------------------------------------- |
132132
| component | <GHComponentLink text='BaseImage' path='/Gallery/BaseImage.tsx'/> |
133133

134-
### ChannelAvatar
135-
136-
Custom UI component to display avatar for a channel in ChannelHeader.
137-
138-
| Type | Default |
139-
| --------- | ------------------------------------------------------------------------ |
140-
| component | <GHComponentLink text='ChannelAvatar' path='/Avatar/ChannelAvatar.tsx'/> |
141-
142134
### CooldownTimer
143135

144136
Custom UI component to display the slow mode cooldown timer.

docusaurus/docs/React/components/core-components/channel-list.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ list from incrementing the list.
187187

188188
### Avatar
189189

190-
Custom UI component to display the channel avatar. The default avatar component for `ChannelList` is `ChannelAvatar`.
190+
Custom UI component to display the channel avatar. The default avatar component for `ChannelList` is `Avatar`.
191191

192-
| Type | Default |
193-
| --------- | ----------------------------------------------------------------- |
194-
| component | <GHComponentLink text='Avatar' path='/Avatar/ChannelAvatar.tsx'/> |
192+
| Type | Default |
193+
| --------- | ---------------------------------------------------------- |
194+
| component | <GHComponentLink text='Avatar' path='/Avatar/Avatar.tsx'/> |
195195

196196
### channelRenderFilterFn
197197

docusaurus/docs/React/components/core-components/channel.mdx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,6 @@ Custom UI component to display a user's avatar.
190190
| --------- | ---------------------------------------------------------- |
191191
| component | <GHComponentLink text='Avatar' path='/Avatar/Avatar.tsx'/> |
192192

193-
### ChannelAvatar
194-
195-
Custom UI component to display avatar for a channel in ChannelHeader.
196-
197-
| Type | Default |
198-
| --------- | ------------------------------------------------------------------------ |
199-
| component | <GHComponentLink text='ChannelAvatar' path='/Avatar/ChannelAvatar.tsx'/> |
200-
201193
### channelQueryOptions
202194

203195
Optional configuration parameters used for the initial channel query. Applied only if the value of `channel.initialized` is false. If the channel instance has already been initialized (channel has been queried), then the channel query will be skipped and channelQueryOptions will not be applied.

docusaurus/docs/React/components/utility-components/avatar.mdx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,17 @@ id: avatar
33
title: Avatar
44
---
55

6-
The SDK supports variety of avatar component types. Different approach is taken to display a channel avatar and an avatar of a message author.
6+
Semantically we can speak about two types of avatars in the SDK. One type is the avatar that represents the channel and the other representing another user. The SDK exports the follwing avatar components:
77

8-
The channel avatar accounts for the fact that the channel may contain more than two members and thus become a group channel. Therefore, it renders a `GroupAvatar` component in case of more than two channel members and `Avatar` in case of two or less channel members.
9-
On the other hand, messages use the avatars for a specific single user and thus using the `Avatar` component exclusively.
8+
- `Avatar` - displays single image or name initials in case image is not available
9+
- `GroupAvatar` - displays images or name initials as a fallback in a 2x2 grid
10+
- `ChannelAvatar` - renders `GroupAvatar` in case a channel has more than two members and `Avatar` otherwise
1011

11-
The `Avatar` component displays an image, with fallback to the first letter of the optional name prop. The `GroupAvatar` displays up to four `Avatar` components in a 2x2 grid.
12+
By default, all the SDK components use `Avatar` to display channel resp. user avatar. However, it makes sense to override the default in `ChannelList` resp. `ChannelPreview` and `ChannelHeader` as those avatars may represent a group of users .
1213

13-
## Customizing Avatar component
14+
## Customizing avatar component
1415

15-
The SDK's default `Avatar` component is used by the following components:
16-
17-
- `ChannelSearch` results for users
18-
- `Message`
19-
- `QuotesMessage`
20-
- `MesageStatus`
21-
- `QuotedMessagePreview` in message composer
22-
- Suggestion items for user mentions
23-
- `Poll`
24-
- Message `Reactions`
25-
- `ThreadList`
26-
27-
Passing your custom avatar component to `Channel` prop `Avatar` overrides the avatar for all the above components.
16+
Passing your custom avatar component to `Channel` prop `Avatar` overrides the avatar for all the `Channel` component's children.
2817

2918
Here's an example of using the `Avatar` component within a custom preview component:
3019

@@ -39,13 +28,14 @@ const Avatar = (props: AvatarProps) => {
3928
<Channel Avatar={Avatar} />;
4029
```
4130

42-
## Customizing Channel Avatar
31+
## Customizing channel avatar
4332

4433
You can also take advantage of the `Avatar` prop on the `ChannelHeader` and `ChannelList` components to override just that aspect of these components specifically, see the example below.
4534

4635
An example of overriding just the `Avatar` component in the default `ChannelPreview` component.
4736

4837
```tsx
38+
import { ChannelList } from 'stream-chat-react';
4939
import type { ChannelAvatarProps } from 'stream-chat-react';
5040

5141
const CustomChannelAvatar = (props: ChannelAvatarProps) => {
@@ -55,19 +45,21 @@ const CustomChannelAvatar = (props: ChannelAvatarProps) => {
5545
<ChannelList Avatar={CustomChannelAvatar} />;
5646
```
5747

58-
To override channel avatar in `ChannelHeader` we need to provide our component to `Channel` prop `ChannelAvatar`:
48+
To override the channel avatar in `ChannelHeader` we need to provide it prop `Avatar`:
5949

6050
```tsx
61-
import { Channel } from 'stream-chat-react';
51+
import { ChannelHeader } from 'stream-chat-react';
6252
import type { ChannelAvatarProps } from 'stream-chat-react';
6353

6454
const CustomChannelAvatar = (props: ChannelAvatarProps) => {
6555
return <div>Custom Channel Avatar</div>;
6656
};
6757

68-
<Channel ChannelAvatar={CustomChannelAvatar} />;
58+
<ChannelHeader Avatar={CustomChannelAvatar} />;
6959
```
7060

61+
Also, we can take the advantage of existing SDK's `ChannelAvatar` and pass it to both `ChannelHeader` and `ChannelList` as described above.
62+
7163
## Avatar Props
7264

7365
### className

src/components/Channel/Channel.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ type ChannelPropsForwardedToComponentContext<
112112
| 'AutocompleteSuggestionList'
113113
| 'Avatar'
114114
| 'BaseImage'
115-
| 'ChannelAvatar'
116115
| 'CooldownTimer'
117116
| 'CustomMessageActionsList'
118117
| 'DateSeparator'
@@ -1235,7 +1234,6 @@ const ChannelInner = <
12351234
AutocompleteSuggestionList: props.AutocompleteSuggestionList,
12361235
Avatar: props.Avatar,
12371236
BaseImage: props.BaseImage,
1238-
ChannelAvatar: props.ChannelAvatar,
12391237
CooldownTimer: props.CooldownTimer,
12401238
CustomMessageActionsList: props.CustomMessageActionsList,
12411239
DateSeparator: props.DateSeparator,
@@ -1295,7 +1293,6 @@ const ChannelInner = <
12951293
props.AutocompleteSuggestionList,
12961294
props.Avatar,
12971295
props.BaseImage,
1298-
props.ChannelAvatar,
12991296
props.CooldownTimer,
13001297
props.CustomMessageActionsList,
13011298
props.DateSeparator,

src/components/ChannelHeader/ChannelHeader.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22

33
import { MenuIcon as DefaultMenuIcon } from './icons';
44

5-
import { ChannelAvatar, ChannelAvatarProps } from '../Avatar';
5+
import { ChannelAvatarProps, Avatar as DefaultAvatar } from '../Avatar';
66
import { useChannelPreviewInfo } from '../ChannelPreview/hooks/useChannelPreviewInfo';
77

88
import { useChannelStateContext } from '../../context/ChannelStateContext';
@@ -12,7 +12,7 @@ import { useTranslationContext } from '../../context/TranslationContext';
1212
import type { DefaultStreamChatGenerics } from '../../types/types';
1313

1414
export type ChannelHeaderProps = {
15-
/** UI component to display a user's avatar, defaults to and accepts same props as: [Avatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/Avatar.tsx) */
15+
/** UI component to display an avatar, defaults to [Avatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/Avatar.tsx) component and accepts the same props as: [ChannelAvatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/ChannelAvatar.tsx) */
1616
Avatar?: React.ComponentType<ChannelAvatarProps>;
1717
/** Manually set the image to render, defaults to the Channel image */
1818
image?: string;
@@ -33,7 +33,7 @@ export const ChannelHeader = <
3333
props: ChannelHeaderProps,
3434
) => {
3535
const {
36-
Avatar = ChannelAvatar,
36+
Avatar = DefaultAvatar,
3737
MenuIcon = DefaultMenuIcon,
3838
image: overrideImage,
3939
live,

src/components/ChannelHeader/__tests__/ChannelHeader.test.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from '../../../mock-builders';
2121
import { toHaveNoViolations } from 'jest-axe';
2222
import { axe } from '../../../../axe-helper';
23+
import { ChannelAvatar } from '../../Avatar';
2324
expect.extend(toHaveNoViolations);
2425

2526
const AVATAR_IMG_TEST_ID = 'avatar-img';
@@ -194,6 +195,10 @@ describe('ChannelHeader', () => {
194195
});
195196

196197
describe('group channel', () => {
198+
const props = {
199+
Avatar: ChannelAvatar,
200+
};
201+
197202
const getChannelState = (memberCount, channelData) => {
198203
const users = Array.from({ length: memberCount }, generateUser);
199204
const members = users.map((user) => generateMember({ user }));
@@ -216,7 +221,7 @@ describe('ChannelHeader', () => {
216221
channelsData: [channelState],
217222
customUser: ownUser,
218223
});
219-
await renderComponentBase({ channel, client });
224+
await renderComponentBase({ channel, client, props });
220225
await waitFor(() => {
221226
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
222227
expect(avatarImages).toHaveLength(4);
@@ -237,7 +242,7 @@ describe('ChannelHeader', () => {
237242
client,
238243
} = await initClientWithChannels({ channelsData: [channelState] });
239244
const updatedAttribute = { name: 'new-name' };
240-
await renderComponentBase({ channel, client });
245+
await renderComponentBase({ channel, client, props });
241246

242247
await waitFor(() => {
243248
expect(screen.queryByText(updatedAttribute.name)).not.toBeInTheDocument();
@@ -263,7 +268,7 @@ describe('ChannelHeader', () => {
263268
customUser: ownUser,
264269
});
265270
const updatedAttribute = { image: 'new-image' };
266-
await renderComponentBase({ channel, client });
271+
await renderComponentBase({ channel, client, props });
267272
await waitFor(() => {
268273
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
269274
expect(avatarImages).toHaveLength(3);
@@ -295,7 +300,7 @@ describe('ChannelHeader', () => {
295300
customUser: ownUser,
296301
});
297302
const updatedAttribute = { image: 'new-image' };
298-
await renderComponentBase({ channel, client });
303+
await renderComponentBase({ channel, client, props });
299304
await waitFor(() => {
300305
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
301306
expect(avatarImages).toHaveLength(3);
@@ -327,7 +332,7 @@ describe('ChannelHeader', () => {
327332
customUser: ownUser,
328333
});
329334
const updatedAttribute = { custom: 'new-custom' };
330-
await renderComponentBase({ channel, client });
335+
await renderComponentBase({ channel, client, props });
331336

332337
await waitFor(() => {
333338
expect(screen.queryByText(updatedAttribute.custom)).not.toBeInTheDocument();
@@ -362,7 +367,7 @@ describe('ChannelHeader', () => {
362367
customUser: ownUser,
363368
});
364369
const updatedAttribute = { custom: 'new-custom' };
365-
await renderComponentBase({ channel, client });
370+
await renderComponentBase({ channel, client, props });
366371

367372
await waitFor(() => {
368373
expect(screen.queryByText(updatedAttribute.custom)).not.toBeInTheDocument();

src/components/ChannelList/ChannelList.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { useNotificationRemovedFromChannelListener } from './hooks/useNotificati
1616
import { CustomQueryChannelsFn, usePaginatedChannels } from './hooks/usePaginatedChannels';
1717
import { useUserPresenceChangedListener } from './hooks/useUserPresenceChangedListener';
1818
import { MAX_QUERY_CHANNELS_LIMIT, moveChannelUp } from './utils';
19-
import { ChannelAvatar } from '../Avatar';
19+
import { Avatar as DefaultAvatar } from '../Avatar';
2020
import { ChannelPreview, ChannelPreviewUIComponentProps } from '../ChannelPreview/ChannelPreview';
2121
import {
2222
ChannelSearchProps,
@@ -54,7 +54,7 @@ export type ChannelListProps<
5454
* to false, which will prevent channels not in the list from incrementing the list. The default is true.
5555
*/
5656
allowNewMessagesFromUnfilteredChannels?: boolean;
57-
/** Custom UI component to display channel avatar, defaults to and accepts same props as: [ChannelAvatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/ChannelAvatar.tsx) */
57+
/** UI component to display an avatar, defaults to [Avatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/Avatar.tsx) component and accepts the same props as: [ChannelAvatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/ChannelAvatar.tsx) */
5858
Avatar?: React.ComponentType<ChannelAvatarProps>;
5959
/** Optional function to filter channels prior to loading in the DOM. Do not use any complex or async logic that would delay the loading of the ChannelList. We recommend using a pure function with array methods like filter/sort/reduce. */
6060
channelRenderFilterFn?: (
@@ -166,7 +166,7 @@ const UnMemoizedChannelList = <
166166
) => {
167167
const {
168168
additionalChannelSearchProps,
169-
Avatar = ChannelAvatar,
169+
Avatar = DefaultAvatar,
170170
allowNewMessagesFromUnfilteredChannels,
171171
channelRenderFilterFn,
172172
ChannelSearch = DefaultChannelSearch,

src/components/ChannelPreview/ChannelPreview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type ChannelPreviewProps<
4848
channel: Channel<StreamChatGenerics>;
4949
/** Current selected channel object */
5050
activeChannel?: Channel<StreamChatGenerics>;
51-
/** Custom UI component to display user avatar, defaults to and accepts same props as: [Avatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/Avatar.tsx) */
51+
/** UI component to display an avatar, defaults to [Avatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/Avatar.tsx) component and accepts the same props as: [ChannelAvatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/ChannelAvatar.tsx) */
5252
Avatar?: React.ComponentType<ChannelAvatarProps<StreamChatGenerics>>;
5353
/** Forces the update of preview component on channel update */
5454
channelUpdateCount?: number;

0 commit comments

Comments
 (0)