import { Link } from "@nirvana/common/models/conversation"; import { conversationService } from "@nirvana/common/services"; import Modal from "antd/lib/modal/Modal"; import Image from "next/image"; import { useState, useEffect, useRef } from "react"; import toast from "react-hot-toast"; import { useRecoilValue } from "recoil"; import { useAuth } from "../../contexts/authContext"; import { selectedConvoAtom } from "../../recoil/main"; import LinkIcon, { getFavicon } from "./LinkIcon"; import isValidHttpUrl from "@nirvana/common/helpers/urlHelper"; export default function CreateItemModal(props: { pastedLink: string; show: boolean; handleClose: () => void; }) { const { currUser } = useAuth(); const [linkVal, setLinkVal] = useState(""); const [linkDesc, setLinkDesc] = useState(""); const linkInput = useRef(null); const [loading, setLoading] = useState(false); const selectedConvoId = useRecoilValue(selectedConvoAtom); useEffect(() => { if (props.pastedLink) { setLinkVal(props.pastedLink); } }, [props.pastedLink]); useEffect(() => { linkInput.current?.focus(); }, [props.show]); const handleCreate = async () => { if (loading) { toast("in the process of sharing..."); return; } setLoading(true); try { // validations if (!linkVal) { toast.error("must put a link"); setLoading(false); return; } if (!linkDesc) { toast.error("must put a name of some sort"); setLoading(false); return; } if (!isValidHttpUrl(linkVal)) { toast.error("not a valid url"); setLoading(false); return; } // service to add link to the collection // change the cacheddraweritem on the convo doc const newLink = new Link(linkDesc, linkVal, currUser!.uid); await conversationService.shareLink( currUser!.uid, newLink, selectedConvoId! ); toast.success("shared"); } catch (error) { console.error(error); toast.error("problem in sharing link"); } setLoading(false); resetForm(); props.handleClose(); }; const resetForm = () => { setLinkDesc(""); setLinkVal(""); }; const handleCancel = () => { console.log("closing"); props.handleClose(); }; const handleChangeLinkInput = (e) => { setLinkVal(e.target.value); // todo: find the tab name of the url }; function addDefaultSrc(ev) { ev.target.src = getFavicon("www.com"); } return ( } visible={props.show} {...props} >
Link Please make sure this is valid for others to access. {/* icon and link input */} {linkVal && ( )} Title Be clear and concise. setLinkDesc(e.target.value)} />
); }