Skip to content
Draft
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
39 changes: 30 additions & 9 deletions src/components/Reactions/MessageReactionsDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useMemo, useState } from 'react';

import type { ReactionSummary, ReactionType } from './types';

import { useFetchReactions } from './hooks/useFetchReactions';
import { Avatar as DefaultAvatar } from '../Avatar';
import type { MessageContextValue } from '../../context';
import {
Expand All @@ -14,8 +13,11 @@ import {
import type { ReactionSort } from 'stream-chat';
import { defaultReactionOptions } from './reactionOptions';
import type { useProcessReactions } from './hooks/useProcessReactions';
import { useReactionPaginator } from './hooks/useReactionPaginator';
import { IconEmojiAdd } from '../Icons';
import { ReactionSelector, type ReactionSelectorProps } from './ReactionSelector';
import { LoadMorePaginator } from '../LoadMore';
import { Button } from '../Button';

export type MessageReactionsDetailProps = Partial<
Pick<MessageContextValue, 'handleFetchReactions' | 'reactionDetailsSort'>
Expand Down Expand Up @@ -52,7 +54,6 @@ interface MessageReactionsDetailInterface {
}

export const MessageReactionsDetail: MessageReactionsDetailInterface = ({
handleFetchReactions,
handleReaction,
onSelectedReactionTypeChange,
own_reactions,
Expand All @@ -62,8 +63,8 @@ export const MessageReactionsDetail: MessageReactionsDetailInterface = ({
selectedReactionType,
totalReactionCount,
}) => {
const [extendedReactionListOpen, setExtendedReactionListOpen] = useState(false);
const { client } = useChatContext();
const [extendedReactionListOpen, setExtendedReactionListOpen] = useState(false);
const {
Avatar = DefaultAvatar,
LoadingIndicator = MessageReactionsDetailLoadingIndicator,
Expand All @@ -82,13 +83,13 @@ export const MessageReactionsDetail: MessageReactionsDetailInterface = ({
propReactionDetailsSort ?? contextReactionDetailsSort ?? defaultReactionDetailsSort;

const {
hasNext,
isLoading: areReactionsLoading,
paginator,
reactions: reactionDetails,
refetch,
} = useFetchReactions({
handleFetchReactions,
} = useReactionPaginator({
reactionType: selectedReactionType,
shouldFetch: true,
sort: reactionDetailsSort,
});

Expand Down Expand Up @@ -191,8 +192,27 @@ export const MessageReactionsDetail: MessageReactionsDetailInterface = ({
className='str-chat__message-reactions-detail__user-list'
data-testid='all-reacting-users'
>
{areReactionsLoading && <LoadingIndicator />}
{!areReactionsLoading && (
<LoadMorePaginator
hasNextPage={hasNext}
isLoading={areReactionsLoading}
LoadMoreButton={({ isLoading, onClick }) => {
if (isLoading) return null;

return (
<Button
appearance='ghost'
aria-label={t('Load more')}
className='str-chat__button--load-more'
onClick={onClick}
size='xs'
variant='secondary'
>
{t('Load more')}
</Button>
);
}}
loadNextPage={paginator.next}
>
<>
{reactionDetails.map(({ type, user }) => {
const belongsToCurrentUser = client.user?.id === user?.id;
Expand Down Expand Up @@ -257,7 +277,8 @@ export const MessageReactionsDetail: MessageReactionsDetailInterface = ({
);
})}
</>
)}
</LoadMorePaginator>
{areReactionsLoading && <LoadingIndicator />}
</div>
</div>
</div>
Expand Down
74 changes: 74 additions & 0 deletions src/components/Reactions/hooks/ReactionPaginator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
BasePaginator,
type PaginationQueryParams,
type PaginatorOptions,
type ReactionFilters,
type ReactionResponse,
type ReactionSort,
type StreamChat,
} from 'stream-chat';

export class ReactionPaginator extends BasePaginator<ReactionResponse> {
private client: StreamChat;
private messageId: string;
private _filters: ReactionFilters;
private _sort: ReactionSort;
protected usesCursorPagination = true;

get filters(): ReactionFilters | undefined {
return this._filters;
}

get sort(): ReactionSort | undefined {
return this._sort;
}

set filters(filters: ReactionFilters) {
this._filters = filters;
this.invalidate();

Check failure on line 28 in src/components/Reactions/hooks/ReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should show extended reaction list when add emoji button is clicked

TypeError: this.invalidate is not a function ❯ ReactionPaginator.set filters [as filters] src/components/Reactions/hooks/ReactionPaginator.ts:28:10 ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:44:32 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22
}

set sort(sort: ReactionSort) {
this._sort = sort;
this.invalidate();
}

constructor({
client,
messageId,
options,
}: {
client: StreamChat;
messageId: string;
options?: PaginatorOptions;
}) {
super(options);
this.client = client;
this.messageId = messageId;
this._filters = {};
this._sort = { created_at: -1 };
}

async query(params: PaginationQueryParams) {
const direction = params.direction;

const response = await this.client.queryReactions(
this.messageId,
this._filters,
this._sort,
{
[direction]: direction === 'next' ? params.next : params.prev,
limit: this.pageSize,
},
);

return {
items: response.reactions,
next: response.next,
};
}

public filterQueryResults(items: ReactionResponse[]) {
return items;
}
}
77 changes: 77 additions & 0 deletions src/components/Reactions/hooks/useReactionPaginator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { useCallback, useEffect, useRef } from 'react';
import type { PaginatorState, ReactionResponse, ReactionSort } from 'stream-chat';

import { useChatContext, useMessageContext } from '../../../context';
import { ReactionPaginator } from './ReactionPaginator';
import { useStateStore } from '../../../store';
import type { ReactionType } from '../types';

export interface FetchReactionsOptions {
reactionType: ReactionType | null;
sort?: ReactionSort;
}

const STABLE_ARRAY: ReactionResponse[] = [];
const reactionSelector = (currentState: PaginatorState<ReactionResponse>) => ({
hasNext: currentState.hasNext,
isLoading: currentState.isLoading,
reactions: currentState.items ?? STABLE_ARRAY,
});

// use null symbol instead of the actual null for the paginator key
const nullSymbol = Symbol('null');

export function useReactionPaginator({ reactionType, sort }: FetchReactionsOptions) {
const { client } = useChatContext();
const { message } = useMessageContext('useReactionPaginator');

const paginatorByTypeRef = useRef<{
[key: string | symbol]: ReactionPaginator | undefined;
}>({});

const normalizedReactionType = reactionType === null ? nullSymbol : reactionType;

const getOrCreateInstance = () => {
const existingInstance = paginatorByTypeRef.current[normalizedReactionType];

if (existingInstance) return existingInstance;

const instance = new ReactionPaginator({
client,
messageId: message.id,

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should provide contextual accessible name for current-user remove button

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should render add emoji button in the reaction type list

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should always display reaction count for each reaction type

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > emits typed notification when fetching reactions fails

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should use custom reaction details comparator if provided

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should display total reaction count

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should switch to display users of different reaction type

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should display a list of reacted users

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41

Check failure on line 41 in src/components/Reactions/hooks/useReactionPaginator.ts

View workflow job for this annotation

GitHub Actions / Test

src/components/Reactions/__tests__/MessageReactionsDetail.test.tsx > MessageReactionsDetail > should render the reactions detail panel

TypeError: Cannot read properties of undefined (reading 'id') ❯ getOrCreateInstance src/components/Reactions/hooks/useReactionPaginator.ts:41:26 ❯ useReactionPaginator src/components/Reactions/hooks/useReactionPaginator.ts:50:29 ❯ MessageReactionsDetail src/components/Reactions/MessageReactionsDetail.tsx:91:7 ❯ Object.react-stack-bottom-frame node_modules/react-dom/cjs/react-dom-client.development.js:22428:20 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom-client.development.js:5757:22 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom-client.development.js:8018:19 ❯ beginWork node_modules/react-dom/cjs/react-dom-client.development.js:9683:18 ❯ runWithFiberInDEV node_modules/react-dom/cjs/react-dom-client.development.js:543:16 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom-client.development.js:15052:22 ❯ workLoopSync node_modules/react-dom/cjs/react-dom-client.development.js:14870:41
options: { pageSize: 25 },
});
if (reactionType) instance.filters = { type: reactionType };
if (sort) instance.sort = sort;

return (paginatorByTypeRef.current[normalizedReactionType] = instance);
};

const selectedPaginator = getOrCreateInstance();

const { hasNext, isLoading, reactions } = useStateStore(
selectedPaginator.state,
reactionSelector,
);

const refetch = useCallback(() => {
selectedPaginator.invalidate();
selectedPaginator.next();
}, [selectedPaginator]);

useEffect(() => {
const data = selectedPaginator.state.getLatestValue().items;

if (data?.length) return;

selectedPaginator.next();
}, [selectedPaginator]);

return {
hasNext,
isLoading,
paginator: selectedPaginator,
reactions,
refetch,
} as const;
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@
position: relative;
padding-block-end: var(--str-chat__spacing-xxs);
max-height: 180px;
display: flex;
flex-direction: column;

.str-chat__button.str-chat__button--load-more {
align-self: center;
flex-shrink: 0;
margin-block-end: var(--str-chat__spacing-xs);
margin-block-start: var(--str-chat__spacing-xxs);
}

.str-chat__message-reactions-detail__skeleton-item {
padding-block: var(--str-chat__spacing-xxs);
Expand Down
Loading