From 1ac846421f50ffd0bafd4c3164da206947a9a7ba Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Thu, 20 Aug 2026 18:30:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(bead-inventory):=20=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E9=81=97=E6=BC=8F=E6=8F=90=E4=BA=A4=E7=9A=84=20BeadSettlementM?= =?UTF-8?q?odal=20=E7=BB=93=E7=AE=97=E5=BC=B9=E7=AA=97=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/BeadSettlementModal.tsx | 319 ++++++++++++++++++ 1 file changed, 319 insertions(+) create mode 100644 perler-lab/src/components/BeadSettlementModal.tsx diff --git a/perler-lab/src/components/BeadSettlementModal.tsx b/perler-lab/src/components/BeadSettlementModal.tsx new file mode 100644 index 0000000..ed7731d --- /dev/null +++ b/perler-lab/src/components/BeadSettlementModal.tsx @@ -0,0 +1,319 @@ +/** + * BeadSettlementModal — 豆仓结算弹窗 + * + * 展示图纸用量 vs 库存缺口,提供确认扣豆、一键补货、换色操作。 + * 用于下载拦截(mode=download)和生成拦截(mode=generate)。 + */ + +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { useT } from '../i18n'; +import { useBeadInventory } from '../lib/useBeadInventory'; +import { getBrandPalette, BRANDS } from '../lib/brand-palettes'; +import type { BeadUsage } from '../lib/bead-inventory'; + +// ─── 类型 ──────────────────────────────────────────────────────────────── + +export interface BeadSettlementModalProps { + open: boolean; + /** 图纸所需色号用量列表(已按 code 聚合)。 */ + entries: readonly BeadUsage[]; + /** 当前品牌 ID(MARD / COCO / 漫漫 / 盼盼 / 咪小窝),用于换色选择。 */ + brandId: string; + onClose: () => void; + /** 确认扣豆(忽略缺口,继续下载/生成)。 */ + onConfirm: () => void; + /** 一键补货(补齐缺口,然后继续)。 */ + onRestock: () => void; + /** + * 换色回调:将图纸中的 oldCode 替换为 newCode。 + * 不传则隐藏 [换色] 按钮。 + */ + onReplace?: (oldCode: string, newCode: string) => void; +} + +interface EntryStock { + code: string; + needed: number; + stock: number; + missing: number; + hex: string; + sufficient: boolean; +} + +// ─── 帮助函数 ──────────────────────────────────────────────────────────── + +/** 构建快速 code → hex 查询表(来自所有 Pattern 品牌)。 */ +function buildHexMap(): ReadonlyMap { + const map = new Map(); + for (const brand of BRANDS) { + for (const c of getBrandPalette(brand.id)) { + if (!map.has(c.code)) map.set(c.code, c.hex); + } + } + return map; +} + +const HEX_MAP = buildHexMap(); + +/** 聚合 entries 按 code 去重。 */ +function aggregateEntries(entries: readonly BeadUsage[]): BeadUsage[] { + const map = new Map(); + for (const e of entries) { + map.set(e.code, (map.get(e.code) ?? 0) + e.count); + } + return [...map.entries()].map(([code, count]) => ({ code, count })); +} + +// ─── 组件 ──────────────────────────────────────────────────────────────── + +export default function BeadSettlementModal({ + open, + entries, + brandId, + onClose, + onConfirm, + onRestock, + onReplace, +}: BeadSettlementModalProps) { + const { t } = useT(); + const { inventory } = useBeadInventory(); + const modalRef = useRef(null); + const closeBtnRef = useRef(null); + + // 换色面板状态 + const [replaceCode, setReplaceCode] = useState(null); + // 替换提示 + const [replaceToast, setReplaceToast] = useState(null); + + // 焦点管理:打开时聚焦关闭按钮 + useEffect(() => { + if (open && closeBtnRef.current) { + closeBtnRef.current.focus(); + } + }, [open]); + + // ESC 关闭 + useEffect(() => { + if (!open) return; + const handleKey = (e: KeyboardEvent) => { + if (e.key === 'Escape') { + setReplaceCode(null); + onClose(); + } + }; + window.addEventListener('keydown', handleKey); + return () => window.removeEventListener('keydown', handleKey); + }, [open, onClose]); + + // 聚合 entries 并计算库存 + const aggregated = useMemo(() => aggregateEntries(entries), [entries]); + const bottles = inventory?.bottles ?? {}; + + const stockList: EntryStock[] = useMemo( + () => + aggregated.map((e) => { + const stock = bottles[e.code] ?? 0; + const missing = Math.max(0, e.count - stock); + return { + code: e.code, + needed: e.count, + stock, + missing, + hex: HEX_MAP.get(e.code) ?? '#ccc', + sufficient: stock >= e.count, + }; + }), + [aggregated, bottles], + ); + + const hasShortage = stockList.some((s) => s.missing > 0); + const totalNeeded = aggregated.reduce((s, e) => s + e.count, 0); + const totalStock = aggregated.reduce((s, e) => s + (bottles[e.code] ?? 0), 0); + + // 换色选择面板 + const replacePalette = useMemo( + () => { + const brand = BRANDS.find((b) => b.id === brandId); + if (!brand) return []; + return getBrandPalette(brand.id); + }, + [brandId], + ); + + const handleReplacePick = useCallback( + (newCode: string) => { + if (!replaceCode || !onReplace) return; + onReplace(replaceCode, newCode); + setReplaceToast(t('beads.replace.done', { oldCode: replaceCode, newCode })); + setReplaceCode(null); + window.setTimeout(() => setReplaceToast(null), 2500); + }, + [replaceCode, onReplace, t], + ); + + // 遮罩点击关闭 + const handleOverlayClick = useCallback( + (e: React.MouseEvent) => { + if (e.target === e.currentTarget) onClose(); + }, + [onClose], + ); + + if (!open) return null; + + return ( +
+
e.stopPropagation()} + > + {/* 标题区 */} +
+ {t('beads.modal.title')} + +
+ + {/* 副标题 */} +
+ {brandId} · {t('beads.modal.needed')} {totalNeeded.toLocaleString()} ·{' '} + {t('beads.modal.stock')} {totalStock.toLocaleString()} +
+ + {/* 列表 */} +
+
+ {stockList.map((s) => ( +
+
+ ))} +
+
+ + {/* 底部按钮 */} +
+ {hasShortage ? ( + {t('beads.modal.shortageWarn')} + ) : ( + + )} + + + + {hasShortage && ( + + )} + + +
+ + {/* 换色提示 Toast */} + {replaceToast && ( +
+ {replaceToast} +
+ )} +
+ + {/* 换色选择面板 */} + {replaceCode !== null && onReplace && ( +
setReplaceCode(null)}> +
e.stopPropagation()}> +
+ + {t('beads.replace.title')} · {replaceCode} + + +
+
+ {replacePalette.map((c) => ( + + ))} +
+
+
+ )} +
+ ); +} \ No newline at end of file