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

7.5 KB
Raw Blame
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { X } from 'lucide-react';
import {
  bindingDisplayKeys,
  bindingHash,
  isCancelKey,
  isModifierKey,
  isValidBinding,
  normalizeKey,
} from '@librechat/chat';
import type { ShortcutBinding } from '@librechat/chat';
import type { RefObject } from 'react';
import { isMac } from '~/hooks/useKeyboardShortcuts';
import ShortcutKeyCombo from './ShortcutKeyCombo';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';

type ConflictInfo = {
  conflictId: string;
  conflictLabel: string;
  binding: ShortcutBinding;
};

type RecorderState = {
  containerRef: RefObject<HTMLDivElement>;
  boundaryRef: RefObject<HTMLDivElement>;
  previewKeys: string[];
  hasConflict: boolean;
  conflict: ConflictInfo | null;
  showInvalid: boolean;
  showHint: boolean;
  onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>) => void;
  onKeyUp: () => void;
  onTryAgain: () => void;
};

type Options = {
  initial: ShortcutBinding | null;
  bindingMap: Map<string, string>;
  ownerId: string;
  getActionLabel: (id: string) => string;
  onSave: (binding: ShortcutBinding) => void;
  onCancel: () => void;
};

export function useShortcutRecorder({
  initial,
  bindingMap,
  ownerId,
  getActionLabel,
  onSave,
  onCancel,
}: Options): RecorderState {
  const containerRef = useRef<HTMLDivElement>(null);
  const boundaryRef = useRef<HTMLDivElement>(null);
  const [pending, setPending] = useState<ShortcutBinding | null>(null);
  const [draft, setDraft] = useState<ShortcutBinding | null>(initial);
  const [error, setError] = useState<'noModifier' | null>(null);
  const [conflict, setConflict] = useState<ConflictInfo | null>(null);

  const previewKeys = useMemo(() => {
    const source = pending ?? draft;
    if (!source) return [];
    return bindingDisplayKeys(source, isMac);
  }, [pending, draft]);

  const onKeyDown = useCallback(
    (e: React.KeyboardEvent<HTMLDivElement>) => {
      e.preventDefault();
      e.stopPropagation();

      if (isCancelKey(e.nativeEvent)) {
        onCancel();
        return;
      }

      const previewBinding: ShortcutBinding = {
        meta: e.metaKey,
        ctrl: e.ctrlKey,
        alt: e.altKey,
        shift: e.shiftKey,
        key: isModifierKey(e.key) ? '' : normalizeKey(e.key, e.shiftKey),
      };

      if (!previewBinding.key) {
        setPending({ ...previewBinding, key: '…' });
        return;
      }

      setPending(null);
      const validation = isValidBinding(previewBinding);
      if (!validation.valid) {
        setDraft(previewBinding);
        setError('noModifier');
        setConflict(null);
        return;
      }
      setError(null);
      const hash = bindingHash(previewBinding);
      const conflictId = bindingMap.get(hash);
      if (conflictId && conflictId !== ownerId) {
        setDraft(previewBinding);
        setConflict({
          conflictId,
          conflictLabel: getActionLabel(conflictId),
          binding: previewBinding,
        });
        return;
      }
      setConflict(null);
      onSave(previewBinding);
    },
    [bindingMap, getActionLabel, onCancel, onSave, ownerId],
  );

  const onKeyUp = useCallback(() => {
    setPending(null);
  }, []);

  const onTryAgain = useCallback(() => {
    setDraft(initial);
    setConflict(null);
    setError(null);
    containerRef.current?.focus();
  }, [initial]);

  useEffect(() => {
    containerRef.current?.focus();
  }, []);

  useEffect(() => {
    function onPointerDown(e: PointerEvent) {
      const root = boundaryRef.current ?? containerRef.current;
      if (root && !root.contains(e.target as Node)) {
        onCancel();
      }
    }
    document.addEventListener('pointerdown', onPointerDown);
    return () => document.removeEventListener('pointerdown', onPointerDown);
  }, [onCancel]);

  return {
    containerRef,
    boundaryRef,
    previewKeys,
    hasConflict: !!conflict,
    conflict,
    showInvalid: error === 'noModifier',
    showHint: previewKeys.length === 0,
    onKeyDown,
    onKeyUp,
    onTryAgain,
  };
}

export function RecorderPill({
  state,
  ariaLabel,
  ownerId,
}: {
  state: RecorderState;
  ariaLabel: string;
  ownerId: string;
}) {
  const localize = useLocalize();
  const { containerRef, previewKeys, hasConflict, showInvalid, showHint, onKeyDown, onKeyUp } =
    state;
  let stateBorder = 'border-border-medium';
  if (hasConflict) {
    stateBorder = 'border-status-warning-border';
  } else if (showInvalid) {
    stateBorder = 'animate-shortcut-shake border-border-destructive';
  }
  return (
    <div
      ref={containerRef}
      role="textbox"
      tabIndex={0}
      aria-label={ariaLabel}
      aria-describedby={`${ownerId}-recorder-hint`}
      data-testid={`shortcut-recorder-${ownerId}`}
      onKeyDown={onKeyDown}
      onKeyUp={onKeyUp}
      className={cn(
        'bg-surface-primary flex h-[1.875rem] items-center gap-1.5 rounded-md border px-2 outline-hidden transition-colors',
        'focus-visible:ring-text-primary focus-visible:ring-offset-surface-primary-alt focus-visible:ring-2 focus-visible:ring-offset-1',
        stateBorder,
      )}
    >
      {showHint ? (
        <span className="text-text-secondary text-[11.5px]">
          {localize('com_shortcut_recorder_placeholder')}
        </span>
      ) : (
        <ShortcutKeyCombo keys={previewKeys} />
      )}
    </div>
  );
}

export function RecorderInfo({
  state,
  ownerId,
  onCancel,
  onSaveReplacing,
}: {
  state: RecorderState;
  ownerId: string;
  onCancel: () => void;
  onSaveReplacing: (binding: ShortcutBinding, conflictId: string) => void;
}) {
  const localize = useLocalize();
  const { hasConflict, conflict, showInvalid, onTryAgain } = state;

  if (hasConflict && conflict) {
    return (
      <div
        id={`${ownerId}-recorder-hint`}
        className="flex flex-wrap items-center justify-end gap-x-2 gap-y-1 ps-1 text-[11.5px]"
      >
        <span className="text-text-secondary">
          {localize('com_shortcut_recorder_conflict_prefix')}{' '}
          <span className="text-text-primary font-medium">{conflict.conflictLabel}</span>
        </span>
        <div className="flex shrink-0 items-center gap-1">
          <button
            type="button"
            onClick={onTryAgain}
            className="text-text-secondary hover:text-text-primary rounded-md px-1.5 py-0.5 whitespace-nowrap transition-colors"
          >
            {localize('com_shortcut_recorder_try_again')}
          </button>
          <button
            type="button"
            onClick={() => onSaveReplacing(conflict.binding, conflict.conflictId)}
            className="bg-surface-tertiary text-text-primary hover:bg-surface-active-alt rounded-md px-2 py-0.5 font-medium whitespace-nowrap transition-colors"
          >
            {localize('com_shortcut_recorder_replace')}
          </button>
        </div>
      </div>
    );
  }

  return (
    <div id={`${ownerId}-recorder-hint`} className="flex items-center justify-end gap-2 ps-1">
      <span
        className={cn(
          'text-[11.5px]',
          showInvalid ? 'text-text-destructive' : 'text-text-secondary',
        )}
      >
        {showInvalid
          ? localize('com_shortcut_recorder_needs_modifier')
          : localize('com_shortcut_recorder_hint')}
      </span>
      <button
        type="button"
        onClick={onCancel}
        aria-label={localize('com_ui_cancel')}
        className="text-text-secondary hover:bg-surface-active-alt hover:text-text-primary inline-flex h-5 w-5 items-center justify-center rounded transition-colors"
      >
        <X className="h-3 w-3" />
      </button>
    </div>
  );
}