Skip to content

Commit 79bfe84

Browse files
authored
[CLNP-6260] forwardRef hook rule violation (#1308)
// PR title (Required) [fix]: forwardRef hook rule violation bug-fix // Footer (Recommended) Fixes [CLNP-6260](https://sendbird.atlassian.net/browse/CLNP-6260) ### Changelogs - Fixed a bug with forwardRef Rules of Hooks violation ### Checklist Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If unsure, ask the members. This is a reminder of what we look for before merging your code. - [x] **All tests pass locally with my changes** - [ ] **I have added tests that prove my fix is effective or that my feature works** - [ ] **Public components / utils / props are appropriately exported** - [ ] I have added necessary documentation (if appropriate) ## External Contributions This project is not yet set up to accept pull requests from external contributors. If you have a pull request that you believe should be accepted, please contact the Developer Relations team <[email protected]> with details and we'll evaluate if we can set up a [CLA](https://en.wikipedia.org/wiki/Contributor_License_Agreement) to allow for the contribution. [CLNP-6260]: https://sendbird.atlassian.net/browse/CLNP-6260?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent f30922e commit 79bfe84

File tree

7 files changed

+35
-11
lines changed

7 files changed

+35
-11
lines changed

src/modules/Channel/components/MessageList/index.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { GroupChannelMessageListProps } from '../../../GroupChannel/components/M
2626
import { GroupChannelUIBasicProps } from '../../../GroupChannel/components/GroupChannelUI/GroupChannelUIView';
2727
import { deleteNullish } from '../../../../utils/utils';
2828
import { getHTMLTextDirection } from '../../../../utils';
29+
import { useLocalization } from '../../../../lib/LocalizationContext';
2930

3031
const SCROLL_BOTTOM_PADDING = 50;
3132

@@ -80,6 +81,7 @@ export const MessageList = (props: MessageListProps) => {
8081
} = useChannelContext();
8182

8283
const store = useSendbirdStateContext();
84+
const { stringSet } = useLocalization();
8385
const allMessagesFiltered = typeof filterMessageList === 'function' ? allMessages.filter(filterMessageList) : allMessages;
8486
const markAsReadScheduler = store.config.markAsReadScheduler;
8587

@@ -205,6 +207,7 @@ export const MessageList = (props: MessageListProps) => {
205207
{allMessagesFiltered.map((m, idx) => {
206208
const { chainTop, chainBottom, hasSeparator } = getMessagePartsInfo({
207209
allMessages: allMessagesFiltered,
210+
stringSet,
208211
replyType,
209212
isMessageGroupingEnabled,
210213
currentIndex: idx,
@@ -232,6 +235,7 @@ export const MessageList = (props: MessageListProps) => {
232235
{localMessages.map((m, idx) => {
233236
const { chainTop, chainBottom } = getMessagePartsInfo({
234237
allMessages: allMessagesFiltered,
238+
stringSet,
235239
replyType,
236240
isMessageGroupingEnabled,
237241
currentIndex: idx,

src/modules/GroupChannel/components/MessageList/__test__/getMessagePartsInfo.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ describe('getMessagePartsInfo', () => {
5555
});
5656
});
5757
it('should group messages that are sent at same time', () => {
58+
const { stringSet } = useLocalization();
5859
const defaultSetting = {
5960
allMessages: messageGroup1,
61+
stringSet,
6062
isMessageGroupingEnabled: true,
6163
currentChannel: mockChannel,
6264
replyType: 'THREAD',
@@ -88,8 +90,10 @@ describe('getMessagePartsInfo', () => {
8890
});
8991

9092
it('should not group messages if isMessageGroupingEnabled is false', () => {
93+
const { stringSet } = useLocalization();
9194
const defaultSetting = {
9295
allMessages: messageGroup1,
96+
stringSet,
9397
isMessageGroupingEnabled: false,
9498
currentChannel: mockChannel,
9599
replyType: 'THREAD',
@@ -121,8 +125,10 @@ describe('getMessagePartsInfo', () => {
121125
});
122126

123127
it('should not group messages if sent time are different', () => {
128+
const { stringSet } = useLocalization();
124129
const defaultSetting = {
125130
allMessages: messageGroup2,
131+
stringSet,
126132
isMessageGroupingEnabled: true,
127133
currentChannel: mockChannel,
128134
replyType: 'THREAD',
@@ -153,8 +159,10 @@ describe('getMessagePartsInfo', () => {
153159
expect(thirdGroupingInfo.hasSeparator).toBe(false);
154160
});
155161
it('should not group messages if sender is different', () => {
162+
const { stringSet } = useLocalization();
156163
const defaultSetting = {
157164
allMessages: messageGroup3,
165+
stringSet,
158166
isMessageGroupingEnabled: true,
159167
currentChannel: mockChannel,
160168
replyType: 'THREAD',

src/modules/GroupChannel/components/MessageList/getMessagePartsInfo.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import isSameDay from 'date-fns/isSameDay';
44
import { compareMessagesForGrouping } from '../../../../utils/messages';
55
import { ReplyType } from '../../../../types';
66
import { CoreMessageType } from '../../../../utils';
7+
import { StringSet } from '../../../../ui/Label/stringSet';
78

89
export interface GetMessagePartsInfoProps {
910
allMessages: Array<CoreMessageType>;
11+
stringSet: StringSet
1012
isMessageGroupingEnabled?: boolean;
1113
currentIndex: number;
1214
currentMessage: CoreMessageType;
@@ -24,7 +26,8 @@ interface OutPuts {
2426
* exported, should be backward compatible
2527
*/
2628
export const getMessagePartsInfo = ({
27-
allMessages = [],
29+
allMessages,
30+
stringSet,
2831
isMessageGroupingEnabled = true,
2932
currentIndex = 0,
3033
currentMessage,
@@ -34,7 +37,7 @@ export const getMessagePartsInfo = ({
3437
const previousMessage = allMessages[currentIndex - 1];
3538
const nextMessage = allMessages[currentIndex + 1];
3639
const [chainTop, chainBottom] = isMessageGroupingEnabled
37-
? compareMessagesForGrouping(previousMessage, currentMessage, nextMessage, currentChannel, (replyType as ReplyType))
40+
? compareMessagesForGrouping(previousMessage, currentMessage, nextMessage, stringSet, currentChannel, (replyType as ReplyType))
3841
: [false, false];
3942
const previousMessageCreatedAt = previousMessage?.createdAt;
4043
const currentCreatedAt = currentMessage.createdAt;

src/modules/GroupChannel/components/MessageList/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { getMessagePartsInfo } from './getMessagePartsInfo';
2121
import { MessageProvider } from '../../../Message/context/MessageProvider';
2222
import { getComponentKeyFromMessage, isContextMenuClosed } from '../../context/utils';
2323
import { InfiniteList } from './InfiniteList';
24+
import { useLocalization } from '../../../../lib/LocalizationContext';
2425

2526
export interface GroupChannelMessageListProps {
2627
className?: string;
@@ -88,6 +89,7 @@ export const MessageList = (props: GroupChannelMessageListProps) => {
8889
} = useGroupChannelContext();
8990

9091
const store = useSendbirdStateContext();
92+
const { stringSet } = useLocalization();
9193

9294
const [unreadSinceDate, setUnreadSinceDate] = useState<Date>();
9395

@@ -185,6 +187,7 @@ export const MessageList = (props: GroupChannelMessageListProps) => {
185187
renderMessage={({ message, index }) => {
186188
const { chainTop, chainBottom, hasSeparator } = getMessagePartsInfo({
187189
allMessages: messages as CoreMessageType[],
190+
stringSet,
188191
replyType: replyType ?? 'NONE',
189192
isMessageGroupingEnabled: isMessageGroupingEnabled ?? false,
190193
currentIndex: index,

src/modules/OpenChannel/components/OpenChannelMessageList/index.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { MessageProvider } from '../../../Message/context/MessageProvider';
1414
import useSendbirdStateContext from '../../../../hooks/useSendbirdStateContext';
1515
import { useHandleOnScrollCallback } from '../../../../hooks/useHandleOnScrollCallback';
1616
import { compareMessagesForGrouping } from '../../../../utils/messages';
17+
import { useLocalization } from '../../../../lib/LocalizationContext';
1718

1819
export type OpenChannelMessageListProps = {
1920
renderMessage?: (props: RenderMessageProps) => React.ReactElement;
@@ -31,6 +32,7 @@ function OpenChannelMessageList(props: OpenChannelMessageListProps, ref: React.F
3132
onScroll,
3233
} = useOpenChannelContext();
3334
const store = useSendbirdStateContext();
35+
const { stringSet } = useLocalization();
3436
const userId = store.config.userId;
3537
const localRef = useRef<HTMLDivElement>(null);
3638
const scrollRef = ref || localRef;
@@ -65,7 +67,7 @@ function OpenChannelMessageList(props: OpenChannelMessageListProps, ref: React.F
6567
));
6668

6769
const [chainTop, chainBottom] = isMessageGroupingEnabled
68-
? compareMessagesForGrouping(previousMessage, message, nextMessage)
70+
? compareMessagesForGrouping(previousMessage, message, nextMessage, stringSet)
6971
: [false, false];
7072
const isByMe = (message as UserMessage)?.sender?.userId === userId;
7173
const key = message?.messageId || (message as UserMessage)?.reqId;

src/modules/Thread/components/ThreadList/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import useSendbirdStateContext from '../../../../hooks/useSendbirdStateContext';
1111
import { isSameDay } from 'date-fns';
1212
import { MessageProvider } from '../../../Message/context/MessageProvider';
1313
import { getCaseResolvedReplyType } from '../../../../lib/utils/resolvedReplyType';
14+
import { useLocalization } from '../../../../lib/LocalizationContext';
1415

1516
export interface ThreadListProps {
1617
className?: string;
@@ -28,6 +29,7 @@ export default function ThreadList({
2829
scrollBottom,
2930
}: ThreadListProps): React.ReactElement {
3031
const { config } = useSendbirdStateContext();
32+
const { stringSet } = useLocalization();
3133
const { userId } = config;
3234
const {
3335
currentChannel,
@@ -47,6 +49,7 @@ export default function ThreadList({
4749
prevMessage as SendableMessageType,
4850
message as SendableMessageType,
4951
nextMessage as SendableMessageType,
52+
stringSet,
5053
currentChannel,
5154
getCaseResolvedReplyType(config.groupChannel.replyType).upperCase,
5255
)

src/utils/messages.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import format from 'date-fns/format';
55
import { ReplyType } from '../types';
66
import type { CoreMessageType } from '.';
77
import { isReadMessage } from '.';
8-
import { useLocalization } from '../lib/LocalizationContext';
8+
import { StringSet } from '../ui/Label/stringSet';
99

1010
/**
1111
* exported, should be backward compatible
@@ -15,13 +15,14 @@ export const compareMessagesForGrouping = (
1515
prevMessage: CoreMessageType,
1616
currMessage: CoreMessageType,
1717
nextMessage: CoreMessageType,
18+
stringSet: StringSet,
1819
currentChannel?: GroupChannel | null,
1920
replyType?: ReplyType,
2021
) => {
2122
if (!currentChannel || (currentChannel as GroupChannel).channelType !== 'group') {
2223
return [
23-
isSameGroup(prevMessage, currMessage),
24-
isSameGroup(currMessage, nextMessage),
24+
isSameGroup(prevMessage, currMessage, stringSet),
25+
isSameGroup(currMessage, nextMessage, stringSet),
2526
];
2627
}
2728

@@ -31,19 +32,19 @@ export const compareMessagesForGrouping = (
3132
const sendingStatus = (currMessage as UserMessage)?.sendingStatus || '';
3233
const isAcceptable = sendingStatus !== 'pending' && sendingStatus !== 'failed';
3334
return [
34-
isSameGroup(prevMessage, currMessage, currentChannel) && isAcceptable,
35-
isSameGroup(currMessage, nextMessage, currentChannel) && isAcceptable,
35+
isSameGroup(prevMessage, currMessage, stringSet, currentChannel) && isAcceptable,
36+
isSameGroup(currMessage, nextMessage, stringSet, currentChannel) && isAcceptable,
3637
];
3738
};
3839

39-
export const getMessageCreatedAt = (message: BaseMessage) => {
40-
const { stringSet } = useLocalization();
40+
export const getMessageCreatedAt = (message: BaseMessage, stringSet: StringSet) => {
4141
return format(message.createdAt, stringSet.DATE_FORMAT__MESSAGE_CREATED_AT);
4242
};
4343

4444
export const isSameGroup = (
4545
message: CoreMessageType,
4646
comparingMessage: CoreMessageType,
47+
stringSet: StringSet,
4748
currentChannel?: GroupChannel,
4849
) => {
4950
if (
@@ -67,7 +68,7 @@ export const isSameGroup = (
6768
return (
6869
message?.sendingStatus === comparingMessage?.sendingStatus
6970
&& message?.sender?.userId === comparingMessage?.sender?.userId
70-
&& getMessageCreatedAt(message) === getMessageCreatedAt(comparingMessage)
71+
&& getMessageCreatedAt(message, stringSet) === getMessageCreatedAt(comparingMessage, stringSet)
7172
) && (
7273
currentChannel ? isReadMessage(currentChannel, message) === isReadMessage(currentChannel, comparingMessage) : true
7374
);

0 commit comments

Comments
 (0)