Skip to content

The platform for continuously improving AI agents.

1.9 KB
Raw Blame
import { useRef } from "react";

import {
  FileImport,
  FileImportPicker,
  IMPORT_ACCEPT,
} from "@/components/ai-components/file-import";
import type { useDatasetUploads } from "@/hooks/use-uploads";

/** Each chosen file as its review card, then "Add files"; with none, the import drop zone. */
export function DatasetFileUpload({
  upload,
  busy = false,
  onError,
}: {
  upload: ReturnType<typeof useDatasetUploads>;
  busy?: boolean;
  onError: (message: string | null) => void;
}) {
  const input = useRef<HTMLInputElement>(null);
  const add = (files: File[]) => {
    if (busy) return;
    if (upload.files.length + files.length > 100) {
      onError("Choose up to 100 files.");
      return;
    }
    onError(null);
    upload.add(files);
  };
  if (upload.files.length === 0)
    return <FileImportPicker disabled={busy} onError={onError} onFiles={add} />;
  return (
    <div className="flex min-w-0 flex-col gap-3">
      {upload.files.map((entry) => (
        <FileImport
          busy={busy}
          entry={entry}
          key={entry.id}
          onContinue={() => upload.confirm(entry.id)}
          onRead={(options) => void upload.inspect(entry, options)}
          onRemove={() => upload.remove(entry.id)}
          onRetry={() =>
            entry.uploadId ? void upload.inspect(entry, entry.options ?? {}) : upload.retry(entry)
          }
        />
      ))}
      <input
        accept={IMPORT_ACCEPT}
        aria-label="Choose dataset files"
        className="hidden"
        disabled={busy}
        multiple
        onChange={(event) => {
          add(Array.from(event.target.files ?? []));
          event.target.value = "";
        }}
        ref={input}
        type="file"
      />
      <button
        className="self-start text-xs text-muted-foreground underline underline-offset-2"
        disabled={busy}
        onClick={() => input.current?.click()}
        type="button"
      >
        Add files
      </button>
    </div>
  );
}