All files / src/features/Merchants/MerchantSidePanel/components ConversationsButton.tsx

100% Statements 22/22
88.23% Branches 45/51
100% Functions 8/8
100% Lines 19/19

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                                                    116x                     524x 524x 524x   524x   524x     524x 96x 64x 64x         524x           524x                       524x   524x               10x                                                                                           524x           116x   2736x   524x                      
import { StyledIconButton } from "@common/IconButton";
import { SIDE_PANEL_BUTTON_OFFSET } from "@constants/constants";
import { Badge, Box } from "@mui/material";
import { ChatsCircleIcon } from "@phosphor-icons/react";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { useAppTheme } from "@theme/v2/Provider";
import { animated, useSpring } from "react-spring";
import { NEW_BASE_PANEL_WIDTH } from "@shared/SidePanel/GiveSidePanel";
import { styled } from "@theme/v2/Provider";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import CounterCircle from "@features/GiveConversation/components/CounterCircle";
import { ConversationCounterVariant } from "@features/GiveConversation/types";
import { useEffect, useState } from "react";
 
type Props = {
  isLoading: boolean;
  handleOpenConversationsModal: (open?: boolean | undefined) => void;
  isOpen?: boolean;
  isOpenChangeLog?: boolean;
  totalUnread: number;
  isReplyFromMerchant?: boolean;
  isDoublePanel?: boolean;
  counterVariant?: ConversationCounterVariant;
  secondPanelWidth?: number;
};
 
const ConversationsButton = ({
  isLoading,
  handleOpenConversationsModal,
  isOpen = false,
  isOpenChangeLog = false,
  isReplyFromMerchant = false,
  isDoublePanel = false,
  counterVariant,
  secondPanelWidth = NEW_BASE_PANEL_WIDTH,
  ...rest
}: Props) => {
  const { isMobileView } = useCustomTheme();
  const { palette } = useAppTheme();
  const { isNewConversationsEnabled } = useGetFeatureFlagValues();
 
  const [visible, setVisible] = useState(false);
 
  const isOpenState = isOpen || isOpenChangeLog;
 
  // Trigger animation once data is loaded and approved
  useEffect(() => {
    if (!isLoading) {
      const timeout = setTimeout(() => setVisible(true), 10);
      return () => clearTimeout(timeout);
    }
  }, [isLoading]);
 
  // Animate slide + opacity
  const springStyles = useSpring({
    opacity: visible && !isOpen ? 1 : 0,
    transform: visible && !isOpen ? "translateY(0px)" : "translateY(100px)",
    config: { duration: 300 },
  });
 
  const badgeContent = isNewConversationsEnabled ? (
    <Box borderRadius="60px" bgcolor={palette.primitive?.neutral[0]}>
      <CounterCircle
        count={rest.totalUnread}
        variant={counterVariant || "unread"}
        capped
      />
    </Box>
  ) : (
    rest.totalUnread || undefined
  );
 
  const AnimatedButton = animated(StyledIconButton);
 
  return (
    <AnimatedButton
      style={{
        ...springStyles,
        transition: "right 250ms ease-in-out",
      }}
      id="conversationButton"
      onClick={() =>
        handleOpenConversationsModal(!isMobileView ? true : !isOpen)
      }
      disabled={isLoading}
      sx={{
        borderRadius: "50%",
        position: isMobileView ? (isOpenState ? "relative" : "fixed") : "fixed",
        bottom: isMobileView ? (isOpenState ? "initial" : "30px") : "24px",
        right: isMobileView
          ? isOpenState
            ? "0px"
            : "initial"
          : `${
              (isDoublePanel
                ? NEW_BASE_PANEL_WIDTH + secondPanelWidth
                : secondPanelWidth) + SIDE_PANEL_BUTTON_OFFSET
            }px`,
 
        ...(isMobileView && { top: isOpen ? "0px" : "initial" }),
        boxShadow: isMobileView
          ? "0px 2.625px 13.125px 0px rgba(2, 2, 2, 0.15)"
          : "initial",
        backgroundColor: palette.neutral.white,
        zIndex: 3,
      }}
      data-testid="conversation-button"
    >
      <StyledContainer>
        <CustomBadge
          color={isNewConversationsEnabled ? undefined : "primary"}
          badgeContent={badgeContent}
          isNewConversationsEnabled={isNewConversationsEnabled}
          data-testid="unread-conversation-badge"
        >
          <ChatsCircleIcon
            width="30px"
            height="30px"
            color={palette.primitive?.neutral[100]}
          />
        </CustomBadge>
      </StyledContainer>
    </AnimatedButton>
  );
};
 
export default ConversationsButton;
 
const StyledContainer = styled(Box)(({ theme }) => ({
  borderRadius: "50%",
  padding: "11px",
  backgroundColor: theme.palette.primitive?.neutral[0],
}));
 
const CustomBadge = styled(Badge, {
  shouldForwardProp: (prop) =>
    prop !== "isReplyFromMerchant" && prop !== "isNewConversationsEnabled",
})<{ isNewConversationsEnabled: boolean }>(
  ({ theme, isNewConversationsEnabled }) => ({
    "& .MuiBadge-badge": {
      right: "-3px",
      top: "-3px",
      ...(!isNewConversationsEnabled && {
        color: theme.palette.primitive?.neutral[0],
        backgroundColor: theme.palette.primitive?.warning[50],
      }),
    },
  }),
);