Skip to content

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

4.6 KB
Raw Blame
import { AnimatePresence, motion } from "motion/react";
import type { JSX } from "react";
import { Popover, PopoverContent, PopoverTrigger } from "@vetta-org/ui/popover";
import { cn } from "@vetta-org/ui/utils";
import { ThemeSurface } from "../appearance/ThemeSurface";

export interface ExecutionModeOptionView {
	readonly mode: string;
	readonly icon: string;
	readonly label: string;
	readonly title: string;
	readonly disabled: boolean;
	readonly selected: boolean;
}

export interface ExecutionModeSelectorViewProps {
	readonly open: boolean;
	readonly disabled: boolean;
	readonly selectedOption: ExecutionModeOptionView;
	readonly options: readonly ExecutionModeOptionView[];
	readonly className?: string;
	/** 始终显示模式名。默认只在宽容器里显示、窄了只留图标;放在弹窗底栏这类没有容器查询的位置时要打开。 */
	readonly showLabel?: boolean;
	/** 弹出菜单与触发器的对齐边。 */
	readonly contentAlign?: "start" | "end";
	readonly classNames?: {
		readonly root?: string;
		readonly trigger?: string;
		readonly content?: string;
		readonly contentInner?: string;
		readonly item?: string;
	};
	readonly onOpenChange: (open: boolean) => void;
	readonly onSelect: (mode: string) => void;
}

export function ExecutionModeSelectorView({
	open,
	disabled,
	selectedOption,
	options,
	className,
	showLabel = false,
	contentAlign = "start",
	classNames,
	onOpenChange,
	onSelect,
}: ExecutionModeSelectorViewProps): JSX.Element {
	return (
		<div className={cn("ml-1 min-w-0", className, classNames?.root)}>
			<Popover open={open} onOpenChange={disabled ? undefined : onOpenChange}>
				<PopoverTrigger asChild>
					<button
						type="button"
						disabled={disabled}
						title={selectedOption.title}
						className={cn(
							// 窄容器只保留图标(title 仍可悬停查看模式名),宽了再显示文案
							"no-drag flex h-7 max-w-full min-w-0 items-center rounded-lg text-[12px] font-medium transition-colors disabled:pointer-events-none disabled:opacity-40",
							showLabel ? "gap-1.5 px-2" : "gap-1 px-1.5 @[22rem]:gap-1.5 @[22rem]:px-2",
							open
								? "bg-accent text-foreground"
								: "text-foreground hover:bg-accent/60",
							classNames?.trigger,
						)}
					>
						<span className={cn(selectedOption.icon, "h-3.5 w-3.5 shrink-0")} />
						<span
							className={cn(
								"min-w-0 max-w-[5.5rem] truncate",
								showLabel ? "inline" : "hidden @[22rem]:inline",
							)}
						>
							{selectedOption.label}
						</span>
					</button>
				</PopoverTrigger>
				<AnimatePresence>
					{open && (
						<PopoverContent
							forceMount
							asChild
							side="top"
							align={contentAlign}
							sideOffset={6}
							className={cn(
								"w-[148px] gap-0 overflow-visible p-0",
								classNames?.content,
							)}
							style={{ animation: "none" }}
						>
							<motion.div
								initial={{ opacity: 0 }}
								animate={{ opacity: 1 }}
								exit={{ opacity: 0 }}
								transition={{ duration: 0.12, ease: "easeOut" }}
								// 这里的 motion.div 就是 PopoverContent 本体(asChild),圆角来自 FLOATING_SURFACE_CLASS;
								// 再写 rounded-[inherit] 会和它并排成两条声明,按样式表顺序覆盖成继承自 portal 的 0。
								className="relative overflow-visible"
							>
								<ThemeSurface slot="chat.executionModeMenu" />
								<div
									className={cn(
										"relative z-10 overflow-hidden rounded-[inherit] p-1",
										classNames?.contentInner,
									)}
								>
									{options.map((option) => (
										<button
											key={option.mode}
											type="button"
											title={option.title}
											disabled={option.disabled}
											onClick={() => {
												onOpenChange(false);
												onSelect(option.mode);
											}}
											className={cn(
												"flex w-full items-center gap-2 rounded-md px-2 py-[5px] text-[12px] font-medium transition-colors disabled:cursor-not-allowed disabled:opacity-45",
												option.selected
													? "bg-accent text-foreground"
													: "text-foreground hover:bg-accent",
												classNames?.item,
											)}
										>
											<span className={cn(option.icon, "h-3.5 w-3.5 shrink-0")} />
											<span className="truncate">{option.label}</span>
											{option.selected && (
												<span className="icon-[solar--check-circle-linear] ml-auto h-3.5 w-3.5 text-primary" />
											)}
										</button>
									))}
								</div>
							</motion.div>
						</PopoverContent>
					)}
				</AnimatePresence>
			</Popover>
		</div>
	);
}