import { ElementType, useEffect, useRef } from 'react';
import { IndexSignature } from '../../utils';

export const WithHocTitle = (Component: ElementType, title?: string) => {
  return function WithHocTitleComponent({ ...props }: IndexSignature) {
    const { current } = useRef(document.title);

    useEffect(() => {
      title ? document.title = title : document.title = current;
    }, [current]);

    return (
      <Component
        {...props}
      />
    );
  };
};
