feat: add recursive depth calculation for mailbox tree structure
This commit is contained in:
+16
-3
@@ -117,6 +117,16 @@ export function buildMailboxTree(mailboxes: Mailbox[]): MailboxNode[] {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Helper to recursively recalculate depths after tree is built
|
||||||
|
const recalculateDepths = (nodes: MailboxNode[], baseDepth: number) => {
|
||||||
|
for (const node of nodes) {
|
||||||
|
node.depth = baseDepth;
|
||||||
|
if (node.children.length > 0) {
|
||||||
|
recalculateDepths(node.children, baseDepth + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Second pass: build tree structure for own mailboxes
|
// Second pass: build tree structure for own mailboxes
|
||||||
ownMailboxes.forEach(mailbox => {
|
ownMailboxes.forEach(mailbox => {
|
||||||
const node = mailboxMap.get(mailbox.id)!;
|
const node = mailboxMap.get(mailbox.id)!;
|
||||||
@@ -124,14 +134,15 @@ export function buildMailboxTree(mailboxes: Mailbox[]): MailboxNode[] {
|
|||||||
if (mailbox.parentId && mailboxMap.has(mailbox.parentId)) {
|
if (mailbox.parentId && mailboxMap.has(mailbox.parentId)) {
|
||||||
const parent = mailboxMap.get(mailbox.parentId)!;
|
const parent = mailboxMap.get(mailbox.parentId)!;
|
||||||
parent.children.push(node);
|
parent.children.push(node);
|
||||||
node.depth = parent.depth + 1;
|
|
||||||
} else {
|
} else {
|
||||||
// Root level mailbox or orphaned mailbox
|
// Root level mailbox or orphaned mailbox
|
||||||
rootMailboxes.push(node);
|
rootMailboxes.push(node);
|
||||||
node.depth = 0;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Third pass: correctly calculate depths from the root down
|
||||||
|
recalculateDepths(rootMailboxes, 0);
|
||||||
|
|
||||||
// If we have shared mailboxes, create a virtual "Shared Folders" parent
|
// If we have shared mailboxes, create a virtual "Shared Folders" parent
|
||||||
if (sharedMailboxes.length > 0) {
|
if (sharedMailboxes.length > 0) {
|
||||||
// Group shared mailboxes by account
|
// Group shared mailboxes by account
|
||||||
@@ -168,12 +179,14 @@ export function buildMailboxTree(mailboxes: Mailbox[]): MailboxNode[] {
|
|||||||
if (mailbox.parentId && accountMailboxMap.has(mailbox.parentId)) {
|
if (mailbox.parentId && accountMailboxMap.has(mailbox.parentId)) {
|
||||||
const parent = accountMailboxMap.get(mailbox.parentId)!;
|
const parent = accountMailboxMap.get(mailbox.parentId)!;
|
||||||
parent.children.push(node);
|
parent.children.push(node);
|
||||||
node.depth = parent.depth + 1;
|
|
||||||
} else {
|
} else {
|
||||||
accountRootNodes.push(node);
|
accountRootNodes.push(node);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Correctly calculate depths from account root level down
|
||||||
|
recalculateDepths(accountRootNodes, 2);
|
||||||
|
|
||||||
// Create virtual account folder node
|
// Create virtual account folder node
|
||||||
const accountName = accountMailboxes[0]?.accountName || accountId;
|
const accountName = accountMailboxes[0]?.accountName || accountId;
|
||||||
const accountNode: MailboxNode = {
|
const accountNode: MailboxNode = {
|
||||||
|
|||||||
Reference in New Issue
Block a user