"use client"; import { useState, useEffect, useRef } from "react"; import { createPortal } from "react-dom"; import { useCopyFeedback, type Position } from "./shared"; import { buildHighlightUrl } from "@/lib/text-highlight"; import { cn } from "@/lib/utils"; import { Bookmark, FolderPlus, Link2, AtSign, Check } from "lucide-react"; import { Drawer, DrawerClose, DrawerContent, DrawerHeader, DrawerTitle, } from "@/components/ui/drawer"; export function MorePopover({ open, position, onClose, onBookmark, onSaveToCollection, onMention, isMobile, selectedText, }: { open: boolean; position: Position; onClose: () => void; onBookmark: () => void; onSaveToCollection: () => void; onMention: () => void; isMobile: boolean; selectedText: string; }) { const ref = useRef(null); const { copied, copy } = useCopyFeedback(); useEffect(() => { if (!open || isMobile) return; function handleClick(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) { onClose(); } } const timer = setTimeout(() => { document.addEventListener("mousedown", handleClick); }, 10); return () => { clearTimeout(timer); document.removeEventListener("mousedown", handleClick); }; }, [open, onClose, isMobile]); if (!open) return null; const actions = [ { key: "bookmark", label: "Add Bookmark", description: "Save your place", icon: , onClick: onBookmark, }, { key: "collection", label: "Save to Collection", description: "Organize into a collection", icon: , onClick: onSaveToCollection, }, { key: "copy-link", label: copied ? "Link Copied!" : "Copy Link", description: "Copy a link to this page", icon: copied ? ( ) : ( ), onClick: () => copy(buildHighlightUrl(selectedText)), }, { key: "mention", label: "Mention", description: "Tag someone with this quote", icon: , onClick: onMention, }, ]; // Mobile: bottom drawer if (isMobile) { return ( !o && onClose()}> More Options
{actions.map((action) => ( ))}
); } // Desktop: floating popover const popoverW = 220; const popoverH = actions.length * 44 + 8; const pad = 8; let popLeft = position.x; let popTop = position.y + 8; if (popLeft + popoverW > window.innerWidth - pad) { popLeft = window.innerWidth - popoverW - pad; } if (popLeft < pad) { popLeft = pad; } if (popTop + popoverH > window.innerHeight - pad) { popTop = position.y - popoverH - 8; } if (popTop < pad) { popTop = pad; } const style: React.CSSProperties = { position: "fixed", zIndex: 90, left: popLeft, top: popTop, }; return createPortal(
{actions.map((action) => ( ))}
, document.body, ); }