"use client" import { Button } from "@/components/ui/button" import { linkEditIdAtom, linkShowCreateAtom } from "@/store/link" import { useAtom } from "jotai" import React, { useEffect, useRef, useState } from "react" import { useKey } from "react-use" import { globalLinkFormExceptionRefsAtom, LinkForm } from "./link-form" import { LaIcon } from "@/components/custom/la-icon" import LinkOptions from "@/components/LinkOptions" // import { FloatingButton } from "./partial/floating-button" const LinkManage: React.FC = () => { const [showCreate, setShowCreate] = useAtom(linkShowCreateAtom) const [editId, setEditId] = useAtom(linkEditIdAtom) const [, setGlobalExceptionRefs] = useAtom(globalLinkFormExceptionRefsAtom) const [showOptions, setShowOptions] = useState(false) const optionsRef = useRef(null) const buttonRef = useRef(null) const toggleForm = (event: React.MouseEvent) => { event.stopPropagation() if (showCreate) return setShowCreate(prev => !prev) } const clickOptionsButton = (e: React.MouseEvent) => { e.preventDefault() setShowOptions(prev => !prev) } const handleFormClose = () => { setShowCreate(false) } const handleFormFail = () => {} // wipes the data from the form when the form is closed React.useEffect(() => { if (!showCreate) { setEditId(null) } }, [showCreate, setEditId]) useKey("Escape", handleFormClose) useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (optionsRef.current && !optionsRef.current.contains(event.target as Node)) { setShowOptions(false) } } if (showOptions) { document.addEventListener("mousedown", handleClickOutside) } return () => { document.removeEventListener("mousedown", handleClickOutside) } }, [showOptions]) /* * This code means that when link form is opened, these refs will be added as an exception to the click outside handler */ React.useEffect(() => { setGlobalExceptionRefs([optionsRef, buttonRef]) }, [setGlobalExceptionRefs]) return ( <> {showCreate && }
{showOptions && }
) } LinkManage.displayName = "LinkManage" export { LinkManage } /* */