Skip to content

🐛(tree-view) enhance useTree hook to manage children references #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PanchoutNathan
Copy link
Collaborator

This update improves the useTree hook by ensuring that children references in the tree data are correctly replaced. It adds logic to handle cases where children may have already been loaded without triggering the loadChildren function. The childrenCount is now calculated based on whether the children have been loaded, and the hasLoadedChildren flag is set appropriately. This change enhances the efficiency of tree data management.

This update improves the useTree hook by ensuring that children
references in the tree data are correctly replaced. It adds logic to
handle cases where children may have already been loaded without
triggering the loadChildren function. The childrenCount is now
calculated based on whether the children have been loaded, and the
hasLoadedChildren flag is set appropriately. This change enhances the
efficiency of tree data management.
@PanchoutNathan PanchoutNathan self-assigned this Apr 7, 2025
@PanchoutNathan PanchoutNathan requested a review from NathanVss April 7, 2025 11:32
Comment on lines +253 to +287
/**
* The tree might have already been loaded without going through loadChildren. (if we loaded the entire tree or part of the tree for example)
* In this case, hasLoadedChildren is not set but the children are loaded.
*/
const hasLoadedChildren =
item.value.hasLoadedChildren ||
item.children?.length > 0 ||
(item.value.children && item.value.children.length > 0);

item.value.children = item.children.map((child) => child.value);

const childrenCount = hasLoadedChildren
? item.children.length
: item.value.childrenCount ?? 0;

item.value.childrenCount = childrenCount;

// If the children have been loaded but the hasLoadedChildren is not set, set it to true.
if (hasLoadedChildren && !item.value.hasLoadedChildren) {
item.value.hasLoadedChildren = true;
}
// Recursively process children

replaceChildrenReferences(item.children);
} else {
// Same as above
const hasLoadedChildren =
item.value.hasLoadedChildren ||
(item.value.children && item.value.children.length > 0);

const childrenCount = hasLoadedChildren
? 0
: item.value.childrenCount;

item.value.children = [];
item.value.childrenCount = childrenCount;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you find a way to avoid having to two code blocks logic that seems to do the same thing inside the "if" and the "else"?

It seems that the logic is a bit duplicated, but I'm not sure if I'm missing a point, maybe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants