diff --git a/components/CollectionListing.tsx b/components/CollectionListing.tsx index ab5848c..a428a10 100644 --- a/components/CollectionListing.tsx +++ b/components/CollectionListing.tsx @@ -13,18 +13,23 @@ import useCollectionStore from "@/store/collections"; import { Collection } from "@prisma/client"; import Link from "next/link"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; +import { useRouter } from "next/router"; interface ExtendedTreeItem extends TreeItem { data: Collection; } -const DragDropWithNestingTree = () => { +const CollectionListing = () => { const [tree, setTree] = useState(); const { collections } = useCollectionStore(); + const router = useRouter(); + + const currentPath = router.asPath; + useEffect(() => { - const initialTree = buildTreeFromCollections(collections); + const initialTree = buildTreeFromCollections(collections, router); collections[0] && setTree(initialTree); }, [collections]); @@ -55,65 +60,66 @@ const DragDropWithNestingTree = () => { }; if (!tree) { - return
Loading...
; - } - - return ( - - ); + return <>; + } else + return ( + renderItem({ ...itemProps }, currentPath)} + onExpand={onExpand} + onCollapse={onCollapse} + onDragEnd={onDragEnd} + isDragEnabled + isNestingEnabled + /> + ); }; -export default DragDropWithNestingTree; +export default CollectionListing; -const renderItem = ({ - item, - onExpand, - onCollapse, - provided, -}: RenderItemParams) => { +const renderItem = ( + { item, onExpand, onCollapse, provided }: RenderItemParams, + currentPath: string +) => { const collection = item.data; return ( -
- {Icon(item as ExtendedTreeItem, onExpand, onCollapse)} - - +
-
- -

{collection.name}

+ {Icon(item as ExtendedTreeItem, onExpand, onCollapse)} - {collection.isPublic ? ( + +
- ) : undefined} -
- {collection._count?.links} +

{collection.name}

+ + {collection.isPublic ? ( + + ) : undefined} +
+ {collection._count?.links} +
-
- + +
); }; @@ -126,20 +132,21 @@ const Icon = ( if (item.children && item.children.length > 0) { return item.isExpanded ? ( ) : ( ); } // return ; - return <>; + return
; }; const buildTreeFromCollections = ( - collections: CollectionIncludingMembersAndLinkCount[] + collections: CollectionIncludingMembersAndLinkCount[], + router: ReturnType ): TreeData => { const items: { [key: string]: ExtendedTreeItem } = collections.reduce( (acc: any, collection) => { @@ -150,6 +157,7 @@ const buildTreeFromCollections = ( isExpanded: false, data: { id: collection.id, + parentId: collection.parentId, name: collection.name, description: collection.description, color: collection.color, @@ -167,6 +175,24 @@ const buildTreeFromCollections = ( {} ); + console.log("items:", items); + + const activeCollectionId = Number(router.asPath.split("/collections/")[1]); + + if (activeCollectionId) { + for (const item in items) { + const collection = items[item]; + if (Number(item) === activeCollectionId && collection.data.parentId) { + // get all the parents of the active collection recursively until root and set isExpanded to true + let parentId = collection.data.parentId || null; + while (parentId) { + items[parentId].isExpanded = true; + parentId = items[parentId].data.parentId; + } + } + } + } + collections.forEach((collection) => { const parentId = collection.parentId; if (parentId && items[parentId] && collection.id) {