Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(CLNP-2051): replaced useGroupChannelMessages #155

Merged
merged 2 commits into from
Jan 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion docs-validation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@react-native-firebase/messaging": "^14.7.0",
"@react-navigation/native": "^6.0.6",
"@react-navigation/native-stack": "^6.7.0",
"@sendbird/chat": "^4.9.8",
"@sendbird/chat": "^4.10.7",
"react": "17.0.2",
"react-native": "0.67.5",
"react-native-create-thumbnail": "^1.5.1",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
]
},
"resolutions": {
"@sendbird/chat": "4.9.10",
"@sendbird/chat": "4.10.7",
"@types/react": "^18",
"@types/react-native": "^0.67"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Action =
}
| {
type: 'delete_messages' | 'delete_new_messages';
value: { messageIds: number[]; reqIds: string[] };
value: { messageIds: (string | number)[]; reqIds: string[] };
};

type State = {
Expand Down Expand Up @@ -153,7 +153,7 @@ export const useChannelMessagesReducer = (sortComparator?: Options['sortComparat
const updateMessages = (messages: SendbirdBaseMessage[], clearBeforeAction: boolean, currentUserId?: string) => {
dispatch({ type: 'update_messages', value: { messages, clearBeforeAction, currentUserId } });
};
const deleteMessages = (messageIds: number[], reqIds: string[]) => {
const deleteMessages = (messageIds: (string | number)[], reqIds: string[]) => {
dispatch({ type: 'delete_messages', value: { messageIds, reqIds } });
};
const updateNewMessages = (messages: SendbirdBaseMessage[], clearBeforeAction: boolean, currentUserId?: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { UseGroupChannelMessages } from '../../types';
import { useGroupChannelMessagesWithCollection } from './useGroupChannelMessagesWithCollection';
import { useGroupChannelMessagesWithQuery } from './useGroupChannelMessagesWithQuery';

/**
* @deprecated This hook is deprecated and will be replaced by the 'uikit-tools' package.
* */
export const useGroupChannelMessages: UseGroupChannelMessages = (sdk, channel, userId, options) => {
if (sdk.isCacheEnabled || options?.enableCollectionWithoutLocalCache) {
if (options?.queryCreator) printIgnoredWarning();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRef } from 'react';

import type { PreviousMessageListQueryParams } from '@sendbird/chat/message';
import type { BaseMessage, PreviousMessageListQueryParams } from '@sendbird/chat/message';
import type {
SendbirdBaseChannel,
SendbirdGroupChannel,
Expand Down Expand Up @@ -116,7 +116,7 @@ export const useGroupChannelMessagesWithQuery: UseGroupChannelMessages = (sdk, c
channelUrl: channel.url,
channelType: channel.channelType,
});
if (message) updateMessages([message], false, sdk.currentUser?.userId);
if (message) updateMessages([message as BaseMessage], false, sdk.currentUser?.userId);
},
// Channels
onChannelChanged: channelUpdater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const useOpenChannelMessagesWithQuery: UseOpenChannelMessages = (sdk, cha
const messageContext = {
updatedMessages: [] as SendbirdBaseMessage[],
addedMessages: [] as SendbirdBaseMessage[],
deletedMessageIds: [] as number[],
deletedMessageIds: [] as (number | string)[],
};
const changeLogsContext = {
hasMore: false,
Expand Down
2 changes: 2 additions & 0 deletions packages/uikit-chat-hooks/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export type UseGroupChannelListOptions = {
};

/**
* @deprecated This hook is deprecated and will be replaced by the 'uikit-tools' package.
*
* @interface UseGroupChannelMessages
* @description interface for group channel messages hook
* - Receive new messages from other users & should count new messages -> append to state(newMessages)
Expand Down
4 changes: 2 additions & 2 deletions packages/uikit-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"dependencies": {
"@sendbird/uikit-chat-hooks": "3.3.0",
"@sendbird/uikit-react-native-foundation": "3.3.0",
"@sendbird/uikit-tools": "0.0.1-alpha.42",
"@sendbird/uikit-tools": "0.0.1-alpha.57",
"@sendbird/uikit-utils": "3.3.0"
},
"devDependencies": {
Expand Down Expand Up @@ -107,7 +107,7 @@
"@react-native-clipboard/clipboard": ">=1.8.5",
"@react-native-community/netinfo": ">=9.3.0",
"@react-native-firebase/messaging": ">=14.4.0",
"@sendbird/chat": "^4.9.8",
"@sendbird/chat": "^4.10.7",
"@sendbird/react-native-scrollview-enhancer": "*",
"date-fns": ">=2.28.0",
"expo-av": ">=12.0.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type ChannelMessageListProps<T extends SendbirdGroupChannel | SendbirdOpe
onEditMessage: (message: HandleableMessage) => void;
onReplyMessage?: (message: HandleableMessage) => void; // only available on group channel
onDeleteMessage: (message: HandleableMessage) => Promise<void>;
onResendFailedMessage: (failedMessage: HandleableMessage) => Promise<void>;
onResendFailedMessage: (failedMessage: HandleableMessage) => Promise<HandleableMessage | void>;
onPressParentMessage?: (parentMessage: SendbirdMessage) => void;
onPressMediaMessage?: (message: SendbirdFileMessage, deleteMessage: () => Promise<void>, uri: string) => void;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { Pressable, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';

import type { BaseMessage } from '@sendbird/chat/message';
import { useChannelHandler } from '@sendbird/uikit-chat-hooks';
import { Icon, Image, createStyleSheet, useUIKitTheme } from '@sendbird/uikit-react-native-foundation';
import { SendbirdBaseChannel, SendbirdBaseMessage, useUniqHandlerId } from '@sendbird/uikit-utils';
Expand All @@ -24,12 +25,12 @@ const BottomSheetReactionAddon = ({ onClose, message, channel }: Props) => {
useChannelHandler(sdk, handlerId, {
async onReactionUpdated(eventChannel, event) {
if (channel?.url === eventChannel.url && event.messageId === message?.messageId) {
const msg = await sdk.message.getMessage({
const msg = (await sdk.message.getMessage({
includeReactions: true,
messageId: message.messageId,
channelUrl: message.channelUrl,
channelType: message.channelType,
});
})) as null | BaseMessage;
if (msg) updateReactionFocusedItem({ message: msg });
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
lazyScrollToIndex({ index: foundMessageIndex, animated: true, timeout });
} else {
if (props.channel.messageOffsetTimestamp <= createdAt) {
if (focusAnimated) props.onUpdateSearchItem({ startingPoint: createdAt });
if (focusAnimated) {
props.onUpdateSearchItem({ startingPoint: createdAt });
}
props.onResetMessageListWithStartingPoint(createdAt);
} else {
return false;
Expand All @@ -44,15 +46,14 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
},
);

const scrollToBottom = useFreshCallback((animated = false) => {
const scrollToBottom = useFreshCallback(async (animated = false) => {
if (props.hasNext()) {
props.onUpdateSearchItem(undefined);
props.onScrolledAwayFromBottom(false);

props.onResetMessageList(() => {
props.onScrolledAwayFromBottom(false);
lazyScrollToBottom({ animated });
});
await props.onResetMessageList();
props.onScrolledAwayFromBottom(false);
lazyScrollToBottom({ animated });
} else {
lazyScrollToBottom({ animated });
}
Expand Down
4 changes: 2 additions & 2 deletions packages/uikit-react-native/src/domain/groupChannel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export interface GroupChannelProps {
| 'hasNext'
| 'searchItem'
> & {
onResetMessageList: (callback?: () => void) => void;
onResetMessageListWithStartingPoint: (startingPoint: number, callback?: () => void) => void;
onResetMessageList: () => Promise<void>;
onResetMessageListWithStartingPoint: (startingPoint: number) => Promise<void>;

// Changing the search item will trigger the focus animation on messages.
onUpdateSearchItem: (searchItem?: GroupChannelProps['MessageList']['searchItem']) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';

import { ReplyType } from '@sendbird/chat/message';
import { useGroupChannelMessages } from '@sendbird/uikit-chat-hooks';
import { Box } from '@sendbird/uikit-react-native-foundation';
import { useGroupChannelMessages } from '@sendbird/uikit-tools';
import {
NOOP,
PASS,
SendbirdFileMessage,
SendbirdGroupChannel,
SendbirdUserMessage,
confirmAndMarkAsRead,
messageComparator,
useFreshCallback,
useIIFE,
Expand Down Expand Up @@ -75,8 +76,8 @@ const createGroupChannelFragment = (initModule?: Partial<GroupChannelModule>): G
messages,
newMessages,
resetNewMessages,
next,
prev,
loadNext,
loadPrevious,
hasNext,
sendFileMessage,
sendUserMessage,
Expand All @@ -85,20 +86,21 @@ const createGroupChannelFragment = (initModule?: Partial<GroupChannelModule>): G
resendMessage,
deleteMessage,
resetWithStartingPoint,
} = useGroupChannelMessages(sdk, channel, currentUser?.userId, {
} = useGroupChannelMessages(sdk, channel, {
shouldCountNewMessages: () => scrolledAwayFromBottomRef.current,
onMessagesReceived(messages) {
groupChannelPubSub.publish({ type: 'MESSAGES_RECEIVED', data: { messages } });
},
onMessagesUpdated(messages) {
groupChannelPubSub.publish({ type: 'MESSAGES_UPDATED', data: { messages } });
},
onChannelDeleted,
onCurrentUserBanned: onChannelDeleted,
collectionCreator,
sortComparator,
onChannelDeleted,
markAsRead: confirmAndMarkAsRead,
replyType,
startingPoint: internalSearchItem?.startingPoint,
enableCollectionWithoutLocalCache: true,
});

const onBlurFragment = () => {
Expand Down Expand Up @@ -144,12 +146,12 @@ const createGroupChannelFragment = (initModule?: Partial<GroupChannelModule>): G
[flatListProps],
);

const onResetMessageList = useCallback((callback?: () => void) => {
resetWithStartingPoint(Number.MAX_SAFE_INTEGER, callback);
const onResetMessageList = useCallback(async () => {
return await resetWithStartingPoint(Number.MAX_SAFE_INTEGER);
}, []);

const onResetMessageListWithStartingPoint = useCallback((startingPoint: number, callback?: () => void) => {
resetWithStartingPoint(startingPoint, callback);
const onResetMessageListWithStartingPoint = useCallback(async (startingPoint: number) => {
return await resetWithStartingPoint(startingPoint);
}, []);

// Changing the search item will trigger the focus animation on messages.
Expand Down Expand Up @@ -224,8 +226,8 @@ const createGroupChannelFragment = (initModule?: Partial<GroupChannelModule>): G
renderMessage={renderItem}
messages={messages}
newMessages={newMessages}
onTopReached={prev}
onBottomReached={next}
onTopReached={loadPrevious}
onBottomReached={loadNext}
hasNext={hasNext}
scrolledAwayFromBottom={scrolledAwayFromBottom}
onScrolledAwayFromBottom={onScrolledAwayFromBottom}
Expand Down
2 changes: 2 additions & 0 deletions packages/uikit-testing-tools/src/mocks/createMockChannel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import {
BannedUserListQuery,
ChannelType,
Expand Down Expand Up @@ -125,6 +126,7 @@ class MockChannel implements GetMockProps<Params, SendbirdBaseChannel & Sendbird
isOperator(): boolean {
throw new Error('Method not implemented.');
}
// @ts-ignore
refresh = jest.fn(async (): Promise<this> => {
this.params.sdk?.__throwIfFailureTest();
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { ChannelType } from '@sendbird/chat';
import {
GroupChannel,
Expand Down
28 changes: 27 additions & 1 deletion packages/uikit-testing-tools/src/mocks/createMockMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @ts-nocheck
import { ChannelType } from '@sendbird/chat';
import { NotificationData } from '@sendbird/chat/feedChannel';
import { MessageType, SendingStatus } from '@sendbird/chat/message';
import { Form } from '@sendbird/chat/lib/__definition';
import { AdminMessage, Feedback, FeedbackStatus, MessageType, SendingStatus } from '@sendbird/chat/message';
import type {
SendbirdAdminMessage,
SendbirdBaseMessage,
Expand Down Expand Up @@ -67,6 +69,10 @@ class MockMessage implements GetMockProps<Params, SendbirdBaseMessage> {
scheduledInfo = null;
extendedMessage = {};
notificationData: NotificationData | null = null;
forms: Form[] | null = null;
myFeedback: Feedback | null = null;
myFeedbackStatus: FeedbackStatus = 'NO_FEEDBACK';
suggestedReplies: string[] | null = null;

isFileMessage(): this is SendbirdFileMessage {
return this.messageType === MessageType.FILE && !Object.prototype.hasOwnProperty.call(this, 'fileInfoList');
Expand Down Expand Up @@ -109,6 +115,26 @@ class MockMessage implements GetMockProps<Params, SendbirdBaseMessage> {
return Object.assign({}, this);
}

deleteFeedback(_: number): Promise<void> {
return Promise.resolve(undefined);
}

hasForm(): this is AdminMessage {
return this.forms !== null;
}

submitFeedback(_: Pick<Feedback, 'rating' | 'comment'>): Promise<void> {
return Promise.resolve(undefined);
}

submitForm(_: { formId?: string; answers?: Record<string, string> }): Promise<void> {
return Promise.resolve(undefined);
}

updateFeedback(_: Feedback): Promise<void> {
return Promise.resolve(undefined);
}

asFileMessage(): SendbirdFileMessage {
return this as unknown as SendbirdFileMessage;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import {
MessageCollectionEventHandler,
MessageCollectionInitHandler,
Expand All @@ -6,7 +7,7 @@ import {
MessageCollectionParams,
MessageFilter,
} from '@sendbird/chat/groupChannel';
import { SendingStatus } from '@sendbird/chat/message';
import { BaseMessage, SendingStatus } from '@sendbird/chat/message';
import type {
SendbirdBaseMessage,
SendbirdGroupChannel,
Expand Down Expand Up @@ -57,7 +58,7 @@ class MockMessageCollection implements GetMockProps<Params, Omit<SendbirdMessage
});

initialize = jest.fn((_policy: MessageCollectionInitPolicy) => {
const initHandler: MessageCollectionInitHandler = {
const initHandler: MessageCollectionInitHandler<BaseMessage> = {
onCacheResult: jest.fn((handler: MessageCollectionInitResultHandler) => {
this.__cacheInitHandler = handler;
return initHandler;
Expand Down
1 change: 1 addition & 0 deletions packages/uikit-testing-tools/src/mocks/createMockQuery.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { ChannelType } from '@sendbird/chat';

import type { GetMockParams } from '../types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import {
ApplicationUserListQueryParams,
ChannelType,
Expand Down
1 change: 1 addition & 0 deletions packages/uikit-testing-tools/src/mocks/createMockUser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { UserOnlineState } from '@sendbird/chat';
import type { Sender } from '@sendbird/chat/message';
import type {
Expand Down
2 changes: 1 addition & 1 deletion packages/uikit-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"typescript": "5.2.2"
},
"peerDependencies": {
"@sendbird/chat": "^4.9.8",
"@sendbird/chat": "^4.10.7",
"react": ">=17.0.2",
"react-native": ">=0.65.0"
},
Expand Down
2 changes: 1 addition & 1 deletion sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@react-navigation/bottom-tabs": "^6.2.0",
"@react-navigation/native": "^6.0.6",
"@react-navigation/native-stack": "^6.7.0",
"@sendbird/chat": "^4.9.8",
"@sendbird/chat": "^4.10.7",
"@sendbird/react-native-scrollview-enhancer": "^0.2.1",
"@storybook/addon-actions": "^6.4.19",
"@storybook/addon-controls": "^6.4.19",
Expand Down
Loading