All files / src/components/chat/DocumentsPanel DocumentsPanel.tsx

76.92% Statements 10/13
80% Branches 36/45
60% Functions 3/5
83.33% Lines 10/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130              13x 2x 2x 2x                             13x                 5x                     7x   7x 7x 7x                                                                                                                                                                
import { FileText, Loader2, Plus, Trash2, X } from "lucide-react";
import moment from "moment";
import React from "react";
import type { UploadedFileRef } from "@/services/api/routers/FileRouter";
import { parseServerDate } from "@/util/time";
 
/** Who uploaded a document, as the library names them. */
const uploaderLabel = (doc: UploadedFileRef): string | null => {
  const by = doc.uploaded_by;
  Iif (!by) return null;
  return by.name?.trim() || by.email || null;
};
 
/**
 * The workspace's document library in a chat: every document uploaded in
 * the workspace the page shows (svc-core `app/services/uploads.py`), to
 * attach to the next message.
 *
 * In an organisation or project (`shared`) the library is the team's, so
 * each document names who uploaded it, and only what the caller may delete
 * — their own as a writer of Knowledge, any as an editor — offers a delete.
 *
 * A document is attachable once its text is read: one still `reading`, or
 * that `failed`, says so in place of Attach (a chat refuses either).
 */
const DocumentsPanel: React.FC<{
  documents: UploadedFileRef[];
  attachedIds: Set<string>;
  onAttach: (doc: UploadedFileRef) => void;
  onDetach: (id: string) => void;
  onDelete?: (id: string) => void;
  /** The workspace is an organisation's or project's. */
  shared?: boolean;
}> = ({ documents, attachedIds, onAttach, onDetach, onDelete, shared = false }) => (
  <div className="border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50 px-4 py-3 space-y-2">
    <p className="text-xs font-semibold uppercase tracking-wide text-gray-400 dark:text-gray-500">
      {shared ? "Shared documents" : "Documents"}
    </p>
    {documents.length === 0 ? (
      <p className="text-xs text-gray-400 dark:text-gray-500 italic">
        No uploaded documents yet. Attach a file to get started.
      </p>
    ) : (
      <div className="space-y-1.5 max-h-44 overflow-y-auto pr-1">
        {documents.map((doc) => {
          const attached = attachedIds.has(doc.id);
          // Older responses carry no flag; the API still decides.
          const deletable = !!onDelete && doc.can_delete !== false;
          const uploader = shared ? uploaderLabel(doc) : null;
          return (
            <div
              key={doc.id}
              className="flex items-center gap-2 px-2.5 py-1.5 rounded-lg bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 text-xs"
            >
              <div className="w-full flex flex-col items-center">
                <div className="w-full flex flex-row">
                  <FileText className="w-3.5 h-3.5 shrink-0 text-gray-400 dark:text-gray-500" />
                  <span className="flex-1 truncate text-gray-700 dark:text-gray-300 min-w-0">
                    {doc.name}
                    {doc.mime_type && (
                      <span className="ml-1 text-gray-400 dark:text-gray-500 text-[10px]">
                        ({doc.mime_type})
                      </span>
                    )}
                  </span>
                  {doc.status === "reading" || doc.status === "failed" ? (
                    <span
                      title={doc.status === "failed" ? doc.error ?? undefined : undefined}
                      className="shrink-0 flex items-center gap-1 px-2 py-0.5 text-gray-400 dark:text-gray-500"
                    >
                      {doc.status === "reading" ? (
                        <>
                          <Loader2 className="w-3 h-3 animate-spin" />
                          Reading…
                        </>
                      ) : (
                        "Couldn't be read"
                      )}
                    </span>
                  ) : (
                    <button
                      onClick={() =>
                        attached ? onDetach(doc.id) : onAttach(doc)
                      }
                      title={
                        attached ? "Remove from message" : "Attach to message"
                      }
                      className={`shrink-0 flex items-center gap-1 px-2 py-0.5 rounded-full border font-medium transition-colors ${
                        attached
                          ? "bg-indigo-100 dark:bg-indigo-900/40 border-indigo-200 dark:border-indigo-800 text-indigo-600 dark:text-indigo-400 hover:bg-indigo-200 dark:hover:bg-indigo-800"
                          : "bg-gray-100 dark:bg-gray-700 border-gray-200 dark:border-gray-600 text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-600"
                      }`}
                    >
                      {attached ? (
                        <X className="w-3 h-3" />
                      ) : (
                        <Plus className="w-3 h-3" />
                      )}
                      {attached ? "Remove" : "Attach"}
                    </button>
                  )}
                  {deletable && (
                    <button
                      onClick={() => onDelete?.(doc.id)}
                      title="Delete document"
                      aria-label={`Delete ${doc.name}`}
                      className="shrink-0 p-0.5 rounded text-gray-300 dark:text-gray-600 hover:text-red-400 dark:hover:text-red-500 transition-colors"
                    >
                      <Trash2 className="w-3 h-3" />
                    </button>
                  )}
                </div>
                <div className="w-full text-gray-400 dark:text-gray-500 text-[10px]">
                  {uploader && <span>Uploaded by {uploader}</span>}
                  {uploader && doc.created_at && <span> · </span>}
                  {doc.created_at && (
                    <span>{moment(parseServerDate(doc.created_at)).fromNow()}</span>
                  )}
                </div>
              </div>
            </div>
          );
        })}
      </div>
    )}
  </div>
);
 
export default DocumentsPanel;