All files / src/features/Merchants/MerchantSidePanel/components/UnderwriterRiskAnalystAssignCard/AssignmentSection AssigneeSection.tsx

86.95% Statements 20/23
51.92% Branches 27/52
66.66% Functions 4/6
86.36% Lines 19/22

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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                                            149x                 21x                 149x 149x       149x                                 149x                                                           21x                 149x 149x 149x 149x   149x                 149x                                                                                                                     21x           149x 149x       149x 149x   149x                                                                                    
import { Box, Stack } from "@mui/material";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import useAssigneeManagedByAcquirer from "@hooks/useAssigneeManagedByAcquirer";
import { useMerchantSidePanelContext } from "@features/Merchants/MerchantSidePanel/Provider/MerchantSidePanelProvider";
import { useConversationsModal } from "@features/Minibuilders/Conversations/hooks/useConversationsModal";
import { AssigneeInfos } from "@shared/AssignButton/GiveAssignButton.types";
import {
  AssigneeButtonProps,
  AssigneeData,
  AssigneeSectionProps,
  EmailAndMessageSectionProps,
} from "./AssigneeSection.types";
import { CustomText } from "./AssigneeSection.styles";
import GiveAssignButton from "@shared/AssignButton/GiveAssignButton";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import GiveLink from "@shared/Link/GiveLink";
import GiveText from "@shared/Text/GiveText";
import { AssigneesType } from "@components/Merchants/MerchantPreview/hooks/useGetTeamMembers";
import { useChangeAssignee } from "@shared/MerchantAssignmentManager/hooks/useChangeAssignee";
import GiveAvatar from "@shared/Avatar/GiveAvatar";
import placeholder from "assets/images/profile-placeholder.png";
 
const mapToAssigneeInfos = (data: AssigneeData): AssigneeInfos => ({
  accID: data.id,
  underwriterName: data.name,
  underwriterImageURL: data.imageURL,
  underwriterEmail: data.email,
  underwriterID: data.id,
  underwriterSetByAcquirer: data.setByAcquirer,
});
 
const AssignButton = ({
  data,
  type,
  merchantId,
  avatarOnly,
  displayNameOnly,
  showCaretIcon,
  isOnlyName = true,
}: AssigneeButtonProps) => {
  const { isNarrowView } = useCustomThemeV2();
  const { handleChangeAssignee } = useChangeAssignee({
    merchantID: merchantId,
  });
 
  const handleChange = (
    oldAssignee: number | undefined,
    newAssignee: string,
    assigneeType?: string,
    onSuccess?: () => void,
  ) => {
    const type =
      assigneeType === "underwriting_admin" ? "underwriter" : assigneeType;
 
    handleChangeAssignee(
      oldAssignee,
      newAssignee,
      type as "underwriter" | "sponsor" | "kyb",
      onSuccess,
    );
  };
 
  return (
    <GiveAssignButton
      title={
        isNarrowView ? (
          <GiveTruncateText
            className="assign-button-label"
            variant="bodyS"
            lineClamp={1}
            sx={{ wordBreak: "break-all" }}
          >
            Assign
          </GiveTruncateText>
        ) : (
          `Assign`
        )
      }
      type={data.assigneeType as AssigneesType}
      isOnlyName={isOnlyName}
      data={{ ...mapToAssigneeInfos(data), accID: merchantId }}
      customContextualMenuPaperStyle={{
        mt: "-48px",
      }}
      onChange={handleChange}
      avatarOnly={avatarOnly}
      displayNameOnly={displayNameOnly}
      showCaretIcon={showCaretIcon}
    />
  );
};
 
const EmailAndMessageSection = ({
  data,
  type,
  merchantId,
  hideMessageSection,
  hideAssignButton,
  avatarOnly,
  showEmailAsName = false,
}: EmailAndMessageSectionProps) => {
  const { id } = useMerchantSidePanelContext();
  const { openConversationHandler } = useConversationsModal();
  const { isMobileView } = useCustomThemeV2();
  const tag = data.name || data.email || "";
 
  const handleMessage = () => {
    openConversationHandler({
      id,
      name: "General",
      paths: [],
      defaultMessage: `@[__${tag}__](user:__${tag}__) `,
    });
  };
 
  return (
    <Box display="flex">
      <Stack
        width={isMobileView ? "auto" : "100%"}
        spacing={1}
        mt={!isMobileView ? 0 : 1}
      >
        {!hideAssignButton && (
          <AssignButton
            type={type}
            data={data}
            merchantId={merchantId}
            avatarOnly={avatarOnly}
            isOnlyName={!showEmailAsName}
          />
        )}
        {isMobileView && data.name && (
          <Box sx={{ "& p": { margin: 0 } }}>
            <AssignButton
              type={type}
              data={data}
              merchantId={merchantId}
              displayNameOnly
            />
          </Box>
        )}
        {(!isMobileView || data.name) && !showEmailAsName ? (
          <CustomText variant="bodyS" color="secondary">
            {data.email}
          </CustomText>
        ) : data.email && !showEmailAsName ? (
          <Box sx={{ "& p": { margin: 0 } }}>
            <AssignButton
              type={type}
              data={data}
              merchantId={merchantId}
              displayNameOnly
              isOnlyName={false}
            />
          </Box>
        ) : (
          <></>
        )}
        {Boolean(data.id) && !hideMessageSection && (
          <GiveLink
            component="button"
            color="secondary"
            variant="bodyS"
            fontFamily="inherit"
            onClick={handleMessage}
          >
            Message
          </GiveLink>
        )}
      </Stack>
    </Box>
  );
};
 
export const AssigneeSection = ({
  data,
  type,
  merchantId,
  hideMessageSection,
}: AssigneeSectionProps) => {
  const { isMobileView } = useCustomThemeV2();
  const { isManagedByAcquirer } = useAssigneeManagedByAcquirer({
    underwriterSetByAcquirer: data.setByAcquirer,
  });
 
  const width = isMobileView ? "100%" : "50%";
  const isMobileButton = isMobileView && !!data?.email;
 
  return (
    <>
      <Box
        display="flex"
        flexDirection={isMobileView && data?.email ? "column" : "row"}
        alignItems={isMobileButton ? "flex-start" : "center"}
      >
        <Stack
          direction="row"
          justifyContent="space-between"
          alignItems="center"
          width={width}
        >
          <GiveText color="secondary" variant="bodyS" flexGrow={1}>
            {type}
          </GiveText>
          {isMobileButton && (
            <GiveAvatar
              size="32px"
              imageUrl={data.imageURL ? `${data.imageURL}/thumb` : placeholder}
            />
          )}
        </Stack>
        <Box flexGrow={1} width={width}>
          {isManagedByAcquirer ? (
            <AssignButton type={type} data={data} merchantId={merchantId} />
          ) : (
            <EmailAndMessageSection
              type={type}
              data={data}
              hideAssignButton={isManagedByAcquirer || isMobileButton}
              merchantId={merchantId}
              hideMessageSection={hideMessageSection}
              avatarOnly={isMobileView && !data?.name}
              showEmailAsName={!isMobileView && !data?.name}
            />
          )}
        </Box>
      </Box>
    </>
  );
};