import { HTMLProps, memo, useCallback, useMemo } from "react"; import { Button, ButtonProps, Intent, Menu, MenuDivider, MenuItem } from "@blueprintjs/core"; import { IPopover2SharedProps, Popover2, Popover2InteractionKind, Popover2Props } from "@blueprintjs/popover2"; import { useAnnotationsSettings, useMetadataSettings, useNotesSettings, useTypemapSettings } from "Components/UserSettings"; import { useRoamCitekeys } from "Components/RoamCitekeysContext"; import { importItemMetadata, openInSidebarByUID, openPageByUID } from "@services/roam"; import { CustomClasses } from "../../constants"; import { getLocalLink, getWebLink } from "../../utils"; import { ZItemAnnotation, ZItemAttachment, ZItemNote, ZItemTop } from "Types/transforms"; import "./_index.sass"; const popoverProps: Partial = { autoFocus: true, boundary: "window" as IPopover2SharedProps>["boundary"], // TODO: Update to Popover2SharedProps["boundary"] once issues with popover2 v1 are fixed className: "zr-library-item-popover", interactionKind: Popover2InteractionKind.HOVER, lazy: true, placement: "right-start", popoverClassName: CustomClasses.POPOVER }; type OwnProps = { closeDialog?: () => void, inGraph: string | false, item: ZItemTop, notes: (ZItemAnnotation | ZItemNote)[], pdfs: ZItemAttachment[] }; const CitekeyPopover = memo(function CitekeyPopover(props) { const { closeDialog, inGraph, item, notes = [], pdfs = [] } = props; const [annotationsSettings] = useAnnotationsSettings(); const [metadataSettings] = useMetadataSettings(); const [notesSettings] = useNotesSettings(); const [typemap] = useTypemapSettings(); const [, updateRoamCitekeys] = useRoamCitekeys(); const handleClose = useCallback(() => { if(closeDialog){ closeDialog(); } }, [closeDialog]); const buttonProps = useMemo>(() => { return inGraph ? { className: CustomClasses.TEXT_SMALL, onClick: () => openPageByUID(inGraph) } : { className: [CustomClasses.TEXT_SMALL, CustomClasses.TEXT_AUXILIARY].join(" ") }; }, [inGraph]); const zoteroLinks = useMemo(() => { return ( <> ); }, [item]); const importMetadata = useCallback(async () => { const outcome = await importItemMetadata({ item, pdfs, notes }, inGraph, metadataSettings, typemap, notesSettings, annotationsSettings); if(outcome.success && outcome.page.new){ updateRoamCitekeys(); } return outcome; }, [annotationsSettings, inGraph, item, metadataSettings, pdfs, notes, notesSettings, typemap, updateRoamCitekeys]); const importMetadataAndOpen = useCallback(async() => { const { success, args: { uid } } = await importMetadata(); if(success){ openInSidebarByUID(uid); } }, [importMetadata]); const navigateToPage = useCallback(() => { if(inGraph != false){ openPageByUID(inGraph); handleClose(); } }, [handleClose, inGraph]); const openPageInSidebar = useCallback(() => { if (inGraph != false) { openInSidebarByUID(inGraph); } }, [inGraph]); const pdfChildren = useMemo(() => { if(pdfs.length > 0){ const libLoc = item.library.type == "group" ? ("groups/" + item.library.id) : "library"; return ( <> {pdfs.map((p,i) => { const pdfHref = (["linked_file", "imported_file", "imported_url"].includes(p.data.linkMode)) ? `zotero://open-pdf/${libLoc}/items/${p.data.key}` : p.data.url; return ; })} ); } else { return null; } }, [item.library, pdfs]); const actionsMenu = useMemo(() => { if(!inGraph){ return ( {zoteroLinks} {pdfChildren} ); } else { return ( {zoteroLinks} {pdfChildren} ); } }, [importMetadata, importMetadataAndOpen, inGraph, navigateToPage, openPageInSidebar, pdfChildren, zoteroLinks]); return (