All files / components/Toaster ToasterItem.tsx

100% Statements 128/128
100% Branches 33/33
100% Functions 5/5
100% Lines 128/128

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 1291x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 2x 32x 2x 32x 2x 32x 32x 26x 32x 32x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 13x 32x 32x 32x 15x 15x 2x 2x 2x 2x 2x 15x 15x 15x 32x 32x 32x 15x 15x 10x 1x 10x 10x 15x 15x 15x 15x 32x 32x 32x 1x 1x 32x 32x 2x 2x 32x 32x 64x 32x 32x 32x 32x 32x 32x 32x 64x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 8x 8x 8x 8x 8x 8x 8x 32x 32x 32x 32x 32x  
import { useEffect, useState } from 'react';
 
import { c } from '../../helpers';
import { setStyleProperties } from '../../helpers/componentPropertiesHelpers';
import ButtonCloser from '../ButtonCloser/ButtonCloser';
import {
  IconError,
  IconInfo,
  IconPin,
  IconSuccess,
  IconWarning,
} from '../Icons';
import {
  TOASTER_TIMEOUT_TO_CLOSE,
  TOASTER_TIMEOUT_TO_SHOW_CLOSE_BUTTON,
  ToasterType,
} from './ToasterConstants';
import { nlToNodes } from './ToasterFormatHelpers';
import { ToasterItemProps } from './ToasterProps';
 
import styles from './Toaster.module.css';
 
const renderIconByType = (type: string): JSX.Element => {
  const { SUCCESS, ERROR, WARNING, INFO } = ToasterType;
  switch (type) {
    case SUCCESS:
      return <IconSuccess type={SUCCESS} className={c(styles.icon)} />;
    case ERROR:
      return <IconError type={ERROR} className={c(styles.icon)} />;
    case WARNING:
      return <IconWarning type={WARNING} className={c(styles.icon)} />;
    case INFO:
    default:
      return <IconInfo type={INFO} className={c(styles.icon)} />;
  }
};
 
export default function ToasterItem({
  onClose,
  toaster,
  className,
}: ToasterItemProps): JSX.Element {
  const { duration, id, message, nlToBr, title, type, stoppable } = toaster;
 
  const [pinned, setPinned] = useState<boolean>(false);
  const [mustClose, setMustClose] = useState<boolean>(false);
  const [cssClassStatus, setCssClassStatus] = useState<string>('');
 
  useEffect(() => {
    setCssClassStatus(styles.open);
  }, []);
 
  useEffect(() => {
    let timeoutId: ReturnType<typeof setTimeout>;
    if (mustClose && id) {
      setCssClassStatus(styles.close);
      timeoutId = setTimeout(() => {
        onClose(id);
      }, TOASTER_TIMEOUT_TO_CLOSE);
    }
    return () => {
      clearTimeout(timeoutId);
    };
  }, [mustClose, id, onClose]);
 
  useEffect(() => {
    let timeoutId: ReturnType<typeof setTimeout>;
    if (duration && !pinned) {
      timeoutId = setTimeout(() => {
        setMustClose(true);
      }, duration);
    }
 
    return () => {
      clearTimeout(timeoutId);
    };
  }, [duration, pinned]);
 
  const handleClose = (): void => {
    setMustClose(true);
  };
 
  const handlePin = (): void => {
    setPinned(!pinned);
  };
 
  const handleRef = (node: HTMLDivElement | null): void => {
    if (node == null) return;
 
    const itemRect = node.getBoundingClientRect();
 
    setStyleProperties({
      height: `${itemRect.height}px`,
      '--toaster-timeout-to-close': `${TOASTER_TIMEOUT_TO_CLOSE}ms`,
    })(node);
  };
 
  const finalMessage = nlToBr && message ? nlToNodes(message) : message;
  const toMuchDuration =
    !duration || duration >= TOASTER_TIMEOUT_TO_SHOW_CLOSE_BUTTON;
 
  return (
    <div
      key={id}
      ref={handleRef}
      className={c(
        styles.toaster_item,
        styles[type],
        className,
        cssClassStatus
      )}
    >
      {renderIconByType(type)}
      {title && <h5 className={c(styles.title)}>{title}</h5>}
      {message && <p className={c(styles.message)}>{finalMessage}</p>}
      {stoppable && toMuchDuration && (
        <button
          onClick={handlePin}
          className={c(styles.pin_button, pinned ? styles.pinned : '')}
        >
          <IconPin className={c(styles.pin_button_icon)} />
          <i>pin</i>
        </button>
      )}
      {toMuchDuration && <ButtonCloser title="close" onClick={handleClose} />}
    </div>
  );
}