Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useAriaLiveAnnouncer,
useChannelPreviewInfo,
useChatContext,
useComponentContextIcons,
useStateStore,
} from 'stream-chat-react';
import './Invitations.css';
Expand Down Expand Up @@ -42,9 +43,14 @@ const Invitation = ({
}) => {
const { client } = useChatContext();
const announce = useAriaLiveAnnouncer();
const { displayImage, displayTitle, groupChannelDisplayInfo } = useChannelPreviewInfo({
channel,
});
const { IconUsers } = useComponentContextIcons();
const {
avatarName,
displayImage,
displayTitle,
groupChannelDisplayInfo,
isGroupChannel,
} = useChannelPreviewInfo({ channel });
const [answering, setAnswering] = useState<Answer>();
const rowRef = useRef<HTMLLIElement>(null);
const inviter = channel.data?.created_by?.name;
Expand Down Expand Up @@ -87,9 +93,10 @@ const Invitation = ({
<ChannelAvatar
aria-hidden='true'
displayMembers={groupChannelDisplayInfo.members}
FallbackIcon={isGroupChannel ? IconUsers : undefined}
imageUrl={displayImage}
size='xl'
userName={displayTitle}
userName={avatarName}
/>
<div className='cookbook-invitation__body'>
<p className='cookbook-invitation__title'>{displayTitle}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Channel, MessagePaginatorAggregateState } from 'stream-chat';
import {
ChannelAvatar,
useChannelPreviewInfo,
useComponentContextIcons,
useLatestMessagePreview,
useStateStore,
useWorkspaceNavigation,
Expand All @@ -14,9 +15,14 @@ const latestMessageSelector = ({ latestMessage }: MessagePaginatorAggregateState

export const ChannelChip = ({ channel }: { channel: Channel }) => {
const { openChannel } = useWorkspaceNavigation();
const { displayImage, displayTitle, groupChannelDisplayInfo } = useChannelPreviewInfo({
channel,
});
const { IconUsers } = useComponentContextIcons();
const {
avatarName,
displayImage,
displayTitle,
groupChannelDisplayInfo,
isGroupChannel,
} = useChannelPreviewInfo({ channel });
const { latestMessage } = useStateStore(
channel.messagePaginator.aggregateState,
latestMessageSelector,
Expand All @@ -35,10 +41,11 @@ export const ChannelChip = ({ channel }: { channel: Channel }) => {
<ChannelAvatar
aria-hidden
displayMembers={groupChannelDisplayInfo.members}
FallbackIcon={isGroupChannel ? IconUsers : undefined}
imageUrl={displayImage}
// Group avatars come in lg, xl and 2xl.
size='lg'
userName={displayTitle}
userName={avatarName}
/>
<span className='cookbook-channel-chip__text'>
<span className='cookbook-channel-chip__title'>{displayTitle}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
composeChannelListItemAccessibleLabel,
SummarizedMessagePreview,
useChatContext,
useComponentContextIcons,
useTranslationContext,
useWorkspaceNavigation,
WithComponents,
Expand All @@ -16,17 +17,20 @@ import './CompactChannelRow.css';
const CompactChannelRow = ({
accessibleLabelConfig,
active,
avatarName,
channel,
className,
displayImage,
displayTitle,
groupChannelDisplayInfo,
isGroupChannel,
messageDeliveryStatus,
onSelect,
previewedMessage,
unread,
}: ChannelListItemUIProps) => {
const { client, isMessageAIGenerated } = useChatContext();
const { IconUsers } = useComponentContextIcons();
const { t, tDateTimeParser, userLanguage } = useTranslationContext();
const { openChannel } = useWorkspaceNavigation();
// What a screen reader announces for the row: name, unread count, last message and its time.
Expand Down Expand Up @@ -66,9 +70,11 @@ const CompactChannelRow = ({
<ChannelAvatar
aria-hidden
displayMembers={groupChannelDisplayInfo?.members}
// A channel without a name shows the person icon, or the people icon for a group.
FallbackIcon={isGroupChannel ? IconUsers : undefined}
imageUrl={displayImage}
size='lg'
userName={displayTitle}
userName={avatarName ?? undefined}
/>
<div className='cookbook-channel-row__main'>
<div className='cookbook-channel-row__top'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TypingIndicatorHeader,
useChannel,
useChannelPreviewInfo,
useComponentContextIcons,
useDisplayedTypingUsers,
useStateStore,
WorkspaceNavigationBackButton,
Expand All @@ -29,9 +30,14 @@ export const CustomChannelHeader = () => {
member.user?.online ||
(member.user?.id && Object.prototype.hasOwnProperty.call(watchers, member.user.id)),
).length;
const { displayImage, displayTitle, groupChannelDisplayInfo } = useChannelPreviewInfo({
channel,
});
const { IconUsers } = useComponentContextIcons();
const {
avatarName,
displayImage,
displayTitle,
groupChannelDisplayInfo,
isGroupChannel,
} = useChannelPreviewInfo({ channel });

// Others typing in the channel itself, kept for the same hide delay as the default header after
// they stop. Empty while typing events are disabled for the channel.
Expand All @@ -42,9 +48,10 @@ export const CustomChannelHeader = () => {
<WorkspaceNavigationBackButton />
<ChannelAvatar
displayMembers={groupChannelDisplayInfo.members}
FallbackIcon={isGroupChannel ? IconUsers : undefined}
imageUrl={displayImage}
size='lg'
userName={displayTitle}
userName={avatarName}
/>
<div className='cookbook-channel-header__content'>
<div className='cookbook-channel-header__title'>{displayTitle}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {
Avatar,
TypingIndicatorHeader,
useChannel,
useChannelPreviewInfo,
useDisplayedTypingUsers,
useStateStore,
useThreadContext,
useThreadPreviewInfo,
WorkspaceNavigationBackButton,
WorkspaceNavigationCloseButton,
} from 'stream-chat-react';
Expand All @@ -22,7 +22,9 @@ export const CustomThreadHeader = () => {
const thread = useThreadContext();
const { participants = [], replyCount = 0 } =
useStateStore(thread?.state, threadStateSelector) ?? {};
const { displayTitle } = useChannelPreviewInfo({ channel });
// The channel's name, else the name of whoever started the thread, as the default header shows.
const { displayTitle } = useThreadPreviewInfo({ channel, thread });
const repliesText = `${replyCount} ${replyCount === 1 ? 'reply' : 'replies'}`;

// Others typing a reply in this thread, kept for the same hide delay as the default header after
// they stop. Empty while typing events are disabled for the channel.
Expand All @@ -34,12 +36,7 @@ export const CustomThreadHeader = () => {
<div className='cookbook-thread-header__participants'>
{participants.map(({ user }) =>
user ? (
<Avatar
imageUrl={user.image}
key={user.id}
size='sm'
userName={user.name ?? user.id}
/>
<Avatar imageUrl={user.image} key={user.id} size='sm' userName={user.name} />
) : null,
)}
</div>
Expand All @@ -49,7 +46,7 @@ export const CustomThreadHeader = () => {
{someoneIsTyping ? (
<TypingIndicatorHeader threadList />
) : (
`${displayTitle} Β· ${replyCount} ${replyCount === 1 ? 'reply' : 'replies'}`
[displayTitle, repliesText].filter(Boolean).join(' Β· ')
)}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const ThreadRow = ({ resetHighlighting }: ThreadListItemUIProps) => {
const participantInfo = (participants ?? []).map(({ user }) => ({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small inconsistency: the title above (line 47) still comes from useChannelPreviewInfo({ channel }).displayTitle, which now never falls back to the author. So in this recipe a thread in an unnamed channel reads "Unnamed channel", while the SDK's ThreadListItemUI shows the author's name, as the PR description says. Suggest useThreadPreviewInfo({ thread }), as the thread-header recipe now uses.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title here changed only because useChannelPreviewInfo().displayTitle had started returning "Unnamed channel". With 1fbc26a the hook returns undefined again, so ThreadRow is back to its output on master. It keeps useChannelPreviewInfo rather than useThreadPreviewInfo because this label is the channel ("# …"); titling it after the thread author would read as if the author were the channel.

id: user?.id,
imageUrl: user?.image,
userName: user?.name ?? user?.id,
userName: user?.name,
}));
const isActive = isThreadActive(thread.id);

Expand Down
8 changes: 4 additions & 4 deletions examples/tutorial/src/6-custom-ui-components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const ellipsis = {

const CustomChannelListItem = ({
active,
avatarName,
channel,
displayImage,
displayTitle,
Expand Down Expand Up @@ -82,14 +83,13 @@ const CustomChannelListItem = ({
<ChannelAvatar
imageUrl={displayImage ?? channel.data?.custom?.image}
size='xl'
userName={displayTitle ?? channel.data?.custom?.name ?? 'Channel'}
// Undefined for a channel without a name, so the avatar shows its person icon.
userName={avatarName ?? undefined}
/>
{/* `minWidth: 0` lets the text column shrink below its content, so a long title or preview
is cut off with an ellipsis instead of squeezing the avatar out of the row. */}
<div style={{ flex: 1, minWidth: 0 }}>
<div style={ellipsis}>
{displayTitle ?? channel.data?.custom?.name ?? 'Unnamed Channel'}
</div>
<div style={ellipsis}>{displayTitle}</div>
{previewedMessage ? (
<div style={{ ...ellipsis, fontSize: '14px', opacity: 0.75 }}>
<SummarizedMessagePreview latestMessage={previewedMessage} />
Expand Down
1 change: 1 addition & 0 deletions examples/vite/src/i18n/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ export const deTranslations = {
'common.unblock.ariaLabel': 'Blockierung aufheben',
'common.unmute.title': 'Stummschaltung aufheben',
'common.unmuted.label': 'Die Stummschaltung von {{ user }} wurde aufgehoben',
'common.unnamedChannel.text': 'Unbenannter Kanal',
'common.unpin.title': 'LoslΓΆsen',
'common.unsupportedAttachment.text': 'Nicht unterstΓΌtzter Anhang',
'common.userBlocked.text': 'Benutzer blockiert',
Expand Down
1 change: 1 addition & 0 deletions examples/vite/src/i18n/it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ export const itTranslations = {
'common.unblock.ariaLabel': 'Sblocca',
'common.unmute.title': 'Riattiva',
'common.unmuted.label': '{{ user }} Γ¨ stato riattivato',
'common.unnamedChannel.text': 'Canale senza nome',
'common.unpin.title': 'Rimuovi dai fissati',
'common.unsupportedAttachment.text': 'Allegato non supportato',
'common.userBlocked.text': 'Utente bloccato',
Expand Down
9 changes: 9 additions & 0 deletions src/components/Avatar/GroupAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export type GroupAvatarProps = {
displayMembers?: GroupAvatarMember[];
size: '2xl' | 'xl' | 'lg' | (string & {}) | null;
badgeSize?: BadgeSize;
/**
* Icon of the single `Avatar` rendered for fewer than 2 `displayMembers`. It shows when that
* member has neither an image nor a name to take initials from, or when there is no member.
* Defaults to the `IconUser` slot. The member tiles rendered for 2 or more members ignore it.
*/
FallbackIcon?: AvatarProps['FallbackIcon'];
isOnline?: boolean;
} & ComponentPropsWithoutRef<'div'>;

Expand All @@ -26,6 +32,7 @@ export const GroupAvatar = ({
badgeSize,
className,
displayMembers = [],
FallbackIcon,
isOnline,
size,
...rest
Expand All @@ -41,6 +48,8 @@ export const GroupAvatar = ({

return (
<Avatar
className={className}
FallbackIcon={FallbackIcon}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not new in this PR, but it's the branch being edited: className is taken out of the props above (for the group div) and never passed to this single Avatar. So when a channel avatar shows one image or initials, its className is dropped. ChannelHeader's str-chat__avatar--channel-header never reaches a DM's header avatar, and neither does an app's className.

Suggested fix: pass className={className} here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2e8a88b: the single Avatar now gets className, with a test.

imageUrl={firstUser?.imageUrl}
isOnline={isOnline}
size={size}
Expand Down
28 changes: 28 additions & 0 deletions src/components/Avatar/__tests__/GroupAvatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ describe('GroupAvatar', () => {
expect(queryByTestId('group-avatar')).not.toBeInTheDocument();
});

it('passes FallbackIcon to the single Avatar', () => {
const FallbackIcon = () => <svg data-testid='custom-fallback' />;
const { getByTestId } = render(<GroupAvatar FallbackIcon={FallbackIcon} size='xl' />);
expect(getByTestId('custom-fallback')).toBeInTheDocument();
});

it('passes className to the single Avatar', () => {
const { getByTestId } = render(
<GroupAvatar className='custom-class' displayMembers={[member1]} size='xl' />,
);
expect(getByTestId('avatar')).toHaveClass('str-chat__avatar', 'custom-class');
});

it('keeps FallbackIcon off the group element', () => {
// React warns about an unknown prop that reaches a DOM element.
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined);
const FallbackIcon = () => <svg data-testid='custom-fallback' />;
render(
<GroupAvatar
displayMembers={[member1, member2]}
FallbackIcon={FallbackIcon}
size='xl'
/>,
);
expect(consoleError).not.toHaveBeenCalled();
consoleError.mockRestore();
});

it('should render group avatar when 2 or more members are provided', () => {
const { getByTestId } = render(
<GroupAvatar displayMembers={[member1, member2]} size='xl' />,
Expand Down
39 changes: 31 additions & 8 deletions src/components/ChannelHeader/ChannelHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,35 @@ import { TypingIndicatorHeader } from '../TypingIndicator/TypingIndicatorHeader'
import { useDisplayedTypingUsers } from '../TypingIndicator/hooks/useDisplayedTypingUsers';
import { useChannelHeaderOnlineStatus } from './hooks/useChannelHeaderOnlineStatus';
import { useChannelPreviewInfo } from '../ChannelListItem/hooks/useChannelPreviewInfo';
import { useChannel } from '../../context';
import {
useChannel,
useComponentContextIcons,
useTranslationContext,
} from '../../context';
import { WorkspaceNavigationBackButton, WorkspaceNavigationCloseButton } from '../Button';

export type ChannelHeaderTitleProps = {
/** The channel the header belongs to. */
channel: Channel;
/** The title to show: the `title` passed to `ChannelHeader`, else the channel display name. */
/**
* The title to show: the `title` passed to `ChannelHeader`, else the channel display name.
* Undefined for a channel without a name.
*/
displayTitle?: string;
};

/** Default title of `ChannelHeader`: the display title in a `div`. */
export const ChannelHeaderTitle = ({ displayTitle }: ChannelHeaderTitleProps) => (
<div className='str-chat__channel-header__data__title'>{displayTitle}</div>
);
/**
* Default title of `ChannelHeader`: the display title in a `div`, or the translated "Unnamed
* channel" for a channel without a name.
*/
export const ChannelHeaderTitle = ({ displayTitle }: ChannelHeaderTitleProps) => {
const { t } = useTranslationContext();
return (
<div className='str-chat__channel-header__data__title'>
{displayTitle ?? t('common.unnamedChannel.text', 'Unnamed channel')}
</div>
);
};

/**
* Default subtitle of `ChannelHeader`, for the channel in context. Shows the typing indicator while
Expand Down Expand Up @@ -89,7 +104,14 @@ export const ChannelHeader = (props: ChannelHeaderProps) => {
} = props;

const channel = useChannel();
const { displayImage, displayTitle, groupChannelDisplayInfo } = useChannelPreviewInfo({
const { IconUsers } = useComponentContextIcons();
const {
avatarName,
displayImage,
displayTitle,
groupChannelDisplayInfo,
isGroupChannel,
} = useChannelPreviewInfo({
channel,
overrideImage,
overrideTitle,
Expand All @@ -108,9 +130,10 @@ export const ChannelHeader = (props: ChannelHeaderProps) => {
<Avatar
className='str-chat__avatar--channel-header'
displayMembers={groupChannelDisplayInfo?.members}
FallbackIcon={isGroupChannel ? IconUsers : undefined}
imageUrl={displayImage}
size='lg'
userName={displayTitle}
userName={avatarName}
/>
<EndContent />
</div>
Expand Down
Loading