import { isDegreeMeasurement } from 'config/measurements.mjs' import { measurementAsMm, formatMm, measurementAsUnits } from 'shared/utils.mjs' import { Collapse } from 'shared/components/collapse.mjs' import { PlusIcon, EditIcon } from 'shared/components/icons.mjs' import { NumberInput } from 'shared/components/workbench/menus/shared/inputs.mjs' import { useState, useCallback } from 'react' export const ns = ['account'] const Mval = ({ m, val = false, imperial = false, className = '' }) => val ? ( isDegreeMeasurement(m) ? ( {val}° ) : ( ) ) : null const heightClasses = { 2: 'h-12', 4: 'h-10', 8: 'h-8', 16: 'h-6', 32: 'h-4', } const fractionClasses = 'w-full border-2 border-solid border-base-100 hover:border-secondary bg-secondary rounded bg-opacity-50 hover:bg-opacity-100' const FractionButtons = ({ t, fraction }) => (
{t('fractions')}
{Array.from({ length: 31 }, (_null, i) => { let denom = 32 let num = i + 1 for (let n = 4; n > 0; n--) { const fac = Math.pow(2, n) if (num % fac === 0) { denom = 32 / fac num = num / fac break } } return (
) export const MeasieRow = (props) => { const { t, m, mset } = props const isSet = typeof mset.measies?.[m] !== 'undefined' return (
{t(m)}
{isSet ? ( ) : (
)} } toggle={isSet ? : } toggleClasses={`btn ${isSet ? 'btn-secondary' : 'btn-neutral bg-opacity-50'}`} > ) } export const MeasieInput = ({ t, m, mset, backend, refresh, toast, children, onUpdate, startLoading = () => null, stopLoading = () => null, }) => { const isDegree = isDegreeMeasurement(m) const factor = isDegree ? 1 : mset.imperial ? 25.4 : 10 const units = mset.imperial ? 'imperial' : 'metric' const [val, setVal] = useState(() => { const measie = mset.measies?.[m] if (!measie) return '' if (isDegree) return measie return measurementAsUnits(measie, units) }) const [valid, setValid] = useState(null) // Update onChange const update = useCallback( (validVal, rawVal) => { setValid(validVal) setVal(validVal || rawVal) if (validVal && typeof onUpdate === 'function') { const useVal = isDegree ? validVal : measurementAsMm(validVal, units) onUpdate(m, useVal) } }, [isDegree, setValid, setVal, onUpdate, units] ) const save = async () => { // FIXME startLoading() const measies = {} measies[m] = isDegree ? val : measurementAsMm(val, units) const result = await backend.updateSet(mset.id, { measies }) if (result.success) { refresh() toast.for.settingsSaved() } else toast.for.backendError() stopLoading() } const fraction = (i, base) => update(Math.floor(('' + val).split(/[\s\.]/)[0]) + i / base) if (!m) return null return (
{mset.imperial && (
{!isDegree && }
)}
{backend && ( )}
) }