import { useContext, useCallback, useState } from 'react'; import { HackleContext } from '../components'; import { InputUtil } from '../utils'; export function useFeature(featureKey: number): boolean { const { hackle, userVersion } = useContext(HackleContext); const getIsOn = useCallback(() => { return hackle?.isFeatureOnSync(featureKey) || false; }, [featureKey, hackle]); const [isOn, setIsOn] = useState(() => { if (!hackle) { return false; } return getIsOn(); }); const currentInput = { key: featureKey, userVersion, }; const [prevInput, setPrevInput] = useState(currentInput); if (!InputUtil.isInputEqual(prevInput, currentInput)) { setPrevInput(currentInput); setIsOn(getIsOn()); } return isOn; }