import LinkSidebar from "@/components/LinkSidebar"; import { ReactNode, useEffect, useState } from "react"; import ModalManagement from "@/components/ModalManagement"; import useModalStore from "@/store/modals"; import { useRouter } from "next/router"; import ClickAwayHandler from "@/components/ClickAwayHandler"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faChevronLeft } from "@fortawesome/free-solid-svg-icons"; import useWindowDimensions from "@/hooks/useWindowDimensions"; import { faPen, faBoxesStacked, faTrashCan, } from "@fortawesome/free-solid-svg-icons"; import useLinkStore from "@/store/links"; import { CollectionIncludingMembersAndLinkCount, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import { useSession } from "next-auth/react"; import useCollectionStore from "@/store/collections"; import EditLinkModal from "@/components/ModalContent/EditLinkModal"; import Link from "next/link"; import PreservedFormatsModal from "@/components/ModalContent/PreservedFormatsModal"; import toast from "react-hot-toast"; import DeleteLinkModal from "@/components/ModalContent/DeleteLinkModal"; interface Props { children: ReactNode; } export default function LinkLayout({ children }: Props) { const { modal } = useModalStore(); const router = useRouter(); useEffect(() => { modal ? (document.body.style.overflow = "hidden") : (document.body.style.overflow = "auto"); }, [modal]); const [sidebar, setSidebar] = useState(false); const { width } = useWindowDimensions(); useEffect(() => { setSidebar(false); }, [width]); useEffect(() => { setSidebar(false); }, [router]); const toggleSidebar = () => { setSidebar(!sidebar); }; const session = useSession(); const userId = session.data?.user.id; const { setModal } = useModalStore(); const { links, removeLink } = useLinkStore(); const { collections } = useCollectionStore(); const [linkCollection, setLinkCollection] = useState(); const [link, setLink] = useState(); useEffect(() => { if (links[0]) setLink(links.find((e) => e.id === Number(router.query.id))); }, [links]); useEffect(() => { if (link) setLinkCollection(collections.find((e) => e.id === link?.collection?.id)); }, [link, collections]); const deleteLink = async () => { const load = toast.loading("Deleting..."); const response = await removeLink(link?.id as number); toast.dismiss(load); response.ok && toast.success(`Link Deleted.`); router.push("/dashboard"); }; const [editLinkModal, setEditLinkModal] = useState(false); const [deleteLinkModal, setDeleteLinkModal] = useState(false); const [preservedFormatsModal, setPreservedFormatsModal] = useState(false); return ( <>
{/*
*/}
{/*
*/} {router.pathname.startsWith("/public") ? linkCollection?.name || link?.collection?.name : "Dashboard"}
{link?.collection?.ownerId === userId || linkCollection?.members.some( (e) => e.userId === userId && e.canUpdate ) ? (
setEditLinkModal(true)} className={`btn btn-ghost btn-square btn-sm`} >
) : undefined}
setPreservedFormatsModal(true)} title="Preserved Formats" className={`btn btn-ghost btn-square btn-sm`} >
{link?.collection?.ownerId === userId || linkCollection?.members.some( (e) => e.userId === userId && e.canDelete ) ? (
{ (document?.activeElement as HTMLElement)?.blur(); e.shiftKey ? deleteLink() : setDeleteLinkModal(true); }} title="Delete" className={`btn btn-ghost btn-square btn-sm`} >
) : undefined}
{children} {sidebar ? (
setSidebar(false)} />
) : null}
{link && editLinkModal ? ( setEditLinkModal(false)} activeLink={link} /> ) : undefined} {link && deleteLinkModal ? ( setDeleteLinkModal(false)} activeLink={link} /> ) : undefined} {link && preservedFormatsModal ? ( setPreservedFormatsModal(false)} activeLink={link} /> ) : undefined}
); }