Skip to content

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

4.4 KB
Raw Blame
import { NodeProps, NodeResizeControl } from '@xyflow/react';

import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormMessage,
} from '@/components/ui/form';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { INoteNode } from '@/interfaces/database/agent';
import { zodResolver } from '@hookform/resolvers/zod';
import { NotebookPen } from 'lucide-react';
import { memo } from 'react';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { z } from 'zod';
import { useCanvasReadonly } from '../../../context';
import { NodeWrapper } from '../node-wrapper';
import { ResizeIcon, controlStyle } from '../resize-icon';
import { ToolBar } from '../toolbar';
import { useWatchFormChange, useWatchNameFormChange } from './use-watch-change';

const FormSchema = z.object({
  text: z.string(),
});

const NameFormSchema = z.object({
  name: z.string(),
});

type NoteNodeProps = NodeProps<INoteNode> & {
  useWatchNoteFormChange?: typeof useWatchFormChange;
  useWatchNoteNameFormChange?: typeof useWatchNameFormChange;
};

function NoteNode({
  data,
  id,
  selected,
  useWatchNoteFormChange,
  useWatchNoteNameFormChange,
}: NoteNodeProps) {
  const { t } = useTranslation();
  const readOnly = useCanvasReadonly();

  const form = useForm<z.infer<typeof FormSchema>>({
    resolver: zodResolver(FormSchema),
    defaultValues: data.form,
  });

  const nameForm = useForm<z.infer<typeof NameFormSchema>>({
    resolver: zodResolver(NameFormSchema),
    defaultValues: { name: data.name },
  });

  // Read-only canvas: pass no id so the sync hooks (which write the form back
  // to the store) stay inert, and the inputs below are disabled.
  (useWatchNoteFormChange || useWatchFormChange)(
    readOnly ? undefined : id,
    form,
  );

  (useWatchNoteNameFormChange || useWatchNameFormChange)(
    readOnly ? undefined : id,
    nameForm,
  );

  return (
    <ToolBar
      selected={selected}
      id={id}
      label={data.label}
      showRun={false}
      showCopy={false}
    >
      <NodeWrapper
        className="p-0  w-full h-full flex flex-col bg-bg-component border border-accent-primary rounded-lg shadow-md pb-1"
        selected={selected}
      >
        {!readOnly && (
          <NodeResizeControl
            minWidth={190}
            minHeight={128}
            style={controlStyle}
          >
            <ResizeIcon />
          </NodeResizeControl>
        )}
        <section className="px-2 py-1 flex gap-2 items-center note-drag-handle rounded-t border-t-2 border-accent-primary">
          <NotebookPen className="size-4" />
          <Form {...nameForm}>
            <form onSubmit={nameForm.handleSubmit(() => {})} className="flex-1">
              <FormField
                control={nameForm.control}
                name="name"
                render={({ field }) => (
                  <FormItem className="h-full">
                    <FormControl>
                      <Input
                        placeholder={t('flow.notePlaceholder')}
                        {...field}
                        type="text"
                        disabled={readOnly}
                        className="bg-transparent border-none focus-visible:outline focus-visible:outline-text-sub-title p-1"
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />
            </form>
          </Form>
        </section>
        <Form {...form}>
          <form className="flex-1 px-1 min-h-1">
            <FormField
              control={form.control}
              name="text"
              render={({ field }) => (
                <FormItem className="h-full pb-2">
                  <FormControl>
                    <Textarea
                      placeholder={t('flow.notePlaceholder')}
                      resize="none"
                      disabled={readOnly}
                      className="resize-none rounded-none p-1 py-0 overflow-auto bg-transparent focus-visible:ring-0 border-none text-text-secondary focus-visible:ring-offset-0 !text-xs h-full"
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
          </form>
        </Form>
      </NodeWrapper>
    </ToolBar>
  );
}

export default memo(NoteNode);