import {SettingsData} from '@models';
import {useMemo} from 'react';
import {useLoaderData, useLocation} from 'react-router-dom';

export function usePreloadSettingData(key?: string) {
  const {pathname} = useLocation();
  const preloadKey = key ?? pathname;
  const preloadData = useLoaderData() as Map<string, SettingsData | null>;

  return useMemo(
    function () {
      return preloadData.get(preloadKey);
    },
    [preloadData, preloadKey],
  );
}
