Skip to content

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

3.3 KB
Raw Blame
import { cn } from "@vetta-org/ui/utils";
import { useId, type JSX, type Ref } from "react";
import { RunningPulseDot } from "../sidebar/RunningPulseDot";
import { WaitingPulseBadge } from "../sidebar/WaitingPulseBadge";
import { PROJECT_TYPE_ICONS, type ProjectTypeIconKey, REMOTE_PROJECT_ICON } from "./types";

export interface ProjectRowViewProps {
	badge?: string;
	displayName: string;
	expanded: boolean;
	hasRunning: boolean;
	/** 组内有会话正等着用户处理;优先于运行灯。 */
	hasWaiting?: boolean;
	isActive: boolean;
	loading?: boolean;
	newSessionTitle: string;
	onCollapse: () => void;
	onExpand: () => void;
	onInteract?: () => void;
	onNavigateProject: () => void;
	onNewSession: () => void;
	onOpenContextMenu: (event: React.MouseEvent) => void;
	projectCwd: string;
	projectType: ProjectTypeIconKey;
	/** 远程(SSH)项目:折叠态用服务器图标替代项目类型图标。 */
	remote?: boolean;
	rowRef?: Ref<HTMLDivElement>;
}

export function ProjectRowView({
	badge,
	displayName,
	expanded,
	hasRunning,
	hasWaiting = false,
	isActive,
	loading = false,
	newSessionTitle,
	onCollapse,
	onExpand,
	onInteract,
	onNavigateProject,
	onNewSession,
	onOpenContextMenu,
	projectCwd,
	projectType,
	remote = false,
	rowRef,
}: ProjectRowViewProps): JSX.Element {
	const labelId = useId();

	return (
		<div
			ref={rowRef}
			className={cn(
				"group flex w-full items-center gap-2 rounded-lg px-2.5 py-[6px] text-left transition-colors duration-100",
				isActive ? "bg-accent text-foreground" : "hover:bg-accent/50",
			)}
			title={projectCwd}
			onContextMenu={onOpenContextMenu}
		>
			<button
				type="button"
				aria-labelledby={labelId}
				aria-expanded={expanded}
				aria-busy={expanded && loading}
				onClick={() => {
					onInteract?.();
					if (expanded) onCollapse();
					else onExpand();
				}}
				className="relative flex shrink-0 items-center justify-center"
			>
				<span
					className={cn(
						expanded && loading
							? "icon-[mdi--loading] motion-safe:animate-spin"
							: expanded
								? "icon-[solar--alt-arrow-down-linear]"
								: remote
									? REMOTE_PROJECT_ICON
									: PROJECT_TYPE_ICONS[projectType],
						"h-4 w-4 text-foreground",
					)}
				/>
				{hasWaiting ? <WaitingPulseBadge /> : hasRunning ? <RunningPulseDot /> : null}
			</button>
			<button
				id={labelId}
				type="button"
				onClick={() => {
					onInteract?.();
					if (!expanded) {
						onExpand();
					}
					onNavigateProject();
				}}
				className={cn(
					"min-w-0 flex-1 truncate text-left text-[13px] font-medium",
					isActive ? "font-semibold text-foreground" : "text-foreground",
				)}
			>
				{displayName}
			</button>
			<div className="relative flex shrink-0 items-center">
				{badge && (
					<span className="rounded-sm bg-accent px-1 py-px text-[10px] text-muted-foreground group-hover:hidden">
						{badge}
					</span>
				)}
				<button
					type="button"
					title={newSessionTitle}
					onClick={(event) => {
						event.stopPropagation();
						onNewSession();
					}}
					className={cn(
						"flex h-[18px] w-[18px] items-center justify-center rounded-[4px] text-foreground hover:bg-accent",
						badge ? "hidden group-hover:flex" : "opacity-0 group-hover:opacity-100",
					)}
				>
					<span className="icon-[solar--add-linear] h-3 w-3" />
				</button>
			</div>
		</div>
	);
}