Skip to content

Commit

Permalink
feat: respect channel.config.polls flag
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Oct 31, 2024
1 parent ebf1f57 commit 72ccbc2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
11 changes: 8 additions & 3 deletions src/components/MessageInput/AttachmentSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ const useAttachmentSelectorActionsFiltered = <
>(
original: AttachmentSelectorAction[],
) => {
const { channelCapabilities } = useChannelStateContext<StreamChatGenerics>();
const { channelCapabilities, channelConfig } = useChannelStateContext<StreamChatGenerics>();
const { isThreadInput } = useMessageInputContext();

return original.filter((action) => {
if (!channelCapabilities['upload-file'] && action.type === 'uploadFile') return false;
if ((isThreadInput || !channelCapabilities['send-poll']) && action.type === 'createPoll')
if (action.type === 'uploadFile' && !channelCapabilities['upload-file']) return false;
if (
action.type === 'createPoll' &&
(!channelConfig?.polls || isThreadInput || !channelCapabilities['send-poll'])
)
return false;
return true;
});
Expand Down Expand Up @@ -169,6 +172,8 @@ export const AttachmentSelector = <

if (actions.length === 0) return null;

if (actions.length === 1 && actions[0].type === 'uploadFile') return <SimpleAttachmentSelector />;

return (
<AttachmentSelectorContextProvider value={{ fileInput, openPollModal }}>
<div className='str-chat__attachment-selector'>
Expand Down
34 changes: 19 additions & 15 deletions src/components/MessageInput/__tests__/AttachmentSelector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { AttachmentSelector } from '../AttachmentSelector';
const ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID = 'attachment-selector-actions-menu';
const POLL_CREATION_DIALOG_TEST_ID = 'poll-creation-dialog';

const ATTACHMENT_SELECTOR_CLASS = 'str-chat__attachment-selector';
const UPLOAD_FILE_BUTTON_CLASS = 'str-chat__attachment-selector-actions-menu__upload-file-button';
const CREATE_POLL_BUTTON_CLASS = 'str-chat__attachment-selector-actions-menu__create-poll-button';

Expand All @@ -25,6 +26,7 @@ const translationContext = {

const defaultChannelStateContext = {
channelCapabilities: { 'send-poll': true, 'upload-file': true },
channelConfig: { polls: true },
};

const defaultMessageInputProps = {
Expand Down Expand Up @@ -79,29 +81,31 @@ describe('AttachmentSelector', () => {
expect(menu).toHaveTextContent('Poll');
});

it('renders with single upload file button if send-poll permission is not granted', async () => {
await renderComponent({
it('falls back to SimpleAttachmentSelector if channel.config.polls is false', async () => {
const { container } = await renderComponent({
channelStateContext: { channelConfig: { polls: false } },
});
expect(container.querySelector(`.${ATTACHMENT_SELECTOR_CLASS}`)).not.toBeInTheDocument();
expect(screen.getByTestId('file-upload-button')).toBeInTheDocument();
});

it('renders SimpleAttachmentSelector if send-poll permission is not granted', async () => {
const { container } = await renderComponent({
channelStateContext: { channelCapabilities: { 'upload-file': true } },
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
expect(menu).toBeInTheDocument();
expect(menu).toHaveTextContent('File');
expect(menu).not.toHaveTextContent('Poll');
expect(container.querySelector(`.${ATTACHMENT_SELECTOR_CLASS}`)).not.toBeInTheDocument();
expect(screen.getByTestId('file-upload-button')).toBeInTheDocument();
});

it('renders with single upload file button if rendered in a thread', async () => {
await renderComponent({
it('renders SimpleAttachmentSelector if rendered in a thread', async () => {
const { container } = await renderComponent({
messageInputProps: { isThreadInput: true },
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
expect(menu).toBeInTheDocument();
expect(menu).toHaveTextContent('File');
expect(menu).not.toHaveTextContent('Poll');
expect(container.querySelector(`.${ATTACHMENT_SELECTOR_CLASS}`)).not.toBeInTheDocument();
expect(screen.getByTestId('file-upload-button')).toBeInTheDocument();
});

it('renders with single poll button if upload-file permission is not granted', async () => {
it('renders SimpleAttachmentSelector if upload-file permission is not granted', async () => {
await renderComponent({
channelStateContext: { channelCapabilities: { 'send-poll': true } },
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/MessageInput/__tests__/MessageInput.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const threadMessage = generateMessage({
user,
});
const mockedChannelData = generateChannel({
channel: { own_capabilities: ['upload-file'] },
channel: { own_capabilities: ['send-poll', 'upload-file'] },
members: [generateMember({ user }), generateMember({ user: mentionUser })],
messages: [mainListMessage],
thread: [threadMessage],
Expand Down
1 change: 1 addition & 0 deletions src/mock-builders/generator/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const generateChannel = (options = { channel: {} }) => {
message_retention: 'infinite',
mutes: true,
name: 'messaging',
polls: true,
reactions: true,
read_events: true,
replies: true,
Expand Down

0 comments on commit 72ccbc2

Please sign in to comment.