import {getScreenListData} from '@apis';
import {useContextSection} from '@hooks';
import {useQuery} from '@tanstack/react-query';
import {context} from './context';
import {deepEqual} from '@utils';
import {useEffect} from 'react';

export function useQueryList() {
  const dispatch = useContextSection(context, state => state[1]);

  const {data} = useQuery({
    queryKey: [getScreenListData.name],
    async queryFn({signal}) {
      const data = await getScreenListData(signal);

      if (data.msg === '200') {
        return data.data;
      }

      return void 0;
    },
    structuralSharing(oldData, newData) {
      return deepEqual(oldData, newData) ? oldData : newData;
    },
    refetchInterval: 1000 * 60 * 10,
  });

  useEffect(
    function () {
      dispatch(data);
    },
    [data, dispatch],
  );
}
