All files / src/components/ProfileMenu/modals/Tabs/NotificationTab index.tsx

96.87% Statements 31/32
89.28% Branches 25/28
93.75% Functions 15/16
100% Lines 30/30

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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255                                      164x                   164x   164x 417x 417x   417x   417x                                           164x 164x   164x 1712x                 164x                           3x                                         3x       164x                                                                                           7x 25x                       50x               7x 25x   175x                           7x 50x                                       7x 25x   75x                         7x 775x 256x                     139x                  
import { Text } from "@common/Text";
import NotificationCheckBox from "@components/ProfileMenu/components/NotificationCheckBox";
import { Box, Skeleton, Stack, styled } from "@mui/material";
import useNotificationTab from "../../hooks/useNotificationTab";
import { ProfileModalPageProps } from "../../types";
import { palette } from "@palette";
import { HiddenComponent } from "@containers/HiddenComponent";
import NotificationGridLayout, {
  TNotificationCheckbox,
} from "@components/ProfileMenu/components/NotificationGridLayout";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { FilterButtonBase } from "features/Notifications/NotificationsCenter/components/Header.atoms";
import { TabsArrayItem } from "../../hooks/useAlertsTabs";
import { TParsedAlert } from "../../types";
import { shouldBeHidden } from "@constants/constants";
import { PaperPlaneTiltIcon, AtIcon } from "@phosphor-icons/react";
 
function NotificationTab({ setDisabled }: ProfileModalPageProps) {
  const { isMobileView } = useCustomTheme();
  const {
    activeTab,
    setActiveTab,
    tabs,
    customData,
    isLoading,
    handleChange,
    handleCheckAll,
    handleSubmit,
  } = useNotificationTab({ setIsDisabled: setDisabled });
 
  const renderTabs = (item: TabsArrayItem) => {
    const { name, value, hidden } = item;
    const isActive = value === activeTab?.value;
 
    const onClick = () => setActiveTab(item.value);
 
    return (
      <HiddenComponent key={value} hidden={hidden || false}>
        {isMobileView ? (
          <FilterButtonBase
            background="tertiary"
            selected={isActive}
            onClick={onClick}
          >
            {name}
          </FilterButtonBase>
        ) : (
          <CustomTabBtx isActive={isActive} onClick={onClick}>
            {name}
          </CustomTabBtx>
        )}
      </HiddenComponent>
    );
  };
 
  const {
    alerts,
    totalAssigned: { emailCount, pushCount },
  } = customData;
  const alertsTotal = alerts.length;
 
  const renderCheckbox = (item: TParsedAlert, idx: number) => (
    <NotificationCheckBox
      key={`${activeTab?.value} ${item?.name || idx}`}
      delay={100 + idx * 50}
      item={item}
      handleChange={handleChange}
      isLast={idx === alertsTotal - 1}
    />
  );
 
  const checkAllActions: TNotificationCheckbox[] = [
    {
      channel: "email",
      label: (
        <>
          <AtIcon weight="regular" size={16} color={palette.gray[300]} />
          Email
        </>
      ),
      labelAlign: "left",
      dataTestId: "email-check-all",
      value: alertsTotal === emailCount,
      indeterminate: alertsTotal === emailCount ? false : emailCount > 0,
      handleChange: () =>
        handleCheckAll("email", emailCount === 0 ? "add" : "remove"),
    },
    {
      channel: "push",
      hidden: shouldBeHidden.pushNotificationSettings,
      label: (
        <>
          <PaperPlaneTiltIcon
            weight="regular"
            size={16}
            color={palette.gray[300]}
            data-testid="push-notifications-column-icon"
          />
          Push
        </>
      ),
      labelAlign: "left",
      dataTestId: "push-check-all",
      value: alertsTotal === pushCount,
      indeterminate: alertsTotal === pushCount ? false : pushCount > 0,
      handleChange: () =>
        handleCheckAll("push", pushCount === 0 ? "add" : "remove"),
    },
  ];
 
  return (
    <Box overflow="hidden" height="100%" width="100%">
      <form
        style={{
          overflow: "hidden",
          height: "100%",
          width: "100%",
        }}
        id="profile-form"
        data-testid="notification-tab"
        onSubmit={handleSubmit}
      >
        {isLoading ? (
          <TabBarSkeleton />
        ) : (
          <NotificationGridLayout
            showBorder={!isMobileView}
            containerSx={{
              ...(!isMobileView && {
                marginBottom: "24px",
              }),
            }}
            mainContent={
              <Stack direction="row" gap={2} alignItems="center">
                {tabs?.map(renderTabs)}
              </Stack>
            }
            checkboxes={
              alertsTotal === 0 || isMobileView ? [] : checkAllActions
            }
          />
        )}
        {isLoading ? (
          <CheckboxesSkeleton />
        ) : (
          <CheckboxesContainer>
            {alerts.map(renderCheckbox)}
          </CheckboxesContainer>
        )}
      </form>
    </Box>
  );
}
 
export default NotificationTab;
 
const TabBarSkeleton = () => {
  return (
    <FadeUpWrapper delay={50}>
      <Stack
        direction="row"
        justifyContent="space-between"
        padding="12px 0"
        borderBottom={`1px solid ${palette.liftedWhite[200]}`}
        marginBottom="24px"
      >
        <TabsSkeleton />
        <Stack direction="row" gap="12px" alignItems="center">
          {Array.from({ length: 2 }, (_, i) => (
            <TabBarEndSkeleton key={i} />
          ))}
        </Stack>
      </Stack>
    </FadeUpWrapper>
  );
};
 
const CheckboxesSkeleton = () => (
  <Box mt="24px" pr="5px">
    {Array.from({ length: 7 }, (c, idx) => (
      <Skeleton
        key={idx}
        sx={{
          borderRadius: "8px",
          marginTop: "8px",
        }}
        width="100%"
        variant="rounded"
        height="64px"
      />
    ))}
  </Box>
);
 
const TabBarEndSkeleton = () => (
  <Stack alignItems="center" gap="6px" flexDirection="row">
    <Skeleton
      sx={{
        borderRadius: "8px",
      }}
      width="50px"
      variant="rounded"
      height="16px"
    />
    <Skeleton
      sx={{
        borderRadius: "6px",
      }}
      width="20px"
      variant="rounded"
      height="20px"
    />
  </Stack>
);
 
const TabsSkeleton = () => (
  <Box gap="16px" display="flex" alignItems="center">
    {Array.from({ length: 3 }, (c, idx) => (
      <Skeleton
        key={idx}
        sx={{
          borderRadius: "8px",
        }}
        width="64px"
        variant="rounded"
        height="16px"
      />
    ))}
  </Box>
);
 
const CustomTabBtx = styled(Text, {
  shouldForwardProp: (prop) => prop !== "isActive",
})<{ isActive: boolean }>(({ isActive }) => ({
  all: "unset",
  cursor: isActive ? "default" : "pointer",
  fontSize: "14px",
  lineHeight: "16.8px",
  fontWeight: 350,
  color: isActive ? palette.neutral[100] : palette.neutral[70],
  userSelect: "none",
  transitions: "all 50ms ease",
}));
 
const CheckboxesContainer = styled(Box)(() => ({
  "&::-webkit-scrollbar": {
    display: "none",
  },
  msOverflowStyle: "none",
  scrollbarWidth: "none",
  overflowY: "auto",
  maxHeight: "calc(100% - 96px)",
}));