Skip to content

Commit

Permalink
use targetpath when did mount TreeItemLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
reiji-h committed Jan 9, 2025
1 parent f62e0ed commit f3b4769
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions apps/app/src/client/components/TreeItem/TreeItemLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export const TreeItemLayout = (props: TreeItemLayoutProps): JSX.Element => {
showAlternativeContent,
} = props;

const { page, children } = itemNode;
const { page } = itemNode;

const [currentChildren, setCurrentChildren] = useState<ItemNode[]>(children);
const [currentChildren, setCurrentChildren] = useState<ItemNode[]>([]);
const [isOpen, setIsOpen] = useState(_isOpen);

const { data } = useSWRxPageChildren(isOpen ? page._id : null);
Expand Down Expand Up @@ -75,17 +75,18 @@ export const TreeItemLayout = (props: TreeItemLayoutProps): JSX.Element => {
const hasDescendants = descendantCount > 0 || isChildrenLoaded;

const hasChildren = useCallback((): boolean => {
return page.path != null && targetPath.startsWith(page.path);
}, [page, targetPath]);
return currentChildren != null && currentChildren.length > 0;
}, [currentChildren]);

const onClickLoadChildren = useCallback(() => {
setIsOpen(!isOpen);
}, [isOpen]);

// didMount
useEffect(() => {
if (hasChildren()) setIsOpen(true);
}, [hasChildren]);
const isPathToTarget = page.path != null && targetPath.startsWith(page.path) && targetPath !== page.path; // Target Page does not need to be opened
if (hasChildren() || isPathToTarget) setIsOpen(true);
}, [hasChildren, targetPath, page.path]);

/*
* When swr fetch succeeded
Expand Down

0 comments on commit f3b4769

Please sign in to comment.