File size: 776 Bytes
81fc505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export function buildCardTree(cards) {
  const map = new Map();
  const roots = [];
  cards
    .slice()
    .sort((a, b) => a.order - b.order || a.title.localeCompare(b.title))
    .forEach((card) => {
      map.set(card._id, { ...card, children: [] });
    });
  map.forEach((card) => {
    if (card.parentId && map.has(card.parentId)) {
      map.get(card.parentId).children.push(card);
    } else {
      roots.push(card);
    }
  });
  return { roots, map };
}
export function getAncestorChain(cardId, cardMap) {
  if (!cardId || !cardMap.has(cardId)) {
    return [];
  }
  const chain = [];
  let current = cardMap.get(cardId);
  while (current) {
    chain.unshift(current);
    current = current.parentId ? cardMap.get(current.parentId) : null;
  }
  return chain;
}