import { Add, Remove } from '@mui/icons-material' import { Box, IconButton, Input, Option, Select } from '@mui/joy' import { useEffect, useState } from 'react' import { secondsToValueAndUnit, TIME_UNITS, valueAndUnitToSeconds, } from '../../utils/DurationUtils' /** * A reusable duration picker: [−] number [+] unit-select * * Props: * value – duration in seconds (positive integer) * onChange – called with new duration in seconds * size – Joy UI size ('sm' | 'md') * minValue – minimum numeric value (default 1) */ const DurationInput = ({ minValue = 1, onChange, size = 'md', value }) => { const derived = value != null && value >= 0 ? secondsToValueAndUnit(value) : { value: 1, unit: 'h' } const [displayValue, setDisplayValue] = useState(derived.value) const [unit, setUnit] = useState(derived.unit) useEffect(() => { if (value != null && value >= 0) { const { unit: u, value: v } = secondsToValueAndUnit(value) setDisplayValue(v) setUnit(u) } }, [value]) const emit = (v, u) => { onChange(valueAndUnitToSeconds(v, u)) } const handleDecrement = () => { const next = Math.max(minValue, displayValue - 1) setDisplayValue(next) emit(next, unit) } const handleIncrement = () => { const next = displayValue + 1 setDisplayValue(next) emit(next, unit) } return ( { const v = Math.max(minValue, parseInt(e.target.value) || minValue) setDisplayValue(v) emit(v, unit) }} /> ) } export default DurationInput