Files
TREK/client/src/components/Packing/PackingListPanelBagModal.tsx
T

98 lines
4.6 KiB
TypeScript

import { useId } from 'react'
import { focusRevealedEditor } from '../shared/dialogFocus'
import { Backpack, Package, Plus } from 'lucide-react'
import { DialogButton, DialogFooter, DialogHeader, DialogShell, DialogTile, FooterSpacer, NEUTRAL_TINT } from '../shared/DialogShell'
import { CardHead, INPUT, PANEL } from '../shared/dialogParts'
import type { PackingState } from './usePackingListPanel'
import { bagFillPct, bagLoadSummary, packedWeight, perPersonLoads } from './packingListPanel.helpers'
import { BagCard } from './PackingListPanelBagCard'
import { PackingWeightSummary } from './PackingWeightSummary'
export function BagModal(S: PackingState) {
const labelId = useId()
const {
setShowBagModal, 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). The
// WEIGHTS no longer do. A bag's load is the bag's, whoever packed it and
// whether or not you may see the items, so it comes from the server (#2191).
const { myItems, bagItemsOf, bagWeightOf, heaviestBagWeight, unassigned, unassignedWeight, totalWeight } =
bagLoadSummary(bags, items, currentUserId, unassignedWeightGrams, serverWeightsFresh)
const close = () => setShowBagModal(false)
const resetNewBag = () => { setShowAddBag(false); setNewBagName('') }
return (
<DialogShell
onClose={close}
labelledBy={labelId}
width="narrow"
header={(
<DialogHeader
tile={<DialogTile><Backpack size={20} className="text-content-muted" /></DialogTile>}
tint={NEUTRAL_TINT}
labelId={labelId}
onClose={close}
title={t('packing.bags')}
/>
)}
footer={(
<DialogFooter>
<FooterSpacer />
<DialogButton onClick={close}>{t('common.close')}</DialogButton>
{canEdit && (
showAddBag
? <DialogButton variant="primary" onClick={handleCreateBag} disabled={!newBagName.trim()}>{t('packing.addBag')}</DialogButton>
: <DialogButton variant="primary" onClick={() => setShowAddBag(true)} icon={<Plus size={16} />}>{t('packing.addBag')}</DialogButton>
)}
</DialogFooter>
)}
>
{bags.map(bag => {
const bagItems = bagItemsOf(bag)
const bagWeight = bagWeightOf(bag)
const pct = bagFillPct(bagWeight, bag.weight_limit_grams, heaviestBagWeight)
return (
<BagCard key={bag.id} bag={bag} bagItems={bagItems} totalWeight={bagWeight} pct={pct} tripId={tripId} tripMembers={tripMembers} canEdit={canEdit} onDelete={() => handleDeleteBag(bag.id)} onUpdate={handleUpdateBag} onSetMembers={handleSetBagMembers} t={t} />
)
})}
{(unassigned.length > 0 || unassignedWeight > 0) && (
<section className={PANEL}>
<CardHead icon={<Package size={16} />} title={t('packing.noBag')} />
<div className="flex items-center justify-between gap-2 rounded-[10px] bg-surface-input px-3 py-2 text-body tabular-nums text-content-muted">
<span>{unassigned.length} {t('admin.packingTemplates.items', { count: unassigned.length })}</span>
<span>{unassignedWeight >= 1000 ? `${(unassignedWeight / 1000).toFixed(1)} kg` : `${unassignedWeight} g`}</span>
</div>
</section>
)}
{/* Total, packed share and who carries what (#1131) */}
<div className="overflow-hidden rounded-[14px] border border-edge-faint bg-surface-input">
<PackingWeightSummary t={t} topRule={false}
// Same rule as the rows above it: a grand total mixing true bag
// weights with a per-viewer remainder would be worse than either.
total={totalWeight}
packed={packedWeight(myItems)}
people={perPersonLoads(bags, bagWeightOf)} />
</div>
{canEdit && showAddBag && (
<div className={PANEL}>
<CardHead icon={<Plus size={16} />} title={t('packing.addBag')} />
<input aria-label={t('packing.bagName')} ref={focusRevealedEditor} value={newBagName}
onChange={e => setNewBagName(e.target.value)}
onKeyDown={e => {
if (e.key === 'Enter' && newBagName.trim()) void handleCreateBag()
if (e.key === 'Escape') { e.preventDefault(); resetNewBag() }
}}
placeholder={t('packing.bagName')}
className={INPUT}
/>
</div>
)}
</DialogShell>
)
}