import {useCallback, useEffect, useState} from 'react';
import {HuDataMember} from '@modules/chats/interfaces';

import {getUserInfo} from '../services';

export const useUserInfo = (userId: string | number) => {
  const [user, setUser] = useState<Nullable<HuDataMember>>(null);

  const fetchUserInfo = useCallback(async () => {
    try {
      const [userData] = await getUserInfo({
        user: Number(userId),
      });
      setUser(userData);
    } catch (error) {
      // Do nothing
    }
  }, [userId]);

  useEffect(() => {
    fetchUserInfo();
  }, [fetchUserInfo]);

  return {data: user, refetch: fetchUserInfo};
};
