feat(client): let people actually type a bag weight limit

The column, the contract, the API route and its tests have all been there
since v2.9.0 — there was simply no field to enter a limit in, so the only
way to record one was to put it in the bag name, where nothing can act on
it. Both the desktop bag card and the mobile bag row now take a limit in
kilograms (how airlines state them) and store it in grams. Emptying the
field clears the limit instead of writing a zero.

While adding it: the fill-bar formula existed in three places, and the one
in the bag modal had been copied without the limit, so a limit set on a
window narrower than xl did nothing. It now lives once in
packingListPanel.helpers as bagFillPct, next to itemWeight.
This commit is contained in:
Maurice
2026-08-03 20:52:59 +02:00
committed by Maurice
parent 628f4e78cd
commit e25a1276dc
8 changed files with 215 additions and 12 deletions
@@ -7,6 +7,7 @@ import { avatarSrc } from '../../../../utils/avatarSrc'
import type { PackingBag, PackingItem, TripMember } from '../../../../types'
import type { TripPlanner } from '../MTripShell'
import { formatWeight, packingItemWeight } from './listsModel'
import { bagFillPct } from '../../../../components/Packing/packingListPanel.helpers'
export interface MBagsSheetProps {
planner: TripPlanner
@@ -37,6 +38,8 @@ export default function MBagsSheet({
const unassigned = items.filter(i => !i.bag_id)
const unassignedWeight = unassigned.reduce((s, i) => s + packingItemWeight(i), 0)
const totalWeight = items.reduce((s, i) => s + packingItemWeight(i), 0)
// Reference for bags without a limit of their own — computed once instead of per bag.
const heaviestBagWeight = Math.max(...bags.map(b => items.filter(i => i.bag_id === b.id).reduce((s, i) => s + packingItemWeight(i), 0)), 1)
const submitNewBag = () => {
if (!newBagName.trim()) return
@@ -53,9 +56,7 @@ export default function MBagsSheet({
{bags.map(bag => {
const bagItems = items.filter(i => i.bag_id === bag.id)
const bagWeight = bagItems.reduce((s, i) => s + packingItemWeight(i), 0)
const maxWeight = bag.weight_limit_grams
|| Math.max(...bags.map(b => items.filter(i => i.bag_id === b.id).reduce((s, i) => s + packingItemWeight(i), 0)), 1)
const pct = Math.min(100, Math.round((bagWeight / maxWeight) * 100))
const pct = bagFillPct(bagWeight, bag.weight_limit_grams, heaviestBagWeight)
return (
<BagRow
key={bag.id}
@@ -159,6 +160,24 @@ function BagRow({ planner, bag, itemCount, weight, pct, tripMembers, canEdit, on
setEditingName(false)
}
// Limits are entered in kg — that is how airlines state them — and stored in grams.
const limitToInput = (grams?: number | null) => (grams ? String(grams / 1000) : '')
const [editingLimit, setEditingLimit] = useState(false)
const [limitVal, setLimitVal] = useState(limitToInput(bag.weight_limit_grams))
const saveLimit = () => {
setEditingLimit(false)
const raw = limitVal.trim().replace(',', '.')
if (raw === '') {
if (bag.weight_limit_grams != null) onUpdate({ weight_limit_grams: null })
return
}
const kg = Number(raw)
if (!Number.isFinite(kg) || kg <= 0) { setLimitVal(limitToInput(bag.weight_limit_grams)); return }
const grams = Math.round(kg * 1000)
if (grams !== bag.weight_limit_grams) onUpdate({ weight_limit_grams: grams })
}
return (
<div className="mb-4">
<div className="mb-1 flex items-center gap-[8px]">
@@ -178,7 +197,34 @@ function BagRow({ planner, bag, itemCount, weight, pct, tripMembers, canEdit, on
{bag.name}
</button>
)}
<span className="font-geist text-[0.71875rem] font-medium text-m-faint">{formatWeight(weight)}</span>
<span className="flex flex-none items-center gap-1 font-geist text-[0.71875rem] font-medium text-m-faint">
{formatWeight(weight)}
{editingLimit && canEdit ? (
<>
<span>/</span>
<input
type="text"
autoFocus
inputMode="decimal"
value={limitVal}
aria-label={t('packing.bagLimit')}
onChange={e => setLimitVal(e.target.value)}
onBlur={saveLimit}
onKeyDown={e => { if (e.key === 'Enter') saveLimit(); if (e.key === 'Escape') { setLimitVal(limitToInput(bag.weight_limit_grams)); setEditingLimit(false) } }}
className="w-9 border-b border-[color:var(--m-rowbr)] bg-transparent text-right text-m-ink outline-none"
/>
<span>kg</span>
</>
) : bag.weight_limit_grams ? (
<button type="button" onClick={() => canEdit && setEditingLimit(true)} aria-label={t('packing.bagLimit')}>
/ {(bag.weight_limit_grams / 1000).toFixed(1)} kg
</button>
) : canEdit ? (
<button type="button" onClick={() => setEditingLimit(true)} className="underline decoration-dotted">
{t('packing.setBagLimit')}
</button>
) : null}
</span>
{canEdit && (
<button type="button" onClick={onDelete} aria-label={t('common.delete')} className="flex flex-none items-center text-m-faint">
<X size={14} strokeWidth={2} />