All files / src/features/Notifications/NotificationsCenter NotificationsCenter.tsx

84% Statements 21/25
66.66% Branches 12/18
87.5% Functions 7/8
83.33% Lines 20/24

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                                              25x 403x 403x                       403x   403x   403x 5x 5x     403x 403x   403x 77x         403x 45x                     403x   403x   403x                                                       942x                                                           25x 942x        
import { Stack } from "@mui/material";
import NotificationsCenterWrapper, {
  NotificationsCenterWrapperProps,
} from "./components/Wrapper";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { MobileHeader, DesktopHeader } from "./components/Header.atoms";
import useNotificationsCenter from "./hooks/useNotificationsCenter";
import useNotificationsActions from "./hooks/useNotificationsActions";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import {
  BodyWrapper,
  NotificationsBodyEmptyState,
  NotificationsItemSkeleton,
} from "./components/Body.atoms";
import NotificationsVirtualList from "./components/NotificationsVirtualList";
import { useEffect, useMemo, useRef } from "react";
import { TScrollToTop } from "./types";
 
type NotificationsCenterProps = Omit<
  NotificationsCenterWrapperProps,
  "children"
>;
 
const NotificationsCenter = (props: NotificationsCenterProps) => {
  const { onClose } = props;
  const { isMobileView } = useCustomTheme();
  const {
    data,
    isLoading,
    isEmpty,
    filters,
    showMarkAllAsRead,
    hasNextPage,
    isFetchingNextPage,
    fetchNextPage,
    totalPages,
    selectedFilter,
  } = useNotificationsCenter(Boolean(props?.anchorEl));
 
  const { markAllAsRead, handleConfigurationClick } = useNotificationsActions();
 
  const handleConfigurationRedirect = () => {
    onClose();
    handleConfigurationClick();
  };
 
  const page = useRef(1);
  const scrollToTop = useRef<TScrollToTop>(null);
 
  useEffect(() => {
    Iif (scrollToTop.current) {
      scrollToTop.current(1);
    }
  }, [selectedFilter]);
 
  const onEndReached = async () => {
    Iif (!isFetchingNextPage && hasNextPage) {
      fetchNextPage({
        pageParam: ++page.current,
      }).then(() => {
        if (scrollToTop.current) {
          scrollToTop.current((totalPages || 1) * 20);
        }
      });
    }
  };
 
  const flatList = useMemo(() => Object.values(data).flat(), [data]);
  const renderGroupLabels =
    data.mandatory.length > 0 && data.generic.length > 0;
 
  return (
    <NotificationsCenterWrapper {...props}>
      <Stack
        direction="column"
        alignItems="stretch"
        height="inherit"
        minHeight="inherit"
        maxHeight="inherit"
      >
        {isMobileView ? (
          <MobileHeader
            filters={filters}
            onClose={onClose}
            showMarkAllAsRead={showMarkAllAsRead}
            markAllAsRead={markAllAsRead}
            handleConfigurationClick={handleConfigurationRedirect}
          />
        ) : (
          <DesktopHeader
            filters={filters}
            showMarkAllAsRead={showMarkAllAsRead}
            markAllAsRead={markAllAsRead}
            handleConfigurationClick={handleConfigurationRedirect}
          />
        )}
        {isLoading ? (
          <BodyWrapper>
            {Array.from({ length: 6 }, (_, index) =>
              renderItem(index, index, <NotificationsItemSkeleton />),
            )}
          </BodyWrapper>
        ) : isEmpty ? (
          <BodyWrapper>
            <NotificationsBodyEmptyState />
          </BodyWrapper>
        ) : (
          <NotificationsVirtualList
            data={flatList}
            renderGroupLabels={renderGroupLabels}
            closeModal={onClose}
            isFetchingNextPage={isFetchingNextPage}
            onEndReached={onEndReached}
            scrollToTop={scrollToTop}
          />
        )}
      </Stack>
    </NotificationsCenterWrapper>
  );
};
 
export default NotificationsCenter;
 
type ItemRenderer = (
  index: number,
  key: string | number,
  element: JSX.Element,
) => JSX.Element;
 
const renderItem: ItemRenderer = (index, key, element) => (
  <FadeUpWrapper key={key} delay={50 + index * 50}>
    {element}
  </FadeUpWrapper>
);