import { useEffect, useState, type FC } from 'react'; import { useRecoilValue } from 'recoil'; import { useLocation } from 'react-router-dom'; import { TConversation } from 'librechat-data-provider'; import { Content, Portal, Root, Trigger } from '@radix-ui/react-popover'; import { BookmarkFilledIcon, BookmarkIcon } from '@radix-ui/react-icons'; import { useConversationTagsQuery, useTagConversationMutation } from '~/data-provider'; import { BookmarkMenuItems } from './Bookmarks/BookmarkMenuItems'; import { BookmarkContext } from '~/Providers/BookmarkContext'; import { Spinner } from '~/components'; import { useLocalize } from '~/hooks'; import { cn } from '~/utils'; import store from '~/store'; const SAVED_TAG = 'Saved'; const BookmarkMenu: FC = () => { const localize = useLocalize(); const location = useLocation(); const activeConvo = useRecoilValue(store.conversationByIndex(0)); const globalConvo = useRecoilValue(store.conversation) ?? ({} as TConversation); const [tags, setTags] = useState(); const [open, setIsOpen] = useState(false); const [conversation, setConversation] = useState(); let thisConversation: TConversation | null | undefined; if (location.state?.from?.pathname.includes('/chat')) { thisConversation = globalConvo; } else { thisConversation = activeConvo; } const { mutateAsync, isLoading } = useTagConversationMutation( thisConversation?.conversationId ?? '', ); const { data } = useConversationTagsQuery(); useEffect(() => { if ( (!conversation && thisConversation) || (conversation && thisConversation && conversation.conversationId !== thisConversation.conversationId) ) { setConversation(thisConversation); setTags(thisConversation.tags ?? []); } if (tags === undefined && conversation) { setTags(conversation.tags ?? []); } }, [thisConversation, conversation, tags]); const isActiveConvo = thisConversation && thisConversation.conversationId && thisConversation.conversationId !== 'new' && thisConversation.conversationId !== 'search'; if (!isActiveConvo) { return <>; } const onOpenChange = async (open: boolean) => { if (!open) { setIsOpen(open); return; } if (open && tags && tags.length > 0) { setIsOpen(open); } else { if (thisConversation && thisConversation.conversationId) { await mutateAsync({ conversationId: thisConversation.conversationId, tags: [SAVED_TAG], }); setTags([SAVED_TAG]); setConversation({ ...thisConversation, tags: [SAVED_TAG] }); } } }; return ( {data && conversation && ( // Display all bookmarks registered by the user and highlight the tags of the currently selected conversation )} ); }; export default BookmarkMenu;