Build image / build-and-push (push) Successful in 7s
Fixes from the PR #27 adversarial review (considered; not graded). Correctness / error-handling - Modal: focus once on mount and attach the keydown listener once (latest onClose/busy via refs), so a parent re-render (e.g. a background refetch) no longer re-fires focus() and steals it from the input being typed in. - Modal takes a `busy` prop; while a save/delete is in flight, Escape and backdrop clicks don't dismiss it (form/delete modals pass busy=pending), so an action can't be interrupted mid-flight and lose its error. - Form validates the *converted* centimeter values against the server's [1cm, 100m] bounds, so sub-cm / over-100m sizes fail client-side with a clear message instead of a generic server error. - displayFromCm rounds imperial to 0.1 ft so an 8 ft garden (244 cm) prefills as "8", not "8.01". Maintainability - Extracted ui/field.ts (useFieldId + fieldControlClass) shared by TextField/TextArea/Select. Added a `danger` Button variant, used by the delete modal; card edit/delete buttons gained focus-visible rings. - useUpdateGarden takes the id in its mutation variables (no dummy 0 during create). GardensPage shares a close handler; dropped a redundant zod annotation; 409 rebase reuses dimString; renamed `del` -> `deletion`. Verified in a browser: 8 ft round-trips to a prefilled "8"; a sub-cm value is rejected client-side with the modal staying open. tsc clean. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
71 lines
2.6 KiB
TypeScript
71 lines
2.6 KiB
TypeScript
// Unit conversion + display for garden dimensions. The API and DB are metric
|
||
// only (centimeters); imperial is purely a display/entry concern here. A minimal
|
||
// helper set for #8 — #9's geometry lib consolidates/extends it.
|
||
|
||
export type UnitPref = 'metric' | 'imperial'
|
||
|
||
const CM_PER_INCH = 2.54
|
||
const CM_PER_METER = 100
|
||
const INCHES_PER_FOOT = 12
|
||
|
||
// Mirrors the server's garden dimension bounds (service/gardens.go): [1cm, 100m].
|
||
export const MIN_DIMENSION_CM = 1
|
||
export const MAX_DIMENSION_CM = 10_000
|
||
|
||
/** Whether a centimeter dimension is within the server's accepted range. */
|
||
export function isValidDimensionCm(cm: number): boolean {
|
||
return Number.isFinite(cm) && cm >= MIN_DIMENSION_CM && cm <= MAX_DIMENSION_CM
|
||
}
|
||
|
||
/** Feet (+ optional inches) → whole centimeters. */
|
||
export function cmFromFtIn(feet: number, inches = 0): number {
|
||
return Math.round((feet * INCHES_PER_FOOT + inches) * CM_PER_INCH)
|
||
}
|
||
|
||
/** Meters → whole centimeters. */
|
||
export function cmFromMeters(meters: number): number {
|
||
return Math.round(meters * CM_PER_METER)
|
||
}
|
||
|
||
/** A value typed in the given unit (meters, or feet) → centimeters. */
|
||
export function cmFromDisplay(value: number, unit: UnitPref): number {
|
||
return unit === 'imperial' ? cmFromFtIn(value) : cmFromMeters(value)
|
||
}
|
||
|
||
/** Centimeters → a number in the given unit, for prefilling an input field.
|
||
* Rounded so cm-quantization doesn't show through (e.g. 244cm shows as 8 ft,
|
||
* not 8.01): meters to the cm (2 dp), feet to 0.1 ft. */
|
||
export function displayFromCm(cm: number, unit: UnitPref): number {
|
||
if (unit === 'imperial') {
|
||
const feet = cm / CM_PER_INCH / INCHES_PER_FOOT
|
||
return Math.round(feet * 10) / 10
|
||
}
|
||
return Math.round((cm / CM_PER_METER) * 100) / 100
|
||
}
|
||
|
||
/** Centimeters → a human string in the given unit (e.g. "1.22 m" or "4′ 0″"). */
|
||
export function formatCm(cm: number, unit: UnitPref): string {
|
||
if (unit === 'imperial') {
|
||
const totalInches = cm / CM_PER_INCH
|
||
let feet = Math.floor(totalInches / INCHES_PER_FOOT)
|
||
let inches = Math.round(totalInches - feet * INCHES_PER_FOOT)
|
||
if (inches === INCHES_PER_FOOT) {
|
||
feet += 1
|
||
inches = 0
|
||
}
|
||
return `${feet}′ ${inches}″`
|
||
}
|
||
const meters = Math.round((cm / CM_PER_METER) * 100) / 100
|
||
return `${meters} m`
|
||
}
|
||
|
||
/** Centimeters width × height → a human string in the given unit. */
|
||
export function formatDimensions(widthCm: number, heightCm: number, unit: UnitPref): string {
|
||
return `${formatCm(widthCm, unit)} × ${formatCm(heightCm, unit)}`
|
||
}
|
||
|
||
/** The input-field label for a single dimension in the given unit. */
|
||
export function dimensionUnitLabel(unit: UnitPref): string {
|
||
return unit === 'imperial' ? 'ft' : 'm'
|
||
}
|