import useCollectionStore from "@/store/collections"; import useLinkStore from "@/store/links"; import { CollectionIncludingMembersAndLinkCount, Sort, ViewMode, } from "@/types/global"; import { useRouter } from "next/router"; import React, { useEffect, useState } from "react"; import MainLayout from "@/layouts/MainLayout"; import ProfilePhoto from "@/components/ProfilePhoto"; import SortDropdown from "@/components/SortDropdown"; import useLinks from "@/hooks/useLinks"; import usePermissions from "@/hooks/usePermissions"; import NoLinksFound from "@/components/NoLinksFound"; import useLocalSettingsStore from "@/store/localSettings"; import useAccountStore from "@/store/account"; import getPublicUserData from "@/lib/client/getPublicUserData"; import EditCollectionModal from "@/components/ModalContent/EditCollectionModal"; import EditCollectionSharingModal from "@/components/ModalContent/EditCollectionSharingModal"; import DeleteCollectionModal from "@/components/ModalContent/DeleteCollectionModal"; import ViewDropdown from "@/components/ViewDropdown"; import CardView from "@/components/LinkViews/Layouts/CardView"; import ListView from "@/components/LinkViews/Layouts/ListView"; import { dropdownTriggerer } from "@/lib/client/utils"; import NewCollectionModal from "@/components/ModalContent/NewCollectionModal"; import BulkDeleteLinksModal from "@/components/ModalContent/BulkDeleteLinksModal"; import toast from "react-hot-toast"; import BulkEditLinksModal from "@/components/ModalContent/BulkEditLinksModal"; import MasonryView from "@/components/LinkViews/Layouts/MasonryView"; export default function Index() { const { settings } = useLocalSettingsStore(); const router = useRouter(); const { links, selectedLinks, setSelectedLinks, deleteLinksById } = useLinkStore(); const { collections } = useCollectionStore(); const [sortBy, setSortBy] = useState(Sort.DateNewestFirst); const [activeCollection, setActiveCollection] = useState(); const permissions = usePermissions(activeCollection?.id as number); useLinks({ collectionId: Number(router.query.id), sort: sortBy }); useEffect(() => { setActiveCollection( collections.find((e) => e.id === Number(router.query.id)) ); }, [router, collections]); const { account } = useAccountStore(); const [collectionOwner, setCollectionOwner] = useState({ id: null as unknown as number, name: "", username: "", image: "", archiveAsScreenshot: undefined as unknown as boolean, archiveAsPDF: undefined as unknown as boolean, }); useEffect(() => { const fetchOwner = async () => { if (activeCollection && activeCollection.ownerId !== account.id) { const owner = await getPublicUserData( activeCollection.ownerId as number ); setCollectionOwner(owner); } else if (activeCollection && activeCollection.ownerId === account.id) { setCollectionOwner({ id: account.id as number, name: account.name, username: account.username as string, image: account.image as string, archiveAsScreenshot: account.archiveAsScreenshot as boolean, archiveAsPDF: account.archiveAsPDF as boolean, }); } }; fetchOwner(); // When the collection changes, reset the selected links setSelectedLinks([]); }, [activeCollection]); const [editCollectionModal, setEditCollectionModal] = useState(false); const [newCollectionModal, setNewCollectionModal] = useState(false); const [editCollectionSharingModal, setEditCollectionSharingModal] = useState(false); const [deleteCollectionModal, setDeleteCollectionModal] = useState(false); const [bulkDeleteLinksModal, setBulkDeleteLinksModal] = useState(false); const [bulkEditLinksModal, setBulkEditLinksModal] = useState(false); const [editMode, setEditMode] = useState(false); useEffect(() => { if (editMode) return setEditMode(false); }, [router]); const [viewMode, setViewMode] = useState( localStorage.getItem("viewMode") || ViewMode.Card ); const linkView = { [ViewMode.Card]: CardView, [ViewMode.List]: ListView, [ViewMode.Masonry]: MasonryView, }; // @ts-ignore const LinkComponent = linkView[viewMode]; const handleSelectAll = () => { if (selectedLinks.length === links.length) { setSelectedLinks([]); } else { setSelectedLinks(links.map((link) => link)); } }; const bulkDeleteLinks = async () => { const load = toast.loading( `Deleting ${selectedLinks.length} Link${ selectedLinks.length > 1 ? "s" : "" }...` ); const response = await deleteLinksById( selectedLinks.map((link) => link.id as number) ); toast.dismiss(load); response.ok && toast.success( `Deleted ${selectedLinks.length} Link${ selectedLinks.length > 1 ? "s" : "" }!` ); }; return (
{activeCollection && (

{activeCollection?.name}

    {permissions === true && (
  • { (document?.activeElement as HTMLElement)?.blur(); setEditCollectionModal(true); }} > Edit Collection Info
  • )}
  • { (document?.activeElement as HTMLElement)?.blur(); setEditCollectionSharingModal(true); }} > {permissions === true ? "Share and Collaborate" : "View Team"}
  • {permissions === true && (
  • { (document?.activeElement as HTMLElement)?.blur(); setNewCollectionModal(true); }} > Create Sub-Collection
  • )}
  • { (document?.activeElement as HTMLElement)?.blur(); setDeleteCollectionModal(true); }} > {permissions === true ? "Delete Collection" : "Leave Collection"}
)} {activeCollection && (
setEditCollectionSharingModal(true)} > {collectionOwner.id ? ( ) : undefined} {activeCollection.members .sort((a, b) => (a.userId as number) - (b.userId as number)) .map((e, i) => { return ( ); }) .slice(0, 3)} {activeCollection.members.length - 3 > 0 ? (
+{activeCollection.members.length - 3}
) : null}

By {collectionOwner.name} {activeCollection.members.length > 0 && ` and ${activeCollection.members.length} others`} .

)} {activeCollection?.description && (

{activeCollection?.description}

)} {/* {collections.some((e) => e.parentId === activeCollection.id) ? (
Sub-Collections
{collections .filter((e) => e.parentId === activeCollection?.id) .map((e, i) => { return (

{e.name}

); })}
) : undefined} */}

Showing {activeCollection?._count?.links} results

{links.length > 0 && (permissions === true || permissions?.canUpdate || permissions?.canDelete) && (
{ setEditMode(!editMode); setSelectedLinks([]); }} className={`btn btn-square btn-sm btn-ghost ${ editMode ? "bg-primary/20 hover:bg-primary/20" : "hover:bg-neutral/20" }`} >
)}
{editMode && links.length > 0 && (
{links.length > 0 && (
handleSelectAll()} checked={ selectedLinks.length === links.length && links.length > 0 } /> {selectedLinks.length > 0 ? ( {selectedLinks.length}{" "} {selectedLinks.length === 1 ? "link" : "links"} selected ) : ( Nothing selected )}
)}
)} {links.some((e) => e.collectionId === Number(router.query.id)) ? ( e.collection.id === activeCollection?.id )} /> ) : ( )}
{activeCollection && ( <> {editCollectionModal && ( setEditCollectionModal(false)} activeCollection={activeCollection} /> )} {editCollectionSharingModal && ( setEditCollectionSharingModal(false)} activeCollection={activeCollection} /> )} {newCollectionModal && ( setNewCollectionModal(false)} parent={activeCollection} /> )} {deleteCollectionModal && ( setDeleteCollectionModal(false)} activeCollection={activeCollection} /> )} {bulkDeleteLinksModal && ( { setBulkDeleteLinksModal(false); }} /> )} {bulkEditLinksModal && ( { setBulkEditLinksModal(false); }} /> )} )}
); }