Skip to content

Open-source, local-first AI agent for real work.

3.1 KB
Raw Blame
import { cn } from "@vetta-org/ui/utils";
import { useRef, type JSX, type ReactNode } from "react";
import { Virtuoso, type VirtuosoHandle } from "react-virtuoso";
import {
	VIRTUAL_SESSION_OVERSCAN,
	VIRTUAL_SESSION_ROW_HEIGHT,
} from "./types";
import { useActiveSessionAutoScroll } from "./useActiveSessionAutoScroll";
import { ProjectSessionsLoadingView } from "./ProjectSessionsLoadingView";

export interface DefaultSessionListViewItem {
	active?: boolean;
	key: string;
}

export interface DefaultSessionListViewLabels {
	collapse: string;
	expand: string;
	/** Accessible name for each row's hover "more" trigger. */
	more: string;
	/** Primary empty-state line. */
	emptyTitle: string;
	/** Guidance under the title. */
	emptyDescription: string;
	/** Optional CTA label; omit when no action (e.g. Claw filter). */
	emptyAction?: string;
}

export interface DefaultSessionListViewProps<T extends DefaultSessionListViewItem> {
	className?: string;
	hasMore: boolean;
	labels: DefaultSessionListViewLabels;
	loading?: boolean;
	onEmptyAction?: () => void;
	onToggleShowAll: () => void;
	renderSession: (session: T) => ReactNode;
	scrollParent: HTMLElement | null;
	sessions: readonly T[];
	/** Full sorted list length for empty check; visible list may be sliced. */
	showAll: boolean;
	totalCount: number;
	visibleSessions: readonly T[];
}

export function DefaultSessionListView<T extends DefaultSessionListViewItem>({
	className,
	hasMore,
	labels,
	loading = false,
	onToggleShowAll,
	renderSession,
	scrollParent,
	sessions,
	showAll,
	totalCount,
	visibleSessions,
}: DefaultSessionListViewProps<T>): JSX.Element {
	const virtuosoRef = useRef<VirtuosoHandle>(null);
	const activeIndex = sessions.findIndex((session) => session.active);
	const activeKey = activeIndex >= 0 ? sessions[activeIndex]?.key : undefined;
	useActiveSessionAutoScroll({ activeIndex, activeKey, scrollParent, virtuosoRef });
	if (loading) return <ProjectSessionsLoadingView />;

	// 空状态的引导统一放在项目区的卡片里,对话区本身不放内容。
	if (totalCount === 0) return <></>;

	const useVirtual = showAll && scrollParent != null;

	return (
		<div className={cn("flex flex-col", className)}>
			{useVirtual ? (
				<Virtuoso
					ref={virtuosoRef}
					customScrollParent={scrollParent}
					data={sessions as T[]}
					defaultItemHeight={VIRTUAL_SESSION_ROW_HEIGHT}
					overscan={VIRTUAL_SESSION_OVERSCAN}
					itemContent={(_, session) => (
						<div className="pb-px">{renderSession(session)}</div>
					)}
				/>
			) : (
				<div className="space-y-px">{visibleSessions.map(renderSession)}</div>
			)}
			{hasMore && (
				<button
					type="button"
					onClick={onToggleShowAll}
					className="flex w-full items-center gap-1 rounded-md px-2.5 py-[6px] text-left text-[12px] text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
				>
					<span
						className={cn(
							showAll ? "icon-[solar--alt-arrow-up-linear]" : "icon-[solar--alt-arrow-down-linear]",
							"h-3.5 w-3.5 shrink-0",
						)}
					/>
					{showAll ? labels.collapse : labels.expand}
				</button>
			)}
		</div>
	);
}