import { useState, useEffect } from 'react';
import socketIOClient from 'socket.io-client';
import sailsIOClient, { Socket } from 'sails.io.js';

const useSocket = (controlled) => {
  const [state, setState] = useState<Socket>();

  useEffect(() => {
    if (controlled) {
      const io = sailsIOClient(socketIOClient);
      io.sails.url = '/';
      io.sails.reconnection = true;
      setState(io.socket);
    }
  }, [controlled]);

  return state;
};

export default useSocket;
