Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(dashboard): let list and table widget cards scroll and load more rows (#21643)

Arvin Xu0042da23d620
diff --git a/src/features/Dashboard/WidgetCard/index.tsx b/src/features/Dashboard/WidgetCard/index.tsx
index 75447e1541d..0ef7efc4f66 100644
--- a/src/features/Dashboard/WidgetCard/index.tsx
+++ b/src/features/Dashboard/WidgetCard/index.tsx
@@ -46,6 +46,11 @@ const styles = createStaticStyles(({ css }) => ({
flex: 1;
min-height: 0;
`,
+ bodyScrollable: css`
+ /* Rows past the card height scroll inside the card instead of being clipped. */
+ overflow-y: auto;
+ overscroll-behavior: contain;
+ `,
card: css`
overflow: hidden;
@@ -129,6 +134,10 @@ const WidgetCard = memo<WidgetCardProps>(
// The running badge would repeat the spinning refresh button and the pulsing dot.
const badges = health.badges.filter((badge) => badge !== 'running');
+ // Lists and tables are the only outputs taller than their card; the rest size to it.
+ const scrollable =
+ bodyState === 'output' && (output?.type === 'list' || output?.type === 'table');
+
let body: ReactNode;
if (bodyState === 'output' && output) {
body = (
@@ -191,7 +200,7 @@ const WidgetCard = memo<WidgetCardProps>(
</Flexbox>
)}
</Flexbox>
- <div className={styles.body}>{body}</div>
+ <div className={cx(styles.body, scrollable && styles.bodyScrollable)}>{body}</div>
{updatedAt && (
<Flexbox horizontal align={'center'} className={styles.footer} gap={6}>
<HealthDot health={health} />
diff --git a/src/features/Dashboard/WidgetCard/views/ListView.tsx b/src/features/Dashboard/WidgetCard/views/ListView.tsx
index 431c2ee82b5..776f3fea809 100644
--- a/src/features/Dashboard/WidgetCard/views/ListView.tsx
+++ b/src/features/Dashboard/WidgetCard/views/ListView.tsx
@@ -7,6 +7,7 @@ import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { formatWidgetValue } from '../../utils/format';
+import { useIncrementalRows } from './useIncrementalRows';
const styles = createStaticStyles(({ css }) => ({
link: css`
@@ -27,17 +28,21 @@ const styles = createStaticStyles(({ css }) => ({
}));
interface ListViewProps {
- /** Rows to show before the "+N more" note; unlimited when omitted. */
+ /** Reveal rows in batches as the card is scrolled instead of mounting them all. */
+ incremental?: boolean;
+ /** Hard cap shown before the "+N more" note; unlimited when omitted. */
limit?: number;
output: WidgetListOutput;
}
-const ListView = memo<ListViewProps>(({ output, limit }) => {
+const ListView = memo<ListViewProps>(({ output, limit, incremental }) => {
const { t } = useTranslation('dashboard');
// Normalize once: an older server may send a list output without `items`.
const allItems = output.items ?? [];
- const items = limit ? allItems.slice(0, limit) : allItems;
- const hidden = allItems.length - items.length;
+ const capped = limit ? allItems.slice(0, limit) : allItems;
+ const { count, hasMore, sentinelRef } = useIncrementalRows(capped.length, !!incremental);
+ const items = capped.slice(0, count);
+ const hidden = allItems.length - capped.length;
if (allItems.length === 0) {
return (
@@ -77,7 +82,8 @@ const ListView = memo<ListViewProps>(({ output, limit }) => {
)}
</Flexbox>
))}
- {hidden > 0 && (
+ {hasMore && <div key={count} ref={sentinelRef} style={{ flex: 'none', height: 1 }} />}
+ {hidden > 0 && !hasMore && (
<Text fontSize={12} style={{ paddingTop: 6 }} type={'secondary'}>
{t('widget.view.more', { count: hidden })}
</Text>
diff --git a/src/features/Dashboard/WidgetCard/views/TableView.tsx b/src/features/Dashboard/WidgetCard/views/TableView.tsx
index 142bfd05191..b29e53176ba 100644
--- a/src/features/Dashboard/WidgetCard/views/TableView.tsx
+++ b/src/features/Dashboard/WidgetCard/views/TableView.tsx
@@ -6,6 +6,7 @@ import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { formatCell } from '../../utils/format';
+import { useIncrementalRows } from './useIncrementalRows';
const styles = createStaticStyles(({ css }) => ({
table: css`
@@ -48,11 +49,13 @@ const styles = createStaticStyles(({ css }) => ({
interface TableViewProps {
/** Column keys to show, in order (`view.columns`). */
columns?: string[];
+ /** Reveal rows in batches as the card is scrolled instead of mounting them all. */
+ incremental?: boolean;
limit?: number;
output: WidgetTableOutput;
}
-const TableView = memo<TableViewProps>(({ output, columns, limit }) => {
+const TableView = memo<TableViewProps>(({ output, columns, limit, incremental }) => {
const { t } = useTranslation('dashboard');
const visibleColumns = useMemo(() => {
@@ -61,8 +64,10 @@ const TableView = memo<TableViewProps>(({ output, columns, limit }) => {
return columns.map((key) => byKey.get(key) ?? { key });
}, [columns, output.columns]);
- const rows = limit ? output.rows.slice(0, limit) : output.rows;
- const hidden = output.rows.length - rows.length;
+ const capped = limit ? output.rows.slice(0, limit) : output.rows;
+ const { count, hasMore, sentinelRef } = useIncrementalRows(capped.length, !!incremental);
+ const rows = capped.slice(0, count);
+ const hidden = output.rows.length - capped.length;
if (output.rows.length === 0) {
return (
@@ -105,7 +110,8 @@ const TableView = memo<TableViewProps>(({ output, columns, limit }) => {
))}
</tbody>
</table>
- {hidden > 0 && (
+ {hasMore && <div key={count} ref={sentinelRef} style={{ height: 1 }} />}
+ {hidden > 0 && !hasMore && (
<Text fontSize={12} style={{ display: 'block', paddingTop: 6 }} type={'secondary'}>
{t('widget.view.more', { count: hidden })}
</Text>
diff --git a/src/features/Dashboard/WidgetCard/views/index.tsx b/src/features/Dashboard/WidgetCard/views/index.tsx
index bc3ee92a2c7..1db999946ed 100644
--- a/src/features/Dashboard/WidgetCard/views/index.tsx
+++ b/src/features/Dashboard/WidgetCard/views/index.tsx
@@ -18,20 +18,19 @@ export interface WidgetOutputViewProps {
view?: WidgetViewHints | null;
}
-/** Card row cap when the version sets no `view.limit`. */
-const COMPACT_ROW_LIMIT = 8;
-
/** Render one widget output by its contract type. */
const WidgetOutputView = memo<WidgetOutputViewProps>(
({ output, trend, view, density = 'compact' }) => {
- const limit = density === 'compact' ? (view?.limit ?? COMPACT_ROW_LIMIT) : undefined;
+ // Cards scroll and load rows as needed; only an explicit `view.limit` caps them.
+ const compact = density === 'compact';
+ const limit = compact ? view?.limit : undefined;
switch (output.type) {
case 'stat': {
return <StatView output={output} trend={trend} />;
}
case 'list': {
- return <ListView limit={limit} output={output} />;
+ return <ListView incremental={compact} limit={limit} output={output} />;
}
case 'series': {
return (
@@ -44,7 +43,9 @@ const WidgetOutputView = memo<WidgetOutputViewProps>(
);
}
case 'table': {
- return <TableView columns={view?.columns} limit={limit} output={output} />;
+ return (
+ <TableView columns={view?.columns} incremental={compact} limit={limit} output={output} />
+ );
}
default: {
return null;
diff --git a/src/features/Dashboard/WidgetCard/views/useIncrementalRows.test.ts b/src/features/Dashboard/WidgetCard/views/useIncrementalRows.test.ts
new file mode 100644
index 00000000000..af9af75d997
--- /dev/null
+++ b/src/features/Dashboard/WidgetCard/views/useIncrementalRows.test.ts
@@ -0,0 +1,50 @@
+import { act, renderHook } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { ROW_BATCH, useIncrementalRows } from './useIncrementalRows';
+
+let trigger: () => void;
+
+beforeEach(() => {
+ vi.stubGlobal(
+ 'IntersectionObserver',
+ class {
+ constructor(callback: IntersectionObserverCallback) {
+ trigger = () =>
+ callback([{ isIntersecting: true } as IntersectionObserverEntry], this as never);
+ }
+ disconnect() {}
+ observe() {}
+ },
+ );
+});
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+describe('useIncrementalRows', () => {
+ it('shows every row when not incremental', () => {
+ const { result } = renderHook(() => useIncrementalRows(100, false));
+ expect(result.current.count).toBe(100);
+ expect(result.current.hasMore).toBe(false);
+ });
+
+ it('starts with one batch and reveals the next when the sentinel scrolls into view', () => {
+ const { result } = renderHook(() => useIncrementalRows(100, true));
+ expect(result.current.count).toBe(ROW_BATCH);
+ expect(result.current.hasMore).toBe(true);
+
+ act(() => result.current.sentinelRef(document.createElement('div')));
+ act(() => trigger());
+ expect(result.current.count).toBe(ROW_BATCH * 2);
+ });
+
+ it('never reveals more rows than exist', () => {
+ const { result } = renderHook(() => useIncrementalRows(25, true));
+ act(() => result.current.sentinelRef(document.createElement('div')));
+ act(() => trigger());
+ expect(result.current.count).toBe(25);
+ expect(result.current.hasMore).toBe(false);
+ });
+});
diff --git a/src/features/Dashboard/WidgetCard/views/useIncrementalRows.ts b/src/features/Dashboard/WidgetCard/views/useIncrementalRows.ts
new file mode 100644
index 00000000000..2b30b6899ee
--- /dev/null
+++ b/src/features/Dashboard/WidgetCard/views/useIncrementalRows.ts
@@ -0,0 +1,33 @@
+import { useCallback, useEffect, useRef, useState } from 'react';
+
+/** Rows a card shows up front, and how many more each scroll to the end reveals. */
+export const ROW_BATCH = 20;
+
+/**
+ * Reveal rows in batches as the user scrolls a card to its end, so a long
+ * list stays cheap to mount but is never cut off. Attach `sentinelRef` to an
+ * element after the last rendered row; it is only needed while `hasMore`.
+ */
+export const useIncrementalRows = (total: number, enabled: boolean) => {
+ const [shown, setShown] = useState(ROW_BATCH);
+ const observerRef = useRef<IntersectionObserver | undefined>(undefined);
+
+ const sentinelRef = useCallback((node: HTMLElement | null) => {
+ observerRef.current?.disconnect();
+ observerRef.current = undefined;
+ if (!node || typeof IntersectionObserver === 'undefined') return;
+ // The scroll container is the card body, so the default (viewport) root still
+ // sees the sentinel as hidden until the body is scrolled to it.
+ observerRef.current = new IntersectionObserver((entries) => {
+ if (entries.some((entry) => entry.isIntersecting)) {
+ setShown((count) => count + ROW_BATCH);
+ }
+ });
+ observerRef.current.observe(node);
+ }, []);
+
+ useEffect(() => () => observerRef.current?.disconnect(), []);
+
+ const count = enabled ? Math.min(shown, total) : total;
+ return { count, hasMore: enabled && count < total, sentinelRef };
+};