Skip to content

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

3.7 KB
Raw Blame
/*
 *  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import { Button } from '@/components/ui/button';
import { ChevronDown, ChevronUp } from 'lucide-react';
import { ReactNode, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';

interface ExpandableContentProps {
  children: ReactNode;
  maxHeight?: number;
  className?: string;
}

export default function ExpandableContent({
  children,
  maxHeight = 208, // 52 * 4 = 208px (max-h-52)
  className = '',
}: ExpandableContentProps) {
  const { t } = useTranslation();
  const contentRef = useRef<HTMLDivElement>(null);
  const [isExpanded, setIsExpanded] = useState(false);
  const [isOverflowing, setIsOverflowing] = useState(false);

  useEffect(() => {
    const element = contentRef.current;
    if (!element) return;

    const checkOverflow = () => {
      setIsOverflowing(element.scrollHeight > maxHeight);
    };

    checkOverflow();

    // ResizeObserver handles element size changes
    const resizeObserver = new ResizeObserver(checkOverflow);
    resizeObserver.observe(element);

    // MutationObserver handles DOM changes (useful for async markdown content)
    const mutationObserver = new MutationObserver(checkOverflow);
    mutationObserver.observe(element, {
      childList: true,
      subtree: true,
      characterData: true,
    });

    // Listen for images to load (they affect scrollHeight)
    const images = element.querySelectorAll('img');
    const imageLoadPromises = Array.from(images).map(
      (img) =>
        new Promise<void>((resolve) => {
          if (img.complete) {
            resolve();
          } else {
            img.addEventListener('load', () => resolve(), { once: true });
            img.addEventListener('error', () => resolve(), { once: true });
          }
        }),
    );

    // Re-check after all images are loaded
    Promise.all(imageLoadPromises).then(checkOverflow);

    return () => {
      resizeObserver.disconnect();
      mutationObserver.disconnect();
    };
  }, [maxHeight]);

  const toggleExpand = () => {
    setIsExpanded(!isExpanded);
  };

  return (
    <div className="relative">
      <div
        ref={contentRef}
        className={`overflow-hidden scrollbar-thin transition-all duration-300 ${className}`}
        style={{
          maxHeight: isExpanded ? contentRef.current?.scrollHeight : maxHeight,
        }}
      >
        {children}
      </div>

      {!isExpanded && isOverflowing && (
        <div className="absolute bottom-0 left-0 right-0 h-20 bg-gradient-to-t from-bg-base to-transparent pointer-events-none" />
      )}

      {isOverflowing && !isExpanded && (
        <Button
          size="sm"
          onClick={toggleExpand}
          className="absolute bottom-2 left-1/2 -translate-x-1/2"
        >
          <ChevronDown size={14} />
          <span>{t('common.viewMore')}</span>
        </Button>
      )}

      {isOverflowing && isExpanded && (
        <div className="flex justify-center pt-2">
          <Button size="sm" onClick={toggleExpand}>
            <ChevronUp size={14} />
            <span>{t('common.viewLess')}</span>
          </Button>
        </div>
      )}
    </div>
  );
}