improvements to the ui

This commit is contained in:
daniel31x13 2024-03-01 14:02:55 -05:00
parent a451e9fa2e
commit 9c51a65f31

View File

@ -13,18 +13,23 @@ import useCollectionStore from "@/store/collections";
import { Collection } from "@prisma/client"; import { Collection } from "@prisma/client";
import Link from "next/link"; import Link from "next/link";
import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import { useRouter } from "next/router";
interface ExtendedTreeItem extends TreeItem { interface ExtendedTreeItem extends TreeItem {
data: Collection; data: Collection;
} }
const DragDropWithNestingTree = () => { const CollectionListing = () => {
const [tree, setTree] = useState<TreeData>(); const [tree, setTree] = useState<TreeData>();
const { collections } = useCollectionStore(); const { collections } = useCollectionStore();
const router = useRouter();
const currentPath = router.asPath;
useEffect(() => { useEffect(() => {
const initialTree = buildTreeFromCollections(collections); const initialTree = buildTreeFromCollections(collections, router);
collections[0] && setTree(initialTree); collections[0] && setTree(initialTree);
}, [collections]); }, [collections]);
@ -55,65 +60,66 @@ const DragDropWithNestingTree = () => {
}; };
if (!tree) { if (!tree) {
return <div>Loading...</div>; return <></>;
} } else
return (
return ( <Tree
<Tree tree={tree}
tree={tree} renderItem={(itemProps) => renderItem({ ...itemProps }, currentPath)}
renderItem={renderItem} onExpand={onExpand}
onExpand={onExpand} onCollapse={onCollapse}
onCollapse={onCollapse} onDragEnd={onDragEnd}
onDragEnd={onDragEnd} isDragEnabled
isDragEnabled isNestingEnabled
isNestingEnabled />
/> );
);
}; };
export default DragDropWithNestingTree; export default CollectionListing;
const renderItem = ({ const renderItem = (
item, { item, onExpand, onCollapse, provided }: RenderItemParams,
onExpand, currentPath: string
onCollapse, ) => {
provided,
}: RenderItemParams) => {
const collection = item.data; const collection = item.data;
return ( return (
<div <div ref={provided.innerRef} {...provided.draggableProps} className="mb-1">
ref={provided.innerRef} <div
{...provided.draggableProps} className={`${
className="flex gap-2 items-center ml-2" currentPath === `/collections/${collection.id}`
> ? "bg-primary/20 is-active"
{Icon(item as ExtendedTreeItem, onExpand, onCollapse)} : "hover:bg-neutral/20"
} duration-100 flex gap-1 items-center pr-2 pl-1 rounded-md`}
<Link
href={`/collections/${collection.id}`}
className="w-full"
{...provided.dragHandleProps}
> >
<div {Icon(item as ExtendedTreeItem, onExpand, onCollapse)}
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 ? ( <Link
href={`/collections/${collection.id}`}
className="w-full"
{...provided.dragHandleProps}
>
<div
className={`py-1 cursor-pointer flex items-center gap-2 w-full rounded-md h-8 capitalize`}
>
<i <i
className="bi-globe2 text-sm text-black/50 dark:text-white/50 drop-shadow" className="bi-folder-fill text-2xl drop-shadow"
title="This collection is being shared publicly." style={{ color: collection.color }}
></i> ></i>
) : undefined} <p className="truncate w-full">{collection.name}</p>
<div className="drop-shadow text-neutral text-xs">
{collection._count?.links} {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> </div>
</div> </Link>
</Link> </div>
</div> </div>
); );
}; };
@ -126,20 +132,21 @@ const Icon = (
if (item.children && item.children.length > 0) { if (item.children && item.children.length > 0) {
return item.isExpanded ? ( return item.isExpanded ? (
<button onClick={() => onCollapse(item.id)}> <button onClick={() => onCollapse(item.id)}>
<div className="bi-chevron-down"></div> <div className="bi-caret-down-fill opacity-50 hover:opacity-100 duration-200"></div>
</button> </button>
) : ( ) : (
<button onClick={() => onExpand(item.id)}> <button onClick={() => onExpand(item.id)}>
<div className="bi-chevron-right"></div> <div className="bi-caret-right-fill opacity-40 hover:opacity-100 duration-200"></div>
</button> </button>
); );
} }
// return <span>&bull;</span>; // return <span>&bull;</span>;
return <></>; return <div className="pl-1"></div>;
}; };
const buildTreeFromCollections = ( const buildTreeFromCollections = (
collections: CollectionIncludingMembersAndLinkCount[] collections: CollectionIncludingMembersAndLinkCount[],
router: ReturnType<typeof useRouter>
): TreeData => { ): TreeData => {
const items: { [key: string]: ExtendedTreeItem } = collections.reduce( const items: { [key: string]: ExtendedTreeItem } = collections.reduce(
(acc: any, collection) => { (acc: any, collection) => {
@ -150,6 +157,7 @@ const buildTreeFromCollections = (
isExpanded: false, isExpanded: false,
data: { data: {
id: collection.id, id: collection.id,
parentId: collection.parentId,
name: collection.name, name: collection.name,
description: collection.description, description: collection.description,
color: collection.color, 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) => { collections.forEach((collection) => {
const parentId = collection.parentId; const parentId = collection.parentId;
if (parentId && items[parentId] && collection.id) { if (parentId && items[parentId] && collection.id) {