import { Search, X } from 'lucide-react'; import { Dialog, DialogPanel, DialogTitle } from '@headlessui/react'; import { useState, useEffect, useCallback } from 'react'; import { useAvailablePluginsQuery } from 'librechat-data-provider/react-query'; import type { TError, TPlugin, TPluginAction } from 'librechat-data-provider'; import type { TPluginStoreDialogProps } from '~/common/types'; import { usePluginDialogHelpers, useSetIndexOptions, usePluginInstall, useAuthContext, useLocalize, } from '~/hooks'; import PluginPagination from './PluginPagination'; import PluginStoreItem from './PluginStoreItem'; import PluginAuthForm from './PluginAuthForm'; function PluginStoreDialog({ isOpen, setIsOpen }: TPluginStoreDialogProps) { const localize = useLocalize(); const { user } = useAuthContext(); const { data: availablePlugins } = useAvailablePluginsQuery(); const { setTools } = useSetIndexOptions(); const [userPlugins, setUserPlugins] = useState([]); const { maxPage, setMaxPage, currentPage, setCurrentPage, itemsPerPage, searchChanged, setSearchChanged, searchValue, setSearchValue, gridRef, handleSearch, handleChangePage, error, setError, errorMessage, setErrorMessage, showPluginAuthForm, setShowPluginAuthForm, selectedPlugin, setSelectedPlugin, } = usePluginDialogHelpers(); const handleInstallError = useCallback( (error: TError) => { setError(true); if (error.response?.data?.message) { setErrorMessage(error.response.data.message); } setTimeout(() => { setError(false); setErrorMessage(''); }, 5000); }, [setError, setErrorMessage], ); const { installPlugin, uninstallPlugin } = usePluginInstall({ onInstallError: handleInstallError, onUninstallError: handleInstallError, onUninstallSuccess: (_data, variables) => { setTools(variables.pluginKey, true); }, }); const handleInstall = (pluginAction: TPluginAction, plugin?: TPlugin) => { if (!plugin) { return; } installPlugin(pluginAction, plugin); setShowPluginAuthForm(false); }; const onPluginInstall = (pluginKey: string) => { const plugin = availablePlugins?.find((p) => p.pluginKey === pluginKey); if (!plugin) { return; } setSelectedPlugin(plugin); const { authConfig, authenticated } = plugin ?? {}; if (authConfig && authConfig.length > 0 && !authenticated) { setShowPluginAuthForm(true); } else { handleInstall({ pluginKey, action: 'install', auth: null }, plugin); } }; 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)); if (searchChanged) { setCurrentPage(1); setSearchChanged(false); } } }, [ availablePlugins, itemsPerPage, user, searchValue, filteredPlugins, searchChanged, setMaxPage, setCurrentPage, setSearchChanged, ]); return ( { setIsOpen(false); setCurrentPage(1); setSearchValue(''); }} 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(action, selectedPlugin)} />
)}
{filteredPlugins && filteredPlugins .slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage) .map((plugin, index) => ( onPluginInstall(plugin.pluginKey)} onUninstall={() => uninstallPlugin(plugin.pluginKey)} /> ))}
{maxPage > 0 ? ( ) : (
)} {/* API not yet implemented: */} {/*
*/}
); } export default PluginStoreDialog;