import { Search, X } from 'lucide-react'; import { Dialog } from '@headlessui/react'; import { useState, useEffect } from 'react'; import { useAvailablePluginsQuery, useUpdateUserPluginsMutation, } from 'librechat-data-provider/react-query'; import type { TError, TPluginAction } from 'librechat-data-provider'; import type { TPluginStoreDialogProps } from '~/common/types'; import { useLocalize, usePluginDialogHelpers, useSetIndexOptions, useAuthContext } 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 updateUserPlugins = useUpdateUserPluginsMutation(); 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 = (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: () => { setTools(plugin, true); }, }, ); }; 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 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); } } // Disabled due to state setters erroneously being flagged as dependencies // eslint-disable-next-line react-hooks/exhaustive-deps }, [availablePlugins, itemsPerPage, user, searchValue, filteredPlugins, searchChanged]); 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(installActionData)} />
)}
{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;