import { useEffect } from 'react';
import { initFrameAndPoll, sendFrameHeight } from '@newswire/frames';

/*
* Hook to initialize the frames library on a page to use it as an embed.
*/
export default function useFrames(opts = {}) {
  const {
    activate = true
  } = opts;
  useEffect(() => {
    if (!activate) return undefined;
    initFrameAndPoll();
    const timer = setTimeout(() => {
      sendFrameHeight();
    }, 1000);
    return () => clearTimeout(timer);
  }, [activate]);
}