All files / src/features/GiveConversation/components ConversationMenu.tsx

94.44% Statements 34/36
76.47% Branches 39/51
90.9% Functions 10/11
94.28% Lines 33/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213                                                                                      142x 142x 142x 142x 142x   142x   142x 142x   142x       4x   4x       4x       4x                 142x 5x   5x 1x 1x   4x 3x   1x       142x   142x                                                               1x                                                                                 1x           1x     2x               33x 53x 10x                   141x 141x     141x                          
import { ChatsCircleIcon } from "@phosphor-icons/react";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { useState } from "react";
import { useOpenGiveConversation } from "../hooks/useOpenGiveConversation";
import {
  ConversationCounterVariant,
  ConversationMenuProps,
  GiveConversationTabType,
} from "../types";
import ImagesStacker from "@features/Minibuilders/Conversations/utils/ImagesStacker";
import { StyledStackProps } from "@features/Merchants/MerchantSidePanel/ApprovalFlowPanel/components/TaskItemCard/TaskItemCard";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { Badge, BadgeProps, Box, Stack } from "@mui/material";
import CounterCircle from "./CounterCircle";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { getConversationColors } from "../utils";
import { checkPortals } from "@utils/routing";
import { NEW_ACTION_DENY_MESSAGE } from "@constants/permissions";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { useConversationPermission } from "../hooks/useConversationaPermission";
 
export function ConversationMenu({
  disabled,
  canSendTeamMessage = true,
  maxImagesShown = 2,
  images,
  hideImages,
  messageCount,
  counterVariant,
  CustomFrontButton,
  isRiskTrigger,
  internalThreadID,
  threadID,
  showAvatarsOnMobile = false,
  containerWidth,
  ...conversationProps
}: ConversationMenuProps & {
  internalThreadID?: number;
  threadID?: number;
  showAvatarsOnMobile?: boolean;
  containerWidth?: "fluid" | "max";
}) {
  const { handleOpenConversation } = useOpenGiveConversation();
  const { palette } = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
  const { isAcquirerEnterprises } = checkPortals();
 
  const { hasAccessToViewConversation } = useConversationPermission();
 
  const hasInternalMessages = Boolean(internalThreadID);
  const conversationID = hasInternalMessages ? "team" : "merchant";
 
  const openConversation = async (
    initialTab: GiveConversationTabType,
    source = "chat",
  ) => {
    const taskThreadID = initialTab === "team" ? internalThreadID : threadID;
 
    const initialThreadID = taskThreadID || conversationProps?.initialThreadID;
 
    // Team messages should not have a predefined message, only messages to merchant
    const predefinedMessage =
      initialTab === "merchant" && source === "chat"
        ? conversationProps?.predefinedMessage
        : "";
 
    handleOpenConversation({
      initialTab,
      ...conversationProps,
      initialThreadID,
      predefinedMessage,
      messageCount,
    });
  };
 
  const handleChatIconClick = (e: React.MouseEvent<HTMLButtonElement>) => {
    e.stopPropagation();
 
    if (isRiskTrigger) {
      openConversation("team");
      return;
    }
    if (canSendTeamMessage) {
      setAnchorEl(e.currentTarget);
    } else {
      openConversation("merchant");
    }
  };
 
  const hasReplyFromMerchant = counterVariant === "reply" && !!messageCount;
 
  return (
    <GiveTooltip
      title={NEW_ACTION_DENY_MESSAGE}
      disableHoverListener={hasAccessToViewConversation}
      width={containerWidth === "max" ? "Max width" : "compact"}
      fluidWidth={containerWidth === "fluid"}
    >
      <Stack
        data-testid="conversation-menu"
        direction="row"
        gap="8px"
        alignItems="center"
        width="100%"
      >
        {CustomFrontButton &&
          CustomFrontButton({
            onClick: handleChatIconClick,
            disabled: !hasAccessToViewConversation,
          })}
        {!!images && (!isMobileView || showAvatarsOnMobile) && (
          <StyledImageStackContainer isHidden={hideImages}>
            <ImagesStacker
              data={images.slice(0, maxImagesShown)}
              imageWidth={24}
              hasBorderedImages
              borderWidth={1}
              borderColor={palette.surface?.primary}
              condition={
                !!messageCount && !!counterVariant && !hasReplyFromMerchant
              }
              renderAdditional={() => {
                // We should show number of users in the conversation only if there are more than 2 users involved
                Eif (images.length <= 2) return null;
                return (
                  <Box
                    border={`1px solid ${palette.surface?.primary}`}
                    borderRadius="30px"
                    bgcolor={palette.surface?.primary}
                  >
                    <CounterCircle
                      // We subtract 2 because we show 2 avatars by default
                      count={images?.length - 2 || 0}
                      variant={counterVariant || "unread"}
                      size={24}
                    />
                  </Box>
                );
              }}
              onHandleImageClick={() =>
                openConversation(conversationID, "avatar")
              }
            />
          </StyledImageStackContainer>
        )}
        {!CustomFrontButton && (
          <ButtonWrapperBadge counterVariant={counterVariant}>
            <GiveIconButton
              Icon={ChatsCircleIcon}
              variant="ghost"
              data-testid="conversation-menu-button"
              disabled={disabled || !hasAccessToViewConversation}
              onClick={handleChatIconClick}
              size="large"
            />
          </ButtonWrapperBadge>
        )}
        <ContextualMenu
          anchorEl={anchorEl}
          color={isMobileView ? "primary" : "tertiary"}
          texture={isMobileView ? "solid" : "blurred"}
          options={[
            {
              text: "Write a team message",
              onClick: () => openConversation("team"),
            },
            {
              text: `Write a message to the ${
                isAcquirerEnterprises ? "provider" : "merchant"
              }`,
              onClick: () => openConversation("merchant"),
            },
          ]}
          handleClose={() => setAnchorEl(null)}
          height="fit-content"
        />
      </Stack>
    </GiveTooltip>
  );
}
 
const StyledImageStackContainer = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isHidden",
})<StyledStackProps>(({ isHidden }) => ({
  position: "relative",
  opacity: isHidden ? 0.5 : 1,
  width: "100%",
}));
 
export function ButtonWrapperBadge({
  children,
  counterVariant,
}: BadgeProps & { counterVariant?: ConversationCounterVariant }) {
  const { palette } = useAppTheme();
  const background = counterVariant
    ? getConversationColors(palette, counterVariant)
    : "transparent";
  return (
    <Badge
      invisible={!counterVariant}
      overlap="circular"
      anchorOrigin={{ vertical: "top", horizontal: "right" }}
      badgeContent={
        <Box bgcolor={background} height="7px" width="7px" borderRadius="50%" />
      }
    >
      {children}
    </Badge>
  );
}