From abf6cd80487e2bf02312339d40253dacac9355cb Mon Sep 17 00:00:00 2001 From: Khushal Agarwal Date: Fri, 5 Jul 2024 12:51:53 +0530 Subject: [PATCH] fix: thread reaction list rerender and add prop to enable/disable press on parent message (#2575) --- .../parent_message_prevent_press.mdx | 5 +++ .../reactnative/contexts/thread-context.mdx | 7 +++- .../docs/reactnative/ui-components/thread.mdx | 11 +++++-- .../Channel/hooks/useCreateThreadContext.ts | 2 ++ package/src/components/Thread/Thread.tsx | 7 ++-- .../components/ThreadFooterComponent.tsx | 32 +++++++++++++++---- .../contexts/threadContext/ThreadContext.tsx | 4 +++ 7 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 docusaurus/docs/reactnative/common-content/contexts/thread-context/parent_message_prevent_press.mdx diff --git a/docusaurus/docs/reactnative/common-content/contexts/thread-context/parent_message_prevent_press.mdx b/docusaurus/docs/reactnative/common-content/contexts/thread-context/parent_message_prevent_press.mdx new file mode 100644 index 0000000000..5f91308fb9 --- /dev/null +++ b/docusaurus/docs/reactnative/common-content/contexts/thread-context/parent_message_prevent_press.mdx @@ -0,0 +1,5 @@ +Boolean to enable/disable parent message press. + +| Type | Default | +| ------- | ------- | +| Boolean | `true` | diff --git a/docusaurus/docs/reactnative/contexts/thread-context.mdx b/docusaurus/docs/reactnative/contexts/thread-context.mdx index 40b741e9e4..f6d86caeb7 100644 --- a/docusaurus/docs/reactnative/contexts/thread-context.mdx +++ b/docusaurus/docs/reactnative/contexts/thread-context.mdx @@ -9,6 +9,7 @@ import Thread from '../common-content/ui-components/channel/props/thread.mdx'; import CloseThread from '../common-content/contexts/thread-context/close_thread.mdx'; import LoadMoreThread from '../common-content/contexts/thread-context/load_more_thread.mdx'; import OpenThread from '../common-content/contexts/thread-context/open_thread.mdx'; +import ParentMessagePreventPress from '../common-content/contexts/thread-context/parent_message_prevent_press.mdx'; import ReloadThread from '../common-content/contexts/thread-context/reload_thread.mdx'; import SetThreadLoadingMore from '../common-content/contexts/thread-context/set_thread_loading_more.mdx'; import ThreadHasMore from '../common-content/contexts/thread-context/thread_has_more.mdx'; @@ -38,7 +39,7 @@ const value = useThreadContext(); ## Value -###
_forwarded from [Channel](../../core-components/channel#allowthreadmessagesinchannel)_ props
allowThreadMessagesInChannel {#allowthreadmessagesinchannel} +###
_forwarded from [Channel](../../core-components/channel#allowthreadmessagesinchannel)_ props
allowThreadMessagesInChannel `{#allowthreadmessagesinchannel}` @@ -54,6 +55,10 @@ const value = useThreadContext(); +### `parentMessagePreventPress` + + + ### `reloadThread` diff --git a/docusaurus/docs/reactnative/ui-components/thread.mdx b/docusaurus/docs/reactnative/ui-components/thread.mdx index 2b9c81e744..294074208e 100644 --- a/docusaurus/docs/reactnative/ui-components/thread.mdx +++ b/docusaurus/docs/reactnative/ui-components/thread.mdx @@ -9,6 +9,7 @@ import Thread from '../common-content/ui-components/channel/props/thread.mdx'; import CloseThread from '../common-content/contexts/thread-context/close_thread.mdx'; import LoadMoreThread from '../common-content/contexts/thread-context/load_more_thread.mdx'; +import ParentMessagePreventPress from '../common-content/contexts/thread-context/parent_message_prevent_press.mdx'; import ReloadThread from '../common-content/contexts/thread-context/reload_thread.mdx'; Component to render thread replies for a message, along with and input box for adding new thread replies. This component internally uses `MessageList` and `MessageInput` components. @@ -70,7 +71,7 @@ Closes thread on dismount, defaults to `true`. -###
overrides the value from [ThreadContext](../../contexts/thread-context#closeThread)
`closeThread` {#closeThread} +###
overrides the value from [ThreadContext](../../contexts/thread-context#closethread)
`closeThread` {#closeThread} @@ -82,7 +83,7 @@ When true, the underlying input box will be disabled. | ------- | ------- | | Boolean | `false` | -###
overrides the value from [ThreadContext](../../contexts/thread-context#loadMoreThread)
`loadMoreThread` {#loadMoreThread} +###
overrides the value from [ThreadContext](../../contexts/thread-context#loadmorethread)
`loadMoreThread` {#loadMoreThread} @@ -94,7 +95,11 @@ Function which gets called when Thread component un-mounts. | -------- | | Function | -###
overrides the value from [ThreadContext](../../contexts/thread-context#reloadThread)
`reloadThread` {#reloadThread} +###
overrides the value from [ThreadContext](../../contexts/thread-context#parentmessagepreventpress)
`parentMessagePreventPress` {#parentMessagePreventPress} + + + +###
overrides the value from [ThreadContext](../../contexts/thread-context#reloadthread)
`reloadthread` {#reloadThread} diff --git a/package/src/components/Channel/hooks/useCreateThreadContext.ts b/package/src/components/Channel/hooks/useCreateThreadContext.ts index 2629ae8355..1024672738 100644 --- a/package/src/components/Channel/hooks/useCreateThreadContext.ts +++ b/package/src/components/Channel/hooks/useCreateThreadContext.ts @@ -20,6 +20,7 @@ export const useCreateThreadContext = < }: ThreadContextValue) => { const threadId = thread?.id; const threadReplyCount = thread?.reply_count; + const threadLatestReactions = thread?.latest_reactions; const threadMessagesStr = reduceMessagesToString(threadMessages); const threadContext: ThreadContextValue = useMemo( @@ -42,6 +43,7 @@ export const useCreateThreadContext = < threadLoadingMore, threadMessagesStr, threadReplyCount, + threadLatestReactions, ], ); diff --git a/package/src/components/Thread/Thread.tsx b/package/src/components/Thread/Thread.tsx index 211674d67c..bc79a5b44a 100644 --- a/package/src/components/Thread/Thread.tsx +++ b/package/src/components/Thread/Thread.tsx @@ -23,7 +23,7 @@ type ThreadPropsWithContext< Pick, 'MessageList'> & Pick< ThreadContextValue, - 'closeThread' | 'loadMoreThread' | 'reloadThread' | 'thread' + 'closeThread' | 'loadMoreThread' | 'parentMessagePreventPress' | 'reloadThread' | 'thread' > & { /** * Additional props for underlying MessageInput component. @@ -68,6 +68,7 @@ const ThreadWithContext = < MessageInput = DefaultMessageInput, MessageList, onThreadDismount, + parentMessagePreventPress = true, thread, } = props; @@ -98,7 +99,9 @@ const ThreadWithContext = < return ( ( + + )} threadList {...additionalMessageListProps} /> diff --git a/package/src/components/Thread/components/ThreadFooterComponent.tsx b/package/src/components/Thread/components/ThreadFooterComponent.tsx index 2a84504d0b..622f81a0eb 100644 --- a/package/src/components/Thread/components/ThreadFooterComponent.tsx +++ b/package/src/components/Thread/components/ThreadFooterComponent.tsx @@ -38,14 +38,14 @@ const styles = StyleSheet.create({ type ThreadFooterComponentPropsWithContext< StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, > = Pick, 'Message'> & - Pick, 'thread'>; + Pick, 'parentMessagePreventPress' | 'thread'>; const ThreadFooterComponentWithContext = < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, >( props: ThreadFooterComponentPropsWithContext, ) => { - const { Message, thread } = props; + const { Message, parentMessagePreventPress, thread } = props; const { t } = useTranslationContext(); const { vw } = useViewport(); @@ -71,7 +71,12 @@ const ThreadFooterComponentWithContext = < return ( - + @@ -114,8 +119,12 @@ const areEqual = , nextProps: ThreadFooterComponentPropsWithContext, ) => { - const { thread: prevThread } = prevProps; - const { thread: nextThread } = nextProps; + const { parentMessagePreventPress: prevParentMessagePreventPress, thread: prevThread } = + prevProps; + const { parentMessagePreventPress: nextParentMessagePreventPress, thread: nextThread } = + nextProps; + + if (prevParentMessagePreventPress !== nextParentMessagePreventPress) return false; const threadEqual = prevThread?.id === nextThread?.id && @@ -143,18 +152,27 @@ const MemoizedThreadFooter = React.memo( areEqual, ) as typeof ThreadFooterComponentWithContext; +export type ThreadFooterComponentProps< + StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, +> = Partial, 'Message'>> & + Partial, 'parentMessagePreventPress' | 'thread'>>; + export const ThreadFooterComponent = < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, ->() => { +>( + props: ThreadFooterComponentProps, +) => { const { Message } = useMessagesContext(); - const { thread } = useThreadContext(); + const { parentMessagePreventPress, thread } = useThreadContext(); return ( ); }; diff --git a/package/src/contexts/threadContext/ThreadContext.tsx b/package/src/contexts/threadContext/ThreadContext.tsx index 3596930c9e..419b70e974 100644 --- a/package/src/contexts/threadContext/ThreadContext.tsx +++ b/package/src/contexts/threadContext/ThreadContext.tsx @@ -22,6 +22,10 @@ export type ThreadContextValue< threadHasMore: boolean; threadLoadingMore: boolean; threadMessages: ChannelState['threads'][string]; + /** + * Boolean to enable/disable parent message press + */ + parentMessagePreventPress?: boolean; }; export const ThreadContext = React.createContext(DEFAULT_BASE_CONTEXT_VALUE as ThreadContextValue);