Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

3.1 KB
Raw Blame
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { RAGFlowNodeType } from '@/interfaces/database/agent';
import { PenLine } from 'lucide-react';
import { useCallback, useLayoutEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { BeginId, Operator } from '../constant';
import { useHandleNodeNameChange } from '../hooks/use-change-node-name';
import { useIsMcp } from '../hooks/use-is-mcp';
import { RAGFlowTooltip } from '@/components/ui/tooltip';

type TitleInputProps = {
  node?: RAGFlowNodeType;
  readOnly?: boolean;
};

export function TitleInput({ node, readOnly = false }: TitleInputProps) {
  const { t } = useTranslation();
  const inputRef = useRef<HTMLInputElement>(null);
  const { name, handleNameBlur, handleNameChange } = useHandleNodeNameChange({
    id: node?.id,
    data: node?.data,
  });

  const operatorName: Operator = node?.data.label as Operator;
  const isMcp = useIsMcp(operatorName);
  const [isEditingMode, setIsEditingMode] = useState(false);

  const switchIsEditingMode = useCallback(() => {
    setIsEditingMode((prev) => !prev);
  }, []);

  const handleBlur = useCallback(
    (e: React.FocusEvent<HTMLInputElement>) => {
      if (handleNameBlur()) {
        setIsEditingMode(false);
      } else {
        // Re-focus the input if name doesn't change successfully
        e.target.focus();
        e.target.select();
      }
    },
    [handleNameBlur],
  );

  useLayoutEffect(() => {
    if (isEditingMode && inputRef.current) {
      inputRef.current.focus();
      inputRef.current.select();
    }
  }, [isEditingMode]);

  if (isMcp) {
    return <div className="flex-1 text-base">MCP Config</div>;
  }

  return (
    // Give a fixed height to prevent layout shift when switching between edit and view modes
    <div className="flex items-center gap-1 flex-1 h-8 mr-2 min-w-0">
      {node?.id === BeginId ? (
        // Begin node is not editable
        <span>{t(`flow.${BeginId}`)}</span>
      ) : readOnly ? (
        // A read-only canvas never enters name-editing mode: renaming writes
        // the store via useHandleNodeNameChange.
        <div className="flex items-center gap-2.5 text-base">{name}</div>
      ) : isEditingMode ? (
        <Input
          ref={inputRef}
          value={name}
          onBlur={handleBlur}
          onKeyDown={(e) => {
            // Support committing the value changes by pressing Enter
            if (e.key === 'Enter') {
              handleBlur(e as unknown as React.FocusEvent<HTMLInputElement>);
            }
          }}
          onChange={handleNameChange}
        />
      ) : (
        <div className="flex items-center text-base w-full">
          <RAGFlowTooltip tooltip={name} asChild>
            <span className="truncate">{name}</span>
          </RAGFlowTooltip>

          <Button
            variant="transparent"
            size="icon"
            className="size-6 !p-0 border-0 bg-transparent"
            onClick={switchIsEditingMode}
          >
            <PenLine className="size-3.5 text-text-secondary cursor-pointer" />
          </Button>
        </div>
      )}
    </div>
  );
}