Skip to content

Commit

Permalink
fix: move render condition to MessageActions directly
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Sep 4, 2024
1 parent 04cfbca commit aa0ccb3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
18 changes: 3 additions & 15 deletions src/components/Message/MessageOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
ReactionIcon as DefaultReactionIcon,
ThreadIcon as DefaultThreadIcon,
} from './icons';
import { MESSAGE_ACTIONS, shouldRenderMessageActions } from './utils';
import { MESSAGE_ACTIONS } from './utils';

import { MessageActions } from '../MessageActions';

import { MessageContextValue, useMessageContext } from '../../context/MessageContext';

import type { DefaultStreamChatGenerics, IconProps } from '../../types/types';
import { useComponentContext, useTranslationContext } from '../../context';
import { useTranslationContext } from '../../context';

export type MessageOptionsProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
Expand Down Expand Up @@ -47,7 +47,6 @@ const UnMemoizedMessageOptions = <
} = props;

const {
customMessageActions,
getMessageActions,
handleOpenThread: contextHandleOpenThread,
initialMessage,
Expand All @@ -57,20 +56,11 @@ const UnMemoizedMessageOptions = <
threadList,
} = useMessageContext<StreamChatGenerics>('MessageOptions');

const { CustomMessageActionsList } = useComponentContext('MessageOptions');

const { t } = useTranslationContext('MessageOptions');

const handleOpenThread = propHandleOpenThread || contextHandleOpenThread;

const messageActions = getMessageActions();
const renderMessageActions = shouldRenderMessageActions({
// @ts-ignore
customMessageActions,
CustomMessageActionsList,
inThread: threadList,
messageActions,
});

const shouldShowReactions = messageActions.indexOf(MESSAGE_ACTIONS.react) > -1;
const shouldShowReplies =
Expand All @@ -92,9 +82,7 @@ const UnMemoizedMessageOptions = <

return (
<div className={rootClassName} data-testid='message-options'>
{renderMessageActions && (
<MessageActions ActionsIcon={ActionsIcon} messageWrapperRef={messageWrapperRef} />
)}
<MessageActions ActionsIcon={ActionsIcon} messageWrapperRef={messageWrapperRef} />
{shouldShowReplies && (
<button
aria-label={t('aria/Open Thread')}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Message/__tests__/MessageOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('<MessageOptions />', () => {
channelStateOpts: { channelCapabilities: minimumCapabilitiesToRenderMessageActions },
customMessageProps: { messageActions: [] },
});
expect(MessageActionsMock).not.toHaveBeenCalled();
expect(MessageActionsMock).;
});

it('should not show actions box for message in thread if only non-thread actions are available', async () => {
Expand Down
21 changes: 19 additions & 2 deletions src/components/MessageActions/MessageActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import clsx from 'clsx';
import { MessageActionsBox } from './MessageActionsBox';

import { ActionsIcon as DefaultActionsIcon } from '../Message/icons';
import { isUserMuted } from '../Message/utils';
import { isUserMuted, shouldRenderMessageActions } from '../Message/utils';

import { useChatContext } from '../../context/ChatContext';
import { MessageContextValue, useMessageContext } from '../../context/MessageContext';

import type { DefaultStreamChatGenerics, IconProps } from '../../types/types';
import { useMessageActionsBoxPopper } from './hooks';
import { useTranslationContext } from '../../context';
import { useComponentContext, useTranslationContext } from '../../context';

type MessageContextPropsToPick =
| 'getMessageActions'
Expand Down Expand Up @@ -65,7 +65,9 @@ export const MessageActions = <
} = props;

const { mutes } = useChatContext<StreamChatGenerics>('MessageActions');

const {
customMessageActions,
getMessageActions: contextGetMessageActions,
handleDelete: contextHandleDelete,
handleFlag: contextHandleFlag,
Expand All @@ -75,8 +77,11 @@ export const MessageActions = <
isMyMessage,
message: contextMessage,
setEditingState,
threadList,
} = useMessageContext<StreamChatGenerics>('MessageActions');

const { CustomMessageActionsList } = useComponentContext('MessageActions');

const { t } = useTranslationContext('MessageActions');

const getMessageActions = propGetMessageActions || contextGetMessageActions;
Expand All @@ -92,6 +97,16 @@ export const MessageActions = <

const isMuted = useCallback(() => isUserMuted(message, mutes), [message, mutes]);

const messageActions = getMessageActions();

const renderMessageActions = shouldRenderMessageActions({
// @ts-expect-error
customMessageActions,
CustomMessageActionsList,
inThread: threadList,
messageActions,
});

const hideOptions = useCallback((event: MouseEvent | KeyboardEvent) => {
if (event instanceof KeyboardEvent && event.key !== 'Escape') {
return;
Expand Down Expand Up @@ -132,6 +147,8 @@ export const MessageActions = <
referenceElement: actionsBoxButtonRef.current,
});

if (!renderMessageActions) return null;

return (
<MessageActionsWrapper
customWrapperClass={customWrapperClass}
Expand Down
10 changes: 2 additions & 8 deletions src/components/MessageActions/__tests__/MessageActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ describe('<MessageActions /> component', () => {
const tree = renderMessageActions({}, testRenderer.create);
expect(tree.toJSON()).toMatchInlineSnapshot(`
<div
className="
str-chat__message-simple__actions__action
str-chat__message-simple__actions__action--options
str-chat__message-actions-container"
className="str-chat__message-simple__actions__action str-chat__message-simple__actions__action--options str-chat__message-actions-container"
data-testid="message-actions"
onClick={[Function]}
>
Expand Down Expand Up @@ -276,10 +273,7 @@ describe('<MessageActions /> component', () => {
);
expect(tree.toJSON()).toMatchInlineSnapshot(`
<span
className="
str-chat__message-simple__actions__action
str-chat__message-simple__actions__action--options
str-chat__message-actions-container"
className="str-chat__message-simple__actions__action str-chat__message-simple__actions__action--options str-chat__message-actions-container"
data-testid="message-actions"
onClick={[Function]}
>
Expand Down

0 comments on commit aa0ccb3

Please sign in to comment.