2024-03-01 05:59:14 -06:00
|
|
|
import React, { useEffect, useState } from "react";
|
2024-02-28 23:05:38 -06:00
|
|
|
import Tree, {
|
|
|
|
mutateTree,
|
|
|
|
moveItemOnTree,
|
|
|
|
RenderItemParams,
|
|
|
|
TreeItem,
|
|
|
|
TreeData,
|
|
|
|
ItemId,
|
|
|
|
TreeSourcePosition,
|
|
|
|
TreeDestinationPosition,
|
|
|
|
} from "@atlaskit/tree";
|
|
|
|
import useCollectionStore from "@/store/collections";
|
2024-03-01 05:59:14 -06:00
|
|
|
import { Collection } from "@prisma/client";
|
|
|
|
import Link from "next/link";
|
2024-03-01 08:33:58 -06:00
|
|
|
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
|
2024-02-05 01:42:54 -06:00
|
|
|
|
2024-03-01 05:59:14 -06:00
|
|
|
interface ExtendedTreeItem extends TreeItem {
|
|
|
|
data: Collection;
|
|
|
|
}
|
2024-02-05 01:42:54 -06:00
|
|
|
|
2024-02-28 23:05:38 -06:00
|
|
|
const DragDropWithNestingTree = () => {
|
|
|
|
const [tree, setTree] = useState<TreeData>();
|
|
|
|
|
|
|
|
const { collections } = useCollectionStore();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-03-01 08:33:58 -06:00
|
|
|
const initialTree = buildTreeFromCollections(collections);
|
2024-02-28 23:05:38 -06:00
|
|
|
collections[0] && setTree(initialTree);
|
|
|
|
}, [collections]);
|
|
|
|
|
2024-03-01 05:59:14 -06:00
|
|
|
const onExpand = (itemId: ItemId) => {
|
|
|
|
if (tree) {
|
|
|
|
setTree((currentTree) =>
|
|
|
|
mutateTree(currentTree!, itemId, { isExpanded: true })
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2024-02-22 01:51:51 -06:00
|
|
|
|
2024-03-01 05:59:14 -06:00
|
|
|
const onCollapse = (itemId: ItemId) => {
|
|
|
|
if (tree) {
|
2024-02-28 23:05:38 -06:00
|
|
|
setTree((currentTree) =>
|
2024-03-01 05:59:14 -06:00
|
|
|
mutateTree(currentTree as TreeData, itemId, { isExpanded: false })
|
2024-02-28 23:05:38 -06:00
|
|
|
);
|
2024-03-01 05:59:14 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDragEnd = (
|
|
|
|
source: TreeSourcePosition,
|
|
|
|
destination: TreeDestinationPosition | undefined
|
|
|
|
) => {
|
|
|
|
if (!destination || !tree) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTree((currentTree) => moveItemOnTree(currentTree!, source, destination));
|
|
|
|
};
|
2024-02-22 02:04:01 -06:00
|
|
|
|
2024-02-28 23:05:38 -06:00
|
|
|
if (!tree) {
|
2024-03-01 05:59:14 -06:00
|
|
|
return <div>Loading...</div>;
|
2024-02-28 23:05:38 -06:00
|
|
|
}
|
2024-02-22 01:51:51 -06:00
|
|
|
|
2024-02-28 23:05:38 -06:00
|
|
|
return (
|
|
|
|
<Tree
|
|
|
|
tree={tree}
|
|
|
|
renderItem={renderItem}
|
|
|
|
onExpand={onExpand}
|
|
|
|
onCollapse={onCollapse}
|
|
|
|
onDragEnd={onDragEnd}
|
|
|
|
isDragEnabled
|
|
|
|
isNestingEnabled
|
|
|
|
/>
|
2024-02-05 01:42:54 -06:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-02-28 23:05:38 -06:00
|
|
|
export default DragDropWithNestingTree;
|
2024-03-01 07:37:20 -06:00
|
|
|
|
|
|
|
const renderItem = ({
|
|
|
|
item,
|
|
|
|
onExpand,
|
|
|
|
onCollapse,
|
|
|
|
provided,
|
|
|
|
}: RenderItemParams) => {
|
|
|
|
const collection = item.data;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.draggableProps}
|
|
|
|
className="flex gap-2 items-center ml-2"
|
|
|
|
>
|
|
|
|
{Icon(item as ExtendedTreeItem, onExpand, onCollapse)}
|
|
|
|
|
|
|
|
<Link
|
|
|
|
href={`/collections/${collection.id}`}
|
|
|
|
className="w-full"
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={`duration-100 py-1 cursor-pointer flex items-center gap-2 w-full rounded-md h-8 capitalize`}
|
|
|
|
>
|
|
|
|
<i
|
|
|
|
className="bi-folder-fill text-2xl drop-shadow"
|
|
|
|
style={{ color: collection.color }}
|
|
|
|
></i>
|
|
|
|
<p className="truncate w-full">{collection.name}</p>
|
|
|
|
|
|
|
|
{collection.isPublic ? (
|
|
|
|
<i
|
|
|
|
className="bi-globe2 text-sm text-black/50 dark:text-white/50 drop-shadow"
|
|
|
|
title="This collection is being shared publicly."
|
|
|
|
></i>
|
|
|
|
) : undefined}
|
|
|
|
<div className="drop-shadow text-neutral text-xs">
|
|
|
|
{collection._count?.links}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Icon = (
|
|
|
|
item: ExtendedTreeItem,
|
|
|
|
onExpand: (id: ItemId) => void,
|
|
|
|
onCollapse: (id: ItemId) => void
|
|
|
|
) => {
|
|
|
|
if (item.children && item.children.length > 0) {
|
|
|
|
return item.isExpanded ? (
|
|
|
|
<button onClick={() => onCollapse(item.id)}>
|
|
|
|
<div className="bi-chevron-down"></div>
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button onClick={() => onExpand(item.id)}>
|
|
|
|
<div className="bi-chevron-right"></div>
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// return <span>•</span>;
|
|
|
|
return <></>;
|
|
|
|
};
|
2024-03-01 08:33:58 -06:00
|
|
|
|
|
|
|
const buildTreeFromCollections = (
|
|
|
|
collections: CollectionIncludingMembersAndLinkCount[]
|
|
|
|
): TreeData => {
|
|
|
|
const items: { [key: string]: ExtendedTreeItem } = collections.reduce(
|
|
|
|
(acc: any, collection) => {
|
|
|
|
acc[collection.id as number] = {
|
|
|
|
id: collection.id,
|
|
|
|
children: [],
|
|
|
|
hasChildren: false,
|
|
|
|
isExpanded: false,
|
|
|
|
data: {
|
|
|
|
id: collection.id,
|
|
|
|
name: collection.name,
|
|
|
|
description: collection.description,
|
|
|
|
color: collection.color,
|
|
|
|
isPublic: collection.isPublic,
|
|
|
|
ownerId: collection.ownerId,
|
|
|
|
createdAt: collection.createdAt,
|
|
|
|
updatedAt: collection.updatedAt,
|
|
|
|
_count: {
|
|
|
|
links: collection._count?.links,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
collections.forEach((collection) => {
|
|
|
|
const parentId = collection.parentId;
|
|
|
|
if (parentId && items[parentId] && collection.id) {
|
|
|
|
items[parentId].children.push(collection.id);
|
|
|
|
items[parentId].hasChildren = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const rootId = "root";
|
|
|
|
items[rootId] = {
|
|
|
|
id: rootId,
|
|
|
|
children: (collections
|
|
|
|
.filter((c) => c.parentId === null)
|
|
|
|
.map((c) => c.id) || "") as unknown as string[],
|
|
|
|
hasChildren: true,
|
|
|
|
isExpanded: true,
|
|
|
|
data: { name: "Root" } as Collection,
|
|
|
|
};
|
|
|
|
|
|
|
|
return { rootId, items };
|
|
|
|
};
|