import { FC, useEffect, useMemo, useState } from 'react'; import { FaChevronLeft, FaChevronRight } from 'react-icons/fa'; import { LocalizeText, WiredFurniType } from '../../../../api'; import { Button, Slider, Text } from '../../../../common'; import { useWired } from '../../../../hooks'; import { WiredConditionBaseView } from './WiredConditionBaseView'; const SOURCE_GROUP_USERS = 0; const SOURCE_GROUP_FURNI = 1; const COMPARISON_OPTIONS = [ 0, 1, 2 ]; const MIN_QUANTITY = 0; const MAX_QUANTITY = 100; const QUANTITY_PATTERN = /^\d*$/; const USER_SOURCES = [ { value: 0, label: 'wiredfurni.params.sources.users.0' }, { value: 200, label: 'wiredfurni.params.sources.users.200' }, { value: 201, label: 'wiredfurni.params.sources.users.201' } ]; const FURNI_SOURCES = [ { value: 0, label: 'wiredfurni.params.sources.furni.0' }, { value: 200, label: 'wiredfurni.params.sources.furni.200' }, { value: 201, label: 'wiredfurni.params.sources.furni.201' } ]; const clampQuantity = (value: number) => { if(isNaN(value)) return MIN_QUANTITY; return Math.max(MIN_QUANTITY, Math.min(MAX_QUANTITY, Math.floor(value))); }; const normalizeSource = (value: number, allowed: number[]) => { return allowed.includes(value) ? value : 0; }; export const WiredConditionSelectionQuantityView: FC<{}> = () => { const { trigger = null, setIntParams = null, setStringParam = null } = useWired(); const [ comparison, setComparison ] = useState(1); const [ quantity, setQuantity ] = useState(0); const [ quantityInput, setQuantityInput ] = useState('0'); const [ sourceGroup, setSourceGroup ] = useState(SOURCE_GROUP_USERS); const [ userSource, setUserSource ] = useState(0); const [ furniSource, setFurniSource ] = useState(0); useEffect(() => { if(!trigger) return; const nextComparison = (trigger.intData.length > 0) ? trigger.intData[0] : 1; const nextQuantity = clampQuantity((trigger.intData.length > 1) ? trigger.intData[1] : 0); const nextSourceGroup = (trigger.intData.length > 2 && trigger.intData[2] === SOURCE_GROUP_FURNI) ? SOURCE_GROUP_FURNI : SOURCE_GROUP_USERS; const nextSourceType = (trigger.intData.length > 3) ? trigger.intData[3] : 0; setComparison(COMPARISON_OPTIONS.includes(nextComparison) ? nextComparison : 1); setQuantity(nextQuantity); setQuantityInput(nextQuantity.toString()); setSourceGroup(nextSourceGroup); setUserSource(nextSourceGroup === SOURCE_GROUP_USERS ? normalizeSource(nextSourceType, USER_SOURCES.map(source => source.value)) : 0); setFurniSource(nextSourceGroup === SOURCE_GROUP_FURNI ? normalizeSource(nextSourceType, FURNI_SOURCES.map(source => source.value)) : 0); }, [ trigger ]); const activeSources = useMemo(() => (sourceGroup === SOURCE_GROUP_FURNI) ? FURNI_SOURCES : USER_SOURCES, [ sourceGroup ]); const activeSource = (sourceGroup === SOURCE_GROUP_FURNI) ? furniSource : userSource; const activeSourceIndex = Math.max(0, activeSources.findIndex(source => source.value === activeSource)); const updateQuantity = (value: number) => { const nextValue = clampQuantity(value); setQuantity(nextValue); setQuantityInput(nextValue.toString()); }; const updateQuantityInput = (value: string) => { if(!QUANTITY_PATTERN.test(value)) return; setQuantityInput(value); if(!value.length) { setQuantity(0); return; } updateQuantity(parseInt(value)); }; const cycleSource = (direction: -1 | 1) => { const nextIndex = (activeSourceIndex + direction + activeSources.length) % activeSources.length; const nextValue = activeSources[nextIndex].value; if(sourceGroup === SOURCE_GROUP_FURNI) setFurniSource(nextValue); else setUserSource(nextValue); }; const save = () => { setIntParams([ comparison, clampQuantity(quantity), sourceGroup, (sourceGroup === SOURCE_GROUP_FURNI) ? furniSource : userSource ]); setStringParam(''); }; return (
{ LocalizeText('wiredfurni.params.comparison_selection') } { COMPARISON_OPTIONS.map(value => { return ( ); }) }
setQuantityInput(clampQuantity(quantity).toString()) } onChange={ event => updateQuantityInput(event.target.value) } /> updateQuantity(event as number) } /> { quantity }
{ LocalizeText('wiredfurni.params.sources.merged.title') }
{ LocalizeText(activeSources[activeSourceIndex].label) }
); };