Skip to content

Commit 82ec7d9

Browse files
authored
chore: remove disable if frozen channel (#2841)
* chore: write tests for disallowed sending messages (#2839) * chore: write tests for disallowed sending messages * fix: make test name more concise * chore: remove redundant assertion * chore: add test for the editing state as well * fix: only disable message input ui when capabilities change (#2836) * chore: remove disableIfFrozenChannel prop * fix: lint issues
1 parent 875c6a4 commit 82ec7d9

File tree

3 files changed

+120
-16
lines changed

3 files changed

+120
-16
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { PropsWithChildren, useCallback, useEffect, useMemo, useRef, useState } from 'react';
1+
import React, { PropsWithChildren, useCallback, useEffect, useRef, useState } from 'react';
22
import { KeyboardAvoidingViewProps, StyleSheet, Text, View } from 'react-native';
33

44
import debounce from 'lodash/debounce';
@@ -371,10 +371,6 @@ export type ChannelPropsWithContext<
371371
* Additional props passed to keyboard avoiding view
372372
*/
373373
additionalKeyboardAvoidingViewProps?: Partial<KeyboardAvoidingViewProps>;
374-
/**
375-
* Disables the channel UI if the channel is frozen
376-
*/
377-
disableIfFrozenChannel?: boolean;
378374
/**
379375
* When true, disables the KeyboardCompatibleView wrapper
380376
*
@@ -503,7 +499,6 @@ const ChannelWithContext = <
503499
CreatePollContent,
504500
DateHeader = DateHeaderDefault,
505501
deletedMessagesVisibilityType = 'always',
506-
disableIfFrozenChannel = true,
507502
disableKeyboardCompatibleView = false,
508503
disableTypingIndicator,
509504
dismissKeyboardOnMessageTouch = true,
@@ -1596,19 +1591,14 @@ const ChannelWithContext = <
15961591
}
15971592
};
15981593

1599-
const disabledValue = useMemo(
1600-
() => !!channel?.data?.frozen && disableIfFrozenChannel,
1601-
[channel.data?.frozen, disableIfFrozenChannel],
1602-
);
1603-
16041594
const ownCapabilitiesContext = useCreateOwnCapabilitiesContext({
16051595
channel,
16061596
overrideCapabilities: overrideOwnCapabilities,
16071597
});
16081598

16091599
const channelContext = useCreateChannelContext({
16101600
channel,
1611-
disabled: disabledValue,
1601+
disabled: !!channel?.data?.frozen,
16121602
EmptyStateIndicator,
16131603
enableMessageGroupingByUser,
16141604
enforceUniqueReaction,

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ export const MessageInput = <
11021102
const { isOnline } = useChatContext();
11031103
const ownCapabilities = useOwnCapabilitiesContext();
11041104

1105-
const { disabled, members, threadList, watchers } = useChannelContext<StreamChatGenerics>();
1105+
const { members, threadList, watchers } = useChannelContext<StreamChatGenerics>();
11061106

11071107
const {
11081108
additionalTextInputProps,
@@ -1181,7 +1181,7 @@ export const MessageInput = <
11811181
* Disable the message input if the channel is frozen, or the user doesn't have the capability to send a message.
11821182
* Enable it in frozen mode, if it the input has editing state.
11831183
*/
1184-
if ((disabled || !ownCapabilities.sendMessage) && !editing && SendMessageDisallowedIndicator) {
1184+
if (!ownCapabilities.sendMessage && !editing && SendMessageDisallowedIndicator) {
11851185
return <SendMessageDisallowedIndicator />;
11861186
}
11871187

package/src/components/MessageInput/__tests__/MessageInput.test.js

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22

33
import { Alert } from 'react-native';
44

5-
import { cleanup, fireEvent, render, userEvent, waitFor } from '@testing-library/react-native';
5+
import { act, cleanup, fireEvent, render, userEvent, waitFor } from '@testing-library/react-native';
66

7+
import { useMessagesContext } from '../../../contexts';
78
import * as AttachmentPickerUtils from '../../../contexts/attachmentPickerContext/AttachmentPickerContext';
89
import { OverlayProvider } from '../../../contexts/overlayContext/OverlayProvider';
910
import { getOrCreateChannelApi } from '../../../mock-builders/api/getOrCreateChannel';
@@ -188,4 +189,117 @@ describe('MessageInput', () => {
188189
expect(Alert.alert).toHaveBeenCalledWith('Hold to start recording.');
189190
});
190191
});
192+
193+
it('should render the SendMessageDisallowedIndicator if the send-message capability is not present', async () => {
194+
await initializeChannel(generateChannelResponse());
195+
196+
const { queryByTestId } = render(
197+
<Chat client={chatClient}>
198+
<Channel audioRecordingEnabled channel={channel}>
199+
<MessageInput />
200+
</Channel>
201+
</Chat>,
202+
);
203+
204+
await waitFor(() => {
205+
expect(queryByTestId('send-message-disallowed-indicator')).toBeNull();
206+
});
207+
208+
act(() => {
209+
chatClient.dispatchEvent({
210+
cid: channel.data.cid,
211+
own_capabilities: channel.data.own_capabilities.filter(
212+
(capability) => capability !== 'send-message',
213+
),
214+
type: 'capabilities.changed',
215+
});
216+
});
217+
218+
await waitFor(() => {
219+
expect(queryByTestId('send-message-disallowed-indicator')).toBeTruthy();
220+
});
221+
});
222+
223+
it('should not render the SendMessageDisallowedIndicator if the channel is frozen and the send-message capability is present', async () => {
224+
await initializeChannel(generateChannelResponse({ channel: { frozen: true } }));
225+
226+
const { queryByTestId } = render(
227+
<Chat client={chatClient}>
228+
<Channel audioRecordingEnabled channel={channel}>
229+
<MessageInput />
230+
</Channel>
231+
</Chat>,
232+
);
233+
234+
await waitFor(() => {
235+
expect(queryByTestId('send-message-disallowed-indicator')).toBeNull();
236+
});
237+
});
238+
239+
it('should render the SendMessageDisallowedIndicator in a frozen channel only if the send-message capability is not present', async () => {
240+
await initializeChannel(generateChannelResponse({ channel: { frozen: true } }));
241+
242+
const { queryByTestId } = render(
243+
<Chat client={chatClient}>
244+
<Channel audioRecordingEnabled channel={channel}>
245+
<MessageInput />
246+
</Channel>
247+
</Chat>,
248+
);
249+
250+
act(() => {
251+
chatClient.dispatchEvent({
252+
channel: {
253+
...channel.data,
254+
own_capabilities: channel.data.own_capabilities.filter(
255+
(capability) => capability !== 'send-message',
256+
),
257+
},
258+
cid: channel.data.cid,
259+
type: 'channel.updated',
260+
});
261+
});
262+
263+
await waitFor(() => {
264+
expect(queryByTestId('send-message-disallowed-indicator')).toBeTruthy();
265+
});
266+
});
267+
268+
const EditingStateMessageInput = () => {
269+
const { setEditingState } = useMessagesContext();
270+
useEffect(() => {
271+
setEditingState({ id: 'some-message-id' });
272+
}, []);
273+
return <MessageInput />;
274+
};
275+
276+
it('should not render the SendMessageDisallowedIndicator if we are editing a message, regardless of capabilities', async () => {
277+
await initializeChannel(generateChannelResponse());
278+
279+
const { queryByTestId } = render(
280+
<Chat client={chatClient}>
281+
<Channel audioRecordingEnabled channel={channel}>
282+
<EditingStateMessageInput />
283+
</Channel>
284+
</Chat>,
285+
);
286+
287+
await waitFor(() => {
288+
expect(queryByTestId('send-message-disallowed-indicator')).toBeNull();
289+
});
290+
291+
act(() => {
292+
chatClient.dispatchEvent({
293+
cid: channel.data.cid,
294+
own_capabilities: channel.data.own_capabilities.filter(
295+
(capability) => capability !== 'send-message',
296+
),
297+
type: 'capabilities.changed',
298+
});
299+
});
300+
301+
await waitFor(() => {
302+
expect(queryByTestId('send-message-disallowed-indicator')).toBeNull();
303+
});
304+
});
191305
});

0 commit comments

Comments
 (0)