import { useState, useEffect, useCallback } from 'react'; import { Dialog } from '@headlessui/react'; import { useRecoilState } from 'recoil'; import { Search, X } from 'lucide-react'; import store from '~/store'; import PluginStoreItem from './PluginStoreItem'; import PluginPagination from './PluginPagination'; import PluginAuthForm from './PluginAuthForm'; import { useAvailablePluginsQuery, useUpdateUserPluginsMutation, TPlugin, TPluginAction, tConversationSchema, TError, } from 'librechat-data-provider'; import { useAuthContext } from '~/hooks/AuthContext'; import { useLocalize } from '~/hooks'; type TPluginStoreDialogProps = { isOpen: boolean; setIsOpen: (open: boolean) => void; }; function PluginStoreDialog({ isOpen, setIsOpen }: TPluginStoreDialogProps) { const localize = useLocalize(); const { data: availablePlugins } = useAvailablePluginsQuery(); const { user } = useAuthContext(); const updateUserPlugins = useUpdateUserPluginsMutation(); const [conversation, setConversation] = useRecoilState(store.conversation) || {}; const [currentPage, setCurrentPage] = useState(1); const [itemsPerPage, setItemsPerPage] = useState(1); const [maxPage, setMaxPage] = useState(1); const [userPlugins, setUserPlugins] = useState([]); const [selectedPlugin, setSelectedPlugin] = useState(undefined); const [showPluginAuthForm, setShowPluginAuthForm] = useState(false); const [error, setError] = useState(false); const [errorMessage, setErrorMessage] = useState(''); const handleInstallError = (error: TError) => { setError(true); if (error.response?.data?.message) { setErrorMessage(error.response?.data?.message); } setTimeout(() => { setError(false); setErrorMessage(''); }, 5000); }; const handleInstall = (pluginAction: TPluginAction) => { updateUserPlugins.mutate(pluginAction, { onError: (error: unknown) => { handleInstallError(error as TError); }, }); setShowPluginAuthForm(false); }; const onPluginUninstall = (plugin: string) => { updateUserPlugins.mutate( { pluginKey: plugin, action: 'uninstall', auth: null }, { onError: (error: unknown) => { handleInstallError(error as TError); }, onSuccess: () => { //@ts-ignore - can't set a default convo or it will break routing let { tools } = conversation; tools = tools.filter((t: TPlugin) => { return t.pluginKey !== plugin; }); localStorage.setItem('lastSelectedTools', JSON.stringify(tools)); setConversation((prevState) => tConversationSchema.parse({ ...prevState, tools, }), ); }, }, ); }; const onPluginInstall = (pluginKey: string) => { const getAvailablePluginFromKey = availablePlugins?.find((p) => p.pluginKey === pluginKey); setSelectedPlugin(getAvailablePluginFromKey); const { authConfig, authenticated } = getAvailablePluginFromKey ?? {}; if (authConfig && authConfig.length > 0 && !authenticated) { setShowPluginAuthForm(true); } else { handleInstall({ pluginKey, action: 'install', auth: null }); } }; const calculateColumns = (node) => { const width = node.offsetWidth; let columns; if (width < 501) { setItemsPerPage(8); return; } else if (width < 640) { columns = 2; } else if (width < 1024) { columns = 3; } else { columns = 4; } setItemsPerPage(columns * 2); // 2 rows }; const gridRef = useCallback( (node) => { if (node !== null) { if (itemsPerPage === 1) { calculateColumns(node); } const resizeObserver = new ResizeObserver(() => calculateColumns(node)); resizeObserver.observe(node); } }, [itemsPerPage], ); const [searchValue, setSearchValue] = useState(''); const filteredPlugins = availablePlugins?.filter((plugin) => plugin.name.toLowerCase().includes(searchValue.toLowerCase()), ); useEffect(() => { if (user && user.plugins) { setUserPlugins(user.plugins); } if (filteredPlugins) { setMaxPage(Math.ceil(filteredPlugins.length / itemsPerPage)); setCurrentPage(1); // Reset the current page to 1 whenever the filtered list changes } }, [availablePlugins, itemsPerPage, user, searchValue]); // Add searchValue to the dependency list const handleChangePage = (page: number) => { setCurrentPage(page); }; return ( setIsOpen(false)} className="relative z-[102]"> {/* The backdrop, rendered as a fixed sibling to the panel container */}
{/* Full-screen container to center the panel */}
{localize('com_nav_plugin_store')}
{error && (
{localize('com_nav_plugin_auth_error')} {errorMessage}
)} {showPluginAuthForm && (
handleInstall(installActionData)} />
)}
setSearchValue(e.target.value)} placeholder={localize('com_nav_plugin_search')} style={{ width: '100%', paddingLeft: '30px', border: '1px solid #ccc', borderRadius: '4px', // This rounds the corners }} />
{filteredPlugins && filteredPlugins .slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage) .map((plugin, index) => ( onPluginInstall(plugin.pluginKey)} onUninstall={() => onPluginUninstall(plugin.pluginKey)} /> ))}
{maxPage > 0 ? ( ) : (
)} {/* API not yet implemented: */} {/*
*/}
); } export default PluginStoreDialog;