From 866a470750a11c7260f52ed568275f89bea06a54 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Mon, 27 Jul 2026 19:34:47 -0400 Subject: [PATCH] feat: cap Amount input max to stored amount and remaining capacity - Withdraw/Transfer tabs: max = currently stored amount of selected resource - Deposit tab: max = remaining capacity divided by resource massPerUnit - Amount auto-clamps when resource selection changes - Show 'Max: X' hint below each amount input --- .../frontend/storage/ManageStorageDialog.tsx | 85 +++++++++++++++++-- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/src/components/frontend/storage/ManageStorageDialog.tsx b/src/components/frontend/storage/ManageStorageDialog.tsx index 49e97d1..7cc0541 100644 --- a/src/components/frontend/storage/ManageStorageDialog.tsx +++ b/src/components/frontend/storage/ManageStorageDialog.tsx @@ -114,6 +114,8 @@ export function ManageStorageDialog({ return total + (resource.massPerUnit ?? 0) * entry.amount; }, 0); + const remainingMass = maxMass ? maxMass - currentMass : null; + const findResource = (id: string): Resource | undefined => allResources.find((r) => r.id === Number(id)); @@ -130,6 +132,32 @@ export function ManageStorageDialog({ return { resource, amount: entry.amount }; }; + const getDepositMax = (): number | undefined => { + const resource = findResource(depositResourceId); + if (!resource) return undefined; + if (remainingMass === null) return undefined; + const massPerUnit = resource.massPerUnit ?? 0; + if (massPerUnit <= 0) return undefined; + return Math.floor(remainingMass / massPerUnit); + }; + + const getWithdrawMax = (): number | undefined => { + const stored = findStoredResource(withdrawResourceId); + return stored?.amount; + }; + + const getTransferMax = (): number | undefined => { + const stored = findStoredResource(transferResourceId); + return stored?.amount; + }; + + const clampAmount = (value: string, max: number | undefined): string => { + const num = Number(value); + if (isNaN(num) || num < 1) return "1"; + if (max !== undefined && num > max) return String(max); + return value; + }; + const getPreviewMass = (tab: "deposit" | "withdraw" | "transfer"): number | null => { if (!maxMass) return null; @@ -331,7 +359,20 @@ export function ManageStorageDialog({
- { + setDepositResourceId(val); + const res = findResource(val); + if (res) { + const massPerUnit = res.massPerUnit ?? 0; + const depositMax = remainingMass !== null && massPerUnit > 0 + ? Math.floor(remainingMass / massPerUnit) + : undefined; + setDepositAmount(clampAmount(depositAmount, depositMax)); + } + }} + > @@ -351,9 +392,15 @@ export function ManageStorageDialog({ setDepositAmount(e.target.value)} + onChange={(e) => setDepositAmount(clampAmount(e.target.value, getDepositMax()))} /> + {getDepositMax() !== undefined && ( + + Max: {getDepositMax()!.toLocaleString()} (limited by remaining capacity) + + )}
{renderCapacityPreview("deposit")}