deckhearth/lib/deck-detail-grouping.js
varutasu 4f6467f077
refactor(deck): extract grouping lib and stats sidebar (Brief 1) (#137)
Reuse computeDeckStats from deck-builder-stats; add groupDeckCards lib,
DeckDetailStatsSidebar component, and vitest coverage. Fixes stray semicolon
after useEffect. Page drops ~205 lines.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-13 00:44:16 -05:00

42 lines
986 B
JavaScript

/**
* Group deck card rows for the deck detail view.
* @param {Array<object>|undefined} cards
* @param {'type'|'cmc'|'color'|'rarity'|string} groupBy
*/
export function groupDeckCards(cards, groupBy) {
if (!cards) {
return {};
}
return cards.reduce((groups, card) => {
let key;
switch (groupBy) {
case 'type':
key = card.card_type ? card.card_type.split(' — ')[0] : 'Unknown';
break;
case 'cmc':
key = `${card.cmc || 0} Mana`;
break;
case 'color':
try {
const colors = card.colors ? JSON.parse(card.colors) : [];
key = colors.length === 0 ? 'Colorless' : colors.join('');
} catch {
key = 'Colorless';
}
break;
case 'rarity':
key = card.rarity || 'Unknown';
break;
default:
key = 'All Cards';
}
if (!groups[key]) {
groups[key] = [];
}
groups[key].push(card);
return groups;
}, {});
}