Skip to content

Commit 755c846

Browse files
authored
feat: Add disableMarkAsRead to the Channel comp (#329)
1 parent 319d263 commit 755c846

File tree

9 files changed

+49
-25
lines changed

9 files changed

+49
-25
lines changed

scripts/index_d_ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ declare module "SendbirdUIKitGlobal" {
465465
queries?: ChannelQueries;
466466
renderUserProfile?: (props: RenderUserProfileProps) => React.ReactNode | React.ReactElement;
467467
disableUserProfile?: boolean;
468+
disableMarkAsRead?: boolean;
468469
};
469470

470471
export interface ChannelUIProps {

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ type ChannelContextProps = {
636636
queries?: ChannelQueries;
637637
renderUserProfile?: (props: RenderUserProfileProps) => React.ReactElement;
638638
disableUserProfile?: boolean;
639+
disableMarkAsRead?: boolean;
639640
};
640641

641642
interface ChannelUIProps {

src/smart-components/Channel/components/ChannelUI/index.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const ChannelUI: React.FC<ChannelUIProps> = ({
4747
setHighLightedMessageId,
4848
scrollRef,
4949
messagesDispatcher,
50+
disableMarkAsRead,
5051
} = useChannelContext();
5152
const [unreadCount, setUnreadCount] = useState(0);
5253
useEffect(() => {
@@ -117,15 +118,17 @@ const ChannelUI: React.FC<ChannelUIProps> = ({
117118
if (scrollRef?.current?.scrollTop) {
118119
scrollRef.current.scrollTop = scrollRef?.current?.scrollHeight - scrollRef?.current?.offsetHeight;
119120
}
120-
try {
121-
currentGroupChannel?.markAsRead();
122-
} catch {
123-
//
121+
if (!disableMarkAsRead) {
122+
try {
123+
currentGroupChannel?.markAsRead();
124+
} catch {
125+
//
126+
}
127+
messagesDispatcher({
128+
type: messageActionTypes.MARK_AS_READ,
129+
payload: { channel: currentGroupChannel },
130+
});
124131
}
125-
messagesDispatcher({
126-
type: messageActionTypes.MARK_AS_READ,
127-
payload: { channel: currentGroupChannel },
128-
});
129132
setInitialTimeStamp(null);
130133
setAnimatedMessageId(null);
131134
setHighLightedMessageId(null);

src/smart-components/Channel/components/MessageList/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const MessageList: React.FC<MessageListProps> = (props: MessageListProps) => {
3939
messagesDispatcher,
4040
messageActionTypes,
4141
currentGroupChannel,
42+
disableMarkAsRead,
4243
} = useChannelContext();
4344
const [scrollBottom, setScrollBottom] = useState(0);
4445

@@ -90,7 +91,7 @@ const MessageList: React.FC<MessageListProps> = (props: MessageListProps) => {
9091
// do this later
9192
setTimeout(() => {
9293
// mark as read if scroll is at end
93-
if (clientHeight + scrollTop === scrollHeight) {
94+
if (!disableMarkAsRead && clientHeight + scrollTop === scrollHeight) {
9495
messagesDispatcher({
9596
type: messageActionTypes.MARK_AS_READ,
9697
payload: { channel: currentGroupChannel },

src/smart-components/Channel/context/ChannelProvider.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export type ChannelContextProps = {
8383
queries?: ChannelQueries;
8484
renderUserProfile?: (props: RenderUserProfileProps) => React.ReactElement;
8585
disableUserProfile?: boolean;
86+
disableMarkAsRead?: boolean;
8687
};
8788

8889
interface MessageStoreInterface {
@@ -117,7 +118,7 @@ interface UpdateMessageProps {
117118

118119
interface ChannelProviderInterface extends ChannelContextProps, MessageStoreInterface {
119120
scrollToMessage?(createdAt: number, messageId: number): void;
120-
messageActionTypes: Record<string ,string>;
121+
messageActionTypes: Record<string, string>;
121122
messagesDispatcher: CustomUseReducerDispatcher;
122123
quoteMessage: UserMessage | FileMessage;
123124
setQuoteMessage: React.Dispatch<React.SetStateAction<UserMessage | FileMessage>>;
@@ -164,6 +165,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
164165
onSearchClick,
165166
replyType,
166167
queries,
168+
disableMarkAsRead = false,
167169
} = props;
168170

169171
const globalStore = useSendbirdStateContext();
@@ -271,7 +273,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
271273
// to create message-datasource
272274
// this hook sets currentGroupChannel asynchronously
273275
useGetChannel(
274-
{ channelUrl, sdkInit },
276+
{ channelUrl, sdkInit, disableMarkAsRead },
275277
{ messagesDispatcher, sdk, logger },
276278
);
277279

@@ -282,7 +284,12 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
282284

283285
// Hook to handle ChannelEvents and send values to useReducer using messagesDispatcher
284286
useHandleChannelEvents(
285-
{ currentGroupChannel, sdkInit, hasMoreNext },
287+
{
288+
currentGroupChannel,
289+
sdkInit,
290+
hasMoreNext,
291+
disableMarkAsRead
292+
},
286293
{
287294
messagesDispatcher,
288295
sdk,
@@ -316,7 +323,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
316323
}, [channelUrl, sdkInit]);
317324

318325
// handling connection breaks
319-
useHandleReconnect({ isOnline, replyType }, {
326+
useHandleReconnect({ isOnline, replyType, disableMarkAsRead }, {
320327
logger,
321328
sdk,
322329
currentGroupChannel,
@@ -368,6 +375,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
368375
onSearchClick,
369376
replyType,
370377
queries,
378+
disableMarkAsRead,
371379

372380
// messagesStore
373381
allMessages,

src/smart-components/Channel/context/hooks/useGetChannel.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect } from 'react';
22

33
import * as messageActionTypes from '../dux/actionTypes';
44

5-
function useSetChannel({ channelUrl, sdkInit }, {
5+
function useSetChannel({ channelUrl, sdkInit, disableMarkAsRead }, {
66
messagesDispatcher,
77
sdk,
88
logger,
@@ -19,11 +19,13 @@ function useSetChannel({ channelUrl, sdkInit }, {
1919
});
2020

2121
logger.info('Channel: Mark as read', groupChannel);
22-
// this order is important - this mark as read should update the event handler up above
23-
try {
24-
groupChannel.markAsRead();
25-
} catch {
26-
//
22+
if (!disableMarkAsRead) {
23+
// this order is important - this mark as read should update the event handler up above
24+
try {
25+
groupChannel.markAsRead();
26+
} catch {
27+
//
28+
}
2729
}
2830
})
2931
.catch((e) => {

src/smart-components/Channel/context/hooks/useHandleChannelEvents.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as messageActions from '../dux/actionTypes';
2020
interface DynamicParams {
2121
sdkInit: boolean;
2222
hasMoreNext: boolean;
23+
disableMarkAsRead: boolean;
2324
currentGroupChannel: GroupChannel;
2425
}
2526
interface StaticParams {
@@ -34,6 +35,7 @@ function useHandleChannelEvents(
3435
{
3536
sdkInit,
3637
hasMoreNext,
38+
disableMarkAsRead,
3739
currentGroupChannel,
3840
}: DynamicParams,
3941
{
@@ -69,7 +71,9 @@ function useHandleChannelEvents(
6971
if (scrollToEnd) {
7072
try {
7173
setTimeout(() => {
72-
currentGroupChannel?.markAsRead?.();
74+
if (!disableMarkAsRead) {
75+
currentGroupChannel?.markAsRead?.();
76+
}
7377
scrollIntoLast();
7478
});
7579
} catch (error) {

src/smart-components/Channel/context/hooks/useHandleReconnect.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { NEXT_RESULT_SIZE } from '../const';
1313
interface DynamicParams {
1414
isOnline: boolean;
1515
replyType?: string;
16+
disableMarkAsRead: boolean;
1617
}
1718

1819
interface StaticParams {
@@ -24,7 +25,7 @@ interface StaticParams {
2425
}
2526

2627
function useHandleReconnect(
27-
{ isOnline, replyType }: DynamicParams,
28+
{ isOnline, replyType, disableMarkAsRead }: DynamicParams,
2829
{
2930
logger,
3031
sdk,
@@ -88,10 +89,12 @@ function useHandleReconnect(
8889
payload: { currentGroupChannel },
8990
});
9091
})
91-
try {
92-
currentGroupChannel?.markAsRead?.();
93-
} catch {
94-
//
92+
if (!disableMarkAsRead) {
93+
try {
94+
currentGroupChannel?.markAsRead?.();
95+
} catch {
96+
//
97+
}
9598
}
9699
});
97100
}

src/smart-components/Channel/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Channel: React.FC<ChannelProps> = (props: ChannelProps) => {
2727
queries={props?.queries}
2828
renderUserProfile={props?.renderUserProfile}
2929
disableUserProfile={props?.disableUserProfile}
30+
disableMarkAsRead={props?.disableMarkAsRead}
3031
>
3132
<ChannelUI
3233
renderPlaceholderLoader={props?.renderPlaceholderLoader}

0 commit comments

Comments
 (0)