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 { useId, useState, useEffect } from 'react';
import { Info } from 'lucide-react';
import { Input } from '@librechat/client';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';

const MAX_LENGTH = 120;

const Description = ({
  initialValue,
  onValueChange,
  disabled,
  tabIndex,
  labelBgClassName = 'bg-surface-primary-alt',
}: {
  initialValue?: string;
  onValueChange?: (value: string) => void;
  disabled?: boolean;
  tabIndex?: number;
  /** Surface the floating label notches out of; must match whatever sits behind the field */
  labelBgClassName?: string;
}) => {
  const localize = useLocalize();
  const descriptionId = useId();
  const [description, setDescription] = useState(initialValue || '');
  const [charCount, setCharCount] = useState(initialValue?.length || 0);

  useEffect(() => {
    setDescription(initialValue || '');
    setCharCount(initialValue?.length || 0);
  }, [initialValue]);

  useEffect(() => {
    setCharCount(description.length);
  }, [description]);

  const handleInputChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
    if (e.target.value.length <= MAX_LENGTH) {
      setDescription(e.target.value);
      onValueChange?.(e.target.value);
    }
  };

  if (disabled && !description) {
    return null;
  }

  return (
    <div className="border-border-inset-medium rounded-xl border">
      <label
        htmlFor={descriptionId}
        className="text-text-secondary block px-4 pt-2 text-sm md:hidden"
      >
        {localize('com_ui_description_placeholder')}
      </label>
      <div className="text-text-secondary relative flex h-10 items-center gap-1 ps-4 pe-2 text-sm">
        <Info className="icon-sm shrink-0" aria-hidden="true" />
        {/* The wrapper reserves the counter's side so it follows the inline end; <Input> owns its own padding. */}
        <div className="relative min-w-0 flex-1 pe-11">
          <Input
            type="text"
            id={descriptionId}
            tabIndex={tabIndex}
            disabled={disabled}
            placeholder=" "
            value={description}
            onChange={handleInputChange}
            className="peer w-full border-none"
            aria-label={localize('com_ui_description_placeholder')}
          />
          <label
            htmlFor={descriptionId}
            className={cn(
              'text-text-secondary peer-focus:text-text-primary pointer-events-none absolute inset-s-0 top-0.5 hidden max-w-[calc(100%-3.5rem)] origin-[0] translate-y-2 scale-100 rounded px-1 text-sm transition-transform duration-200 peer-placeholder-shown:translate-y-2 peer-placeholder-shown:scale-100 peer-focus:-translate-y-3 peer-focus:scale-75 peer-[:not(:placeholder-shown)]:-translate-y-3 peer-[:not(:placeholder-shown)]:scale-75 md:block rtl:origin-right',
              labelBgClassName,
            )}
          >
            {localize('com_ui_description_placeholder')}
          </label>
        </div>
        {!disabled && (
          <span className="text-text-secondary absolute inset-e-2 shrink-0 text-xs md:text-sm">{`${charCount}/${MAX_LENGTH}`}</span>
        )}
      </div>
    </div>
  );
};

export default Description;