The platform for continuously improving AI agents.
import { useEffect, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icons";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { datasetDisplayName, useProjectDatasetsQuery } from "@/hooks/use-datasets";
import { datasetWorkspaces } from "@/lib/dataset-workspaces";
import type { Dataset } from "@/openapi";
export function ComposerAttachmentsMenu({
projectId,
selected,
disabled,
onFiles,
onAdd,
onRemove,
isAvailable,
}: {
projectId: string;
selected: Dataset[];
disabled: boolean;
onFiles: () => void;
onAdd: (dataset: Dataset) => void;
onRemove: (id: string) => void;
isAvailable?: (dataset: Dataset) => boolean;
}) {
const [open, setOpen] = useState(false);
const [view, setView] = useState<"actions" | "datasets">("actions");
const [search, setSearch] = useState("");
const input = useRef<HTMLInputElement>(null);
const query = useProjectDatasetsQuery(projectId, open && view === "datasets");
const options = datasetWorkspaces(query.data ?? []).filter((dataset) =>
datasetDisplayName(dataset).toLowerCase().includes(search.toLowerCase())
);
useEffect(() => {
if (view === "datasets") input.current?.focus();
}, [view]);
return (
<Popover
onOpenChange={(next) => {
setOpen(next);
if (!next) {
setView("actions");
setSearch("");
}
}}
open={open}
>
<PopoverTrigger asChild>
<Button
aria-label="Add attachment"
disabled={disabled}
size="icon-sm"
type="button"
variant="ghost"
>
<Icon.add />
</Button>
</PopoverTrigger>
<PopoverContent
align="start"
className={view === "actions" ? "w-56 p-1" : "w-80 p-2"}
side="top"
>
{view === "actions" ? (
<div className="space-y-0.5">
<button
className="flex w-full items-center h-7 gap-1.5 rounded-sm px-2 text-left text-xs hover:bg-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
onClick={() => {
setOpen(false);
onFiles();
}}
type="button"
>
<Icon.file className="size-4 text-muted-foreground" /> Attach files
</button>
<button
className="flex w-full items-center h-7 gap-1.5 rounded-sm px-2 text-left text-xs hover:bg-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
onClick={() => setView("datasets")}
type="button"
>
<Icon.dataset className="size-4 text-cat-2" /> Existing dataset
<Icon.chevronRight className="ml-auto size-4 text-muted-foreground" />
</button>
</div>
) : (
<>
<div className="mb-2 flex items-center gap-2">
<Button
aria-label="Back to attachment options"
onClick={() => setView("actions")}
size="icon-xs"
type="button"
variant="ghost"
>
<Icon.back />
</Button>
<span className="text-xs">Existing datasets</span>
</div>
<input
aria-label="Search datasets"
className="mb-2 w-full rounded-sm border border-border bg-card px-2 py-1.5 text-xs"
onChange={(event) => setSearch(event.target.value)}
placeholder="Search datasets…"
ref={input}
value={search}
/>
<div className="max-h-64 overflow-y-auto">
{options.map((dataset) => {
const attached = selected.some((item) => item.id === dataset.id);
return (
<button
aria-pressed={attached}
className="flex w-full items-center h-7 gap-1.5 rounded-sm px-2 text-left text-xs hover:bg-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-50"
disabled={disabled || (isAvailable !== undefined && !isAvailable(dataset))}
key={dataset.id}
onClick={() => (attached ? onRemove(dataset.id) : onAdd(dataset))}
type="button"
>
<Icon.dataset className="size-4 shrink-0 text-cat-2" />
<span className="min-w-0 flex-1 truncate">{datasetDisplayName(dataset)}</span>
{attached && <Icon.success className="size-4 shrink-0 text-success" />}
</button>
);
})}
{query.isError ? (
<p className="p-2 text-xs text-destructive">Datasets could not be loaded.</p>
) : (
!options.length && (
<p className="p-2 text-xs text-muted-foreground">
{query.isPending
? "Loading datasets…"
: search
? "No matching datasets"
: "No datasets"}
</p>
)
)}
</div>
</>
)}
</PopoverContent>
</Popover>
);
}