Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

3.1 KB
Raw Blame
import { useState } from 'react';
import { Label, Switch, HoverCard, HoverCardTrigger } from '@librechat/client';
import type { DynamicSettingProps } from 'librechat-data-provider';
import { TranslationKeys, useLocalize, useParameterEffects } from '~/hooks';
import { useChatContext } from '~/Providers';
import OptionHover from './OptionHover';
import { ESide } from '~/common';

function DynamicSwitch({
  label = '',
  settingKey,
  defaultValue,
  enumMappings,
  description = '',
  columnSpan,
  setOption,
  readonly = false,
  showDefault = false,
  labelCode = false,
  descriptionCode = false,
  conversation,
}: DynamicSettingProps) {
  const localize = useLocalize();
  const { preset } = useChatContext();
  const [inputValue, setInputValue] = useState<boolean>(!!(defaultValue as boolean | undefined));
  useParameterEffects({
    preset,
    settingKey,
    defaultValue,
    conversation,
    inputValue,
    setInputValue,
    preventDelayedUpdate: true,
  });

  const savedValue = conversation?.[settingKey] ?? defaultValue;
  const effectiveValue = enumMappings?.[String(savedValue)];
  const selectedValue = typeof effectiveValue === 'boolean' ? effectiveValue : savedValue;
  const routeIsForced =
    typeof enumMappings?.true === 'boolean' && enumMappings.true === enumMappings.false;

  const handleCheckedChange = (checked: boolean) => {
    setInputValue(checked);
    setOption(settingKey)(checked);
  };

  return (
    <div
      className={`flex flex-col items-center justify-start gap-6 ${
        columnSpan != null ? `col-span-${columnSpan}` : 'col-span-full'
      }`}
    >
      <HoverCard openDelay={300}>
        <HoverCardTrigger asChild>
          <div className="grid h-full w-full content-between items-center gap-2">
            <div className="flex justify-between">
              <Label
                htmlFor={`${settingKey}-dynamic-switch`}
                className="text-start text-xs font-medium break-words"
              >
                {labelCode ? (localize(label as TranslationKeys) ?? label) : label || settingKey}{' '}
                {showDefault && (
                  <small className="high-contrast:opacity-100 opacity-40">
                    ({localize('com_endpoint_default')}:{' '}
                    {defaultValue != null ? localize('com_ui_on') : localize('com_ui_off')})
                  </small>
                )}
              </Label>
            </div>
            <Switch
              id={`${settingKey}-dynamic-switch`}
              checked={selectedValue}
              onCheckedChange={handleCheckedChange}
              disabled={readonly || routeIsForced}
              className="flex"
              aria-label={
                labelCode ? (localize(label as TranslationKeys) ?? label) : label || settingKey
              }
            />
          </div>
        </HoverCardTrigger>
        {description && (
          <OptionHover
            description={
              descriptionCode
                ? (localize(description as TranslationKeys) ?? description)
                : description
            }
            side={ESide.Left}
          />
        )}
      </HoverCard>
    </div>
  );
}

export default DynamicSwitch;