LibreChat/packages/client/src/components/Radio.tsx
Marco Beretta e4870ed0b0
🔌 refactor: MCP UI with Improved Accessibility and Reusable Components (#11118)
* feat: enhance MCP server selection UI with new components and improved accessibility

* fix(i18n): add missing com_ui_mcp_servers translation key

The MCP server menu aria-label was referencing a non-existent translation
key. Added the missing key for accessibility.

* feat(MCP): enhance MCP components with improved accessibility and focus management

* fix(i18n): remove outdated MCP server translation keys

* fix(MCPServerList): improve color contrast by updating text color for no MCP servers message

* refactor(MCP): Server status components and improve user action handling
Updated MCPServerStatusIcon to use a unified icon system for better clarity
Introduced new MCPCardActions component for standardized action buttons on server cards
Created MCPServerCard component to encapsulate server display logic and actions
Enhanced MCPServerList to render MCPServerCard components, improving code organization
Added MCPStatusBadge for consistent status representation in dialogs
Updated utility functions for status color and text retrieval to align with new design
Improved localization keys for better clarity and consistency in user messages

* style(MCP): update button and card background styles for improved UI consistency

* feat(MCP): implement global server initialization state management using Jotai

* refactor(MCP): modularize MCPServerDialog into structured component architecture

- Split monolithic dialog into dedicated section components (Auth, BasicInfo, Connection, Transport, Trust)
- Extract form logic into useMCPServerForm custom hook
- Add utility modules for JSON import and URL handling
- Introduce reusable SecretInput component in @librechat/client
- Remove deprecated MCPAuth component

* style(MCP): update button styles for improved layout and adjust empty state background color

* refactor(Radio): enhance component mounting logic and background style updates

* refactor(translation): remove unused keys and streamline localization strings
2025-12-28 12:20:15 -05:00

130 lines
4 KiB
TypeScript

import React, { useState, useRef, useLayoutEffect, useEffect, useCallback, memo } from 'react';
import { useLocalize } from '~/hooks';
interface Option {
value: string;
label: string;
icon?: React.ReactNode;
}
interface RadioProps {
options: Option[];
value?: string;
onChange?: (value: string) => void;
disabled?: boolean;
className?: string;
fullWidth?: boolean;
}
const Radio = memo(function Radio({
options,
value,
onChange,
disabled = false,
className = '',
fullWidth = false,
}: RadioProps) {
const localize = useLocalize();
const buttonRefs = useRef<(HTMLButtonElement | null)[]>([]);
const [isMounted, setIsMounted] = useState(false);
const [currentValue, setCurrentValue] = useState<string>(value ?? '');
const [backgroundStyle, setBackgroundStyle] = useState<React.CSSProperties>({});
const handleChange = (newValue: string) => {
setCurrentValue(newValue);
onChange?.(newValue);
};
const updateBackgroundStyle = useCallback(() => {
const selectedIndex = options.findIndex((opt) => opt.value === currentValue);
if (selectedIndex >= 0 && buttonRefs.current[selectedIndex]) {
const selectedButton = buttonRefs.current[selectedIndex];
const container = selectedButton?.parentElement;
if (selectedButton && container) {
const containerRect = container.getBoundingClientRect();
const buttonRect = selectedButton.getBoundingClientRect();
const offsetLeft = buttonRect.left - containerRect.left - 4;
setBackgroundStyle({
width: `${buttonRect.width}px`,
transform: `translateX(${offsetLeft}px)`,
});
}
}
}, [currentValue, options]);
// Mark as mounted after dialog animations settle
// Timeout ensures we wait for CSS transitions to complete
useEffect(() => {
const timeout = setTimeout(() => {
setIsMounted(true);
}, 50);
return () => clearTimeout(timeout);
}, []);
useLayoutEffect(() => {
if (isMounted) {
updateBackgroundStyle();
}
}, [isMounted, updateBackgroundStyle]);
useLayoutEffect(() => {
if (value !== undefined) {
setCurrentValue(value);
}
}, [value]);
if (options.length === 0) {
return (
<div
className="relative inline-flex items-center rounded-lg bg-muted p-1 opacity-50"
role="radiogroup"
>
<span className="px-4 py-2 text-xs text-muted-foreground">
{localize('com_ui_no_options')}
</span>
</div>
);
}
const selectedIndex = options.findIndex((opt) => opt.value === currentValue);
return (
<div
className={`relative ${fullWidth ? 'flex' : 'inline-flex'} items-center rounded-lg bg-muted p-1 ${className}`}
role="radiogroup"
>
{selectedIndex >= 0 && isMounted && (
<div
className="pointer-events-none absolute inset-y-1 rounded-md border border-border/50 bg-background shadow-sm transition-all duration-300 ease-out"
style={backgroundStyle}
/>
)}
{options.map((option, index) => (
<button
key={option.value}
ref={(el) => {
buttonRefs.current[index] = el;
}}
type="button"
role="radio"
aria-checked={currentValue === option.value}
onClick={() => handleChange(option.value)}
disabled={disabled}
className={`relative z-10 flex h-[34px] items-center justify-center gap-2 rounded-md px-4 text-sm font-medium transition-colors duration-150 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring ${
currentValue === option.value ? 'text-foreground' : 'text-foreground'
} ${disabled ? 'cursor-not-allowed opacity-50' : ''} ${fullWidth ? 'flex-1' : ''}`}
>
{option.icon && (
<span className="flex-shrink-0" aria-hidden="true">
{option.icon}
</span>
)}
<span className="whitespace-nowrap">{option.label}</span>
</button>
))}
</div>
);
});
export default Radio;