All files / src/features/Minibuilders/Conversations/SearchModal ConversationTopicsModal.tsx

84.61% Statements 33/39
61.11% Branches 11/18
100% Functions 14/14
86.11% Lines 31/36

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                      21x       21x                                   315x       21x                     2x 2x 2x   2x   2x 30x     30x         2x 1x         2x       1x 1x                                 1x     2x                         3x     19x     4x 4x 2x       2x         4x         4x         1x                                             21x                                                             21x 30x 3x                                                      
import { Box, CircularProgress, Paper, TextField, styled } from "@mui/material";
import { Autocomplete } from "@mui/material";
import { SyntheticEvent, useCallback, useEffect, useState } from "react";
import { createFilterOptions } from "@mui/material";
import { useCreateTopic } from "../hooks/useCreateTopic";
import { SearchIcon } from "@assets/rebrandIcons";
import { GlobalTopics } from "../hooks/useConversationsModal";
import { useAppDispatch } from "@redux/hooks";
import { setConversationTopic } from "@redux/slices/conversations";
import { MERCHANT_FILE_TEXT } from "@constants/stringConstants";
 
const filter = createFilterOptions<{ label: string; inputValue?: string }>();
 
type TopicCategory = { label: string };
 
const categoryLabels = [
  MERCHANT_FILE_TEXT,
  "Bank Accounts",
  "Primary Account Holder",
  "Business Profile",
  "Business Address",
  "Business Owners",
  "PEP Check",
  "OFAC Check",
  "Snapshots",
  "Documents",
  "Merchant Fees",
  "Merchant Agreement",
  "Risk Activity",
  "MATCH Check",
  "Sponsor",
];
 
export const categories: TopicCategory[] = categoryLabels.map((label) => ({
  label,
}));
 
const ConversationTopicsModal = ({
  handleOpenTopicModal,
  merchantId,
  isLoading,
  isEnterprise,
}: {
  handleOpenTopicModal: () => void;
  merchantId: number;
  isLoading: boolean;
  isEnterprise: boolean;
}) => {
  const [value, setValue] = useState<string>("");
  const { handleSubmit, isSuccess } = useCreateTopic({ merchantId });
  const dispatch = useAppDispatch();
 
  const roleReplacer = useCallback(
    (arr: Array<{ label: string }>) =>
      arr.map((v) => {
        Iif (isEnterprise && v.label.includes("Merchant")) {
          v.label = v.label.replace("Merchant", "Provider");
        }
        return v;
      }),
    [isEnterprise],
  );
 
  useEffect(() => {
    Iif (isSuccess) {
      handleOpenTopicModal();
    }
  }, [isSuccess]);
 
  const onChange = (
    e: SyntheticEvent<Element, Event>,
    topic: TopicCategory | null,
  ) => {
    Iif (!topic) return;
    Iif (GlobalTopics[topic.label]) {
      dispatch(
        setConversationTopic({
          isOpen: false,
          disableGlobalLauncher: false,
          queryObject: {
            id: undefined,
            name: topic.label,
            paths: [],
            defaultMessage: "",
            challengeId: undefined,
          },
        }),
      );
      handleOpenTopicModal();
      return;
    } else {
      handleSubmit(topic.label);
    }
  };
  return (
    <AutocompleteContainer>
      <Autocomplete
        id="combo-box-demo"
        options={roleReplacer(categories) || []}
        sx={{
          width: "100%",
          boxShadow: "0px 8px 25px 0px rgba(0, 0, 0, 0.15)",
          borderRadius: "8px",
        }}
        open
        onBlur={handleOpenTopicModal}
        PaperComponent={(props) => (
          <OptionsContainer {...props} isTyping={value.length > 0} />
        )}
        getOptionDisabled={(option) => {
          return option.label === "Add Custom topic";
        }}
        filterOptions={(options, params) => {
          const filtered = filter(options, params);
          if (params.inputValue !== "") {
            filtered.push({
              inputValue: params.inputValue,
              label: "Add Custom topic",
            });
            filtered.push({
              inputValue: params.inputValue,
              label: `${params.inputValue}`,
            });
          }
          return filtered;
        }}
        onChange={onChange}
        loading={isLoading}
        renderInput={({ InputProps, ...params }) => (
          <Box sx={{ padding: "8px" }}>
            <TextField
              placeholder="Search..."
              value={value}
              autoFocus
              onChange={(e) => setValue(e.target.value)}
              InputProps={{
                ...InputProps,
                endAdornment: (
                  <>
                    {isLoading ? (
                      <CircularProgress color="inherit" size={20} />
                    ) : null}
                  </>
                ),
                startAdornment: <SearchIcon width={24} height={24} />,
              }}
              {...params}
            />
          </Box>
        )}
      />
    </AutocompleteContainer>
  );
};
 
export default ConversationTopicsModal;
 
const AutocompleteContainer = styled(Box)(() => ({
  position: "absolute",
  bottom: 0,
 
  width: "273px",
  marginBottom: "300px",
  zIndex: 99,
 
  backgroundColor: "#FAFAFA",
 
  left: "50%",
  transform: `translateX(-50%)`,
  ".Mui-expanded .MuiOutlinedInput-root .MuiAutocomplete-input": {
    margin: 0,
    padding: "initial",
  },
  ".MuiAutocomplete-hasPopupIcon .MuiOutlinedInput-root, .MuiAutocomplete-hasClearIcon .MuiOutlinedInput-root":
    {
      padding: 0,
    },
  ".Mui-expanded.MuiAutocomplete-hasPopupIcon .MuiOutlinedInput-root": {
    borderRadius: "90px",
    padding: "4px 12px",
    alignItems: "center",
  },
  ".MuiOutlinedInput-root": {
    padding: 0,
    borderRadius: "90px",
  },
}));
 
const OptionsContainer = styled(Paper, {
  shouldForwardProp: (prop) => prop !== "isTyping",
})<{ isTyping: boolean }>(({ isTyping }) => ({
  transform: "translate(-10px, 0px)",
  width: "273px",
  boxShadow:
    "-4px 0px 10px 0px rgba(0, 0, 0, 0), 4px 0px 10px 0px rgba(0, 0, 0, 0.05), 0px 4px 10px 0px rgba(0, 0, 0, 0.05)",
  borderRadius: "0px 0px 8px 8px",
  "& .MuiAutocomplete-listbox": {
    width: "100%",
    padding: "8px 0px 4px",
    maxHeight: "214px",
    backgroundColor: "#FAFAFA",
 
    "& .MuiAutocomplete-option": {
      height: "40px",
      padding: "4px 8px",
      ...(isTyping && {
        "&:nth-last-child(2)": {
          fontSize: "12px",
          padding: "12px 12px",
          fontWeight: 350,
          height: "14px",
          lineHeight: "14.4px",
        },
      }),
    },
  },
}));