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

4.5 KB
Raw Blame
import { X, Plus } from 'lucide';
import { motion } from 'framer-motion';
import { JSX } from 'react/jsx-runtime';
import type { ButtonHTMLAttributes } from 'react';
import type { LucideIcon } from 'lucide-react';
import type React from 'react';
import { MorphIcon } from './MorphIcon';
import { cn } from '~/utils';

interface BadgeProps
  extends Omit<
    ButtonHTMLAttributes<HTMLButtonElement>,
    'onAnimationStart' | 'onDragStart' | 'onDragEnd' | 'onDrag'
  > {
  icon?: LucideIcon;
  label: string;
  id?: string;
  isActive?: boolean;
  isEditing?: boolean;
  isDragging?: boolean;
  isAvailable: boolean;
  isInChat?: boolean;
  onBadgeAction?: () => void;
  onToggle?: () => void;
}

export default function Badge({
  icon: Icon,
  label,
  id,
  isActive = false,
  isEditing = false,
  isDragging = false,
  isAvailable = true,
  isInChat = false,
  onBadgeAction,
  onToggle,
  className,
  ...props
}: BadgeProps): JSX.Element {
  const isMoveable = isEditing && isAvailable;
  const isDisabled = id === '1' && isInChat;

  const handleClick: React.MouseEventHandler<HTMLElement> = (e) => {
    if (isDisabled) {
      e.preventDefault();
      e.stopPropagation();
      return;
    }

    if (!isEditing && onToggle) {
      e.preventDefault();
      e.stopPropagation();
      onToggle();
    }
  };

  const getWhileTapScale = () => {
    if (isDragging) {
      return 1.1;
    }
    if (isDisabled) {
      return 1;
    }
    return 0.97;
  };

  const { type: buttonType, onClick: customOnClick, ...buttonProps } = props;
  const handleRootClick: React.MouseEventHandler<HTMLElement> = customOnClick
    ? (event) => customOnClick(event as React.MouseEvent<HTMLButtonElement>)
    : handleClick;
  const badgeClassName = cn(
    'group relative inline-flex items-center gap-1.5 rounded-full px-4 py-1.5',
    'border border-border-medium text-sm font-medium transition-shadow',
    'size-9 p-2',
    /** A resting badge takes the badge label ink; a hovered or selected one moves to the primary
     *  ink, which holds AA on the hover and active fills where a muted label may not. */
    isActive
      ? 'bg-surface-active text-text-primary shadow-md'
      : 'bg-surface-chat text-badge-label shadow-xs hover:bg-surface-hover hover:text-text-primary hover:shadow-md',
    'active:scale-95 active:shadow-inner',
    isMoveable && 'cursor-move',
    isDisabled && 'cursor-not-allowed opacity-50 hover:shadow-xs',
    /** A disabled badge keeps its resting ink on hover; a selected one already rests in the
     *  primary ink. */
    isDisabled && !isActive && 'hover:text-badge-label',
    className,
  );
  const badgeContent = (
    <>
      {Icon && <Icon className={cn('relative h-5 w-5', !label && 'mx-auto')} aria-hidden="true" />}
      <span className="relative hidden">{label}</span>

      {isEditing && !isDragging && (
        <motion.button
          type="button"
          className="bg-surface-secondary-alt text-text-primary absolute -inset-e-1 -top-1 flex h-6 w-6 items-center justify-center rounded-full shadow-xs"
          aria-label={isAvailable ? `Remove ${label}` : `Restore ${label}`}
          initial={{ opacity: 0, scale: 0.8 }}
          animate={{ opacity: 1, scale: 1 }}
          exit={{ opacity: 0, scale: 0.8 }}
          whileTap={{ scale: 0.9 }}
          onMouseDown={(e) => e.stopPropagation()}
          onClick={(e) => {
            e.stopPropagation();
            onBadgeAction?.();
          }}
        >
          <MorphIcon icon={isAvailable ? X : Plus} className="h-3 w-3" />
        </motion.button>
      )}
    </>
  );

  if (isEditing) {
    return (
      <motion.div
        {...(buttonProps as React.ComponentProps<typeof motion.div>)}
        onClick={handleRootClick}
        className={badgeClassName}
        animate={{
          scale: isDragging ? 1.1 : 1,
          boxShadow: isDragging ? 'var(--theme-elevation-drag)' : undefined,
        }}
        whileTap={{ scale: getWhileTapScale() }}
        transition={{ type: 'tween', duration: 0.1, ease: 'easeOut' }}
      >
        {badgeContent}
      </motion.div>
    );
  }

  return (
    <motion.button
      type={buttonType ?? 'button'}
      onClick={handleRootClick}
      className={badgeClassName}
      animate={{
        scale: isDragging ? 1.1 : 1,
        boxShadow: isDragging ? 'var(--theme-elevation-drag)' : undefined,
      }}
      whileTap={{ scale: getWhileTapScale() }}
      transition={{ type: 'tween', duration: 0.1, ease: 'easeOut' }}
      {...(buttonProps as React.ComponentProps<typeof motion.button>)}
    >
      {badgeContent}
    </motion.button>
  );
}