import { Plus } from 'lucide-react' import type { PackingState } from './usePackingListPanel' import { bagFillPct, bagTotalWeight, countsTowardsMyLoad, unassignedTotalWeight } from './packingListPanel.helpers' import { BagCard } from './PackingListPanelBagCard' export function BagSidebar(S: PackingState) { const { t, bags, items, tripId, tripMembers, canEdit, currentUserId, handleDeleteBag, handleUpdateBag, handleSetBagMembers, showAddBag, setShowAddBag, newBagName, setNewBagName, handleCreateBag, unassignedWeightGrams, serverWeightsFresh, } = S // The ITEM LISTS still describe what you are carrying — an item someone shared // with you stays in your list, but they are the one bringing it (#1767). const myItems = items.filter(i => countsTowardsMyLoad(i, currentUserId)) // The WEIGHTS no longer do: a bag's load is the bag's, whoever packed it (#2191). const bagWeightOf = (bag: typeof bags[number]) => bagTotalWeight(bag, myItems.filter(i => i.bag_id === bag.id), serverWeightsFresh) // Reference for bags without a limit of their own — computed once instead of per bag. const heaviestBagWeight = Math.max(...bags.map(bagWeightOf), 1) return (
{t('packing.bags')}
{bags.map(bag => { const bagItems = myItems.filter(i => i.bag_id === bag.id) const totalWeight = bagWeightOf(bag) const pct = bagFillPct(totalWeight, bag.weight_limit_grams, heaviestBagWeight) return ( handleDeleteBag(bag.id)} onUpdate={handleUpdateBag} onSetMembers={handleSetBagMembers} t={t} compact /> ) })} {/* Unassigned */} {(() => { const unassigned = myItems.filter(i => !i.bag_id) const unassignedWeight = unassignedTotalWeight(unassignedWeightGrams, unassigned, serverWeightsFresh) // Shown whenever there is weight to account for, even with no visible // items — the grand total counts it either way (#2191). if (unassigned.length === 0 && unassignedWeight === 0) return null return (
{t('packing.noBag')} {unassignedWeight >= 1000 ? `${(unassignedWeight / 1000).toFixed(1)} kg` : `${unassignedWeight} g`}
{unassigned.length} {t('admin.packingTemplates.items')}
) })()} {/* Total */}
{t('packing.totalWeight')} {(() => { // Same rule as the rows above it (#2191). const w = bags.reduce((s, b) => s + bagWeightOf(b), 0) + unassignedTotalWeight(unassignedWeightGrams, myItems.filter(i => !i.bag_id), serverWeightsFresh) return w >= 1000 ? `${(w / 1000).toFixed(1)} kg` : `${w} g` })()}
{/* Add bag */} {canEdit && (showAddBag ? (
setNewBagName(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') handleCreateBag(); if (e.key === 'Escape') { setShowAddBag(false); setNewBagName('') } }} placeholder={t('packing.bagName')} style={{ flex: 1, padding: '5px 8px', borderRadius: 8, border: '1px solid var(--border-primary)', fontSize: 'calc(11px * var(--fs-scale-caption, 1))', fontFamily: 'inherit', outline: 'none' }} />
) : ( ))}
) }