import React, { useState } from "react"; import { useGleanClick } from "../../telemetry/glean-context"; import { Button } from "../../ui/atoms/button"; import MDNModal from "../../ui/atoms/modal"; import NoteCard from "../../ui/molecules/notecards"; import { SubscriptionType, useUserData } from "../../user-context"; import { SignUpLink } from "../../ui/atoms/signup-link"; import { Collection, NewCollection, useCollectionCreate, useCollectionEdit, useCollections, } from "./api"; import { PLUS_COLLECTIONS } from "../../telemetry/constants"; import LimitedInput from "../../ui/atoms/form/limited-input"; import ExpandingTextarea from "../../ui/atoms/form/expanding-textarea"; export default function NewEditCollectionModal({ show, setShow, onClose, editingCollection, source, }: { show: boolean; setShow: React.Dispatch>; onClose?: (collection_id?: string) => void; editingCollection?: Collection; source: string; }) { const user = useUserData(); const { data: collections } = useCollections(); const freeLimitReached = user?.subscriptionType === SubscriptionType["MDN_CORE"] && (collections?.length || 0) > 2; const defaultCollection: Collection | NewCollection = editingCollection || { name: "", description: "", }; const [collection, setCollection] = useState(defaultCollection); const gleanClick = useGleanClick(); const { mutator: edit, ...editHook } = useCollectionEdit(); const { mutator: create, ...createHook } = useCollectionCreate(); const { isPending, resetError, error } = "id" in collection ? editHook : createHook; const changeHandler: React.ChangeEventHandler< HTMLInputElement | HTMLTextAreaElement > = (e) => { const { name, value } = e.target; setCollection({ ...collection, [name]: value }); }; const cancelHandler = (e: React.MouseEvent | React.KeyboardEvent) => { e.preventDefault(); if (isPending) return; if (onClose) onClose(); setCollection(defaultCollection); resetError(); setShow(false); }; const saveHandler = async (e: React.BaseSyntheticEvent) => { e.preventDefault(); let savedCollection; if ("id" in collection) { savedCollection = await edit(collection); } else { gleanClick(source); savedCollection = await create(collection); } if (onClose) onClose(savedCollection.id); setCollection(editingCollection ? savedCollection : defaultCollection); setShow(false); }; return ( {!editingCollection && freeLimitReached ? ( <>

Want more?

You've reached the maximum number of collections you can have as a "Core" user.

Upgrade now to receive unlimited access to collections, and more:

) : ( <>

{editingCollection ? "Edit Collection" : "Create Collection"}

{error && (

Error: {error.message}

)}
{({ value, changeWrapper }) => ( )}
{({ value, changeWrapper }) => ( )}
)}
); }