import { useContext, useCallback, useState } from 'react'; import { HackleRemoteConfig, EmptyHackleRemoteConfig } from '../models'; import { HackleContext } from '../components'; import { InputUtil } from '../utils'; export function useRemoteConfig(): HackleRemoteConfig { const { hackle, userVersion } = useContext(HackleContext); const getRemoteConfig = useCallback(() => { return hackle?.remoteConfigSync() || new EmptyHackleRemoteConfig(); }, [hackle]); const [remoteConfig, setRemoteConfig] = useState(() => { return getRemoteConfig(); }); const currentInput = { userVersion, }; const [prevInput, setPrevInput] = useState(currentInput); if (!InputUtil.isInputEqual(prevInput, currentInput)) { setPrevInput(currentInput); setRemoteConfig(getRemoteConfig()); } return remoteConfig; }