import { WritableAtom, useAtom } from 'jotai'; import { RecoilState, useRecoilState } from 'recoil'; import { Switch, InfoHoverCard, ESide } from '@librechat/client'; import { useLocalize } from '~/hooks'; type LocalizeFn = ReturnType; type LocalizeKey = Parameters[0]; interface ToggleSwitchProps { stateAtom: RecoilState | WritableAtom; localizationKey: LocalizeKey; hoverCardText?: LocalizeKey; switchId: string; onCheckedChange?: (value: boolean) => void; showSwitch?: boolean; disabled?: boolean; strongLabel?: boolean; } function isRecoilState(atom: unknown): atom is RecoilState { return atom != null && typeof atom === 'object' && 'key' in atom; } const RecoilToggle: React.FC< Omit & { stateAtom: RecoilState } > = ({ stateAtom, localizationKey, hoverCardText, switchId, onCheckedChange, disabled = false, strongLabel = false, }) => { const [switchState, setSwitchState] = useRecoilState(stateAtom); const localize = useLocalize(); const handleCheckedChange = (value: boolean) => { setSwitchState(value); onCheckedChange?.(value); }; const labelId = `${switchId}-label`; return (
{strongLabel ? {localize(localizationKey)} : localize(localizationKey)}
{hoverCardText && }
); }; const JotaiToggle: React.FC< Omit & { stateAtom: WritableAtom } > = ({ stateAtom, localizationKey, hoverCardText, switchId, onCheckedChange, disabled = false, strongLabel = false, }) => { const [switchState, setSwitchState] = useAtom(stateAtom); const localize = useLocalize(); const handleCheckedChange = (value: boolean) => { setSwitchState(value); onCheckedChange?.(value); }; const labelId = `${switchId}-label`; return (
{strongLabel ? {localize(localizationKey)} : localize(localizationKey)}
{hoverCardText && }
); }; const ToggleSwitch: React.FC = (props) => { const { stateAtom, showSwitch = true } = props; if (!showSwitch) { return null; } const isRecoil = isRecoilState(stateAtom); if (isRecoil) { return } />; } return } />; }; export default ToggleSwitch;