Open-source, local-first AI agent for real work.
TypeScript agent-toolsai-agentbyokcoding-agentcross-platformdesktop-appdeveloper-toolselectronharnessknowledge-basellmwikilocal-firstmcpplugin-systemprivacy-firstskillstypescriptvettaworkflow-automation
import { Button } from "@vetta-org/ui/button";
import { cn } from "@vetta-org/ui/utils";
import { Dialog } from "radix-ui";
import type { JSX, KeyboardEvent } from "react";
import { StageDialogContent } from "../shared/StageDialogContent";
import { StagePickerListView } from "./StagePickerListView";
export interface ProjectSelectorOptionView {
readonly cwd: string;
readonly name: string;
readonly selected: boolean;
}
export interface ProjectSelectorViewLabels {
/** 未选中态的 trigger 文案。 */
readonly placeholder: string;
/** 列表顶部那条「回到未选中」的条目。 */
readonly clear: string;
readonly searchPlaceholder: string;
readonly empty: string;
readonly newProject: string;
readonly openProject: string;
/** 待创建项目名后缀,例如「待创建」。 */
readonly pendingHint: string;
/** 点发送后、项目正在落盘时替换 trigger 的文案。 */
readonly creating: string;
readonly triggerTitle: string;
/** 舞台右上角关闭按钮的读屏名称。 */
readonly close: string;
/** 舞台右侧说明栏里标题下的一句话。 */
readonly description: string;
}
export interface ProjectSelectorViewProps {
readonly open: boolean;
readonly options: readonly ProjectSelectorOptionView[];
readonly labels: ProjectSelectorViewLabels;
/** 当前选中的项目名;未选中时为 null。 */
readonly selectedName: string | null;
/** 选中的是尚未落盘的新项目。 */
readonly pendingCreate: boolean;
/** 发送流程正在创建项目:trigger 换文案并禁用交互。 */
readonly creating: boolean;
readonly searchVisible: boolean;
readonly query: string;
readonly highlightIndex: number;
readonly className?: string;
readonly classNames?: {
readonly root?: string;
readonly trigger?: string;
};
readonly onOpenChange: (open: boolean) => void;
readonly onQueryChange: (query: string) => void;
readonly onHighlightMove: (delta: number) => void;
/** 选中某个项目;传 null 表示「不指定项目」。 */
readonly onSelect: (cwd: string | null) => void;
/** 搜索框里回车:由宿主决定落到当前高亮项。 */
readonly onCommitHighlight: () => void;
readonly onCreateProject: () => void;
readonly onOpenProject: () => void;
}
/**
* 新会话页 hero 与输入框之间的项目选择器:chip 点开是舞台浮层(与切换智能体同一套),
* 左栏单列列出项目,右栏是说明与新建 / 打开本地项目。
*/
export function ProjectSelectorView({
open,
options,
labels,
selectedName,
pendingCreate,
creating,
searchVisible,
query,
highlightIndex,
className,
classNames,
onOpenChange,
onQueryChange,
onHighlightMove,
onSelect,
onCommitHighlight,
onCreateProject,
onOpenProject,
}: ProjectSelectorViewProps): JSX.Element {
const triggerLabel = creating ? labels.creating : (selectedName ?? labels.placeholder);
const triggerIcon = pendingCreate
? "icon-[solar--add-folder-linear]"
: selectedName
? "icon-[solar--folder-linear]"
: "icon-[solar--folder-with-files-linear]";
function handleSearchKeyDown(event: KeyboardEvent<HTMLInputElement>): void {
if (event.key === "ArrowDown") {
event.preventDefault();
onHighlightMove(1);
return;
}
if (event.key === "ArrowUp") {
event.preventDefault();
onHighlightMove(-1);
return;
}
if (event.key === "Enter") {
event.preventDefault();
onCommitHighlight();
}
}
return (
<div className={cn("min-w-0", className, classNames?.root)}>
<Dialog.Root open={open} onOpenChange={creating ? undefined : onOpenChange}>
<Dialog.Trigger asChild>
<button
type="button"
disabled={creating}
title={labels.triggerTitle}
aria-label={labels.triggerTitle}
className={cn(
// 与同一行的模式切换、智能体选择共用实色底:半透明底会透出背景纹理,
// 浅色下 card 又与页面背景几乎同亮,所以浅色用深一档的 muted。
"no-drag flex h-7 max-w-[16rem] min-w-0 items-center gap-1.5 rounded-lg bg-muted px-2.5 dark:bg-card text-[12px] font-medium transition-colors disabled:pointer-events-none disabled:opacity-60",
open ? "bg-accent text-foreground dark:bg-accent" : "text-foreground hover:bg-accent",
!selectedName && !creating && "text-muted-foreground/80 hover:text-foreground",
classNames?.trigger,
)}
>
<span className={cn(triggerIcon, "h-3.5 w-3.5 shrink-0")} aria-hidden />
<span className="min-w-0 truncate">{triggerLabel}</span>
{pendingCreate && !creating && (
<span className="shrink-0 text-[11px] font-normal text-muted-foreground/70">
{labels.pendingHint}
</span>
)}
<span className="icon-[solar--arrow-right-up-linear] h-3 w-3 shrink-0 opacity-70" aria-hidden />
</button>
</Dialog.Trigger>
<StageDialogContent
closeLabel={labels.close}
onOpenAutoFocus={(event) => {
// 有搜索框时让它接住焦点,方便直接打字;没有时不要把焦点环落在第一张卡片上。
if (!searchVisible) event.preventDefault();
}}
artwork={
<StagePickerListView
sections={[
{
listLabel: labels.placeholder,
options: options.map((option, index) => ({
key: option.cwd,
title: option.name,
icon: "icon-[solar--folder-linear]",
selected: option.selected,
highlighted: index === highlightIndex,
})),
},
]}
labels={{
searchPlaceholder: labels.searchPlaceholder,
clear: labels.clear,
empty: labels.empty,
}}
searchVisible={searchVisible}
query={query}
clearVisible={selectedName !== null}
onQueryChange={onQueryChange}
onSearchKeyDown={handleSearchKeyDown}
onSelect={onSelect}
/>
}
>
<Dialog.Title className="text-[20px] font-semibold text-foreground">{labels.placeholder}</Dialog.Title>
<Dialog.Description className="mt-2 text-[13px] leading-relaxed text-muted-foreground">
{labels.description}
</Dialog.Description>
<Button variant="primary" className="mt-7 w-full" onClick={onCreateProject}>
<span className="icon-[solar--add-folder-linear] h-4 w-4" aria-hidden />
{labels.newProject}
</Button>
<Button variant="field" className="mt-2 w-full" onClick={onOpenProject}>
<span className="icon-[solar--folder-open-linear] h-4 w-4" aria-hidden />
{labels.openProject}
</Button>
</StageDialogContent>
</Dialog.Root>
</div>
);
}