Skip to content

The platform for continuously improving AI agents.

4.1 KB
Raw Blame
// Adapted from AI SDK Agents Attachments Tray: https://www.aisdkagents.com/ai-components/attachments-tray
import { AnimatePresence, motion, useReducedMotion } from "motion/react";

import { Attachment } from "@/components/datasets/attachment";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icons";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { datasetDisplayName } from "@/hooks/use-datasets";
import type { DatasetUpload } from "@/hooks/use-uploads";
import type { Dataset } from "@/openapi";

export function AttachmentsTray({
  datasets,
  files,
  locked,
  onRemoveDataset,
  onRemoveFile,
  onRetryFile,
  onPreviewFile,
}: {
  datasets: Dataset[];
  files: DatasetUpload[];
  locked: boolean;
  onRemoveDataset: (id: string) => void;
  onRemoveFile: (id: string) => void;
  onRetryFile: (file: DatasetUpload) => void;
  onPreviewFile?: (id: string) => void;
}) {
  const reduce = useReducedMotion();
  if (!datasets.length && !files.length) return null;
  return (
    <ul
      aria-label="Attached data"
      className="assistant-attachments relative flex min-w-0 items-center gap-1.5 overflow-x-auto scroll-fade-x px-2 pt-2 pb-1"
    >
      <AnimatePresence initial={false}>
        {datasets.map((dataset, index) => (
          <motion.li
            animate={{ opacity: 1, scale: 1 }}
            className="assistant-attachment flex h-9 w-44 shrink-0 items-center gap-1 rounded-md border border-border bg-card pr-1 pl-1.5 text-xs leading-4"
            exit={{ opacity: 0, scale: 0.92 }}
            initial={{ opacity: 0, scale: 0.92 }}
            key={dataset.id}
            layout={!reduce}
            transition={{ duration: reduce ? 0 : 0.2, ease: [0.4, 0, 0.2, 1] }}
          >
            <Popover>
              <PopoverTrigger asChild>
                <button
                  aria-label={`Dataset details: ${datasetDisplayName(dataset)}`}
                  className="flex min-w-0 flex-1 items-center gap-1.5 rounded-sm text-left outline-none focus-visible:ring-2 focus-visible:ring-ring/60"
                  type="button"
                >
                  <span className="grid size-6 shrink-0 place-items-center rounded-sm bg-cat-2/15 text-cat-2">
                    <Icon.dataset className="size-4" />
                  </span>
                  <span className="min-w-0 flex-1">
                    <span className="block truncate">{datasetDisplayName(dataset)}</span>
                    <span className="block truncate text-muted-foreground">
                      Dataset · {dataset.rows.toLocaleString()} rows
                    </span>
                  </span>
                </button>
              </PopoverTrigger>
              <PopoverContent align="start" className="w-64 space-y-2 p-3 text-xs" side="top">
                <p className="break-words font-medium">{datasetDisplayName(dataset)}</p>
                <p className="text-muted-foreground">
                  {dataset.activeVersion} · {dataset.rows.toLocaleString()} rows
                </p>
                <p className="text-muted-foreground">
                  {index === 0 && !files.length ? "Working dataset" : "Reference dataset"}
                </p>
              </PopoverContent>
            </Popover>
            <Button
              aria-label={`Remove ${datasetDisplayName(dataset)} from chat`}
              className="assistant-attachment-remove text-muted-foreground transition-opacity"
              disabled={locked}
              onClick={() => onRemoveDataset(dataset.id)}
              size="icon-xs"
              type="button"
              variant="ghost"
            >
              <Icon.close />
            </Button>
          </motion.li>
        ))}
        {files.map((entry) => (
          <Attachment
            compact
            disabled={locked}
            entry={entry}
            key={entry.id}
            onPreview={onPreviewFile ? () => onPreviewFile(entry.id) : undefined}
            onRemove={() => onRemoveFile(entry.id)}
            onRetry={() => onRetryFile(entry)}
          />
        ))}
      </AnimatePresence>
    </ul>
  );
}