Skip to content

Commit

Permalink
feat: Add disableMarkAsRead to the Channel comp (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
HoonBaek authored Oct 14, 2022
1 parent 319d263 commit 755c846
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 25 deletions.
1 change: 1 addition & 0 deletions scripts/index_d_ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ declare module "SendbirdUIKitGlobal" {
queries?: ChannelQueries;
renderUserProfile?: (props: RenderUserProfileProps) => React.ReactNode | React.ReactElement;
disableUserProfile?: boolean;
disableMarkAsRead?: boolean;
};

export interface ChannelUIProps {
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ type ChannelContextProps = {
queries?: ChannelQueries;
renderUserProfile?: (props: RenderUserProfileProps) => React.ReactElement;
disableUserProfile?: boolean;
disableMarkAsRead?: boolean;
};

interface ChannelUIProps {
Expand Down
19 changes: 11 additions & 8 deletions src/smart-components/Channel/components/ChannelUI/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const ChannelUI: React.FC<ChannelUIProps> = ({
setHighLightedMessageId,
scrollRef,
messagesDispatcher,
disableMarkAsRead,
} = useChannelContext();
const [unreadCount, setUnreadCount] = useState(0);
useEffect(() => {
Expand Down Expand Up @@ -117,15 +118,17 @@ const ChannelUI: React.FC<ChannelUIProps> = ({
if (scrollRef?.current?.scrollTop) {
scrollRef.current.scrollTop = scrollRef?.current?.scrollHeight - scrollRef?.current?.offsetHeight;
}
try {
currentGroupChannel?.markAsRead();
} catch {
//
if (!disableMarkAsRead) {
try {
currentGroupChannel?.markAsRead();
} catch {
//
}
messagesDispatcher({
type: messageActionTypes.MARK_AS_READ,
payload: { channel: currentGroupChannel },
});
}
messagesDispatcher({
type: messageActionTypes.MARK_AS_READ,
payload: { channel: currentGroupChannel },
});
setInitialTimeStamp(null);
setAnimatedMessageId(null);
setHighLightedMessageId(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const MessageList: React.FC<MessageListProps> = (props: MessageListProps) => {
messagesDispatcher,
messageActionTypes,
currentGroupChannel,
disableMarkAsRead,
} = useChannelContext();
const [scrollBottom, setScrollBottom] = useState(0);

Expand Down Expand Up @@ -90,7 +91,7 @@ const MessageList: React.FC<MessageListProps> = (props: MessageListProps) => {
// do this later
setTimeout(() => {
// mark as read if scroll is at end
if (clientHeight + scrollTop === scrollHeight) {
if (!disableMarkAsRead && clientHeight + scrollTop === scrollHeight) {
messagesDispatcher({
type: messageActionTypes.MARK_AS_READ,
payload: { channel: currentGroupChannel },
Expand Down
16 changes: 12 additions & 4 deletions src/smart-components/Channel/context/ChannelProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export type ChannelContextProps = {
queries?: ChannelQueries;
renderUserProfile?: (props: RenderUserProfileProps) => React.ReactElement;
disableUserProfile?: boolean;
disableMarkAsRead?: boolean;
};

interface MessageStoreInterface {
Expand Down Expand Up @@ -117,7 +118,7 @@ interface UpdateMessageProps {

interface ChannelProviderInterface extends ChannelContextProps, MessageStoreInterface {
scrollToMessage?(createdAt: number, messageId: number): void;
messageActionTypes: Record<string ,string>;
messageActionTypes: Record<string, string>;
messagesDispatcher: CustomUseReducerDispatcher;
quoteMessage: UserMessage | FileMessage;
setQuoteMessage: React.Dispatch<React.SetStateAction<UserMessage | FileMessage>>;
Expand Down Expand Up @@ -164,6 +165,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
onSearchClick,
replyType,
queries,
disableMarkAsRead = false,
} = props;

const globalStore = useSendbirdStateContext();
Expand Down Expand Up @@ -271,7 +273,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
// to create message-datasource
// this hook sets currentGroupChannel asynchronously
useGetChannel(
{ channelUrl, sdkInit },
{ channelUrl, sdkInit, disableMarkAsRead },
{ messagesDispatcher, sdk, logger },
);

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

// Hook to handle ChannelEvents and send values to useReducer using messagesDispatcher
useHandleChannelEvents(
{ currentGroupChannel, sdkInit, hasMoreNext },
{
currentGroupChannel,
sdkInit,
hasMoreNext,
disableMarkAsRead
},
{
messagesDispatcher,
sdk,
Expand Down Expand Up @@ -316,7 +323,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
}, [channelUrl, sdkInit]);

// handling connection breaks
useHandleReconnect({ isOnline, replyType }, {
useHandleReconnect({ isOnline, replyType, disableMarkAsRead }, {
logger,
sdk,
currentGroupChannel,
Expand Down Expand Up @@ -368,6 +375,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
onSearchClick,
replyType,
queries,
disableMarkAsRead,

// messagesStore
allMessages,
Expand Down
14 changes: 8 additions & 6 deletions src/smart-components/Channel/context/hooks/useGetChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect } from 'react';

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

function useSetChannel({ channelUrl, sdkInit }, {
function useSetChannel({ channelUrl, sdkInit, disableMarkAsRead }, {
messagesDispatcher,
sdk,
logger,
Expand All @@ -19,11 +19,13 @@ function useSetChannel({ channelUrl, sdkInit }, {
});

logger.info('Channel: Mark as read', groupChannel);
// this order is important - this mark as read should update the event handler up above
try {
groupChannel.markAsRead();
} catch {
//
if (!disableMarkAsRead) {
// this order is important - this mark as read should update the event handler up above
try {
groupChannel.markAsRead();
} catch {
//
}
}
})
.catch((e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as messageActions from '../dux/actionTypes';
interface DynamicParams {
sdkInit: boolean;
hasMoreNext: boolean;
disableMarkAsRead: boolean;
currentGroupChannel: GroupChannel;
}
interface StaticParams {
Expand All @@ -34,6 +35,7 @@ function useHandleChannelEvents(
{
sdkInit,
hasMoreNext,
disableMarkAsRead,
currentGroupChannel,
}: DynamicParams,
{
Expand Down Expand Up @@ -69,7 +71,9 @@ function useHandleChannelEvents(
if (scrollToEnd) {
try {
setTimeout(() => {
currentGroupChannel?.markAsRead?.();
if (!disableMarkAsRead) {
currentGroupChannel?.markAsRead?.();
}
scrollIntoLast();
});
} catch (error) {
Expand Down
13 changes: 8 additions & 5 deletions src/smart-components/Channel/context/hooks/useHandleReconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { NEXT_RESULT_SIZE } from '../const';
interface DynamicParams {
isOnline: boolean;
replyType?: string;
disableMarkAsRead: boolean;
}

interface StaticParams {
Expand All @@ -24,7 +25,7 @@ interface StaticParams {
}

function useHandleReconnect(
{ isOnline, replyType }: DynamicParams,
{ isOnline, replyType, disableMarkAsRead }: DynamicParams,
{
logger,
sdk,
Expand Down Expand Up @@ -88,10 +89,12 @@ function useHandleReconnect(
payload: { currentGroupChannel },
});
})
try {
currentGroupChannel?.markAsRead?.();
} catch {
//
if (!disableMarkAsRead) {
try {
currentGroupChannel?.markAsRead?.();
} catch {
//
}
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/smart-components/Channel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Channel: React.FC<ChannelProps> = (props: ChannelProps) => {
queries={props?.queries}
renderUserProfile={props?.renderUserProfile}
disableUserProfile={props?.disableUserProfile}
disableMarkAsRead={props?.disableMarkAsRead}
>
<ChannelUI
renderPlaceholderLoader={props?.renderPlaceholderLoader}
Expand Down

0 comments on commit 755c846

Please sign in to comment.