Skip to content

Commit

Permalink
Component adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Aug 28, 2024
1 parent b3a1357 commit bd094cc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/components/Message/hooks/useReactionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const useReactionHandler = <

try {
updateMessage(tempMessage);
// @ts-expect-error
thread?.upsertReplyLocally({ message: tempMessage });

const messageResponse = add
Expand All @@ -107,6 +108,7 @@ export const useReactionHandler = <
} catch (error) {
// revert to the original message if the API call fails
updateMessage(message);
// @ts-expect-error
thread?.upsertReplyLocally({ message });
}
}, 1000);
Expand Down
1 change: 0 additions & 1 deletion src/components/Message/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export const getMessageActions = (

export const ACTIONS_NOT_WORKING_IN_THREAD = [
MESSAGE_ACTIONS.pin,
MESSAGE_ACTIONS.react,
MESSAGE_ACTIONS.reply,
MESSAGE_ACTIONS.markUnread,
];
Expand Down
4 changes: 2 additions & 2 deletions src/components/Thread/Thread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const Thread = <
};

const selector = (nextValue: ThreadState) =>
[nextValue.latestReplies, nextValue.loadingPreviousPage, nextValue.parentMessage] as const;
[nextValue.replies, nextValue.pagination.isLoadingPrev, nextValue.parentMessage] as const;

const ThreadInner = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
Expand Down Expand Up @@ -145,7 +145,7 @@ const ThreadInner = <
> = threadInstance
? {
loadingMore: loadingPreviousPage,
loadMore: threadInstance.loadPreviousPage,
loadMore: threadInstance.loadPrevPage,
messages: latestReplies,
}
: {
Expand Down
35 changes: 18 additions & 17 deletions src/components/Threads/ThreadList/ThreadListItemUI.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import React, { useCallback } from 'react';
import clsx from 'clsx';

import type { FormatMessageResponse, ThreadState } from 'stream-chat';
import type { ComponentPropsWithoutRef } from 'react';

import { Timestamp } from '../../Message/Timestamp';
import { Avatar } from '../../Avatar';
import { Icon } from '../icons';
import { UnreadCountBadge } from '../UnreadCountBadge';
Expand All @@ -22,15 +23,6 @@ export type ThreadListItemUIProps = ComponentPropsWithoutRef<'button'>;
* - handle deleted message [in progress]
*/

const selector = (nextValue: ThreadState) =>
[
nextValue.latestReplies.at(-1),
nextValue.read,
nextValue.parentMessage,
nextValue.channel,
nextValue.deletedAt,
] as const;

export const attachmentTypeIconMap = {
audio: '🔈',
file: '📄',
Expand Down Expand Up @@ -79,15 +71,26 @@ export const ThreadListItemUI = (props: ThreadListItemUIProps) => {
const { client } = useChatContext();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const thread = useThreadListItemContext()!;
const [latestReply, read, parentMessage, channelData, deletedAt] = useStateStore(

const selector = useCallback(
(nextValue: ThreadState) =>
[
nextValue.replies.at(-1),
(client.userID && nextValue.read[client.userID]?.unreadMessageCount) || 0,
nextValue.parentMessage,
nextValue.channel,
nextValue.deletedAt,
] as const,
[client],
);

const [latestReply, ownUnreadMessageCount, parentMessage, channelData, deletedAt] = useStateStore(
thread.state,
selector,
);

const { activeThread, setActiveThread } = useThreadsViewContext();

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const unreadMessagesCount = read[client.user!.id]?.unread_messages ?? 0;
const avatarProps = deletedAt ? null : latestReply?.user;

return (
Expand All @@ -110,7 +113,7 @@ export const ThreadListItemUI = (props: ThreadListItemUIProps) => {
{/* TODO: use thread.title instead? */}
replied to: {getTitleFromMessage({ message: parentMessage })}
</div>
{!deletedAt && <UnreadCountBadge count={unreadMessagesCount} />}
{!deletedAt && <UnreadCountBadge count={ownUnreadMessageCount} />}
</div>
<div className='str-chat__thread-list-item__latest-reply'>
<Avatar {...avatarProps} />
Expand All @@ -127,9 +130,7 @@ export const ThreadListItemUI = (props: ThreadListItemUIProps) => {
: getTitleFromMessage({ currentUserId: client.user?.id, message: latestReply })}
</div>
<div className='str-chat__thread-list-item__latest-reply-timestamp'>
{deletedAt
? deletedAt.toLocaleTimeString()
: latestReply?.created_at.toLocaleTimeString() || 'N/A'}
<Timestamp timestamp={deletedAt ?? latestReply?.created_at} />
</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Threads/hooks/useThreadState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { useThreadListItemContext } from '../ThreadList';
import { useThreadContext } from '../ThreadContext';

/**
* @description returns thread state, prioritizes `ThreadListItemContext`, uses `ThreadContext` if not present
* @description returns thread state, prioritizes `ThreadListItemContext` falls back to `ThreadContext` if not former is not present
*/
export const useThreadState = <T extends readonly unknown[]>(
selector: (nextValue: ThreadState) => T,
) => {
const listItemThread = useThreadListItemContext();
const thread = useThreadContext();

return useStateStore(listItemThread?.state ?? thread.state, selector);
return useStateStore(listItemThread?.state ?? thread?.state, selector);
};
7 changes: 2 additions & 5 deletions src/context/MessageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,12 @@ export const MessageProvider = <
export const useMessageContext = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
>(
componentName?: string,
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
_componentName?: string,
) => {
const contextValue = useContext(MessageContext);

if (!contextValue) {
console.warn(
`The useMessageContext hook was called outside of the MessageContext provider. Make sure this hook is called within the Message's UI component. The errored call is located in the ${componentName} component.`,
);

return {} as MessageContextValue<StreamChatGenerics>;
}

Expand Down

0 comments on commit bd094cc

Please sign in to comment.