diff --git a/components/CollectionListing.tsx b/components/CollectionListing.tsx index c6119ed..2eb3a75 100644 --- a/components/CollectionListing.tsx +++ b/components/CollectionListing.tsx @@ -1,4 +1,4 @@ -import React, { Component, useCallback, useEffect, useState } from "react"; +import React, { useEffect, useState } from "react"; import Tree, { mutateTree, moveItemOnTree, @@ -9,159 +9,53 @@ import Tree, { TreeSourcePosition, TreeDestinationPosition, } from "@atlaskit/tree"; -import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; import useCollectionStore from "@/store/collections"; +import { Collection } from "@prisma/client"; +import Link from "next/link"; -const collections = [ - { - id: 262, - name: "dasd", - description: "", - color: "#0ea5e9", - parentId: null, - isPublic: false, - ownerId: 1, - createdAt: "2024-02-18T23:40:44.043Z", - updatedAt: "2024-02-19T19:16:14.873Z", - parent: null, - members: [ - { - userId: 17, - collectionId: 262, - canCreate: true, - canUpdate: false, - canDelete: false, - createdAt: "2024-02-19T19:16:14.873Z", - updatedAt: "2024-02-19T19:16:14.873Z", - user: { username: "test", name: "ben", image: "" }, - }, - ], - _count: { links: 0 }, - }, - { - id: 268, - name: "ab", - description: "", - color: "#0ea5e9", - parentId: 267, - isPublic: false, - ownerId: 17, - createdAt: "2024-02-19T21:06:52.545Z", - updatedAt: "2024-02-19T21:06:52.545Z", - parent: { id: 267, name: "a" }, - members: [], - _count: { links: 0 }, - }, - { - id: 269, - name: "abc", - description: "", - color: "#0ea5e9", - parentId: 268, - isPublic: false, - ownerId: 17, - createdAt: "2024-02-19T21:07:08.565Z", - updatedAt: "2024-02-19T21:07:08.565Z", - parent: { id: 268, name: "ab" }, - members: [], - _count: { links: 0 }, - }, - { - id: 267, - name: "a", - description: "", - color: "#0ea5e9", - parentId: null, - isPublic: false, - ownerId: 17, - createdAt: "2024-02-19T21:06:45.402Z", - updatedAt: "2024-02-26T16:59:20.312Z", - parent: null, - members: [], - _count: { links: 0 }, - }, - { - id: 80, - name: "abc", - description: "s", - color: "#0ea5e9", - parentId: 79, - isPublic: false, - ownerId: 1, - createdAt: "2024-02-05T07:00:46.881Z", - updatedAt: "2024-02-27T06:11:46.358Z", - parent: { id: 79, name: "ab" }, - members: [ - { - userId: 17, - collectionId: 80, - canCreate: false, - canUpdate: false, - canDelete: false, - createdAt: "2024-02-27T06:11:46.358Z", - updatedAt: "2024-02-27T06:11:46.358Z", - user: { username: "test", name: "ben", image: "" }, - }, - { - userId: 2, - collectionId: 80, - canCreate: false, - canUpdate: false, - canDelete: false, - createdAt: "2024-02-27T06:11:46.358Z", - updatedAt: "2024-02-27T06:11:46.358Z", - user: { - username: "sarah_connor", - name: "Sarah Smith", - image: "uploads/avatar/2.jpg", - }, - }, - ], - _count: { links: 0 }, - }, -]; +interface ExtendedTreeItem extends TreeItem { + data: Collection; +} const DragDropWithNestingTree = () => { - const buildTreeFromCollections = (collections) => { - // Step 1: Map Collections to TreeItems - const items = collections.reduce((acc, collection) => { - acc[collection.id] = { - id: collection.id.toString(), - children: [], - hasChildren: false, // Initially assume no children, adjust in Step 2 - isExpanded: false, - data: { - title: collection.name, - description: collection.description, - color: collection.color, - isPublic: collection.isPublic, - ownerId: collection.ownerId, - createdAt: collection.createdAt, - updatedAt: collection.updatedAt, - }, - }; - return acc; - }, {}); + const buildTreeFromCollections = (collections: Collection[]): TreeData => { + const items: { [key: string]: ExtendedTreeItem } = collections.reduce( + (acc: any, collection) => { + acc[collection.id] = { + id: collection.id, + children: [], + hasChildren: false, + isExpanded: false, + data: { + name: collection.name, + description: collection.description, + color: collection.color, + isPublic: collection.isPublic, + ownerId: collection.ownerId, + createdAt: collection.createdAt, + updatedAt: collection.updatedAt, + }, + }; + return acc; + }, + {} + ); - // Step 2: Build Hierarchy collections.forEach((collection) => { const parentId = collection.parentId; - if (parentId !== null && items[parentId]) { - items[parentId].children.push(collection.id.toString()); + if (parentId && items[parentId]) { + items[parentId].children.push(collection.id); items[parentId].hasChildren = true; } }); - // Define a root item to act as the top-level node of your tree if needed const rootId = "root"; items[rootId] = { id: rootId, - children: collections - .filter((c) => c.parentId === null) - .map((c) => c.id.toString()), + children: collections.filter((c) => c.parentId === null).map((c) => c.id), hasChildren: true, isExpanded: true, - data: { title: "Root" }, + data: { name: "Root" } as Collection, }; return { rootId, items }; @@ -172,11 +66,17 @@ const DragDropWithNestingTree = () => { const { collections } = useCollectionStore(); useEffect(() => { - const initialTree = buildTreeFromCollections(collections); + const initialTree = buildTreeFromCollections( + collections as unknown as Collection[] + ); collections[0] && setTree(initialTree); }, [collections]); - const getIcon = useCallback((item, onExpand, onCollapse) => { + const getIcon = ( + item: ExtendedTreeItem, + onExpand: (id: ItemId) => void, + onCollapse: (id: ItemId) => void + ) => { if (item.children && item.children.length > 0) { return item.isExpanded ? ( ); } - return •; - }, []); + // return •; + }; - const renderItem = useCallback( - ({ item, onExpand, onCollapse, provided }: RenderItemParams) => { - return ( -
{collection.name}
+ + {collection.isPublic ? ( + + ) : undefined} +