import { useState } from 'react'; import { Settings } from 'lucide-react'; import { useRecoilValue } from 'recoil'; import { EModelEndpoint } from 'librechat-data-provider'; import { useGetEndpointsQuery } from 'librechat-data-provider/react-query'; import type { TConversation } from 'librechat-data-provider'; import type { FC } from 'react'; import { cn, getConvoSwitchLogic, getEndpointField, getIconKey } from '~/utils'; import { useLocalize, useUserKey, useDefaultConvo } from '~/hooks'; import { SetKeyDialog } from '~/components/Input/SetKeyDialog'; import { useChatContext } from '~/Providers'; import { icons } from './Icons'; import store from '~/store'; type MenuItemProps = { title: string; value: EModelEndpoint; selected: boolean; description?: string; userProvidesKey: boolean; // iconPath: string; // hoverContent?: string; }; const MenuItem: FC = ({ title, value: endpoint, description, selected, userProvidesKey, ...rest }) => { const modularChat = useRecoilValue(store.modularChat); const [isDialogOpen, setDialogOpen] = useState(false); const { data: endpointsConfig } = useGetEndpointsQuery(); const { conversation, newConversation } = useChatContext(); const getDefaultConversation = useDefaultConvo(); const { getExpiry } = useUserKey(endpoint); const localize = useLocalize(); const expiryTime = getExpiry() ?? ''; const onSelectEndpoint = (newEndpoint?: EModelEndpoint) => { if (!newEndpoint) { return; } if (!expiryTime) { setDialogOpen(true); } const { template, shouldSwitch, isNewModular, newEndpointType, isCurrentModular, isExistingConversation, } = getConvoSwitchLogic({ newEndpoint, modularChat, conversation, endpointsConfig, }); const isModular = isCurrentModular && isNewModular && shouldSwitch; if (isExistingConversation && isModular) { template.endpointType = newEndpointType; const currentConvo = getDefaultConversation({ /* target endpointType is necessary to avoid endpoint mixing */ conversation: { ...(conversation ?? {}), endpointType: template.endpointType }, preset: template, }); /* We don't reset the latest message, only when changing settings mid-converstion */ newConversation({ template: currentConvo, preset: currentConvo, keepLatestMessage: true, keepAddedConvos: true, }); return; } newConversation({ template: { ...(template as Partial) }, keepAddedConvos: isModular, }); }; const endpointType = getEndpointField(endpointsConfig, endpoint, 'type'); const iconKey = getIconKey({ endpoint, endpointsConfig, endpointType }); const Icon = icons[iconKey]; return ( <>
onSelectEndpoint(endpoint)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); onSelectEndpoint(endpoint); } }} >
{Icon != null && ( )}
{title}
{description}
{userProvidesKey ? (
) : null} {selected && ( )} {(!userProvidesKey || expiryTime) && (
{!userProvidesKey &&
{localize('com_ui_new_chat')}
}
)}
{userProvidesKey && ( )} ); }; export default MenuItem;