import { useState, useEffect, useCallback } from 'react'; import { Dialog } from '@headlessui/react'; import { useRecoilState } from 'recoil'; import { X } from 'lucide-react'; import store from '~/store'; import { PluginStoreItem, PluginPagination, PluginAuthForm } from '.'; import { useAvailablePluginsQuery, useUpdateUserPluginsMutation, TPlugin, } from '@librechat/data-provider'; import { useAuthContext } from '~/hooks/AuthContext'; type TPluginStoreDialogProps = { isOpen: boolean; setIsOpen: (open: boolean) => void; }; export type TPluginAction = { pluginKey: string; action: 'install' | 'uninstall'; auth?: unknown; }; function PluginStoreDialog({ isOpen, setIsOpen }: TPluginStoreDialogProps) { 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: any) => { 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) => { handleInstallError(error); }, }); setShowPluginAuthForm(false); }; const onPluginUninstall = (plugin: string) => { updateUserPlugins.mutate( { pluginKey: plugin, action: 'uninstall', auth: null }, { onError: (error: any) => { handleInstallError(error); }, 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: any) => ({ ...prevState, tools, })); }, }, ); }; const onPluginInstall = (pluginKey: string) => { const getAvailablePluginFromKey = availablePlugins?.find((p) => p.pluginKey === pluginKey); setSelectedPlugin(getAvailablePluginFromKey); if ( getAvailablePluginFromKey!.authConfig.length > 0 && !getAvailablePluginFromKey?.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], ); useEffect(() => { if (user) { if (user.plugins) { setUserPlugins(user.plugins); } } if (availablePlugins) { setMaxPage(Math.ceil(availablePlugins.length / itemsPerPage)); } }, [availablePlugins, itemsPerPage, user]); 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 */}
Plugin store
{error && (
There was an error attempting to authenticate this plugin. Please try again.{' '} {errorMessage}
)} {showPluginAuthForm && (
handleInstall(installActionData)} />
)}
{availablePlugins && availablePlugins .slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage) .map((plugin, index) => ( onPluginInstall(plugin.pluginKey)} onUninstall={() => onPluginUninstall(plugin.pluginKey)} /> ))}
{maxPage > 1 && (
)} {/* API not yet implemented: */} {/*
*/}
); } export default PluginStoreDialog;