import React, { useEffect, useState } from "react"; 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 { ArchivedFormat, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import { useSession } from "next-auth/react"; import { useRouter } from "next/router"; import toast from "react-hot-toast"; import Modal from "../Modal"; type Props = { onClose: Function; }; export default function UploadFileModal({ onClose }: Props) { const { data } = useSession(); const initial = { name: "", url: "", description: "", type: "url", tags: [], preview: "", image: "", pdf: "", readable: "", textContent: "", collection: { name: "", ownerId: data?.user.id as number, }, } as LinkIncludingShortenedCollectionAndTags; const [link, setLink] = useState(initial); const [file, setFile] = useState(); const { uploadFile } = useLinkStore(); const [submitLoader, setSubmitLoader] = useState(false); 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(() => { 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, }, }); }, []); const submit = async () => { if (!submitLoader && file) { setSubmitLoader(true); const load = toast.loading("Creating..."); const response = await uploadFile(link, file); toast.dismiss(load); if (response.ok) { toast.success(`Created!`); onClose(); } else toast.error(response.data as string); setSubmitLoader(false); return response; } }; return (

Upload File

File

PDF, PNG, JPG (Up to {process.env.NEXT_PUBLIC_MAX_FILE_SIZE || 30} MB)

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