fix(bead-inventory): 补充遗漏提交的 BeadSettlementModal 结算弹窗组件
This commit is contained in:
1 parent
e690194643
commit
1ac846421f
1 file changed
+319
@@ -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<string, string> {
|
||||
const map = new Map<string, string>();
|
||||
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<string, number>();
|
||||
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<HTMLDivElement>(null);
|
||||
const closeBtnRef = useRef<HTMLButtonElement>(null);
|
||||
|
||||
// 换色面板状态
|
||||
const [replaceCode, setReplaceCode] = useState<string | null>(null);
|
||||
// 替换提示
|
||||
const [replaceToast, setReplaceToast] = useState<string | null>(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 (
|
||||
<div className="bd-modal-overlay" onClick={handleOverlayClick}>
|
||||
<div
|
||||
className="bd-modal"
|
||||
ref={modalRef}
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label={t('beads.modal.title')}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{/* 标题区 */}
|
||||
<div className="bd-modal-header">
|
||||
<span className="bd-modal-title">{t('beads.modal.title')}</span>
|
||||
<button
|
||||
type="button"
|
||||
className="bd-modal-close"
|
||||
ref={closeBtnRef}
|
||||
onClick={onClose}
|
||||
aria-label={t('modal.close')}
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 副标题 */}
|
||||
<div className="bd-modal-sub">
|
||||
{brandId} · {t('beads.modal.needed')} {totalNeeded.toLocaleString()} ·{' '}
|
||||
{t('beads.modal.stock')} {totalStock.toLocaleString()}
|
||||
</div>
|
||||
|
||||
{/* 列表 */}
|
||||
<div className="bd-modal-body">
|
||||
<div className="bd-modal-list">
|
||||
{stockList.map((s) => (
|
||||
<div key={s.code} className="bd-modal-row">
|
||||
<span className="bd-modal-swatch" style={{ background: s.hex }} aria-hidden="true" />
|
||||
<span className="bd-modal-code">{s.code}</span>
|
||||
<div className="bd-modal-detail">
|
||||
{s.sufficient ? (
|
||||
<span className="bd-modal-detail-ok">{t('beads.modal.ok')}</span>
|
||||
) : (
|
||||
<>
|
||||
<span>
|
||||
{t('beads.modal.needed')} <b>{s.needed}</b>
|
||||
</span>
|
||||
<span>
|
||||
{t('beads.modal.stock')} <b>{s.stock}</b>
|
||||
</span>
|
||||
<span className="bd-modal-detail-missing">
|
||||
{t('beads.modal.missing')} <b>{s.missing}</b>
|
||||
</span>
|
||||
{onReplace && (
|
||||
<button
|
||||
type="button"
|
||||
className="bd-modal-replace-btn"
|
||||
onClick={() => setReplaceCode(s.code)}
|
||||
>
|
||||
换色
|
||||
</button>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 底部按钮 */}
|
||||
<div className="bd-modal-footer">
|
||||
{hasShortage ? (
|
||||
<span className="bd-modal-footer-note">{t('beads.modal.shortageWarn')}</span>
|
||||
) : (
|
||||
<span className="bd-modal-footer-note" style={{ color: 'var(--success)' }} />
|
||||
)}
|
||||
|
||||
<button type="button" className="bd-modal-btn bd-modal-btn--outline" onClick={onClose}>
|
||||
{t('beads.modal.cancel')}
|
||||
</button>
|
||||
|
||||
{hasShortage && (
|
||||
<button type="button" className="bd-modal-btn bd-modal-btn--success" onClick={onRestock}>
|
||||
{t('beads.modal.restock')}
|
||||
</button>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="bd-modal-btn bd-modal-btn--primary"
|
||||
disabled={hasShortage}
|
||||
onClick={onConfirm}
|
||||
>
|
||||
{t('beads.modal.confirmConsume')}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 换色提示 Toast */}
|
||||
{replaceToast && (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
bottom: '100%',
|
||||
left: '50%',
|
||||
transform: 'translateX(-50%)',
|
||||
marginBottom: 8,
|
||||
padding: '6px 16px',
|
||||
borderRadius: 'var(--radius-md)',
|
||||
background: 'var(--success)',
|
||||
color: 'var(--text-on-dark)',
|
||||
fontSize: 'var(--text-sm)',
|
||||
fontWeight: 600,
|
||||
whiteSpace: 'nowrap',
|
||||
pointerEvents: 'none',
|
||||
}}
|
||||
>
|
||||
{replaceToast}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 换色选择面板 */}
|
||||
{replaceCode !== null && onReplace && (
|
||||
<div className="bd-replace-overlay" onClick={() => setReplaceCode(null)}>
|
||||
<div className="bd-replace-panel" onClick={(e) => e.stopPropagation()}>
|
||||
<div className="bd-replace-header">
|
||||
<span className="bd-replace-title">
|
||||
{t('beads.replace.title')} · {replaceCode}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="bd-replace-close"
|
||||
onClick={() => setReplaceCode(null)}
|
||||
aria-label={t('modal.close')}
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
<div className="bd-replace-grid">
|
||||
{replacePalette.map((c) => (
|
||||
<button
|
||||
key={c.code}
|
||||
type="button"
|
||||
className="bd-replace-color"
|
||||
onClick={() => handleReplacePick(c.code)}
|
||||
>
|
||||
<span className="bd-replace-swatch" style={{ background: c.hex }} />
|
||||
<span className="bd-replace-code">{c.code}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user