Skip to content

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

feat(web): add virtual scrolling to CSV viewer and chunk list (#19621)

chanx9172eb2acabd
diff --git a/web/package-lock.json b/web/package-lock.json
index b849f18c48..334ef81ef5 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -59,6 +59,7 @@
"@tanstack/react-query": "^5.40.0",
"@tanstack/react-query-devtools": "^5.51.5",
"@tanstack/react-table": "^8.20.5",
+ "@tanstack/react-virtual": "^3.14.12",
"@types/papaparse": "^5.5.1",
"@uiw/react-markdown-preview": "^5.1.3",
"@welldone-software/why-did-you-render": "^8.0.3",
@@ -8407,12 +8408,12 @@
}
},
"node_modules/@tanstack/react-virtual": {
- "version": "3.14.2",
- "resolved": "https://registry.npmmirror.com/@tanstack/react-virtual/-/react-virtual-3.14.2.tgz",
- "integrity": "sha512-IpWnmCLvuymRfeeLNVXIzNEYBFLpd3drVIS91sqV78VTZFyldlChkOocZRCPp1B+Wnk09bcLNme8WaMU/9/9bQ==",
+ "version": "3.14.12",
+ "resolved": "https://registry.npmmirror.com/@tanstack/react-virtual/-/react-virtual-3.14.12.tgz",
+ "integrity": "sha512-EOJkBss/FRqzFrCpX8M96lIh+ZMc0jPt0LILhxWQ3tOHQ3XdVvuE0jceS2yZXpDtayNfpjeg2EnuYPm90CxIlA==",
"license": "MIT",
"dependencies": {
- "@tanstack/virtual-core": "3.17.0"
+ "@tanstack/virtual-core": "3.17.10"
},
"funding": {
"type": "github",
@@ -8437,9 +8438,9 @@
}
},
"node_modules/@tanstack/virtual-core": {
- "version": "3.17.0",
- "resolved": "https://registry.npmmirror.com/@tanstack/virtual-core/-/virtual-core-3.17.0.tgz",
- "integrity": "sha512-gOxY/hFkPh/XQYhnThBHzkbkX3Ed+z/iushyz+R+JAr213aXxUDgQoTgTdrDpBSRsjFM73P/KfUyWmaF9WHMkQ==",
+ "version": "3.17.10",
+ "resolved": "https://registry.npmmirror.com/@tanstack/virtual-core/-/virtual-core-3.17.10.tgz",
+ "integrity": "sha512-eMorKWIi2nekElu+8DgSMJR4iR+sTDeh+W8VjKiMH79yUfEDJ3W9aL/96/wwbxNjyV6EfN39HKszSxjd90zAqQ==",
"license": "MIT",
"funding": {
"type": "github",
diff --git a/web/package.json b/web/package.json
index 9cda8a375e..de05c02d72 100644
--- a/web/package.json
+++ b/web/package.json
@@ -69,6 +69,7 @@
"@tanstack/react-query": "^5.40.0",
"@tanstack/react-query-devtools": "^5.51.5",
"@tanstack/react-table": "^8.20.5",
+ "@tanstack/react-virtual": "^3.14.12",
"@types/papaparse": "^5.5.1",
"@uiw/react-markdown-preview": "^5.1.3",
"@welldone-software/why-did-you-render": "^8.0.3",
diff --git a/web/src/components/document-preview/csv-preview.tsx b/web/src/components/document-preview/csv-preview.tsx
index db7a842253..62d3cf5d8a 100644
--- a/web/src/components/document-preview/csv-preview.tsx
+++ b/web/src/components/document-preview/csv-preview.tsx
@@ -20,7 +20,7 @@ import request from '@/utils/request';
import { decodeBlobText } from '@/utils/file-util';
import classNames from 'classnames';
import Papa from 'papaparse';
-import React, { useEffect, useRef, useState } from 'react';
+import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
interface CSVData {
rows: string[][];
@@ -32,11 +32,18 @@ interface FileViewerProps {
url: string;
}
-const CSVFileViewer: React.FC<FileViewerProps> = ({ url }) => {
+const CSVFileViewer: React.FC<FileViewerProps> = ({ className, url }) => {
const [data, setData] = useState<CSVData | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const containerRef = useRef<HTMLDivElement>(null);
- // const url = useGetDocumentUrl();
+ const tableRef = useRef<HTMLDivElement>(null);
+ const [contentWidth, setContentWidth] = useState<number>(0);
+
+ /**
+ * Parse CSV text into headers and data rows using PapaParse.
+ * @param csvText - Raw CSV string to parse
+ * @returns Parsed CSVData with headers array and rows 2D array
+ */
const parseCSV = (csvText: string): CSVData => {
const result = Papa.parse<string[]>(csvText, {
header: false,
@@ -81,47 +88,118 @@ const CSVFileViewer: React.FC<FileViewerProps> = ({ url }) => {
};
}, [url]);
+ // Virtual row rendering state
+ const [scrollTop, setScrollTop] = useState(0);
+ const [containerHeight, setContainerHeight] = useState(0);
+ const rowHeight = 36;
+
+ // Recalculate container height on resize and track scroll position
+ useEffect(() => {
+ const el = containerRef.current;
+ if (!el) return;
+ const update = () => setContainerHeight(el.clientHeight);
+ update();
+ const ro = new ResizeObserver(update);
+ ro.observe(el);
+ return () => ro.disconnect();
+ }, []);
+
+ // Measure the natural width of the table (header + data determine column
+ // widths together via table layout) so the container can be sized to match.
+ // This ensures horizontal scroll reaches the last column.
+ useEffect(() => {
+ const el = tableRef.current;
+ if (!el) return;
+ const update = () => setContentWidth(el.offsetWidth);
+ update();
+ const ro = new ResizeObserver(update);
+ ro.observe(el);
+ return () => ro.disconnect();
+ }, [data]);
+
+ // Scroll handler on the actual scrolling container
+ const handleScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
+ setScrollTop(e.currentTarget.scrollTop);
+ }, []);
+
+ // Calculate visible row range with overscan
+ const { startIdx, endIdx } = useMemo(() => {
+ const overscan = 10;
+ const start = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);
+ const visibleCount = Math.ceil(containerHeight / rowHeight) + 2 * overscan;
+ const end = data ? Math.min(data.rows.length, start + visibleCount) : 0;
+ return { startIdx: start, endIdx: end };
+ }, [scrollTop, containerHeight, rowHeight, data]);
+
return (
<div
ref={containerRef}
className={classNames(
- 'relative w-full h-full p-4 bg-background-paper border border-border-normal rounded-md',
- 'overflow-auto max-h-[80vh] p-2',
+ 'relative w-full h-full bg-background-paper border border-border-normal rounded-md',
+ 'overflow-auto',
+ className,
)}
+ onScroll={handleScroll}
>
{isLoading ? (
<div className="absolute inset-0 flex items-center justify-center">
<Spin />
</div>
) : data ? (
- <table className="min-w-full divide-y divide-border-normal">
- <thead className="bg-background-header-bar">
- <tr>
- {data.headers.map((header, index) => (
- <th
- key={`header-${index}`}
- className="px-6 py-3 text-left text-sm font-medium text-text-primary"
- >
- {header}
- </th>
- ))}
- </tr>
- </thead>
- <tbody className="bg-background-paper divide-y divide-border-normal">
- {data.rows.map((row, rowIndex) => (
- <tr key={`row-${rowIndex}`}>
- {row.map((cell, cellIndex) => (
- <td
- key={`cell-${rowIndex}-${cellIndex}`}
- className="px-6 py-4 whitespace-nowrap text-sm text-text-secondary"
+ /* Use CSS table layout so header and data cells share consistent
+ column widths. The table's natural width determines the scroll
+ range, ensuring all columns are reachable via horizontal scroll. */
+ <div
+ style={{ width: contentWidth || undefined }}
+ className={contentWidth ? '' : 'w-fit'}
+ >
+ <div ref={tableRef} className="table w-fit">
+ {/* Sticky header row */}
+ <div className="table-row-group">
+ <div className="table-row sticky top-0 z-10 bg-background-header-bar">
+ {data.headers.map((header, index) => (
+ <div
+ key={`header-${index}`}
+ className="table-cell px-6 py-3 text-left text-sm font-medium text-text-primary whitespace-nowrap border-b border-border-normal"
>
- {cell || '-'}
- </td>
+ {header}
+ </div>
))}
- </tr>
- ))}
- </tbody>
- </table>
+ </div>
+ </div>
+ {/* Data rows with virtual scroll padding */}
+ <div className="table-row-group">
+ {/* Top spacer row */}
+ {startIdx > 0 && (
+ <div className="table-row" style={{ height: startIdx * rowHeight }}>
+ <div className="table-cell border-b border-border-normal" />
+ </div>
+ )}
+ {data.rows.slice(startIdx, endIdx).map((row, i) => {
+ const actualIndex = startIdx + i;
+ return (
+ <div key={`row-${actualIndex}`} className="table-row hover:bg-gray-50">
+ {row.map((cell, cellIndex) => (
+ <div
+ key={`cell-${actualIndex}-${cellIndex}`}
+ className="table-cell px-6 py-2 whitespace-nowrap text-sm text-text-secondary border-b border-border-normal overflow-hidden text-ellipsis"
+ style={{ height: rowHeight }}
+ >
+ {cell || '-'}
+ </div>
+ ))}
+ </div>
+ );
+ })}
+ {/* Bottom spacer row */}
+ {data.rows.length - endIdx > 0 && (
+ <div className="table-row" style={{ height: (data.rows.length - endIdx) * rowHeight }}>
+ <div className="table-cell border-b border-border-normal" />
+ </div>
+ )}
+ </div>
+ </div>
+ </div>
) : null}
</div>
);
diff --git a/web/src/components/document-preview/index.tsx b/web/src/components/document-preview/index.tsx
index b065ef56fc..4e1637390c 100644
--- a/web/src/components/document-preview/index.tsx
+++ b/web/src/components/document-preview/index.tsx
@@ -103,7 +103,7 @@ const DocumentPreview = function ({
</section>
)}
{['csv'].indexOf(fileType) > -1 && (
- <section>
+ <section className="h-full">
<CSVFileViewer className={className} url={url} />
</section>
)}
diff --git a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/index.tsx b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/index.tsx
index d24bcaad15..a4994a502d 100644
--- a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/index.tsx
+++ b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/index.tsx
@@ -2,7 +2,8 @@ import {
useFetchNextChunkList,
useSwitchChunk,
} from '@/hooks/use-chunk-request';
-import { useCallback, useEffect, useMemo, useState } from 'react';
+import { useVirtualizer } from '@tanstack/react-virtual';
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import ChunkCard from './components/chunk-card';
import CreatingModal from './components/chunk-creating-modal';
@@ -223,6 +224,15 @@ function Chunk() {
return 'unknown';
}, [documentInfo]);
+ // Virtual list setup for chunk cards
+ const scrollContainerRef = useRef<HTMLDivElement>(null);
+ const virtualizer = useVirtualizer({
+ count: chunkList.length,
+ getScrollElement: () => scrollContainerRef.current,
+ estimateSize: () => 120, // Estimated card height
+ overscan: 5, // Render 5 extra items above/below viewport
+ });
+
return (
<main className="h-dvh flex flex-col">
<PageHeader>
@@ -347,23 +357,50 @@ function Chunk() {
/>
</div>
- <div className="space-y-4 flex-1 overflow-y-auto min-h-0">
- {chunkList.map((item) => (
- <ChunkCard
- item={item}
- key={item.chunk_id}
- editChunk={showChunkUpdatingModal}
- checked={selectedChunkIds.some(
- (x) => x === item.chunk_id,
- )}
- handleCheckboxClick={handleSingleCheckboxClick}
- switchChunk={handleSwitchChunk}
- clickChunkCard={handleChunkCardClick}
- selected={item.chunk_id === selectedChunkId}
- textMode={textMode}
- t={dataUpdatedAt}
- />
- ))}
+ <div
+ ref={scrollContainerRef}
+ className="flex-1 overflow-y-auto min-h-0"
+ >
+ <div
+ style={{
+ height: `${virtualizer.getTotalSize()}px`,
+ width: '100%',
+ position: 'relative',
+ }}
+ >
+ {virtualizer.getVirtualItems().map((virtualItem) => {
+ const item = chunkList[virtualItem.index];
+ return (
+ <div
+ key={item.chunk_id}
+ data-index={virtualItem.index}
+ ref={virtualizer.measureElement}
+ style={{
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ width: '100%',
+ transform: `translateY(${virtualItem.start}px)`,
+ }}
+ className="pb-4"
+ >
+ <ChunkCard
+ item={item}
+ editChunk={showChunkUpdatingModal}
+ checked={selectedChunkIds.some(
+ (x) => x === item.chunk_id,
+ )}
+ handleCheckboxClick={handleSingleCheckboxClick}
+ switchChunk={handleSwitchChunk}
+ clickChunkCard={handleChunkCardClick}
+ selected={item.chunk_id === selectedChunkId}
+ textMode={textMode}
+ t={dataUpdatedAt}
+ />
+ </div>
+ );
+ })}
+ </div>
</div>
<footer className="mt-5">