import React, { useEffect, useState } from "react"; import { Toaster } from "react-hot-toast"; import CollectionSelection from "@/components/InputSelect/CollectionSelection"; import TagSelection from "@/components/InputSelect/TagSelection"; import TextInput from "@/components/TextInput"; import unescapeString from "@/lib/client/unescapeString"; import useCollectionStore from "@/store/collections"; import useLinkStore from "@/store/links"; import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; import { useSession } from "next-auth/react"; import { useRouter } from "next/router"; import toast from "react-hot-toast"; type Props = { modalId: string; isOpen: boolean; onClose: Function; }; export default function NewLinkModal({ modalId, isOpen, onClose }: Props) { const modal = document.getElementById(modalId); const { data } = useSession(); const initial = { name: "", url: "", description: "", tags: [], screenshotPath: "", pdfPath: "", readabilityPath: "", textContent: "", collection: { name: "", ownerId: data?.user.id as number, }, }; const [link, setLink] = useState(initial); const { addLink } = useLinkStore(); const [submitLoader, setSubmitLoader] = useState(false); const [resetCollectionSelection, setResetCollectionSelection] = useState(""); const [optionsExpanded, setOptionsExpanded] = useState(false); const router = useRouter(); const { collections } = useCollectionStore(); const setCollection = (e: any) => { if (e?.__isNew__) e.value = null; setLink({ ...link, collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId }, }); }; const setTags = (e: any) => { const tagNames = e.map((e: any) => { return { name: e.label }; }); setLink({ ...link, tags: tagNames }); }; useEffect(() => { setResetCollectionSelection(Date.now().toString()); console.log(link); modal?.scrollTo(0, 0); setOptionsExpanded(false); if (router.query.id) { const currentCollection = collections.find( (e) => e.id == Number(router.query.id) ); if ( currentCollection && currentCollection.ownerId && router.asPath.startsWith("/collections/") ) setLink({ ...initial, collection: { id: currentCollection.id, name: currentCollection.name, ownerId: currentCollection.ownerId, }, }); } else setLink({ ...initial, collection: { name: "Unorganized", ownerId: data?.user.id as number, }, }); modal?.addEventListener("close", () => { onClose(); }); return () => { modal?.addEventListener("close", () => { onClose(); }); }; }, [isOpen]); const submit = async () => { if (!submitLoader) { setSubmitLoader(true); let response; const load = toast.loading("Creating..."); response = await addLink(link); toast.dismiss(load); if (response.ok) { toast.success(`Created!`); (document?.getElementById(modalId) as any)?.close(); } else toast.error(response.data as string); setSubmitLoader(false); return response; } }; return (

Create a New Link

Link

setLink({ ...link, url: e.target.value })} placeholder="e.g. http://example.com/" className="bg-base-200" />

Collection

{link.collection.name ? ( ) : null}
{optionsExpanded ? (
{/*
*/}

Name

setLink({ ...link, name: e.target.value })} placeholder="e.g. Example Link" className="bg-base-200" />

Tags

{ return { label: e.name, value: e.id }; })} />

Description