All files / src/features/Merchants/MerchantSidePanel/hooks useHandleScroll.tsx

100% Statements 19/19
75% Branches 9/12
100% Functions 5/5
100% Lines 18/18

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                    112x   112x 112x 1x 1x 1x 1x 1x         1x       112x       112x 1x 1x 1x               112x 81x               112x                     81x                        
import GiveButton from "@shared/Button/GiveButton";
import React, { useRef, useState } from "react";
import { ArrowDownIcon } from "@phosphor-icons/react";
import { useInView } from "react-intersection-observer";
import { CustomTheme } from "@theme/v2/palette.interface";
export default function useHandleScroll({
  defaultHasAgreed = false,
}: {
  defaultHasAgreed?: boolean;
}) {
  const containerRef = useRef<HTMLDivElement | null>(null);
  const [enabledAgreementCheckbox, setEnabledAgreementCheckbox] =
  useState(defaultHasAgreed);
  const handleScroll = (e: React.UIEvent<HTMLElement>) => {
    const { scrollTop, clientHeight, scrollHeight } = e.currentTarget;
    const scrolledHeight = Math.ceil(scrollTop + clientHeight);
    const totalScrollHeight = Math.ceil(scrollHeight);
    const tolerance = 2;
    Eif (
      scrolledHeight >= totalScrollHeight - tolerance &&
      !enabledAgreementCheckbox &&
      !defaultHasAgreed
    ) {
      setEnabledAgreementCheckbox(true);
    }
  };
 
  const { ref, inView } = useInView({
    threshold: 0.1,
  });
 
  const scrollToBottom = () => {
    const container = containerRef.current;
    Eif (container) {
      container.scrollTo({
        top: container.scrollHeight,
        behavior: "smooth",
      });
    }
  };
 
  const scrollButton = (
    <GiveButton
      sx={(theme) => buttonStyle(!inView, theme)}
      onClick={scrollToBottom}
      label="Scroll to Bottom"
      size="large"
      variant="filled"
      endIcon={<ArrowDownIcon size={18} color="white" />}
    />
  );
  return {
    scrollButton,
    handleScroll,
    containerRef,
    viewRef: ref,
    enabledAgreementCheckbox,
    setEnabledAgreementCheckbox,
    inView,
  };
}
 
const buttonStyle = (visible: boolean, theme: CustomTheme) => ({
  margin: "0 auto",
  bottom: "106px",
  left: "calc( 50% - 66px )",
  zIndex: 99,
  borderRadius: `${theme.customs?.radius?.extraSmall}px`,
  position: "absolute",
  display: "flex",
  opacity: visible ? 1 : 0,
  visibility: visible ? "visible" : "hidden",
  transition: "opacity 0.3s ease, visibility 0.3s ease",
});