UNPKG

@livelike/react-native

Version:

LiveLike React Native package

57 lines 2.39 kB
import { WidgetKind } from '@livelike/javascript'; import { useMemo } from 'react'; import { useIsWidgetDisabled } from './useIsWidgetDisabled'; import { useWidget } from './useWidget'; import { useWidgetInteractions } from './useWidgetInteractions'; import { useWidgetOptions } from './useWidgetOptions'; const NUMBER_PREDICTION_FOLLOW_UP_WIDGET_KIND = [WidgetKind.IMAGE_NUMBER_PREDICTION_FOLLOW_UP, WidgetKind.TEXT_NUMBER_PREDICTION_FOLLOW_UP]; export function useNumberPredictionWidgetOption(_ref) { let { widgetId, optionIndex, numberOption } = _ref; const widgetOptions = useWidgetOptions({ widgetId }); const isWidgetOptionDisabled = useIsWidgetDisabled({ widgetId }); const widgetInteractions = useWidgetInteractions({ widgetId }); const widget = useWidget({ widgetId }); const widgetOption = widgetOptions === null || widgetOptions === void 0 ? void 0 : widgetOptions[optionIndex]; const widgetOptionId = widgetOption === null || widgetOption === void 0 ? void 0 : widgetOption.id; const interactedNumberOption = useMemo(() => { var _widgetInteractions$; if (!(widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length) || !((_widgetInteractions$ = widgetInteractions[0]) !== null && _widgetInteractions$ !== void 0 && (_widgetInteractions$ = _widgetInteractions$.votes) !== null && _widgetInteractions$ !== void 0 && _widgetInteractions$.length)) { return numberOption; } const widgetInteraction = widgetInteractions[0]; const currentVote = widgetInteraction.votes.find(option => option.option_id === widgetOptionId); if (!currentVote) { return numberOption; } return { optionId: currentVote.id, number: currentVote.number }; }, [widgetInteractions, optionIndex, numberOption, widgetOptionId]); const widgetKind = widget.kind; const showWidgetResult = NUMBER_PREDICTION_FOLLOW_UP_WIDGET_KIND.includes(widgetKind); if (!widgetOptions || !widgetOption) { return undefined; } const isCorrect = widgetOption.correct_number === (interactedNumberOption === null || interactedNumberOption === void 0 ? void 0 : interactedNumberOption.number); return { isCorrect, widgetOption, showWidgetResult, interactedNumberOption, isWidgetOptionDisabled }; } //# sourceMappingURL=useNumberPredictionWidgetOption.js.map