import { useSelector, useDispatch } from '@tarojs/redux';
import Taro, { useEffect } from '@tarojs/taro';
import { updateSystemInfo } from '@/store/rootReducers/systemInfo';

const useSystemInfo = () => {
  const systemInfo = useSelector(
    (state: { systemInfo: Taro.getSystemInfo.Promised }) => state.systemInfo,
  );
  const dispatch = useDispatch();
  useEffect(() => {
    if (!systemInfo.SDKVersion) {
      Taro.getSystemInfo().then(res => {
        dispatch(updateSystemInfo(res));
      });
    }
  }, []);

  return systemInfo;
};

export default useSystemInfo;
